1 19 20 package org.netbeans.modules.db.util; 21 22 import java.awt.event.WindowAdapter ; 23 import java.awt.event.WindowEvent ; 24 import java.util.ArrayList ; 25 import java.util.List ; 26 import javax.swing.AbstractListModel ; 27 import javax.swing.ComboBoxModel ; 28 import javax.swing.JFrame ; 29 import javax.swing.JOptionPane ; 30 import junit.framework.TestCase; 31 32 40 public class DataComboBoxInteractive extends TestCase { 41 42 public DataComboBoxInteractive(String name) { 43 super(name); 44 } 45 46 public void testInteractive() throws Exception { 47 java.awt.EventQueue.invokeAndWait(new Runnable () { 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 { 58 59 private javax.swing.JComboBox 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 (); 69 70 addWindowListener(new WindowAdapter () { 71 public void windowClosing(WindowEvent 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 () { 82 public void actionPerformed(java.awt.event.ActionEvent 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 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 getItemTooltipText(Object item) { 116 return item + " tooltip"; 117 } 118 119 public String getItemDisplayName(Object item) { 120 return String.valueOf(item); 121 } 122 123 public void newItemActionPerformed() { 124 String newItem = JOptionPane.showInputDialog("New item:"); 125 if (newItem != null) { 126 comboBoxModel.addSelectedItem(newItem); 127 } 128 } 129 130 public String getNewItemDisplayName() { 131 return "Add item"; 132 } 133 134 public ComboBoxModel getListModel() { 135 return comboBoxModel; 136 } 137 } 138 139 private static final class TestComboBoxModel extends AbstractListModel implements ComboBoxModel { 140 141 private List items = new ArrayList (); 142 private Object selectedItem; 143 144 public TestComboBoxModel() { 145 items.add("first"); 146 items.add("second"); 147 } 148 149 public void setSelectedItem(Object anItem) { 150 selectedItem = anItem; 151 } 152 153 public Object getElementAt(int index) { 154 return items.get(index); 155 } 156 157 public int getSize() { 158 return items.size(); 159 } 160 161 public Object getSelectedItem() { 162 return selectedItem; 163 } 164 165 public void addSelectedItem(String item) { 166 items.add(item); 167 selectedItem = item; 168 fireContentsChanged(this, 0, items.size()); 169 } 170 } 171 } 172 | Popular Tags |