001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.support.components;
014:
015: import java.beans.PropertyChangeEvent;
016: import java.beans.PropertyChangeListener;
017:
018: import javax.swing.AbstractListModel;
019: import javax.swing.ComboBoxModel;
020:
021: import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
022: import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
023: import com.eviware.soapui.model.support.TestSuiteListenerAdapter;
024: import com.eviware.soapui.model.testsuite.TestStep;
025:
026: public class TestStepComboBoxModel extends AbstractListModel implements
027: ComboBoxModel {
028: private final WsdlTestCase testCase;
029: private WsdlTestStep selectedStep;
030: private int selectedStepIndex = -1;
031: private TestStepNameListener testStepNameListener = new TestStepNameListener();
032: private InternalTestSuiteListener testSuiteListener;
033:
034: public TestStepComboBoxModel(WsdlTestCase testCase) {
035: this .testCase = testCase;
036:
037: testSuiteListener = new InternalTestSuiteListener();
038: testCase.getTestSuite().addTestSuiteListener(testSuiteListener);
039: }
040:
041: public void release() {
042: testCase.getTestSuite().removeTestSuiteListener(
043: testSuiteListener);
044: }
045:
046: public Object getElementAt(int index) {
047: return testCase.getTestStepAt(index).getName();
048: }
049:
050: public int getSize() {
051: return testCase.getTestStepCount();
052: }
053:
054: private final class InternalTestSuiteListener extends
055: TestSuiteListenerAdapter {
056: @Override
057: public void testStepAdded(TestStep testStep, int index) {
058: if (testStep.getTestCase() == testCase)
059: fireIntervalAdded(TestStepComboBoxModel.this , index,
060: index);
061: }
062:
063: @Override
064: public void testStepMoved(TestStep testStep, int fromIndex,
065: int offset) {
066: if (testStep.getTestCase() == testCase)
067: fireContentsChanged(TestStepComboBoxModel.this ,
068: fromIndex, fromIndex + offset);
069: }
070:
071: @Override
072: public void testStepRemoved(TestStep testStep, int index) {
073: if (testStep.getTestCase() == testCase)
074: fireIntervalRemoved(TestStepComboBoxModel.this , index,
075: index);
076:
077: if (index == selectedStepIndex)
078: setSelectedItem(null);
079: }
080: }
081:
082: public Object getSelectedItem() {
083: return selectedStep == null ? null : selectedStep.getName();
084: }
085:
086: public void setSelectedItem(Object anItem) {
087: if (selectedStep != null)
088: selectedStep
089: .removePropertyChangeListener(testStepNameListener);
090:
091: selectedStep = testCase.getTestStepByName((String) anItem);
092: if (selectedStep != null) {
093: selectedStep.addPropertyChangeListener(
094: WsdlTestStep.NAME_PROPERTY, testStepNameListener);
095: selectedStepIndex = testCase
096: .getIndexOfTestStep(selectedStep);
097: } else
098: selectedStepIndex = -1;
099:
100: fireContentsChanged(this , -1, -1);
101: }
102:
103: /**
104: * Listen for testStep name changes and modify comboBox model accordingly
105: */
106:
107: private final class TestStepNameListener implements
108: PropertyChangeListener {
109: public void propertyChange(PropertyChangeEvent evt) {
110: Object oldItem = evt.getOldValue();
111: int stepIndex = testCase
112: .getTestStepIndexByName((String) oldItem);
113: if (stepIndex != -1) {
114: fireContentsChanged(TestStepComboBoxModel.this ,
115: stepIndex, stepIndex);
116:
117: if (selectedStep != null
118: && testCase.getIndexOfTestStep(selectedStep) == stepIndex)
119: fireContentsChanged(this , -1, -1);
120: }
121: }
122: }
123:
124: public WsdlTestStep getSelectedStep() {
125: return selectedStep;
126: }
127: }
|