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-2007 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.visualweb.complib.ui;
043:
044: import java.awt.Component;
045: import java.beans.PropertyChangeEvent;
046: import java.beans.PropertyChangeListener;
047: import java.util.ArrayList;
048: import java.util.List;
049:
050: import javax.swing.AbstractListModel;
051: import javax.swing.DefaultListCellRenderer;
052: import javax.swing.Icon;
053: import javax.swing.ImageIcon;
054: import javax.swing.JButton;
055: import javax.swing.JList;
056: import javax.swing.event.ListSelectionEvent;
057: import javax.swing.event.ListSelectionListener;
058:
059: import org.netbeans.api.project.Project;
060: import org.netbeans.modules.visualweb.complib.Complib;
061: import org.netbeans.modules.visualweb.complib.ComplibServiceProvider;
062: import org.netbeans.modules.visualweb.complib.ExtensionComplib;
063: import org.openide.util.HelpCtx;
064: import org.openide.util.Utilities;
065: import org.openide.util.WeakListeners;
066:
067: /**
068: * Derived from NetBeans LibrariesChooser
069: *
070: * @author Edwin Goei
071: */
072: class ComplibChooser extends javax.swing.JPanel implements
073: HelpCtx.Provider {
074:
075: private static final ComplibServiceProvider csp = ComplibServiceProvider
076: .getInstance();
077:
078: /**
079: * Creates new form ComplibChooser
080: */
081: public ComplibChooser(final JButton addComplibOption,
082: Project project) {
083: initComponents();
084:
085: jList1.setPrototypeCellValue("0123456789012345678901234"); // NOI18N
086: ComplibListModel complibListModel = new ComplibListModel(
087: project);
088: jList1.setModel(complibListModel);
089: jList1.setCellRenderer(new ComplibRenderer());
090: jList1.addListSelectionListener(new ListSelectionListener() {
091: public void valueChanged(ListSelectionEvent e) {
092: if (e.getValueIsAdjusting()) {
093: return;
094: }
095: addComplibOption
096: .setEnabled(jList1.getSelectedIndices().length != 0);
097: }
098: });
099:
100: // If there are items in the list, select the first one
101: if (complibListModel.getSize() > 0) {
102: jList1.setSelectedIndex(0);
103: }
104: }
105:
106: public HelpCtx getHelpCtx() {
107: return new HelpCtx(ComplibChooser.class);
108: }
109:
110: public ArrayList<ExtensionComplib> getSelectedComplibs() {
111: ArrayList<ExtensionComplib> retVal = new ArrayList<ExtensionComplib>();
112: Object[] selectedValues = jList1.getSelectedValues();
113: for (Object val : selectedValues) {
114: if (val instanceof ExtensionComplib) {
115: retVal.add((ExtensionComplib) val);
116: }
117: }
118: return retVal;
119: }
120:
121: /** This method is called from within the constructor to
122: * initialize the form.
123: * WARNING: Do NOT modify this code. The content of this method is
124: * always regenerated by the Form Editor.
125: */
126: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
127: private void initComponents() {
128: java.awt.GridBagConstraints gridBagConstraints;
129:
130: jLabel1 = new javax.swing.JLabel();
131: jScrollPane1 = new javax.swing.JScrollPane();
132: jList1 = new javax.swing.JList();
133: edit = new javax.swing.JButton();
134:
135: setLayout(new java.awt.GridBagLayout());
136:
137: getAccessibleContext().setAccessibleDescription(
138: org.openide.util.NbBundle.getBundle(
139: ComplibChooser.class).getString(
140: "ComplibChooser.AD"));
141: jLabel1.setLabelFor(jList1);
142: org.openide.awt.Mnemonics.setLocalizedText(jLabel1,
143: org.openide.util.NbBundle.getBundle(
144: ComplibChooser.class).getString(
145: "ComplibChooser.installedLabel"));
146: gridBagConstraints = new java.awt.GridBagConstraints();
147: gridBagConstraints.gridx = 0;
148: gridBagConstraints.gridy = 0;
149: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
150: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
151: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
152: gridBagConstraints.weightx = 1.0;
153: gridBagConstraints.insets = new java.awt.Insets(12, 12, 0, 12);
154: add(jLabel1, gridBagConstraints);
155:
156: jScrollPane1.setViewportView(jList1);
157: jList1.getAccessibleContext().setAccessibleDescription(
158: org.openide.util.NbBundle.getBundle(
159: ComplibChooser.class).getString(
160: "ComplibChooser.scrollpane.AD"));
161:
162: gridBagConstraints = new java.awt.GridBagConstraints();
163: gridBagConstraints.gridx = 0;
164: gridBagConstraints.gridy = 1;
165: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
166: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
167: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
168: gridBagConstraints.weightx = 1.0;
169: gridBagConstraints.weighty = 1.0;
170: gridBagConstraints.insets = new java.awt.Insets(6, 12, 12, 12);
171: add(jScrollPane1, gridBagConstraints);
172:
173: org.openide.awt.Mnemonics.setLocalizedText(edit,
174: org.openide.util.NbBundle.getBundle(
175: ComplibChooser.class).getString(
176: "ComplibChooser.edit"));
177: edit.addActionListener(new java.awt.event.ActionListener() {
178: public void actionPerformed(java.awt.event.ActionEvent evt) {
179: editLibraries(evt);
180: }
181: });
182:
183: gridBagConstraints = new java.awt.GridBagConstraints();
184: gridBagConstraints.gridx = 0;
185: gridBagConstraints.gridy = 2;
186: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
187: gridBagConstraints.insets = new java.awt.Insets(0, 12, 0, 12);
188: add(edit, gridBagConstraints);
189: edit.getAccessibleContext().setAccessibleDescription(
190: org.openide.util.NbBundle.getBundle(
191: ComplibChooser.class).getString(
192: "ComplibChooser.button.AD"));
193:
194: }// </editor-fold>//GEN-END:initComponents
195:
196: private void editLibraries(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editLibraries
197: // TODO Make this nicer where new complib is selected, etc.
198: new CompLibManagerPanel().showDialog();
199:
200: // ComplibListModel model = (ComplibListModel) jList1.getModel ();
201: // Collection oldLibraries = Arrays.asList(model.getLibraries());
202: // LibrariesCustomizer.showCustomizer((Library)this.jList1.getSelectedValue());
203: // List currentLibraries = Arrays.asList(model.getLibraries());
204: // Collection newLibraries = new ArrayList (currentLibraries);
205: //
206: // newLibraries.removeAll(oldLibraries);
207: // int indexes[] = new int [newLibraries.size()];
208: //
209: // Iterator it = newLibraries.iterator();
210: // for (int i=0; it.hasNext();i++) {
211: // Library lib = (Library) it.next ();
212: // indexes[i] = currentLibraries.indexOf (lib);
213: // }
214: // this.jList1.setSelectedIndices (indexes);
215: }//GEN-LAST:event_editLibraries
216:
217: // Variables declaration - do not modify//GEN-BEGIN:variables
218: private javax.swing.JButton edit;
219: private javax.swing.JLabel jLabel1;
220: private javax.swing.JList jList1;
221: private javax.swing.JScrollPane jScrollPane1;
222:
223: // End of variables declaration//GEN-END:variables
224:
225: private static final class ComplibListModel extends
226: AbstractListModel implements PropertyChangeListener {
227: private Project project;
228:
229: private List<ExtensionComplib> addableComplibs;
230:
231: public ComplibListModel(Project project) {
232: this .project = project;
233: csp
234: .addPropertyChangeListener((PropertyChangeListener) WeakListeners
235: .create(PropertyChangeListener.class, this ,
236: csp));
237: }
238:
239: public int getSize() {
240: loadAddableComplibs();
241: return addableComplibs.size();
242: }
243:
244: public Object getElementAt(int index) {
245: loadAddableComplibs();
246: return addableComplibs.get(index);
247: }
248:
249: public void propertyChange(PropertyChangeEvent evt) {
250: addableComplibs = null;
251: fireContentsChanged(this , 0, getSize() - 1);
252: }
253:
254: private void loadAddableComplibs() {
255: if (addableComplibs == null) {
256: addableComplibs = csp.getAddableComplibs(project);
257: }
258: }
259: }
260:
261: private static class ComplibRenderer extends
262: DefaultListCellRenderer {
263: private static final Icon icon;
264: static {
265: String iconPath = ComplibChooser.class.getPackage()
266: .getName().replace('.', '/')
267: + "/images/library.png";
268: icon = new ImageIcon(Utilities.loadImage(iconPath));
269: }
270:
271: public Component getListCellRendererComponent(JList list,
272: Object value, int index, boolean isSelected,
273: boolean cellHasFocus) {
274: String displayName = null;
275: if (value instanceof Complib) {
276: Complib complib = (Complib) value;
277: displayName = complib.getVersionedTitle();
278: }
279: super.getListCellRendererComponent(list, displayName,
280: index, isSelected, cellHasFocus);
281:
282: setIcon(icon);
283: return this;
284: }
285: }
286: }
|