KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > palette > ChooseBeansWizardPanel


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.form.palette;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import javax.swing.*;
25 import javax.swing.border.*;
26 import java.io.File JavaDoc;
27 import java.util.*;
28 import javax.swing.event.*;
29 import java.awt.event.*;
30
31 import org.openide.WizardDescriptor;
32
33 import org.netbeans.modules.form.project.ClassSource;
34
35 /**
36  * The second panel in the wizard for adding new components to the palette.
37  * Lets the user choose components from a list of all available components
38  * in selected source.
39  *
40  * @author Tomas Pavek
41  */

42
43 class ChooseBeansWizardPanel implements WizardDescriptor.Panel {
44
45     private File JavaDoc[] currentFiles; // roots (typically JAR files) chosen by the user
46
private Map libraryNameMap; // map from root file names to library names
47

48     private java.util.List JavaDoc markedBeans; // beans marked in JAR manifest
49
private java.util.List JavaDoc allBeans; // all bean classes under given roots
50
private String JavaDoc sourceType;
51
52     private BeanSelector beanSelector;
53     private JLabel noBeansLabel;
54
55     private EventListenerList listenerList;
56
57     // ----------
58
// WizardDescriptor.Panel implementation
59

60     public Component JavaDoc getComponent() {
61         if ((markedBeans == null) && ((allBeans == null) || (allBeans.size() == 0))) {
62             // No beans found
63
String JavaDoc messageKey;
64             if (ClassSource.JAR_SOURCE.equals(sourceType))
65                 messageKey = "MSG_NoBeanInJAR"; // NOI18N
66
else if (ClassSource.LIBRARY_SOURCE.equals(sourceType))
67                 messageKey = "MSG_NoBeanInLibrary"; // NOI18N
68
else if (ClassSource.PROJECT_SOURCE.equals(sourceType))
69                 messageKey = "MSG_NoBeanInProject"; // NOI18N
70
else
71                 throw new IllegalArgumentException JavaDoc();
72             noBeansLabel = new JLabel(PaletteUtils.getBundleString(messageKey));
73             noBeansLabel.setPreferredSize(new Dimension JavaDoc(400, 300));
74             noBeansLabel.setVerticalAlignment(SwingConstants.TOP);
75             noBeansLabel.setName(PaletteUtils.getBundleString("CTL_NoBeans_Caption")); // NOI18N
76
noBeansLabel.putClientProperty("WizardPanel_contentSelectedIndex", // NOI18N
77
new Integer JavaDoc(1));
78             return noBeansLabel;
79         } else {
80             if (beanSelector == null) { // create the UI component for the wizard step
81
beanSelector = new BeanSelector();
82                 
83                 // wizard API: set the caption and index of this panel
84
beanSelector.setName(PaletteUtils.getBundleString("CTL_SelectBeans_Caption")); // NOI18N
85
beanSelector.putClientProperty("WizardPanel_contentSelectedIndex", // NOI18N
86
new Integer JavaDoc(1));
87                 if (markedBeans != null || allBeans != null)
88                     beanSelector.setBeans(markedBeans, allBeans);
89                 
90                 Listener listener = new Listener();
91                 beanSelector.list.addListSelectionListener(listener);
92                 beanSelector.radio1.addActionListener(listener);
93                 beanSelector.radio2.addActionListener(listener);
94             }
95         }
96
97         return beanSelector;
98     }
99
100     public org.openide.util.HelpCtx getHelp() {
101         // PENDING
102
return new org.openide.util.HelpCtx("beans.adding"); // NOI18N
103
}
104
105     public boolean isValid() {
106         return beanSelector != null && beanSelector.getSelectedBeans().size() > 0;
107     }
108
109     public void readSettings(Object JavaDoc settings) {
110         AddToPaletteWizard wizard = (AddToPaletteWizard) settings;
111         sourceType = wizard.getSourceType();
112         File JavaDoc[] jarFiles = wizard.getJARFiles();
113
114         if (currentFiles != null && currentFiles.length == jarFiles.length)
115             for (int i=0; i < jarFiles.length; i++)
116                 if (jarFiles[i].equals(currentFiles[i])) {
117                     if (i+1 == jarFiles.length)
118                         return; // no change from the last time
119
}
120                 else break;
121
122         currentFiles = jarFiles;
123         libraryNameMap = wizard.libraryNameMap;
124
125         allBeans = null; // don't read all the beans until needed
126
markedBeans = BeanInstaller.findJavaBeansInJar(jarFiles);
127         if (markedBeans != null) {
128             if (libraryNameMap != null) // need to change root file names to library names
129
remapLibraryNames(markedBeans, libraryNameMap);
130             Collections.sort(markedBeans);
131         }
132         else {
133             allBeans = BeanInstaller.findJavaBeans(jarFiles);
134             if (libraryNameMap != null) // need to change root file names to library names
135
remapLibraryNames(allBeans, libraryNameMap);
136             Collections.sort(allBeans);
137         }
138
139         if (beanSelector != null)
140             beanSelector.setBeans(markedBeans, allBeans);
141     }
142
143     public void storeSettings(Object JavaDoc settings) {
144         if (beanSelector != null) {
145             java.util.List JavaDoc itemList = beanSelector.getSelectedBeans();
146             BeanInstaller.ItemInfo[] itemArray =
147                 new BeanInstaller.ItemInfo[itemList.size()];
148             itemList.toArray(itemArray);
149             ((AddToPaletteWizard)settings).setSelectedBeans(itemArray);
150         }
151     }
152
153     public void addChangeListener(ChangeListener listener) {
154         if (listenerList == null)
155             listenerList = new EventListenerList();
156         listenerList.add(ChangeListener.class, listener);
157     }
158
159     public void removeChangeListener(ChangeListener listener) {
160         if (listenerList != null)
161             listenerList.remove(ChangeListener.class, listener);
162     }
163
164     // -----
165

166     void fireStateChanged() {
167         if (listenerList == null)
168             return;
169
170         ChangeEvent e = null;
171         Object JavaDoc[] listeners = listenerList.getListenerList();
172         for (int i=listeners.length-2; i >= 0; i-=2) {
173             if (listeners[i] == ChangeListener.class) {
174                 if (e == null)
175                     e = new ChangeEvent(this);
176                 ((ChangeListener)listeners[i+1]).stateChanged(e);
177             }
178         }
179     }
180
181     private static void remapLibraryNames(List beans, Map map) {
182         for (int i=0, n=beans.size(); i < n; i++) {
183             BeanInstaller.ItemInfo ii = (BeanInstaller.ItemInfo) beans.get(i);
184             ii.source = (String JavaDoc) map.get(ii.source);
185         }
186     }
187
188     // -------
189

190     static class BeanSelector extends JPanel {
191
192         JList list;
193         JRadioButton radio1, radio2;
194
195         BeanSelector() {
196             setLayout(new java.awt.GridBagLayout JavaDoc());
197             java.awt.GridBagConstraints JavaDoc gridBagConstraints;
198
199             JLabel label1 = new JLabel();
200             org.openide.awt.Mnemonics.setLocalizedText(
201                 label1, PaletteUtils.getBundleString("CTL_SelectBeans")); // NOI18N
202
label1.setLabelFor(list);
203             gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
204             gridBagConstraints.gridwidth = 3;
205             gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 0);
206             gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
207             add(label1, gridBagConstraints);
208
209             list = new JList();
210             list.setLayoutOrientation(JList.VERTICAL_WRAP);
211             list.setVisibleRowCount(0);
212             list.setCellRenderer(new ItemInfoRenderer());
213             list.getAccessibleContext().setAccessibleDescription(
214                 PaletteUtils.getBundleString("ACSD_CTL_SelectBeans")); // NOI18N
215

216             JScrollPane scrollPane = new JScrollPane();
217             scrollPane.setViewportView(list);
218             gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
219             gridBagConstraints.gridx = 0;
220             gridBagConstraints.gridy = 1;
221             gridBagConstraints.gridwidth = 3;
222             gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
223             gridBagConstraints.weightx = 1.0;
224             gridBagConstraints.weighty = 1.0;
225             add(scrollPane, gridBagConstraints);
226
227             radio1 = new JRadioButton();
228             radio1.setActionCommand("SHOW MARKED"); // NOI18N
229
org.openide.awt.Mnemonics.setLocalizedText(
230                 radio1, PaletteUtils.getBundleString("CTL_ShowMarked")); // NOI18N
231
radio1.setToolTipText(PaletteUtils.getBundleString("HINT_ShowMarked")); // NOI18N
232
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
233             gridBagConstraints.gridx = 0;
234             gridBagConstraints.gridy = 2;
235             gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 0, 0, 0);
236             add(radio1, gridBagConstraints);
237             // PENDING A11Y
238

239             radio2 = new JRadioButton();
240             radio2.setActionCommand("SHOW ALL"); // NOI18N
241
org.openide.awt.Mnemonics.setLocalizedText(
242                 radio2, PaletteUtils.getBundleString("CTL_ShowAllClasses")); // NOI18N
243
radio2.setToolTipText(PaletteUtils.getBundleString("HINT_ShowAllClasses")); // NOI18N
244
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
245             gridBagConstraints.gridx = 1;
246             gridBagConstraints.gridy = 2;
247             gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 0, 0, 0);
248             add(radio2, gridBagConstraints);
249             // PENDING A11Y
250

251             ButtonGroup buttonGroup = new ButtonGroup();
252             buttonGroup.add(radio1);
253             buttonGroup.add(radio2);
254
255             getAccessibleContext().setAccessibleDescription(
256                 PaletteUtils.getBundleString("ACSD_SelectBeansDialog")); // NOI18N
257
}
258
259         void setBeans(List markedBeans, List allBeans) {
260             if (markedBeans == null) {
261                 radio1.setEnabled(false);
262             }
263             else {
264                 radio1.setEnabled(true);
265                 radio1.setSelected(true);
266                 setDisplayedBeans(markedBeans);
267             }
268             if (allBeans != null && markedBeans == null) {
269                 radio2.setSelected(true);
270                 setDisplayedBeans(allBeans);
271             }
272         }
273
274         /** @param list of BeanInstaller.ItemInfo */
275         void setDisplayedBeans(final List beans) {
276             list.setModel(new AbstractListModel() {
277                 public int getSize() { return beans.size(); }
278                 public Object JavaDoc getElementAt(int i) { return beans.get(i); }
279             });
280         }
281
282         /** @return list of BeanInstaller.ItemInfo */
283         List getSelectedBeans() {
284             Object JavaDoc[] sel = list.getSelectedValues();
285             List al = new ArrayList(sel.length);
286             for (int i = 0; i < sel.length; i++)
287                 al.add(sel[i]);
288             return al;
289         }
290
291         public Dimension JavaDoc getPreferredSize() {
292             return new Dimension JavaDoc(400, 300);
293         }
294     }
295
296     // --------
297

298     private static class ItemInfoRenderer extends JLabel
299                                                implements ListCellRenderer
300     {
301         private static final Border hasFocusBorder =
302             new LineBorder(UIManager.getColor("List.focusCellHighlight")); // NOI18N
303
private static final Border noFocusBorder = BorderFactory.createEmptyBorder(1, 1, 1, 1);
304
305         public ItemInfoRenderer() {
306             setOpaque(true);
307             setBorder(noFocusBorder);
308         }
309
310         public Component JavaDoc getListCellRendererComponent(JList list,
311                                                       Object JavaDoc value,
312                                                       int index,
313                                                       boolean isSelected,
314                                                       boolean cellHasFocus)
315         {
316             String JavaDoc name = ((BeanInstaller.ItemInfo)value).classname;
317             setToolTipText(name); // full class name as tooltip
318

319             int i = name.lastIndexOf('.');
320             if (i >= 0)
321                 name = name.substring(i+1);
322
323             setText(name); // short class name as the label text
324

325             if (isSelected){
326                 setBackground(UIManager.getColor("List.selectionBackground")); // NOI18N
327
setForeground(UIManager.getColor("List.selectionForeground")); // NOI18N
328
}
329             else {
330                 setBackground(list.getBackground());
331                 setForeground(list.getForeground());
332             }
333             setBorder(cellHasFocus ? hasFocusBorder : noFocusBorder);
334             return this;
335         }
336     }
337
338     // -------
339

340     class Listener implements ListSelectionListener, ActionListener {
341
342         public void valueChanged(ListSelectionEvent e) {
343             fireStateChanged();
344         }
345
346         public void actionPerformed(ActionEvent ev) {
347             if ("SHOW MARKED".equals(ev.getActionCommand())) { // NOI18N
348
beanSelector.setDisplayedBeans(markedBeans);
349             }
350             else if ("SHOW ALL".equals(ev.getActionCommand())) { // NOI18N
351
if (allBeans == null) { // not read yet
352
// PENDING wait cursor
353
allBeans = BeanInstaller.findJavaBeans(currentFiles);
354                     if (libraryNameMap != null) { // need to change root file names to library names
355
remapLibraryNames(allBeans, libraryNameMap);
356                         libraryNameMap = null;
357                     }
358                     Collections.sort(allBeans);
359                 }
360                 beanSelector.setDisplayedBeans(allBeans);
361             }
362         }
363     }
364 }
365
Popular Tags