KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.event.WindowAdapter JavaDoc;
23 import java.awt.event.WindowEvent JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26 import javax.swing.AbstractListModel JavaDoc;
27 import javax.swing.ComboBoxModel JavaDoc;
28 import javax.swing.JFrame JavaDoc;
29 import javax.swing.JOptionPane JavaDoc;
30 import junit.framework.TestCase;
31
32 /**
33  * Not a real test, just a JFrame to test DataComboBoxSupport interactively.
34  * Looks like a test, however, in order to be able to invoke the Run File
35  * action on it (otherwise, if it was e.g. a JFrame with a main() method, the
36  * NetBeans build script would still run it as a TestCase...).
37  *
38  * @author Andrei Badea
39  */

40 public class DataComboBoxInteractive extends TestCase {
41
42     public DataComboBoxInteractive(String JavaDoc name) {
43         super(name);
44     }
45
46     public void testInteractive() throws Exception JavaDoc {
47         java.awt.EventQueue.invokeAndWait(new Runnable JavaDoc() {
48             public void run() {
49                 new TestFrame().setVisible(true);
50             }
51         });
52         synchronized (DataComboBoxInteractive.class) {
53             DataComboBoxInteractive.class.wait();
54         }
55     }
56
57     private static final class TestFrame extends JFrame JavaDoc {
58
59         private javax.swing.JComboBox JavaDoc comboBox;
60
61         public TestFrame() {
62             initComponents();
63
64             DataComboBoxSupport.connect(comboBox, new TestDataComboBoxModel());
65         }
66
67         private void initComponents() {
68             comboBox = new javax.swing.JComboBox JavaDoc();
69
70             addWindowListener(new WindowAdapter JavaDoc() {
71                 public void windowClosing(WindowEvent JavaDoc e) {
72                     super.windowClosing(e);
73
74                     setVisible(false);
75                     synchronized (DataComboBoxInteractive.class) {
76                         DataComboBoxInteractive.class.notifyAll();
77                     }
78                 }
79             });
80
81             comboBox.addActionListener(new java.awt.event.ActionListener JavaDoc() {
82                 public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
83                     comboBoxActionPerformed(evt);
84                 }
85             });
86
87             org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
88             getContentPane().setLayout(layout);
89             layout.setHorizontalGroup(
90                 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
91                 .add(layout.createSequentialGroup()
92                     .addContainerGap()
93                     .add(comboBox, 0, 376, Short.MAX_VALUE)
94                     .addContainerGap())
95             );
96             layout.setVerticalGroup(
97                 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
98                 .add(layout.createSequentialGroup()
99                     .addContainerGap()
100                     .add(comboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
101                     .addContainerGap(264, Short.MAX_VALUE))
102             );
103             pack();
104         }
105
106         private void comboBoxActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
107             System.out.println("Action performed: " + comboBox.getSelectedItem());
108         }
109     }
110
111     private static final class TestDataComboBoxModel implements DataComboBoxModel {
112
113         private final TestComboBoxModel comboBoxModel = new TestComboBoxModel();
114
115         public String JavaDoc getItemTooltipText(Object JavaDoc item) {
116             return item + " tooltip";
117         }
118
119         public String JavaDoc getItemDisplayName(Object JavaDoc item) {
120             return String.valueOf(item);
121         }
122
123         public void newItemActionPerformed() {
124             String JavaDoc newItem = JOptionPane.showInputDialog("New item:");
125             if (newItem != null) {
126                 comboBoxModel.addSelectedItem(newItem);
127             }
128         }
129
130         public String JavaDoc getNewItemDisplayName() {
131             return "Add item";
132         }
133
134         public ComboBoxModel JavaDoc getListModel() {
135             return comboBoxModel;
136         }
137     }
138
139     private static final class TestComboBoxModel extends AbstractListModel JavaDoc implements ComboBoxModel JavaDoc {
140
141         private List JavaDoc items = new ArrayList JavaDoc();
142         private Object JavaDoc selectedItem;
143
144         public TestComboBoxModel() {
145             items.add("first");
146             items.add("second");
147         }
148
149         public void setSelectedItem(Object JavaDoc anItem) {
150            selectedItem = anItem;
151         }
152
153         public Object JavaDoc getElementAt(int index) {
154             return items.get(index);
155         }
156
157         public int getSize() {
158             return items.size();
159         }
160
161         public Object JavaDoc getSelectedItem() {
162             return selectedItem;
163         }
164
165         public void addSelectedItem(String JavaDoc item) {
166             items.add(item);
167             selectedItem = item;
168             fireContentsChanged(this, 0, items.size());
169         }
170     }
171 }
172
Popular Tags