KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seproject > 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.java.j2seproject.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.*;
36 import java.util.List JavaDoc;
37
38
39 import org.openide.util.Utilities;
40 import org.openide.util.NbBundle;
41 /**
42  *
43  * @author tz97951
44  */

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

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

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

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

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

254             setIcon(createIcon());
255 //Commented out to be compatible with Web Project
256
// if (color != null) {
257
// setForeground (color);
258
// }
259
return this;
260         }
261         
262         private synchronized Icon createIcon () {
263             if (this.cachedIcon == null) {
264                 Image img = Utilities.loadImage(LIBRARY_ICON);
265                 this.cachedIcon = new ImageIcon (img);
266             }
267             return this.cachedIcon;
268         }
269         
270     }
271
272 }
273
Popular Tags