KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.swing.*;
23 import javax.swing.border.*;
24 import javax.swing.event.*;
25 import java.awt.Component JavaDoc;
26 import java.awt.event.*;
27 import java.beans.*;
28 import java.util.*;
29 import java.io.File JavaDoc;
30 import java.net.URL JavaDoc;
31
32 import org.openide.WizardDescriptor;
33 import org.openide.ErrorManager;
34 import org.openide.filesystems.*;
35 import org.netbeans.api.project.libraries.*;
36
37 /**
38  * The first panel in the wizard for adding new components to the palette from
39  * a library. In this panel the user chooses a library from available libraries
40  * installed in the IDE.
41  *
42  * @author Tomas Pavek
43  */

44
45 class ChooseLibraryWizardPanel implements WizardDescriptor.Panel {
46
47     private LibrarySelector librarySelector;
48
49 // private AddToPaletteWizard wizard;
50

51     private EventListenerList listenerList;
52
53     // ----------
54
// WizardDescriptor.Panel implementation
55

56     public java.awt.Component JavaDoc getComponent() {
57         if (librarySelector == null) {
58             librarySelector = new LibrarySelector();
59
60             // wizard API: set the caption and index of this panel
61
librarySelector.setName(
62                 PaletteUtils.getBundleString("CTL_SelectLibrary_Caption")); // NOI18N
63
librarySelector.putClientProperty("WizardPanel_contentSelectedIndex", // NOI18N
64
new Integer JavaDoc(0));
65
66             librarySelector.list.addListSelectionListener(
67                 new ListSelectionListener() {
68                     public void valueChanged(ListSelectionEvent e) {
69                         fireStateChanged();
70                     }
71                 });
72
73 // librarySelector.list.addActionListener(new ActionListener() {
74
// public void actionPerformed(ActionEvent ev) {
75
// wizard.stepToNext();
76
// }
77
// });
78
}
79
80         return librarySelector;
81     }
82
83     public org.openide.util.HelpCtx getHelp() {
84         // PENDING
85
return new org.openide.util.HelpCtx("beans.adding"); // NOI18N
86
}
87
88     public boolean isValid() {
89         return librarySelector != null
90                && librarySelector.getSelectedLibraries() != null;
91     }
92
93     public void readSettings(Object JavaDoc settings) {
94 // wizard = (AddToPaletteWizard) settings;
95
}
96
97     public void storeSettings(Object JavaDoc settings) {
98         if (librarySelector != null) { // create the UI component for the wizard step
99
Library[] libraries = librarySelector.getSelectedLibraries();
100
101             // collect the roots making up the classpath of the libraries
102
// (presumably JAR files)
103

104             Map fileMap = new HashMap(); // to avoid duplicities in case some JAR file is in more libraries
105
Map libraryMap = new HashMap(); // to remember libraries for JAR files
106

107             for (int i=0; i < libraries.length; i++) {
108                 List content = libraries[i].getContent("classpath"); // NOI18N
109
// go through classpath roots of the library
110
for (Iterator it=content.iterator(); it.hasNext(); ) {
111                     URL JavaDoc rootURL = (URL JavaDoc) it.next();
112                     if ("jar".equals(rootURL.getProtocol())) { // NOI18N
113
String JavaDoc path = rootURL.getPath();
114                         int index = path.lastIndexOf('!');
115                         if (index != -1) {
116                             try {
117                                 rootURL = new URL JavaDoc(path.substring(0, index));
118                             } catch (java.net.MalformedURLException JavaDoc mex) {
119                                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, mex);
120                                 continue;
121                             }
122                         }
123                     }
124                     File JavaDoc rootFile = FileUtil.toFile(URLMapper.findFileObject(rootURL));
125                     String JavaDoc rootPath = rootFile.getAbsolutePath();
126                     fileMap.put(rootPath, rootFile);
127                     libraryMap.put(rootPath, libraries[i].getName());
128                 }
129             }
130
131             File JavaDoc[] libFiles = new File JavaDoc[fileMap.size()];
132             fileMap.values().toArray(libFiles);
133
134             AddToPaletteWizard wizard = (AddToPaletteWizard) settings;
135             wizard.setJARFiles(libFiles);
136             wizard.libraryNameMap = libraryMap;
137         }
138     }
139
140     public void addChangeListener(ChangeListener listener) {
141         if (listenerList == null)
142             listenerList = new EventListenerList();
143         listenerList.add(ChangeListener.class, listener);
144     }
145
146     public void removeChangeListener(ChangeListener listener) {
147         if (listenerList != null)
148             listenerList.remove(ChangeListener.class, listener);
149     }
150
151     // -------
152

153     void fireStateChanged() {
154         if (listenerList == null)
155             return;
156
157         ChangeEvent e = null;
158         Object JavaDoc[] listeners = listenerList.getListenerList();
159         for (int i=listeners.length-2; i >= 0; i-=2) {
160             if (listeners[i] == ChangeListener.class) {
161                 if (e == null)
162                     e = new ChangeEvent(this);
163                 ((ChangeListener)listeners[i+1]).stateChanged(e);
164             }
165         }
166     }
167
168     // ---------
169

170     static class LibrarySelector extends JPanel {
171
172         JList list;
173         List libList;
174
175         LibrarySelector() {
176             list = new JList();
177             list.setCellRenderer(new LibraryRenderer());
178             list.setLayoutOrientation(JList.VERTICAL_WRAP);
179             list.setVisibleRowCount(0);
180             updateLibraryList();
181
182             setLayout(new java.awt.GridBagLayout JavaDoc());
183             java.awt.GridBagConstraints JavaDoc gridBagConstraints;
184
185             JLabel label1 = new JLabel();
186             org.openide.awt.Mnemonics.setLocalizedText(
187                 label1, PaletteUtils.getBundleString("CTL_Libraries")); // NOI18N
188
label1.setLabelFor(list);
189             gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
190             gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 0);
191             gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
192             add(label1, gridBagConstraints);
193
194 // JButton button1 = new JButton();
195
// org.openide.awt.Mnemonics.setLocalizedText(
196
// button1, PaletteUtils.getBundleString("CTL_LibrariesManager")); // NOI18N
197
// gridBagConstraints = new java.awt.GridBagConstraints();
198
// gridBagConstraints.gridx = 2;
199
// gridBagConstraints.gridy = 2;
200
// gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 0);
201
// gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
202
// add(button1, gridBagConstraints);
203

204             JScrollPane scrollpane1 = new javax.swing.JScrollPane JavaDoc();
205             scrollpane1.setViewportView(list);
206             gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
207             gridBagConstraints.gridx = 0;
208             gridBagConstraints.gridy = 1;
209             gridBagConstraints.gridwidth = 3;
210             gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
211             gridBagConstraints.weightx = 1.0;
212             gridBagConstraints.weighty = 1.0;
213             add(scrollpane1, gridBagConstraints);
214
215 // button1.addActionListener(new ActionListener() {
216
// public void actionPerformed(ActionEvent ev) {
217
// Library lib = null;
218
// Object[] selected = list.getSelectedValues();
219
// if (selected.length > 0)
220
// lib = (Library) selected[0];
221
// if (LibrariesCustomizer.showCustomizer(lib))
222
// updateLibraryList();
223
// }
224
// });
225
}
226
227         Library[] getSelectedLibraries() {
228             Object JavaDoc[] selected = list.getSelectedValues();
229             Library[] libraries = new Library[selected.length];
230             for (int i=0; i < selected.length; i++)
231                 libraries[i] = (Library) selected[i];
232             return libraries;
233         }
234
235         void updateLibraryList() {
236             Library[] libraries = LibraryManager.getDefault().getLibraries();
237             libList = new ArrayList(libraries.length);
238             for (int i=0; i < libraries.length; i++)
239                 if (libraries[i].getType().equals("j2se")) // NOI18N
240
libList.add(libraries[i]);
241
242             list.setModel(new AbstractListModel() {
243                 public int getSize() { return libList.size(); }
244                 public Object JavaDoc getElementAt(int i) { return libList.get(i); }
245             });
246         }
247
248         public void addNotify() {
249             super.addNotify();
250             list.requestFocus();
251         }
252
253         public java.awt.Dimension JavaDoc getPreferredSize() {
254             return new java.awt.Dimension JavaDoc(400, 300);
255         }
256     }
257
258     private static class LibraryRenderer extends JLabel
259                                          implements ListCellRenderer
260     {
261         private static final Border hasFocusBorder =
262             new LineBorder(UIManager.getColor("List.focusCellHighlight")); // NOI18N
263
private static final Border noFocusBorder = BorderFactory.createEmptyBorder(1, 1, 1, 1);
264
265         public LibraryRenderer() {
266             setOpaque(true);
267             setBorder(noFocusBorder);
268         }
269
270         public Component JavaDoc getListCellRendererComponent(JList list,
271                                                       Object JavaDoc value,
272                                                       int index,
273                                                       boolean isSelected,
274                                                       boolean cellHasFocus)
275         {
276             Library lib = (Library) value;
277             setText(lib.getDisplayName());
278
279             if (isSelected){
280                 setBackground(UIManager.getColor("List.selectionBackground")); // NOI18N
281
setForeground(UIManager.getColor("List.selectionForeground")); // NOI18N
282
}
283             else {
284                 setBackground(list.getBackground());
285                 setForeground(list.getForeground());
286             }
287             setBorder(cellHasFocus ? hasFocusBorder : noFocusBorder);
288             return this;
289         }
290     }
291 }
292
Popular Tags