KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > util > DataComboBoxSupportTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.db.util;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import javax.swing.AbstractListModel JavaDoc;
25 import javax.swing.ComboBoxModel JavaDoc;
26 import javax.swing.JComboBox JavaDoc;
27 import javax.swing.ListModel JavaDoc;
28 import org.netbeans.junit.NbTestCase;
29
30 /**
31  *
32  * @author Andrei Badea
33  */

34 public class DataComboBoxSupportTest extends NbTestCase {
35
36     public DataComboBoxSupportTest(String JavaDoc testName) {
37         super(testName);
38     }
39
40     public boolean runInEQ() {
41         return true;
42     }
43
44     public void testBasic() {
45         JComboBox JavaDoc comboBox = new JComboBox JavaDoc();
46         ListModelImpl listModel = new ListModelImpl();
47         DataModelImpl dataModel = new DataModelImpl(listModel);
48         DataComboBoxSupport support = new DataComboBoxSupport(comboBox, dataModel, true);
49
50         assertSame(support.NEW_ITEM, comboBox.getItemAt(0));
51         assertEquals("Add", comboBox.getItemAt(0).toString());
52         assertEquals(-1, comboBox.getSelectedIndex());
53
54         List JavaDoc items = new ArrayList JavaDoc();
55         items.add("foo");
56         items.add("bar");
57         listModel.setItems(items);
58
59         assertEquals("foo", comboBox.getItemAt(0));
60         assertEquals("bar", comboBox.getItemAt(1));
61         assertSame(support.SEPARATOR_ITEM, comboBox.getItemAt(2));
62         assertEquals("Add", comboBox.getItemAt(3).toString());
63         assertEquals("The old selected item was removed, nothing should be selected now", -1, comboBox.getSelectedIndex());
64
65         comboBox.setSelectedIndex(1); // bar
66
items.remove("foo");
67         listModel.setItems(items);
68
69         assertEquals("bar", comboBox.getItemAt(0));
70         assertSame(support.SEPARATOR_ITEM, comboBox.getItemAt(1));
71         assertEquals("Add", comboBox.getItemAt(2).toString());
72         assertEquals("Bar should still be selected", 0, comboBox.getSelectedIndex());
73
74         items.add("new");
75         listModel.setItems(items, "new");
76
77         assertEquals("bar", comboBox.getItemAt(0));
78         assertEquals("new", comboBox.getItemAt(1));
79         assertSame(support.SEPARATOR_ITEM, comboBox.getItemAt(2));
80         assertEquals("Add", comboBox.getItemAt(3).toString());
81         assertEquals("new", comboBox.getSelectedItem());
82         assertEquals("New should be selected", 1, comboBox.getSelectedIndex());
83     }
84
85     public void testNoAddition() {
86         JComboBox JavaDoc comboBox = new JComboBox JavaDoc();
87         ListModelImpl listModel = new ListModelImpl();
88         DataModelImpl dataModel = new DataModelImpl(listModel);
89         DataComboBoxSupport support = new DataComboBoxSupport(comboBox, dataModel, false);
90
91         assertEquals(0, comboBox.getItemCount());
92
93         List JavaDoc items = new ArrayList JavaDoc();
94         items.add("foo");
95         items.add("bar");
96         listModel.setItems(items);
97
98         assertEquals(2, comboBox.getItemCount());
99         assertEquals("foo", comboBox.getItemAt(0));
100         assertEquals("bar", comboBox.getItemAt(1));
101     }
102
103     private static final class DataModelImpl implements DataComboBoxModel {
104
105         private ComboBoxModel JavaDoc listModel;
106
107         public DataModelImpl(ComboBoxModel JavaDoc listModel) {
108             this.listModel = listModel;
109         }
110
111         public String JavaDoc getItemTooltipText(Object JavaDoc item) {
112             return null;
113         }
114
115         public String JavaDoc getItemDisplayName(Object JavaDoc item) {
116             return (String JavaDoc)item + "-display";
117         }
118
119         public void newItemActionPerformed() {
120             System.out.println("action performed");
121         }
122
123         public String JavaDoc getNewItemDisplayName() {
124             return "Add";
125         }
126
127         public ComboBoxModel JavaDoc getListModel() {
128             return listModel;
129         }
130     }
131
132     private static final class ListModelImpl extends AbstractListModel JavaDoc implements ComboBoxModel JavaDoc {
133
134         Object JavaDoc[] items = new Object JavaDoc[0];
135         Object JavaDoc selectedItem;
136
137         public Object JavaDoc getElementAt(int index) {
138             return items[index];
139         }
140
141         public int getSize() {
142             return items.length;
143         }
144
145         public Object JavaDoc getSelectedItem() {
146             return selectedItem;
147         }
148
149         public void setSelectedItem(Object JavaDoc selectedItem) {
150             this.selectedItem = selectedItem;
151         }
152
153         private void setItems(List JavaDoc items) {
154             this.items = (Object JavaDoc[])items.toArray(new Object JavaDoc[items.size()]);
155             fireContentsChanged(this, 0, this.items.length);
156         }
157
158         private void setItems(List JavaDoc items, Object JavaDoc selectedItem) {
159             this.items = (Object JavaDoc[])items.toArray(new Object JavaDoc[items.size()]);
160             this.selectedItem = selectedItem;
161             fireContentsChanged(this, 0, this.items.length);
162         }
163     }
164 }
165
Popular Tags