KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > util > PersistenceProviderComboboxHelper


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.persistence.util;
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.Collections JavaDoc;
31 import java.util.List JavaDoc;
32 import javax.swing.DefaultComboBoxModel JavaDoc;
33 import javax.swing.DefaultListCellRenderer JavaDoc;
34 import javax.swing.JComboBox JavaDoc;
35 import javax.swing.JList JavaDoc;
36 import javax.swing.JSeparator JavaDoc;
37 import org.netbeans.api.project.Project;
38 import org.netbeans.api.project.libraries.LibrariesCustomizer;
39 import org.netbeans.modules.j2ee.persistence.provider.DefaultProvider;
40 import org.netbeans.modules.j2ee.persistence.provider.Provider;
41 import org.netbeans.modules.j2ee.persistence.provider.ProviderUtil;
42 import org.netbeans.modules.j2ee.persistence.spi.provider.PersistenceProviderSupplier;
43 import org.netbeans.modules.j2ee.persistence.wizard.library.PersistenceLibraryCustomizer;
44 import org.netbeans.modules.j2ee.persistence.wizard.library.PersistenceLibrarySupport;
45 import org.openide.util.NbBundle;
46 import org.openide.util.Parameters;
47
48 /**
49  * A helper class for populating combo box with persistence providers.
50  * Providers may be provided (no pun intended) by server in a container
51  * managed environment or they might come from libraries.
52  *
53  * @author Libor Kotouc
54  * @author Erno Mononen
55  */

56 public final class PersistenceProviderComboboxHelper {
57     
58     private final static String JavaDoc SEPARATOR = "PersistenceProviderComboboxHelper.SEPARATOR";
59     private final static String JavaDoc EMPTY = "PersistenceProviderComboboxHelper.EMPTY";
60
61     private final PersistenceProviderSupplier providerSupplier;
62
63     /**
64      * Creates a new PersistenceProviderComboboxHelper.
65      * @param project the current project. Must have an implementation of
66      * the PersistenceProviderSupplier in its lookup.
67      * @throws IllegalArgumentException if the project did not have an implementation of
68      * the PersistenceProviderSupplier in its lookup.
69      */

70     public PersistenceProviderComboboxHelper(Project project) {
71         Parameters.notNull("project", project);
72         
73         PersistenceProviderSupplier aProviderSupplier =project.getLookup().lookup(PersistenceProviderSupplier.class);
74         
75         if (aProviderSupplier == null){
76             // a java se project
77
aProviderSupplier = new DefaultPersistenceProviderSupplier();
78         }
79         
80         this.providerSupplier = aProviderSupplier;
81     }
82     
83     /**
84      * Populates the given <code>providerCombo</code> with persistence providers. Supported
85      * providers from the project's server (if it had one) are also added. If the project
86      * doesn't have a server, only providers from libraries
87      * are added. Items for adding and managing libraries are always included.
88      * @param providerCombo the combo box to be populated.
89      */

90     public void connect(final JComboBox JavaDoc providerCombo) {
91         providerCombo.setEditable(false);
92         initCombo(providerCombo);
93         // handling of <ENTER> key event
94
providerCombo.addKeyListener(new KeyAdapter JavaDoc() {
95             public void keyPressed(KeyEvent JavaDoc e) {
96                 int keyCode = e.getKeyCode();
97                 if (KeyEvent.VK_ENTER == keyCode) {
98                     Object JavaDoc selectedItem = providerCombo.getSelectedItem();
99                     if (selectedItem instanceof LibraryItem) {
100                         ((LibraryItem) selectedItem).performAction();
101                         providerCombo.setPopupVisible(false);
102                         e.consume();
103                         initCombo(providerCombo);
104                     }
105                 }
106             }
107         });
108         
109         providerCombo.addActionListener(new ActionListener JavaDoc() {
110             
111             Object JavaDoc currentItem = providerCombo.getSelectedItem();
112             int currentIndex = providerCombo.getSelectedIndex();
113             
114             public void actionPerformed(ActionEvent JavaDoc e) {
115                 Object JavaDoc selectedItem = providerCombo.getSelectedItem();
116                 // skipping of separator
117
if (SEPARATOR.equals(selectedItem)) {
118                     int selectedIndex = providerCombo.getSelectedIndex();
119                     if (selectedIndex > currentIndex) {
120                         currentIndex = selectedIndex + 1;
121                         currentItem = providerCombo.getItemAt(currentIndex);
122                     } else {
123                         currentIndex = selectedIndex - 1;
124                         currentItem = providerCombo.getItemAt(currentIndex);
125                     }
126                     providerCombo.setSelectedItem(currentItem);
127                     // handling mouse click, see KeyEvent.getKeyModifiersText(e.getModifiers())
128
} else if ((e.getModifiers() & InputEvent.BUTTON1_MASK) != 0) {
129                     if (selectedItem instanceof LibraryItem) {
130                         ((LibraryItem) selectedItem).performAction();
131                         providerCombo.setPopupVisible(false);
132                         initCombo(providerCombo);
133                     }
134                 } else {
135                     currentItem = selectedItem;
136                     currentIndex = providerCombo.getSelectedIndex();
137                 }
138             }
139         });
140     }
141     
142     private void initCombo(JComboBox JavaDoc providerCombo) {
143         
144         DefaultComboBoxModel JavaDoc providers = new DefaultComboBoxModel JavaDoc();
145         
146         for(Provider each : providerSupplier.getSupportedProviders()){
147            providers.addElement(each);
148         }
149
150         if (providers.getSize() == 0 && providerSupplier.supportsDefaultProvider()){
151             providers.addElement(ProviderUtil.DEFAULT_PROVIDER);
152         }
153         
154         addProvidersFromLibraries(providers);
155         
156         providerCombo.setModel(providers);
157         providerCombo.addItem(SEPARATOR);
158         providerCombo.addItem(new NewPersistenceLibraryItem());
159         providerCombo.addItem(new ManageLibrariesItem());
160         providerCombo.setRenderer(new PersistenceProviderCellRenderer((Provider)providers.getElementAt(0)));
161         providerCombo.setSelectedIndex(0);
162     }
163     
164     
165     /**
166      * Adds persistence providers found from libraries to the given model.
167      */

168     private void addProvidersFromLibraries(DefaultComboBoxModel JavaDoc model){
169         for (Provider each : PersistenceLibrarySupport.getProvidersFromLibraries()){
170             boolean found = false;
171             for (int i = 0; i < model.getSize(); i++) {
172                 Object JavaDoc elem = model.getElementAt(i);
173                 if (elem instanceof Provider && each.equals(elem)){
174                     found = true;
175                     break;
176                 }
177             }
178             if (!found){
179                 model.addElement(each);
180             }
181         }
182     }
183     
184     
185     public static interface LibraryItem {
186         String JavaDoc getText();
187         void performAction();
188     }
189     
190     private static class NewPersistenceLibraryItem implements LibraryItem {
191         public String JavaDoc getText() {
192             return NbBundle.getMessage(PersistenceProviderComboboxHelper.class, "LBL_NewPersistenceLibrary");
193         }
194         public void performAction() {
195             PersistenceLibraryCustomizer.showCustomizer();
196         }
197     }
198     
199     private static class ManageLibrariesItem implements LibraryItem {
200         public String JavaDoc getText() {
201             return NbBundle.getMessage(PersistenceProviderComboboxHelper.class, "LBL_ManageLibraries");
202         }
203         public void performAction() {
204             LibrariesCustomizer.showCustomizer(null);
205         }
206     }
207
208     private static class PersistenceProviderCellRenderer extends DefaultListCellRenderer JavaDoc {
209         
210         Provider defaultProvider;
211         
212         PersistenceProviderCellRenderer(Provider defaultProvider) {
213             this.defaultProvider = defaultProvider;
214         }
215         
216         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
217             
218             if (isSelected) {
219                 setBackground(list.getSelectionBackground());
220                 setForeground(list.getSelectionForeground());
221             } else {
222                 setBackground(list.getBackground());
223                 setForeground(list.getForeground());
224             }
225             
226             if (value instanceof Provider) {
227                 Provider provider = (Provider)value;
228                 String JavaDoc text = provider.getDisplayName();
229                 if (value.equals(defaultProvider) && (!(value instanceof DefaultProvider))) {
230                     text += NbBundle.getMessage(PersistenceProviderComboboxHelper.class, "LBL_DEFAULT_PROVIDER");
231                 }
232                 setText(text);
233                 
234             } else if (SEPARATOR.equals(value)) {
235                 JSeparator JavaDoc s = new JSeparator JavaDoc();
236                 s.setPreferredSize(new Dimension JavaDoc(s.getWidth(), 1));
237                 s.setForeground(Color.BLACK);
238                 return s;
239                 
240             } else if (EMPTY.equals(value)) {
241                 setText(" ");
242                 
243             } else if (value instanceof LibraryItem) {
244                 setText(((LibraryItem) value).getText());
245                 
246             } else {
247                 setText(value != null ? value.toString() : ""); // NOI18N
248
}
249             
250             return this;
251         }
252         
253     }
254     
255     /**
256      * An implementation of the PersistenceProviderSupplier that returns an empty list for supported
257      * providers and doesn't support a default provider. Used when an implementation of
258      * the PersistenceProviderSupplier can't be found in the project lookup (as is the case
259      * for instance for Java SE projects).
260      */

261     private static class DefaultPersistenceProviderSupplier implements PersistenceProviderSupplier{
262         
263         public List JavaDoc<Provider> getSupportedProviders() {
264             return Collections.<Provider>emptyList();
265         }
266
267         public boolean supportsDefaultProvider() {
268             return false;
269         }
270 }
271 }
272
Popular Tags