KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > earproject > 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.j2ee.earproject.ui.customizer;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Image JavaDoc;
24 import java.beans.PropertyChangeEvent JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Comparator JavaDoc;
30 import java.util.List JavaDoc;
31 import javax.swing.AbstractListModel JavaDoc;
32 import javax.swing.DefaultListCellRenderer JavaDoc;
33 import javax.swing.Icon JavaDoc;
34 import javax.swing.ImageIcon JavaDoc;
35 import javax.swing.JList JavaDoc;
36 import javax.swing.event.ListSelectionListener JavaDoc;
37 import org.netbeans.api.project.libraries.LibrariesCustomizer;
38 import org.netbeans.api.project.libraries.Library;
39 import org.netbeans.api.project.libraries.LibraryManager;
40 import org.openide.util.HelpCtx;
41 import org.openide.util.NbBundle;
42 import org.openide.util.Utilities;
43 import org.openide.util.WeakListeners;
44
45 /**
46  * @author tz97951
47  */

48 public class LibrariesChooser extends javax.swing.JPanel JavaDoc implements HelpCtx.Provider {
49
50     private final Collection JavaDoc incompatibleLibs;
51
52     /** Creates new form LibrariesChooser */
53     public LibrariesChooser(Collection JavaDoc alreadySelectedLibs, String JavaDoc j2eePlatform) {
54         initComponents();
55         jList1.setPrototypeCellValue("0123456789012345678901234"); //NOI18N
56
jList1.setModel(new LibrariesListModel());
57         // XXX Examine this to see if we need pass this in from somewhere...
58
incompatibleLibs = java.util.Collections.EMPTY_LIST;
59                 // VisualClasspathSupport.getLibrarySet(WebProjectGenerator.getIncompatibleLibraries(j2eePlatform));
60
jList1.setCellRenderer(new LibraryRenderer(alreadySelectedLibs, incompatibleLibs, j2eePlatform));
61     }
62
63     public Library[] getSelectedLibraries () {
64         Object JavaDoc[] selected = this.jList1.getSelectedValues();
65         Collection JavaDoc<Library> libs = new ArrayList JavaDoc<Library>();
66         for (int i = 0; i < selected.length; i++) {
67             Library lib = (Library) selected[i];
68             if(!incompatibleLibs.contains(lib)) { // incompatible libraries are not added
69
libs.add(lib);
70             }
71         }
72         return libs.toArray(new Library[libs.size()]);
73     }
74
75     public HelpCtx getHelpCtx() {
76         return new HelpCtx(LibrariesChooser.class);
77     }
78
79     public void addListSelectionListener(ListSelectionListener JavaDoc listener) {
80         jList1.addListSelectionListener(listener);
81     }
82
83     public boolean isValidSelection() {
84         Object JavaDoc[] selected = this.jList1.getSelectedValues();
85         if(selected.length == 0) {
86             return false;
87         }
88         for (int i = 0; i < selected.length; i++) {
89             if(incompatibleLibs.contains(selected[i])) {
90                 return false;
91             }
92         }
93         return true;
94     }
95
96     /** This method is called from within the constructor to
97      * initialize the form.
98      * WARNING: Do NOT modify this code. The content of this method is
99      * always regenerated by the Form Editor.
100      */

101     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
102
private void initComponents() {
103         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
104
105         jLabel1 = new javax.swing.JLabel JavaDoc();
106         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
107         jList1 = new javax.swing.JList JavaDoc();
108         edit = new javax.swing.JButton JavaDoc();
109
110         setLayout(new java.awt.GridBagLayout JavaDoc());
111
112         setPreferredSize(new java.awt.Dimension JavaDoc(350, 250));
113         getAccessibleContext().setAccessibleDescription(null); // NOI18N
114
jLabel1.setLabelFor(jList1);
115         org.openide.awt.Mnemonics.setLocalizedText(jLabel1, NbBundle.getMessage(LibrariesChooser.class, "CTL_Libraries")); // NOI18N
116
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
117         gridBagConstraints.gridx = 0;
118         gridBagConstraints.gridy = 0;
119         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
120         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
121         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
122         gridBagConstraints.weightx = 1.0;
123         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 0, 12);
124         add(jLabel1, gridBagConstraints);
125
126         jScrollPane1.setViewportView(jList1);
127         jList1.getAccessibleContext().setAccessibleDescription(null); // NOI18N
128

129         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
130         gridBagConstraints.gridx = 0;
131         gridBagConstraints.gridy = 1;
132         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
133         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
134         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
135         gridBagConstraints.weightx = 1.0;
136         gridBagConstraints.weighty = 1.0;
137         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 12, 12, 12);
138         add(jScrollPane1, gridBagConstraints);
139
140         org.openide.awt.Mnemonics.setLocalizedText(edit, NbBundle.getMessage(LibrariesChooser.class, "CTL_ManageLibraries")); // NOI18N
141
edit.addActionListener(new java.awt.event.ActionListener JavaDoc() {
142             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
143                 editLibraries(evt);
144             }
145         });
146
147         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
148         gridBagConstraints.gridx = 0;
149         gridBagConstraints.gridy = 2;
150         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
151         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 0, 12);
152         add(edit, gridBagConstraints);
153         edit.getAccessibleContext().setAccessibleDescription(null); // NOI18N
154

155     }// </editor-fold>//GEN-END:initComponents
156

157     private void editLibraries(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_editLibraries
158
LibrariesListModel model = (LibrariesListModel) jList1.getModel ();
159         Collection JavaDoc<Library> oldLibraries = Arrays.asList(model.getLibraries());
160         LibrariesCustomizer.showCustomizer((Library)this.jList1.getSelectedValue());
161         List JavaDoc<Library> currentLibraries = Arrays.asList(model.getLibraries());
162         Collection JavaDoc<Library> newLibraries = new ArrayList JavaDoc<Library>(currentLibraries);
163
164         newLibraries.removeAll(oldLibraries);
165         int indexes[] = new int[newLibraries.size()];
166
167         int i = 0;
168         for (Library lib : newLibraries) {
169             indexes[i++] = currentLibraries.indexOf(lib);
170         }
171         this.jList1.setSelectedIndices (indexes);
172     }//GEN-LAST:event_editLibraries
173

174
175     // Variables declaration - do not modify//GEN-BEGIN:variables
176
private javax.swing.JButton JavaDoc edit;
177     private javax.swing.JLabel JavaDoc jLabel1;
178     private javax.swing.JList JavaDoc jList1;
179     private javax.swing.JScrollPane JavaDoc jScrollPane1;
180     // End of variables declaration//GEN-END:variables
181

182
183
184     private static final class LibrariesListModel extends AbstractListModel JavaDoc implements PropertyChangeListener JavaDoc {
185
186         private Library[] cache;
187         /** Number of libraries in the LibraryManager when last refreshed. */
188         private int numberOfLibs;
189         
190         // XXX more stuff that needs a look-see
191

192         //VisualClasspathSupport.getLibrarySet(
193
// WebProjectGenerator.getIncompatibleLibraries(WebModule.J2EE_13_LEVEL));
194
//VisualClasspathSupport.getLibrarySet(
195
// WebProjectGenerator.getIncompatibleLibraries(WebModule.J2EE_14_LEVEL));
196

197         public LibrariesListModel() {
198             LibraryManager manager = LibraryManager.getDefault();
199             manager.addPropertyChangeListener((PropertyChangeListener JavaDoc)WeakListeners.create(PropertyChangeListener JavaDoc.class,
200                     this, manager));
201         }
202
203         public synchronized int getSize() {
204             if (this.cache == null) {
205                 this.cache = this.createLibraries();
206             }
207             return this.cache.length;
208         }
209
210         public synchronized Object JavaDoc getElementAt(int index) {
211             if (this.cache == null) {
212                 this.cache = this.createLibraries();
213             }
214             if (index >= 0 && index < this.cache.length) {
215                 return this.cache[index];
216             }
217             else {
218                 return null;
219             }
220         }
221
222         public synchronized void propertyChange(PropertyChangeEvent JavaDoc evt) {
223             int oldSize = this.cache == null ? 0 : numberOfLibs;
224             this.cache = createLibraries();
225             int newSize = numberOfLibs;
226             this.fireContentsChanged(this, 0, Math.min(oldSize-1,newSize-1));
227             if (oldSize > newSize) {
228                 this.fireIntervalRemoved(this,newSize,oldSize-1);
229             }
230             else if (oldSize < newSize) {
231                 this.fireIntervalAdded(this,oldSize,newSize-1);
232             }
233         }
234
235         public synchronized Library[] getLibraries () {
236             if (this.cache == null) {
237                 this.cache = this.createLibraries();
238             }
239             return this.cache;
240         }
241
242         private Library[] createLibraries () {
243             Library[] libs = LibraryManager.getDefault().getLibraries();
244             numberOfLibs = libs.length;
245             Arrays.sort(libs, new Comparator JavaDoc<Library> () {
246                 public int compare (Library l1, Library l2) {
247                     String JavaDoc name1 = l1.getDisplayName();
248                     String JavaDoc name2 = l2.getDisplayName();
249                     return name1.compareToIgnoreCase(name2);
250                 }
251             });
252             return libs;
253         }
254     }
255
256
257     private static final class LibraryRenderer extends DefaultListCellRenderer JavaDoc {
258
259         private static final String JavaDoc LIBRARY_ICON = "org/netbeans/modules/j2ee/earproject/ui/resources/libraries.gif"; //NOI18N
260
private Icon JavaDoc cachedIcon;
261         private final Collection JavaDoc alreadySelectedLibs;
262         private final Collection JavaDoc incompatibleLibs;
263         private final String JavaDoc j2eePlatform;
264
265         public LibraryRenderer(Collection JavaDoc alreadySelectedLibs, Collection JavaDoc incompatibleLibs, String JavaDoc j2eePlatform) {
266             this.alreadySelectedLibs = alreadySelectedLibs;
267             this.j2eePlatform = j2eePlatform;
268             this.incompatibleLibs = incompatibleLibs;
269         }
270
271         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
272             String JavaDoc displayName = null;
273             if (value instanceof Library) {
274                 displayName = ((Library)value).getDisplayName();
275             }
276             super.getListCellRendererComponent(list, displayName, index, isSelected, cellHasFocus);
277             final String JavaDoc toolTipText;
278             if (value instanceof Library) {
279                 final String JavaDoc libraryString = VisualClasspathSupport.getLibraryString((Library) value);
280                 if (alreadySelectedLibs.contains(value)) {
281                     toolTipText = NbBundle.getMessage(LibrariesChooser.class, "LBL_LibraryAlreadyInProject_ToolTip") +
282                             " !!! (" + libraryString + ")";
283                 } else if (incompatibleLibs.contains(value)) {
284                     toolTipText = NbBundle.getMessage(LibrariesChooser.class, "LBL_IncompatibleLibrary_ToolTip")
285                             + " (" + j2eePlatform + ") !!! (" + libraryString + ")";
286                     setEnabled(false);
287                 } else {
288                     toolTipText = libraryString;
289                 }
290             } else {
291                 toolTipText = null;
292             }
293             setToolTipText(toolTipText);
294             setIcon(createIcon());
295             return this;
296         }
297
298         private synchronized Icon JavaDoc createIcon () {
299             if (this.cachedIcon == null) {
300                 Image JavaDoc img = Utilities.loadImage(LIBRARY_ICON);
301                 this.cachedIcon = new ImageIcon JavaDoc (img);
302             }
303             return this.cachedIcon;
304         }
305
306     }
307
308 }
309
Popular Tags