1 19 20 package org.netbeans.modules.j2ee.ejbcore.ui.logicalview.entres; 21 22 import java.awt.Color ; 23 import java.awt.Component ; 24 import java.awt.Dimension ; 25 import java.awt.event.ActionEvent ; 26 import java.awt.event.ActionListener ; 27 import java.awt.event.InputEvent ; 28 import java.awt.event.KeyAdapter ; 29 import java.awt.event.KeyEvent ; 30 import java.util.Comparator ; 31 import java.util.LinkedList ; 32 import java.util.List ; 33 import java.util.Set ; 34 import java.util.TreeSet ; 35 import javax.swing.AbstractListModel ; 36 import javax.swing.ComboBoxModel ; 37 import javax.swing.DefaultListCellRenderer ; 38 import javax.swing.JComboBox ; 39 import javax.swing.JList ; 40 import javax.swing.JSeparator ; 41 import javax.swing.SwingUtilities ; 42 import org.netbeans.modules.j2ee.deployment.common.api.Datasource; 43 import org.openide.util.NbBundle; 44 45 53 final class DatasourceComboBoxHelper { 54 55 private static class DatasourceImpl implements Datasource { 56 57 private final String jndiName; 58 private final String url; 59 private final String username; 60 private final String password; 61 private final String driverClassName; 62 private String displayName; 63 64 public DatasourceImpl(String jndiName, String url, String username, String password, String driverClassName) { 65 this.jndiName = jndiName; 66 this.url = url; 67 this.username = username; 68 this.password = password; 69 this.driverClassName = driverClassName; 70 } 71 72 public String getJndiName() { 73 return jndiName; 74 } 75 76 public String getUrl() { 77 return url; 78 } 79 80 public String getUsername() { 81 return username; 82 } 83 84 public String getPassword() { 85 return password; 86 } 87 88 public String getDriverClassName() { 89 return driverClassName; 90 } 91 92 public String getDisplayName() { 93 if (displayName == null) { 94 displayName = getJndiName() + " [" + getUrl() + "]"; } 96 return displayName; 97 } 98 99 } 100 101 private static final class Separator extends JSeparator { 102 Separator() { 103 setPreferredSize(new Dimension (getWidth(), 1)); 104 setForeground(Color.BLACK); 105 } 106 } 107 108 static final Separator SEPARATOR_ITEM = new Separator (); 109 static final Object NEW_ITEM = new Object () { 110 public String toString() { 111 return NbBundle.getMessage(DatasourceComboBoxHelper.class, "LBL_NEW_DATASOURCE"); } 113 }; 114 115 private static class DatasourceComboBoxModel extends AbstractListModel implements ComboBoxModel { 116 117 private final Object [] items; 118 private Object selectedItem; 119 private Object previousItem; 120 121 DatasourceComboBoxModel(Object [] items) { 122 this.items = items; 123 if (items.length > 0 && items[0] instanceof Datasource) { 124 selectedItem = items[0]; 125 } 126 } 127 128 public void setSelectedItem(Object anItem) { 129 130 if (selectedItem == null || !selectedItem.equals(anItem)) { 131 132 previousItem = selectedItem; 133 selectedItem = anItem; 134 135 fireContentsChanged(this, 0, -1); 136 } 137 } 138 139 public Object getSelectedItem() { 140 return selectedItem; 141 } 142 143 public Object getElementAt(int index) { 144 return items[index]; 145 } 146 147 public int getSize() { 148 return items.length; 149 } 150 151 Object getPreviousItem() { 152 return previousItem; 153 } 154 } 155 156 private static class DatasourceListCellRenderer extends DefaultListCellRenderer { 157 158 public DatasourceListCellRenderer() {} 159 160 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 161 162 if (isSelected) { 163 setBackground(list.getSelectionBackground()); 164 setForeground(list.getSelectionForeground()); 165 } else { 166 setBackground(list.getBackground()); 167 setForeground(list.getForeground()); 168 } 169 170 if (value instanceof Datasource) { 171 Datasource dataSources = (Datasource) value; 172 setText(dataSources != null ? dataSources.getDisplayName() : ""); setToolTipText(dataSources.toString()); 174 } else 175 if (value == SEPARATOR_ITEM) { 176 return SEPARATOR_ITEM; 177 } else { 178 setText(value != null ? value.toString() : ""); setToolTipText(""); } 181 182 return this; 183 } 184 185 } 186 187 private static class DatasourceComparator implements Comparator <Datasource> { 188 189 public DatasourceComparator() {} 190 191 public int compare(Datasource ds1, Datasource ds2) { 192 193 if (ds1 == null) { 194 return ds2 == null ? 0 : -1; 195 } else { 196 if (ds2 == null) { 197 return 1; 198 } else { 199 String dispName1 = ds1.getDisplayName(); 200 String dispName2 = ds2.getDisplayName(); 201 if (dispName1 == null) { 202 return dispName2 == null ? 0 : -1; 203 } else { 204 return dispName2 == null ? 1 : dispName1.compareToIgnoreCase(dispName2); 205 } 206 } 207 } 208 } 209 } 210 211 private final Set <Datasource> datasources = new TreeSet <Datasource>(new DatasourceComparator()); 212 213 219 DatasourceComboBoxHelper(final JComboBox combo) { 220 221 combo.setRenderer(new DatasourceListCellRenderer()); 222 223 populate(combo, null, false); 224 225 combo.addKeyListener(new KeyAdapter () { 226 public void keyPressed(KeyEvent keyEvent) { 227 int keyCode = keyEvent.getKeyCode(); 228 if (KeyEvent.VK_ENTER == keyCode) { 229 Object selectedItem = combo.getSelectedItem(); 230 if (selectedItem == NEW_ITEM) { 231 Datasource datasource = performCreateDsAction(combo); 232 combo.setPopupVisible(false); 233 keyEvent.consume(); 234 if (datasource != null) 235 populate(combo, datasource, false); 236 } 237 } 238 } 239 }); 240 241 combo.addActionListener(new ActionListener () { 242 243 Object previousItem; 244 int previousIndex = combo.getSelectedIndex(); 245 246 public void actionPerformed(ActionEvent actionEvent) { 247 248 Object selectedItem = combo.getSelectedItem(); 249 if (selectedItem == SEPARATOR_ITEM) { 251 int selectedIndex = combo.getSelectedIndex(); 252 if (selectedIndex > previousIndex) { 253 previousIndex = selectedIndex + 1; 254 previousItem = combo.getItemAt(previousIndex); 255 } else { 256 previousIndex = selectedIndex - 1; 257 previousItem = combo.getItemAt(previousIndex); 258 } 259 combo.setSelectedItem(previousItem); 260 } else if ((actionEvent.getModifiers() & InputEvent.BUTTON1_MASK) != 0) { 262 if (selectedItem == NEW_ITEM) { 263 Datasource datasource = performCreateDsAction(combo); 264 combo.setPopupVisible(false); 265 if (datasource != null) 266 populate(combo, datasource, true); 267 } 268 } 269 } 270 }); 271 272 } 273 274 private Datasource performCreateDsAction(final JComboBox combo) { 275 276 final DatasourceComboBoxModel model = (DatasourceComboBoxModel)combo.getModel(); 277 278 DatasourceComboBoxCustomizer dsc = new DatasourceComboBoxCustomizer(datasources); 279 boolean accept = dsc.showDialog(); 280 Datasource datasource = null; 281 if (accept) { 282 if (dsc.getPassword() != null) { 283 datasource = new DatasourceImpl( 284 dsc.getJndiName(), 285 dsc.getUrl(), 286 dsc.getUsername(), 287 dsc.getPassword(), 288 dsc.getDriverClassName()); 289 datasources.add(datasource); 290 } 291 } 292 293 if (datasource == null) { 294 SwingUtilities.invokeLater(new Runnable () { public void run() { 296 combo.setSelectedItem(model.getPreviousItem()); 297 } 298 }); 299 } 300 301 return datasource; 302 } 303 304 private void populate(final JComboBox combo, final Datasource selectedDatasource, boolean selectItemLater) { 305 306 List <Object > items = new LinkedList <Object >(datasources); 307 308 if (items.size() > 0) { 309 items.add(SEPARATOR_ITEM); 310 } 311 312 items.add(NEW_ITEM); 313 314 Object [] itemsArray = items.toArray(); 315 DatasourceComboBoxModel model = new DatasourceComboBoxModel(itemsArray); 316 317 combo.setModel(model); 318 319 if (selectedDatasource != null) { 320 321 combo.setSelectedItem(selectedDatasource); 326 327 if (selectItemLater) { 328 SwingUtilities.invokeLater(new Runnable () { public void run() { 330 combo.setSelectedItem(selectedDatasource); 331 } 332 }); 333 } 334 335 } 336 } 337 338 } 339 | Popular Tags |