KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbjarproject > 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.ejbjarproject.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     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
83
private void initComponents() {
84         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
85
86         jLabel1 = new javax.swing.JLabel JavaDoc();
87         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
88         jList1 = new javax.swing.JList JavaDoc();
89         edit = new javax.swing.JButton JavaDoc();
90
91         setLayout(new java.awt.GridBagLayout JavaDoc());
92
93         setPreferredSize(new java.awt.Dimension JavaDoc(350, 250));
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     }
138     // </editor-fold>//GEN-END:initComponents
139

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

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

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

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