KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbcore > ui > logicalview > entres > DatasourceComboBoxHelper


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.j2ee.ejbcore.ui.logicalview.entres;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.Dimension JavaDoc;
25 import java.awt.event.ActionEvent JavaDoc;
26 import java.awt.event.ActionListener JavaDoc;
27 import java.awt.event.InputEvent JavaDoc;
28 import java.awt.event.KeyAdapter JavaDoc;
29 import java.awt.event.KeyEvent JavaDoc;
30 import java.util.Comparator JavaDoc;
31 import java.util.LinkedList JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Set JavaDoc;
34 import java.util.TreeSet JavaDoc;
35 import javax.swing.AbstractListModel JavaDoc;
36 import javax.swing.ComboBoxModel JavaDoc;
37 import javax.swing.DefaultListCellRenderer JavaDoc;
38 import javax.swing.JComboBox JavaDoc;
39 import javax.swing.JList JavaDoc;
40 import javax.swing.JSeparator JavaDoc;
41 import javax.swing.SwingUtilities JavaDoc;
42 import org.netbeans.modules.j2ee.deployment.common.api.Datasource;
43 import org.openide.util.NbBundle;
44
45 /**
46  *
47  * DatasourceUIHelper populates and manages the content of the combobox for a data sources management.
48  *
49  * @author Libor Kotouc
50  *
51  * @since 1.6
52  */

53 final class DatasourceComboBoxHelper {
54     
55     private static class DatasourceImpl implements Datasource {
56         
57         private final String JavaDoc jndiName;
58         private final String JavaDoc url;
59         private final String JavaDoc username;
60         private final String JavaDoc password;
61         private final String JavaDoc driverClassName;
62         private String JavaDoc displayName;
63         
64         public DatasourceImpl(String JavaDoc jndiName, String JavaDoc url, String JavaDoc username, String JavaDoc password, String JavaDoc 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 JavaDoc getJndiName() {
73             return jndiName;
74         }
75         
76         public String JavaDoc getUrl() {
77             return url;
78         }
79         
80         public String JavaDoc getUsername() {
81             return username;
82         }
83         
84         public String JavaDoc getPassword() {
85             return password;
86         }
87         
88         public String JavaDoc getDriverClassName() {
89             return driverClassName;
90         }
91         
92         public String JavaDoc getDisplayName() {
93             if (displayName == null) {
94                 displayName = getJndiName() + " [" + getUrl() + "]"; // NOI18N
95
}
96             return displayName;
97         }
98         
99     }
100     
101     private static final class Separator extends JSeparator JavaDoc {
102         Separator() {
103             setPreferredSize(new Dimension JavaDoc(getWidth(), 1));
104             setForeground(Color.BLACK);
105         }
106     }
107     
108     static final Separator JavaDoc SEPARATOR_ITEM = new Separator JavaDoc();
109     static final Object JavaDoc NEW_ITEM = new Object JavaDoc() {
110         public String JavaDoc toString() {
111             return NbBundle.getMessage(DatasourceComboBoxHelper.class, "LBL_NEW_DATASOURCE"); // NOI18N
112
}
113     };
114     
115     private static class DatasourceComboBoxModel extends AbstractListModel JavaDoc implements ComboBoxModel JavaDoc {
116         
117         private final Object JavaDoc[] items;
118         private Object JavaDoc selectedItem;
119         private Object JavaDoc previousItem;
120         
121         DatasourceComboBoxModel(Object JavaDoc[] 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 JavaDoc 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 JavaDoc getSelectedItem() {
140             return selectedItem;
141         }
142         
143         public Object JavaDoc getElementAt(int index) {
144             return items[index];
145         }
146         
147         public int getSize() {
148             return items.length;
149         }
150         
151         Object JavaDoc getPreviousItem() {
152             return previousItem;
153         }
154     }
155     
156     private static class DatasourceListCellRenderer extends DefaultListCellRenderer JavaDoc {
157         
158         public DatasourceListCellRenderer() {}
159         
160         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc 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() : ""); // NOI18N
173
setToolTipText(dataSources.toString());
174             } else
175                 if (value == SEPARATOR_ITEM) {
176                 return SEPARATOR_ITEM;
177                 } else {
178                 setText(value != null ? value.toString() : ""); // NOI18N
179
setToolTipText(""); // NOI18N
180
}
181             
182             return this;
183         }
184         
185     }
186     
187     private static class DatasourceComparator implements Comparator JavaDoc<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 JavaDoc dispName1 = ds1.getDisplayName();
200                     String JavaDoc 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 JavaDoc<Datasource> datasources = new TreeSet JavaDoc<Datasource>(new DatasourceComparator());
212     
213     /**
214      * Entry point for the combobox initialization. It connects combobox with its content and
215      * add items for the combobox content management.
216      *
217      * @param combo combobox to manage
218      */

219     DatasourceComboBoxHelper(final JComboBox JavaDoc combo) {
220         
221         combo.setRenderer(new DatasourceListCellRenderer());
222         
223         populate(combo, null, false);
224         
225         combo.addKeyListener(new KeyAdapter JavaDoc() {
226             public void keyPressed(KeyEvent JavaDoc keyEvent) {
227                 int keyCode = keyEvent.getKeyCode();
228                 if (KeyEvent.VK_ENTER == keyCode) {
229                     Object JavaDoc 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 JavaDoc() {
242             
243             Object JavaDoc previousItem;
244             int previousIndex = combo.getSelectedIndex();
245             
246             public void actionPerformed(ActionEvent JavaDoc actionEvent) {
247                 
248                 Object JavaDoc selectedItem = combo.getSelectedItem();
249                 // skipping of separator
250
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                     // handling mouse click, see KeyEvent.getKeyModifiersText(e.getModifiers())
261
} 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 JavaDoc 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 JavaDoc() { // postpone item selection to enable event firing from JCombobox.setSelectedItem()
295
public void run() {
296                     combo.setSelectedItem(model.getPreviousItem());
297                 }
298             });
299         }
300         
301         return datasource;
302     }
303     
304     private void populate(final JComboBox JavaDoc combo, final Datasource selectedDatasource, boolean selectItemLater) {
305         
306         List JavaDoc<Object JavaDoc> items = new LinkedList JavaDoc<Object JavaDoc>(datasources);
307         
308         if (items.size() > 0) {
309             items.add(SEPARATOR_ITEM);
310         }
311         
312         items.add(NEW_ITEM);
313         
314         Object JavaDoc[] itemsArray = items.toArray();
315         DatasourceComboBoxModel model = new DatasourceComboBoxModel(itemsArray);
316         
317         combo.setModel(model);
318         
319         if (selectedDatasource != null) {
320             
321             // Ensure that the correct item is selected before listeners like FocusListener are called.
322
// ActionListener.actionPerformed() is not called if this method is already called from
323
// actionPerformed(), in that case selectItemLater should be set to true and setSelectedItem()
324
// below is called asynchronously so that the actionPerformed() is called
325
combo.setSelectedItem(selectedDatasource);
326             
327             if (selectItemLater) {
328                 SwingUtilities.invokeLater(new Runnable JavaDoc() { // postpone item selection to enable event firing from JCombobox.setSelectedItem()
329
public void run() {
330                         combo.setSelectedItem(selectedDatasource);
331                     }
332                 });
333             }
334             
335         }
336     }
337     
338 }
339
Popular Tags