KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > clientproject > 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.clientproject.ui.customizer;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.Image JavaDoc;
25 import javax.swing.event.ListSelectionEvent JavaDoc;
26 import javax.swing.event.ListSelectionListener JavaDoc;
27 import org.netbeans.api.project.libraries.Library;
28 import org.netbeans.api.project.libraries.LibraryManager;
29 import org.netbeans.api.project.libraries.LibrariesCustomizer;
30 import org.openide.util.HelpCtx;
31 import org.openide.util.WeakListeners;
32
33 import java.beans.PropertyChangeListener JavaDoc;
34 import java.beans.PropertyChangeEvent JavaDoc;
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
41 import java.util.List JavaDoc;
42 import java.util.Set JavaDoc;
43 import javax.swing.AbstractListModel JavaDoc;
44 import javax.swing.DefaultListCellRenderer JavaDoc;
45 import javax.swing.Icon JavaDoc;
46 import javax.swing.ImageIcon JavaDoc;
47 import javax.swing.JButton JavaDoc;
48 import javax.swing.JList JavaDoc;
49
50
51 import org.openide.util.Utilities;
52
53 /**
54  *
55  * @author tz97951
56  */

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

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

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

169
170     // Variables declaration - do not modify//GEN-BEGIN:variables
171
private javax.swing.JButton JavaDoc edit;
172     private javax.swing.JLabel JavaDoc jLabel1;
173     private javax.swing.JList JavaDoc jList1;
174     private javax.swing.JScrollPane JavaDoc jScrollPane1;
175     // End of variables declaration//GEN-END:variables
176

177
178
179     private static final class LibrariesListModel extends AbstractListModel JavaDoc implements PropertyChangeListener JavaDoc {
180
181         private Library[] cache;
182
183         public LibrariesListModel () {
184             LibraryManager manager = LibraryManager.getDefault();
185             manager.addPropertyChangeListener((PropertyChangeListener JavaDoc)WeakListeners.create(PropertyChangeListener JavaDoc.class,
186                     this, manager));
187         }
188
189         public synchronized int getSize() {
190             if (this.cache == null) {
191                 this.cache = this.createLibraries();
192             }
193             return this.cache.length;
194         }
195
196         public synchronized Object JavaDoc getElementAt(int index) {
197             if (this.cache == null) {
198                 this.cache = this.createLibraries();
199             }
200             if (index >= 0 && index < this.cache.length) {
201                 return this.cache[index];
202             }
203             else {
204                 return null;
205             }
206         }
207
208         public synchronized void propertyChange(PropertyChangeEvent JavaDoc evt) {
209             int oldSize = this.cache == null ? 0 : this.cache.length;
210             this.cache = createLibraries();
211             int newSize = this.cache.length;
212             this.fireContentsChanged(this, 0, Math.min(oldSize-1,newSize-1));
213             if (oldSize > newSize) {
214                 this.fireIntervalRemoved(this,newSize,oldSize-1);
215             }
216             else if (oldSize < newSize) {
217                 this.fireIntervalAdded(this,oldSize,newSize-1);
218             }
219         }
220         
221         public synchronized Library[] getLibraries () {
222             if (this.cache == null) {
223                 this.cache = this.createLibraries();
224             }
225             return this.cache;
226         }
227
228         private Library[] createLibraries () {
229             Library[] libs = LibraryManager.getDefault().getLibraries();
230             Arrays.sort(libs, new Comparator JavaDoc<Library>() {
231                 public int compare (Library l1, Library l2) {
232                     String JavaDoc name1 = l1.getDisplayName();
233                     String JavaDoc name2 = l2.getDisplayName();
234                     return name1.compareToIgnoreCase(name2);
235                 }
236             });
237             return libs;
238         }
239     }
240
241
242     private final class LibraryRenderer extends DefaultListCellRenderer JavaDoc {
243         
244         private static final String JavaDoc LIBRARY_ICON = "org/netbeans/modules/j2ee/clientproject/ui/resources/libraries.gif"; //NOI18N
245
private Icon JavaDoc cachedIcon;
246         
247         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
248             String JavaDoc displayName = null;
249             String JavaDoc toolTip = null;
250             Color JavaDoc color = null;
251             if (value instanceof Library) {
252                 Library lib = ((Library)value);
253                 displayName = lib.getDisplayName();
254 //Commented out to be compatible with Web Project
255
// if (containedLibraries.contains(lib)) {
256
// color = Color.GRAY;
257
// toolTip = NbBundle.getMessage(LibrariesChooser.class,"MSG_LibraryAlreadyIncluded");
258
// }
259
}
260             super.getListCellRendererComponent(list, displayName, index, isSelected, cellHasFocus);
261 //Commented out to be compatible with Web Project
262
// if (toolTip!=null) {
263
// setToolTipText (toolTip);
264
// }
265

266             setIcon(createIcon());
267 //Commented out to be compatible with Web Project
268
// if (color != null) {
269
// setForeground (color);
270
// }
271
return this;
272         }
273         
274         private synchronized Icon JavaDoc createIcon () {
275             if (this.cachedIcon == null) {
276                 Image JavaDoc img = Utilities.loadImage(LIBRARY_ICON);
277                 this.cachedIcon = new ImageIcon JavaDoc (img);
278             }
279             return this.cachedIcon;
280         }
281         
282     }
283
284 }
285
Popular Tags