001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.project.support.customizer;
043:
044: import org.netbeans.api.project.libraries.Library;
045: import org.netbeans.api.project.libraries.LibraryManager;
046: import org.netbeans.api.project.libraries.LibrariesCustomizer;
047: import org.openide.util.WeakListeners;
048:
049: import java.util.Comparator;
050: import java.util.Arrays;
051: import javax.swing.*;
052: import java.awt.*;
053: import java.beans.PropertyChangeListener;
054: import java.beans.PropertyChangeEvent;
055: import java.util.ArrayList;
056: import org.openide.util.NbBundle;
057:
058: /**
059: *
060: * @author tz97951
061: */
062: public class LibrariesChooser extends javax.swing.JPanel {
063:
064: /** Creates new form LibrariesChooser */
065: public LibrariesChooser(String libraryType) {
066: initComponents();
067: initAccessibility();
068: jList1.setPrototypeCellValue("0123456789012345678901234"); //NOI18N
069: jList1.setModel(new LibrariesListModel(libraryType));
070: jList1.setCellRenderer(new LibraryRenderer());
071: }
072:
073: public Library[] getSelectedLibraries() {
074: final Object[] selected = this .jList1.getSelectedValues();
075: final Library[] libraries = new Library[selected.length];
076: System.arraycopy(selected, 0, libraries, 0, selected.length);
077: return libraries;
078: }
079:
080: /** This method is called from within the constructor to
081: * initialize the form.
082: * WARNING: Do NOT modify this code. The content of this method is
083: * always regenerated by the Form Editor.
084: */
085: // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
086: private void initComponents() {
087: java.awt.GridBagConstraints gridBagConstraints;
088:
089: jLabel1 = new javax.swing.JLabel();
090: jScrollPane1 = new javax.swing.JScrollPane();
091: jList1 = new javax.swing.JList();
092: edit = new javax.swing.JButton();
093:
094: setLayout(new java.awt.GridBagLayout());
095:
096: jLabel1.setLabelFor(jList1);
097: org.openide.awt.Mnemonics.setLocalizedText(jLabel1, NbBundle
098: .getMessage(LibrariesChooser.class,
099: "LAB_LIbCh_InstalledLibraries")); // NOI18N
100: gridBagConstraints = new java.awt.GridBagConstraints();
101: gridBagConstraints.gridx = 0;
102: gridBagConstraints.gridy = 0;
103: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
104: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
105: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
106: gridBagConstraints.weightx = 1.0;
107: gridBagConstraints.insets = new java.awt.Insets(12, 12, 6, 12);
108: add(jLabel1, gridBagConstraints);
109: jLabel1.getAccessibleContext().setAccessibleName(
110: org.openide.util.NbBundle.getMessage(
111: LibrariesChooser.class,
112: "ACCESSIBLE_NAME_jLabel1")); // NOI18N
113: jLabel1.getAccessibleContext().setAccessibleDescription(
114: org.openide.util.NbBundle.getMessage(
115: LibrariesChooser.class,
116: "ACCESSIBLE_DESCRIPTION_jLabel1")); // NOI18N
117:
118: jScrollPane1.setViewportView(jList1);
119:
120: gridBagConstraints = new java.awt.GridBagConstraints();
121: gridBagConstraints.gridx = 0;
122: gridBagConstraints.gridy = 1;
123: gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
124: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
125: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
126: gridBagConstraints.weightx = 1.0;
127: gridBagConstraints.weighty = 1.0;
128: gridBagConstraints.insets = new java.awt.Insets(6, 12, 12, 12);
129: add(jScrollPane1, gridBagConstraints);
130:
131: org.openide.awt.Mnemonics.setLocalizedText(edit, NbBundle
132: .getMessage(LibrariesChooser.class, "LBL_LibCh_Edit")); // NOI18N
133: edit.addActionListener(new java.awt.event.ActionListener() {
134: public void actionPerformed(java.awt.event.ActionEvent evt) {
135: editLibraries(evt);
136: }
137: });
138: gridBagConstraints = new java.awt.GridBagConstraints();
139: gridBagConstraints.gridx = 1;
140: gridBagConstraints.gridy = 1;
141: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
142: gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
143: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
144: gridBagConstraints.insets = new java.awt.Insets(6, 0, 12, 12);
145: add(edit, gridBagConstraints);
146: edit.getAccessibleContext()
147: .setAccessibleName(
148: org.openide.util.NbBundle.getMessage(
149: LibrariesChooser.class,
150: "ACCESSIBLE_NAME_edit")); // NOI18N
151: edit.getAccessibleContext().setAccessibleDescription(
152: org.openide.util.NbBundle.getMessage(
153: LibrariesChooser.class,
154: "ACCESSIBLE_DESCRIPTION_edit")); // NOI18N
155: }// </editor-fold>//GEN-END:initComponents
156:
157: private void initAccessibility() {
158: //getAccessibleContext().setAccessibleName();
159: //getAccessibleContext().setAccessibleDescription();
160: }
161:
162: private void editLibraries(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editLibraries
163: LibrariesCustomizer.showCustomizer((Library) this .jList1
164: .getSelectedValue());
165: }//GEN-LAST:event_editLibraries
166:
167: // Variables declaration - do not modify//GEN-BEGIN:variables
168: private javax.swing.JButton edit;
169: private javax.swing.JLabel jLabel1;
170: private javax.swing.JList jList1;
171: private javax.swing.JScrollPane jScrollPane1;
172:
173: // End of variables declaration//GEN-END:variables
174:
175: private final class LibrariesListModel extends AbstractListModel
176: implements PropertyChangeListener {
177:
178: private Library[] cache;
179: private final String libraryType;
180:
181: public LibrariesListModel(String libraryType) {
182: this .libraryType = libraryType;
183: LibraryManager manager = LibraryManager.getDefault();
184: manager.addPropertyChangeListener(WeakListeners.create(
185: PropertyChangeListener.class, this , manager));
186: }
187:
188: public synchronized int getSize() {
189: if (this .cache == null) {
190: this .cache = this .getLibraries();
191: }
192: return this .cache.length;
193: }
194:
195: public synchronized Object getElementAt(final int index) {
196: if (this .cache == null) {
197: this .cache = this .getLibraries();
198: }
199: if (index >= 0 && index < this .cache.length) {
200: return this .cache[index];
201: }
202: return null;
203: }
204:
205: public synchronized void propertyChange(
206: @SuppressWarnings("unused")
207: final PropertyChangeEvent evt) {
208: this .cache = null;
209: }
210:
211: private Library[] getLibraries() {
212: Library[] libs = LibraryManager.getDefault().getLibraries();
213: final ArrayList<Library> filtered = new ArrayList<Library>();
214: for (int i = 0; libs != null && i < libs.length; i++) {
215: if (libraryType == null
216: || libraryType.equals(libs[i].getType())) {
217: filtered.add(libs[i]);
218: }
219: }
220: libs = filtered.toArray(new Library[filtered.size()]);
221: Arrays.sort(libs, new Comparator<Library>() {
222: public int compare(Library o1, Library o2) {
223: final String name1 = o1.getDisplayName();
224: final String name2 = o2.getDisplayName();
225: return name1.compareToIgnoreCase(name2);
226: }
227: });
228: return libs;
229: }
230: }
231:
232: private static final class LibraryRenderer extends
233: DefaultListCellRenderer {
234:
235: private LibraryRenderer() {
236: //Just to avoid creation of accessor class
237: }
238:
239: public Component getListCellRendererComponent(final JList list,
240: final Object value, final int index,
241: final boolean isSelected, final boolean cellHasFocus) {
242: String displayName;
243: if (value instanceof Library) {
244: displayName = ((Library) value).getDisplayName();
245: } else if (value instanceof String) {
246: displayName = (String) value;
247: } else {
248: displayName = value.toString();
249: }
250: return super.getListCellRendererComponent(list,
251: displayName, index, isSelected, cellHasFocus);
252: }
253: }
254:
255: }
|