001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.db.util;
043:
044: import java.util.ArrayList;
045: import java.util.List;
046: import javax.swing.AbstractListModel;
047: import javax.swing.ComboBoxModel;
048: import javax.swing.JComboBox;
049: import javax.swing.ListModel;
050: import org.netbeans.junit.NbTestCase;
051:
052: /**
053: *
054: * @author Andrei Badea
055: */
056: public class DataComboBoxSupportTest extends NbTestCase {
057:
058: public DataComboBoxSupportTest(String testName) {
059: super (testName);
060: }
061:
062: public boolean runInEQ() {
063: return true;
064: }
065:
066: public void testBasic() {
067: JComboBox comboBox = new JComboBox();
068: ListModelImpl listModel = new ListModelImpl();
069: DataModelImpl dataModel = new DataModelImpl(listModel);
070: DataComboBoxSupport support = new DataComboBoxSupport(comboBox,
071: dataModel, true);
072:
073: assertSame(support.NEW_ITEM, comboBox.getItemAt(0));
074: assertEquals("Add", comboBox.getItemAt(0).toString());
075: assertEquals(-1, comboBox.getSelectedIndex());
076:
077: List items = new ArrayList();
078: items.add("foo");
079: items.add("bar");
080: listModel.setItems(items);
081:
082: assertEquals("foo", comboBox.getItemAt(0));
083: assertEquals("bar", comboBox.getItemAt(1));
084: assertSame(support.SEPARATOR_ITEM, comboBox.getItemAt(2));
085: assertEquals("Add", comboBox.getItemAt(3).toString());
086: assertEquals(
087: "The old selected item was removed, nothing should be selected now",
088: -1, comboBox.getSelectedIndex());
089:
090: comboBox.setSelectedIndex(1); // bar
091: items.remove("foo");
092: listModel.setItems(items);
093:
094: assertEquals("bar", comboBox.getItemAt(0));
095: assertSame(support.SEPARATOR_ITEM, comboBox.getItemAt(1));
096: assertEquals("Add", comboBox.getItemAt(2).toString());
097: assertEquals("Bar should still be selected", 0, comboBox
098: .getSelectedIndex());
099:
100: items.add("new");
101: listModel.setItems(items, "new");
102:
103: assertEquals("bar", comboBox.getItemAt(0));
104: assertEquals("new", comboBox.getItemAt(1));
105: assertSame(support.SEPARATOR_ITEM, comboBox.getItemAt(2));
106: assertEquals("Add", comboBox.getItemAt(3).toString());
107: assertEquals("new", comboBox.getSelectedItem());
108: assertEquals("New should be selected", 1, comboBox
109: .getSelectedIndex());
110: }
111:
112: public void testNoAddition() {
113: JComboBox comboBox = new JComboBox();
114: ListModelImpl listModel = new ListModelImpl();
115: DataModelImpl dataModel = new DataModelImpl(listModel);
116: DataComboBoxSupport support = new DataComboBoxSupport(comboBox,
117: dataModel, false);
118:
119: assertEquals(0, comboBox.getItemCount());
120:
121: List items = new ArrayList();
122: items.add("foo");
123: items.add("bar");
124: listModel.setItems(items);
125:
126: assertEquals(2, comboBox.getItemCount());
127: assertEquals("foo", comboBox.getItemAt(0));
128: assertEquals("bar", comboBox.getItemAt(1));
129: }
130:
131: private static final class DataModelImpl implements
132: DataComboBoxModel {
133:
134: private ComboBoxModel listModel;
135:
136: public DataModelImpl(ComboBoxModel listModel) {
137: this .listModel = listModel;
138: }
139:
140: public String getItemTooltipText(Object item) {
141: return null;
142: }
143:
144: public String getItemDisplayName(Object item) {
145: return (String) item + "-display";
146: }
147:
148: public void newItemActionPerformed() {
149: System.out.println("action performed");
150: }
151:
152: public String getNewItemDisplayName() {
153: return "Add";
154: }
155:
156: public ComboBoxModel getListModel() {
157: return listModel;
158: }
159: }
160:
161: private static final class ListModelImpl extends AbstractListModel
162: implements ComboBoxModel {
163:
164: Object[] items = new Object[0];
165: Object selectedItem;
166:
167: public Object getElementAt(int index) {
168: return items[index];
169: }
170:
171: public int getSize() {
172: return items.length;
173: }
174:
175: public Object getSelectedItem() {
176: return selectedItem;
177: }
178:
179: public void setSelectedItem(Object selectedItem) {
180: this .selectedItem = selectedItem;
181: }
182:
183: private void setItems(List items) {
184: this .items = (Object[]) items.toArray(new Object[items
185: .size()]);
186: fireContentsChanged(this , 0, this .items.length);
187: }
188:
189: private void setItems(List items, Object selectedItem) {
190: this .items = (Object[]) items.toArray(new Object[items
191: .size()]);
192: this .selectedItem = selectedItem;
193: fireContentsChanged(this , 0, this.items.length);
194: }
195: }
196: }
|