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.awt.event.WindowAdapter;
045: import java.awt.event.WindowEvent;
046: import java.util.ArrayList;
047: import java.util.List;
048: import javax.swing.AbstractListModel;
049: import javax.swing.ComboBoxModel;
050: import javax.swing.JFrame;
051: import javax.swing.JOptionPane;
052: import junit.framework.TestCase;
053:
054: /**
055: * Not a real test, just a JFrame to test DataComboBoxSupport interactively.
056: * Looks like a test, however, in order to be able to invoke the Run File
057: * action on it (otherwise, if it was e.g. a JFrame with a main() method, the
058: * NetBeans build script would still run it as a TestCase...).
059: *
060: * @author Andrei Badea
061: */
062: public class DataComboBoxInteractive extends TestCase {
063:
064: public DataComboBoxInteractive(String name) {
065: super (name);
066: }
067:
068: public void testInteractive() throws Exception {
069: java.awt.EventQueue.invokeAndWait(new Runnable() {
070: public void run() {
071: new TestFrame().setVisible(true);
072: }
073: });
074: synchronized (DataComboBoxInteractive.class) {
075: DataComboBoxInteractive.class.wait();
076: }
077: }
078:
079: private static final class TestFrame extends JFrame {
080:
081: private javax.swing.JComboBox comboBox;
082:
083: public TestFrame() {
084: initComponents();
085:
086: DataComboBoxSupport.connect(comboBox,
087: new TestDataComboBoxModel());
088: }
089:
090: private void initComponents() {
091: comboBox = new javax.swing.JComboBox();
092:
093: addWindowListener(new WindowAdapter() {
094: public void windowClosing(WindowEvent e) {
095: super .windowClosing(e);
096:
097: setVisible(false);
098: synchronized (DataComboBoxInteractive.class) {
099: DataComboBoxInteractive.class.notifyAll();
100: }
101: }
102: });
103:
104: comboBox
105: .addActionListener(new java.awt.event.ActionListener() {
106: public void actionPerformed(
107: java.awt.event.ActionEvent evt) {
108: comboBoxActionPerformed(evt);
109: }
110: });
111:
112: org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
113: getContentPane());
114: getContentPane().setLayout(layout);
115: layout.setHorizontalGroup(layout.createParallelGroup(
116: org.jdesktop.layout.GroupLayout.LEADING).add(
117: layout.createSequentialGroup().addContainerGap()
118: .add(comboBox, 0, 376, Short.MAX_VALUE)
119: .addContainerGap()));
120: layout
121: .setVerticalGroup(layout
122: .createParallelGroup(
123: org.jdesktop.layout.GroupLayout.LEADING)
124: .add(
125: layout
126: .createSequentialGroup()
127: .addContainerGap()
128: .add(
129: comboBox,
130: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
131: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
132: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
133: .addContainerGap(264,
134: Short.MAX_VALUE)));
135: pack();
136: }
137:
138: private void comboBoxActionPerformed(
139: java.awt.event.ActionEvent evt) {
140: System.out.println("Action performed: "
141: + comboBox.getSelectedItem());
142: }
143: }
144:
145: private static final class TestDataComboBoxModel implements
146: DataComboBoxModel {
147:
148: private final TestComboBoxModel comboBoxModel = new TestComboBoxModel();
149:
150: public String getItemTooltipText(Object item) {
151: return item + " tooltip";
152: }
153:
154: public String getItemDisplayName(Object item) {
155: return String.valueOf(item);
156: }
157:
158: public void newItemActionPerformed() {
159: String newItem = JOptionPane.showInputDialog("New item:");
160: if (newItem != null) {
161: comboBoxModel.addSelectedItem(newItem);
162: }
163: }
164:
165: public String getNewItemDisplayName() {
166: return "Add item";
167: }
168:
169: public ComboBoxModel getListModel() {
170: return comboBoxModel;
171: }
172: }
173:
174: private static final class TestComboBoxModel extends
175: AbstractListModel implements ComboBoxModel {
176:
177: private List items = new ArrayList();
178: private Object selectedItem;
179:
180: public TestComboBoxModel() {
181: items.add("first");
182: items.add("second");
183: }
184:
185: public void setSelectedItem(Object anItem) {
186: selectedItem = anItem;
187: }
188:
189: public Object getElementAt(int index) {
190: return items.get(index);
191: }
192:
193: public int getSize() {
194: return items.size();
195: }
196:
197: public Object getSelectedItem() {
198: return selectedItem;
199: }
200:
201: public void addSelectedItem(String item) {
202: items.add(item);
203: selectedItem = item;
204: fireContentsChanged(this , 0, items.size());
205: }
206: }
207: }
|