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.util.ArrayList;
016: import java.util.Arrays;
017: import java.util.List;
018:
019: import javax.swing.AbstractListModel;
020: import javax.swing.ComboBoxModel;
021:
022: import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
023: import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStepListenerAdapter;
024:
025: public class TestStepPropertyComboBoxModel extends AbstractListModel
026: implements ComboBoxModel {
027: private WsdlTestStep testStep;
028: private List<String> names;
029: private String selectedName;
030: private InternalTestStepListener testStepListener = new InternalTestStepListener();
031:
032: public TestStepPropertyComboBoxModel(WsdlTestStep testStep) {
033: this .testStep = testStep;
034:
035: names = new ArrayList<String>();
036:
037: if (testStep != null) {
038: names.addAll(Arrays.asList(testStep.getPropertyNames()));
039: testStep.addTestStepListener(testStepListener);
040: }
041: }
042:
043: public void release() {
044: if (testStep != null)
045: testStep.removeTestStepListener(testStepListener);
046: }
047:
048: public WsdlTestStep getTestStep() {
049: return testStep;
050: }
051:
052: public void setTestStep(WsdlTestStep testStep) {
053: if (this .testStep != null) {
054: this .testStep.removeTestStepListener(testStepListener);
055: }
056:
057: int sz = names.size();
058: if (sz > 0) {
059: names.clear();
060: fireIntervalRemoved(this , 0, sz - 1);
061: }
062:
063: this .testStep = testStep;
064: if (testStep != null) {
065: testStep.addTestStepListener(testStepListener);
066: names.addAll(Arrays.asList(testStep.getPropertyNames()));
067: if (!names.isEmpty()) {
068: fireIntervalAdded(this , 0, names.size() - 1);
069: }
070: }
071:
072: setSelectedItem(null);
073: }
074:
075: public Object getElementAt(int index) {
076: return names.get(index);
077: }
078:
079: public int getSize() {
080: return names.size();
081: }
082:
083: private final class InternalTestStepListener extends
084: WsdlTestStepListenerAdapter {
085: @Override
086: public void propertyAdded(String name) {
087: names.add(name);
088: fireIntervalAdded(TestStepPropertyComboBoxModel.this , names
089: .size() - 1, names.size() - 1);
090: }
091:
092: @Override
093: public void propertyRemoved(String name) {
094: int ix = names.indexOf(name);
095: if (ix >= 0) {
096: names.remove(ix);
097: fireIntervalRemoved(TestStepPropertyComboBoxModel.this ,
098: ix, ix);
099:
100: if (name.equals(selectedName))
101: setSelectedItem(null);
102: }
103: }
104:
105: @Override
106: public void propertyRenamed(String oldName, String newName) {
107: int ix = names.indexOf(oldName);
108: fireContentsChanged(TestStepPropertyComboBoxModel.this , ix,
109: ix);
110:
111: if (oldName.equals(selectedName))
112: setSelectedItem(newName);
113: }
114: }
115:
116: public Object getSelectedItem() {
117: return selectedName;
118: }
119:
120: public void setSelectedItem(Object anItem) {
121: if (anItem == null && selectedName == null)
122: return;
123:
124: if (anItem != null && selectedName != null
125: && anItem.equals(selectedName))
126: return;
127:
128: selectedName = anItem == null ? null : anItem.toString();
129:
130: fireContentsChanged(this , -1, -1);
131: }
132: }
|