KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > ui > customizer > LibrariesChooser


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.web.project.ui.customizer;
21
22 import javax.swing.event.ListSelectionEvent JavaDoc;
23 import javax.swing.event.ListSelectionListener JavaDoc;
24 import org.netbeans.api.project.libraries.Library;
25 import org.netbeans.api.project.libraries.LibraryManager;
26 import org.netbeans.api.project.libraries.LibrariesCustomizer;
27 import org.openide.util.HelpCtx;
28 import org.openide.util.WeakListeners;
29
30 import javax.swing.*;
31 import java.awt.*;
32 import java.beans.PropertyChangeListener JavaDoc;
33 import java.beans.PropertyChangeEvent JavaDoc;
34
35 import java.util.ArrayList JavaDoc;
36 import java.util.Arrays JavaDoc;
37 import java.util.Collection JavaDoc;
38 import java.util.Comparator JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.List JavaDoc;
41
42
43 import org.openide.util.Utilities;
44
45 /**
46  *
47  * @author tz97951
48  */

49 public class LibrariesChooser extends javax.swing.JPanel JavaDoc implements HelpCtx.Provider {
50
51     /** Creates new form LibrariesChooser */
52     public LibrariesChooser (final JButton addLibraryOption, String JavaDoc j2eePlatform) {
53         initComponents();
54         jList1.setPrototypeCellValue("0123456789012345678901234"); //NOI18N
55
jList1.setModel(new LibrariesListModel(j2eePlatform));
56         jList1.setCellRenderer(new LibraryRenderer());
57         jList1.addListSelectionListener( new ListSelectionListener JavaDoc () {
58             public void valueChanged(ListSelectionEvent JavaDoc e) {
59                 if (e.getValueIsAdjusting()) {
60                     return;
61                 }
62                 addLibraryOption.setEnabled (jList1.getSelectedIndices().length != 0);
63             }
64         });
65     }
66
67     public HelpCtx getHelpCtx() {
68         return new HelpCtx( LibrariesChooser.class );
69     }
70
71     public Library[] getSelectedLibraries () {
72         Object JavaDoc[] selected = this.jList1.getSelectedValues();
73         Library[] libraries = new Library[selected.length];
74         System.arraycopy(selected,0,libraries,0,selected.length);
75         return libraries;
76     }
77
78     /** This method is called from within the constructor to
79      * initialize the form.
80      * WARNING: Do NOT modify this code. The content of this method is
81      * always regenerated by the Form Editor.
82      */

83     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
84
private void initComponents() {
85         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
86
87         jLabel1 = new javax.swing.JLabel JavaDoc();
88         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
89         jList1 = new javax.swing.JList JavaDoc();
90         edit = new javax.swing.JButton JavaDoc();
91
92         setLayout(new java.awt.GridBagLayout JavaDoc());
93
94         getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(LibrariesChooser.class).getString("AD_LibrariesChooser"));
95         jLabel1.setDisplayedMnemonic(org.openide.util.NbBundle.getBundle(LibrariesChooser.class).getString("MNE_InstalledLibraries").charAt(0));
96         jLabel1.setLabelFor(jList1);
97         org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getBundle(LibrariesChooser.class).getString("CTL_InstalledLibraries"));
98         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
99         gridBagConstraints.gridx = 0;
100         gridBagConstraints.gridy = 0;
101         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
102         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
103         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
104         gridBagConstraints.weightx = 1.0;
105         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 0, 12);
106         add(jLabel1, gridBagConstraints);
107
108         jScrollPane1.setViewportView(jList1);
109         jList1.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(LibrariesChooser.class).getString("AD_jScrollPaneLibraries"));
110
111         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
112         gridBagConstraints.gridx = 0;
113         gridBagConstraints.gridy = 1;
114         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
115         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
116         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
117         gridBagConstraints.weightx = 1.0;
118         gridBagConstraints.weighty = 1.0;
119         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 12, 12, 12);
120         add(jScrollPane1, gridBagConstraints);
121
122         org.openide.awt.Mnemonics.setLocalizedText(edit, org.openide.util.NbBundle.getBundle(LibrariesChooser.class).getString("CTL_EditLibraries"));
123         edit.addActionListener(new java.awt.event.ActionListener JavaDoc() {
124             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
125                 editLibraries(evt);
126             }
127         });
128
129         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
130         gridBagConstraints.gridx = 0;
131         gridBagConstraints.gridy = 2;
132         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
133         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 0, 12);
134         add(edit, gridBagConstraints);
135         edit.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(LibrariesChooser.class).getString("AD_jButtonManageLibraries"));
136
137     }// </editor-fold>//GEN-END:initComponents
138

139     private void editLibraries(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_editLibraries
140
LibrariesListModel model = (LibrariesListModel) jList1.getModel ();
141         Collection JavaDoc oldLibraries = Arrays.asList(model.getLibraries());
142         LibrariesCustomizer.showCustomizer((Library)this.jList1.getSelectedValue());
143         List JavaDoc currentLibraries = Arrays.asList(model.getLibraries());
144         Collection JavaDoc newLibraries = new ArrayList JavaDoc (currentLibraries);
145         
146         newLibraries.removeAll(oldLibraries);
147         int indexes[] = new int [newLibraries.size()];
148         
149         Iterator JavaDoc it = newLibraries.iterator();
150         for (int i=0; it.hasNext();i++) {
151             Library lib = (Library) it.next ();
152             indexes[i] = currentLibraries.indexOf (lib);
153         }
154         this.jList1.setSelectedIndices (indexes);
155     }//GEN-LAST:event_editLibraries
156

157
158     // Variables declaration - do not modify//GEN-BEGIN:variables
159
private javax.swing.JButton JavaDoc edit;
160     private javax.swing.JLabel JavaDoc jLabel1;
161     private javax.swing.JList JavaDoc jList1;
162     private javax.swing.JScrollPane JavaDoc jScrollPane1;
163     // End of variables declaration//GEN-END:variables
164

165
166
167     private static final class LibrariesListModel extends AbstractListModel implements PropertyChangeListener JavaDoc {
168
169         private Library[] cache;
170
171         private String JavaDoc j2eePlatform;
172         private int numberOfLibs;
173         private static ArrayList JavaDoc filter13 = new ArrayList JavaDoc ();
174         
175         static {
176             filter13.add(LibraryManager.getDefault().getLibrary("jstl11"));
177             filter13.add(LibraryManager.getDefault().getLibrary("jaxrpc16"));
178             filter13.add(LibraryManager.getDefault().getLibrary("toplink"));
179             filter13.add(LibraryManager.getDefault().getLibrary("Spring"));
180             filter13.add(LibraryManager.getDefault().getLibrary("jaxws20"));
181             filter13.add(LibraryManager.getDefault().getLibrary("jaxb20"));
182             filter13.add(LibraryManager.getDefault().getLibrary("struts"));
183             filter13.add(LibraryManager.getDefault().getLibrary("jsf"));
184         }
185
186         public LibrariesListModel (String JavaDoc j2eePlatform) {
187             this.j2eePlatform = j2eePlatform;
188             LibraryManager manager = LibraryManager.getDefault();
189             manager.addPropertyChangeListener((PropertyChangeListener JavaDoc)WeakListeners.create(PropertyChangeListener JavaDoc.class,
190                     this, manager));
191         }
192
193         public synchronized int getSize() {
194             if (this.cache == null) {
195                 this.cache = this.createLibraries();
196             }
197             return this.cache.length;
198         }
199
200         public synchronized Object JavaDoc getElementAt(int index) {
201             if (this.cache == null) {
202                 this.cache = this.createLibraries();
203             }
204             if (index >= 0 && index < this.cache.length) {
205                 return this.cache[index];
206             }
207             else {
208                 return null;
209             }
210         }
211
212         public synchronized void propertyChange(PropertyChangeEvent JavaDoc evt) {
213             int oldSize = this.cache == null ? 0 : numberOfLibs;
214             this.cache = createLibraries();
215             int newSize = numberOfLibs;
216             this.fireContentsChanged(this, 0, Math.min(oldSize-1,newSize-1));
217             if (oldSize > newSize) {
218                 this.fireIntervalRemoved(this,newSize,oldSize-1);
219             }
220             else if (oldSize < newSize) {
221                 this.fireIntervalAdded(this,oldSize,newSize-1);
222             }
223         }
224         
225         public synchronized Library[] getLibraries () {
226             if (this.cache == null) {
227                 this.cache = this.createLibraries();
228             }
229             return this.cache;
230         }
231
232         private Library[] createLibraries () {
233             Library[] libs = LibraryManager.getDefault().getLibraries();
234             
235             numberOfLibs = libs.length;
236             ArrayList JavaDoc asList = new ArrayList JavaDoc ();
237             for (int i = 0; i < libs.length; i++) {
238                 if (j2eePlatform.equals("1.3") && filter13.contains(libs[i]))
239                     continue;
240                 asList.add(libs [i]);
241             }
242             libs = (Library[]) asList.toArray(new Library [asList.size()]);
243             
244             Arrays.sort(libs, new Comparator JavaDoc () {
245                 public int compare (Object JavaDoc o1, Object JavaDoc o2) {
246                     assert (o1 instanceof Library) && (o2 instanceof Library);
247                     String JavaDoc name1 = ((Library)o1).getDisplayName();
248                     String JavaDoc name2 = ((Library)o2).getDisplayName();
249                     return name1.compareToIgnoreCase(name2);
250                 }
251             });
252             return libs;
253         }
254     }
255
256
257     private final class LibraryRenderer extends DefaultListCellRenderer {
258         
259         private static final String JavaDoc LIBRARY_ICON = "org/netbeans/modules/web/project/ui/resources/libraries.gif"; //NOI18N
260
private Icon cachedIcon;
261         
262         public Component getListCellRendererComponent(JList list, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
263             String JavaDoc displayName = null;
264             if (value instanceof Library) {
265                 Library lib = ((Library)value);
266                 displayName = lib.getDisplayName();
267             }
268             super.getListCellRendererComponent(list, displayName, index, isSelected, cellHasFocus);
269
270             setIcon(createIcon());
271             return this;
272         }
273         
274         private synchronized Icon createIcon () {
275             if (this.cachedIcon == null) {
276                 Image img = Utilities.loadImage(LIBRARY_ICON);
277                 this.cachedIcon = new ImageIcon (img);
278             }
279             return this.cachedIcon;
280         }
281         
282     }
283
284 }
285
Popular Tags