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 java.awt.Container;
045: import java.beans.PropertyChangeEvent;
046: import java.beans.PropertyChangeListener;
047: import java.io.File;
048: import java.io.IOException;
049: import java.lang.reflect.Method;
050: import java.net.URI;
051: import javax.swing.DefaultListModel;
052: import javax.swing.JButton;
053: import javax.swing.JFileChooser;
054: import javax.swing.JPanel;
055: import javax.swing.event.ListSelectionEvent;
056: import javax.swing.event.ListSelectionListener;
057: import org.netbeans.api.project.Project;
058: import org.netbeans.api.project.ProjectManager;
059: import org.netbeans.api.project.ProjectUtils;
060: import org.openide.filesystems.FileObject;
061: import org.openide.filesystems.FileUtil;
062: import org.netbeans.api.project.ant.AntArtifact;
063: import org.netbeans.api.project.ant.AntArtifactQuery;
064: import org.netbeans.spi.project.ui.support.ProjectChooser;
065: import org.openide.DialogDescriptor;
066: import org.openide.DialogDisplayer;
067: import org.openide.util.NbBundle;
068:
069: /** Accessory component used in the ProjectChooser for choosing project
070: * artifacts.
071: *
072: * @author phrebejk
073: */
074: public class AntArtifactChooser extends javax.swing.JPanel implements
075: PropertyChangeListener {
076:
077: // XXX to become an array later
078: private String artifactType;
079: private JFileChooser chooser;
080: private JButton approveButton;
081:
082: /** Creates new form JarArtifactChooser */
083: public AntArtifactChooser(String artifactType, JFileChooser chooser) {
084: this .artifactType = artifactType;
085: this .chooser = chooser;
086: initComponents();
087: initAccessibility();
088: jListArtifacts.setModel(new DefaultListModel());
089: chooser.addPropertyChangeListener(this );
090: super .addNotify();
091: if (setValid(false))
092: jListArtifacts
093: .addListSelectionListener(new ListSelectionListener() {
094: @SuppressWarnings("synthetic-access")
095: public void valueChanged(
096: @SuppressWarnings("unused")
097: final ListSelectionEvent e) {
098: setValid(jListArtifacts.getSelectedIndex() >= 0);
099: }
100: });
101: }
102:
103: /** This method is called from within the constructor to
104: * initialize the form.
105: * WARNING: Do NOT modify this code. The content of this method is
106: * always regenerated by the Form Editor.
107: */
108: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
109: private void initComponents() {
110: java.awt.GridBagConstraints gridBagConstraints;
111:
112: jLabelName = new javax.swing.JLabel();
113: jTextFieldName = new javax.swing.JTextField();
114: jLabelJarFiles = new javax.swing.JLabel();
115: jScrollPane1 = new javax.swing.JScrollPane();
116: jListArtifacts = new javax.swing.JList();
117:
118: setLayout(new java.awt.GridBagLayout());
119:
120: jLabelName.setLabelFor(jTextFieldName);
121: org.openide.awt.Mnemonics.setLocalizedText(jLabelName, NbBundle
122: .getMessage(AntArtifactChooser.class,
123: "LBL_AAChooser_ProjectName")); // NOI18N
124: gridBagConstraints = new java.awt.GridBagConstraints();
125: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
126: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
127: gridBagConstraints.insets = new java.awt.Insets(0, 12, 2, 0);
128: add(jLabelName, gridBagConstraints);
129:
130: jTextFieldName.setEditable(false);
131: gridBagConstraints = new java.awt.GridBagConstraints();
132: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
133: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
134: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
135: gridBagConstraints.weightx = 1.0;
136: gridBagConstraints.insets = new java.awt.Insets(0, 12, 6, 0);
137: add(jTextFieldName, gridBagConstraints);
138:
139: jLabelJarFiles.setLabelFor(jListArtifacts);
140: org.openide.awt.Mnemonics.setLocalizedText(jLabelJarFiles,
141: NbBundle.getMessage(AntArtifactChooser.class,
142: "LBL_AAChooser_JARFiles")); // NOI18N
143: gridBagConstraints = new java.awt.GridBagConstraints();
144: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
145: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
146: gridBagConstraints.insets = new java.awt.Insets(0, 12, 2, 0);
147: add(jLabelJarFiles, gridBagConstraints);
148:
149: jScrollPane1.setViewportView(jListArtifacts);
150:
151: gridBagConstraints = new java.awt.GridBagConstraints();
152: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
153: gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
154: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
155: gridBagConstraints.weightx = 1.0;
156: gridBagConstraints.weighty = 1.0;
157: gridBagConstraints.insets = new java.awt.Insets(0, 12, 0, 0);
158: add(jScrollPane1, gridBagConstraints);
159: }// </editor-fold>//GEN-END:initComponents
160:
161: private void initAccessibility() {
162: //getAccessibleContext().setAccessibleName();
163: //getAccessibleContext().setAccessibleDescription();
164: }
165:
166: private boolean setValid(final boolean valid) {
167: synchronized (this ) {
168: if (approveButton == null)
169: try {
170: final Object ui = chooser.getUI();
171: final Method m = ui.getClass().getDeclaredMethod(
172: "getApproveButton",
173: new Class[] { JFileChooser.class }); //NOI18N
174: m.setAccessible(true);
175: approveButton = (JButton) m.invoke(ui,
176: new Object[] { chooser });
177: } catch (Exception e) {
178: }
179: }
180: if (approveButton != null)
181: approveButton.setEnabled(valid);
182: return approveButton != null;
183: }
184:
185: public void propertyChange(final PropertyChangeEvent e) {
186:
187: if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(e
188: .getPropertyName())) {
189: // We have to update the Accessory
190: final JFileChooser chooser = (JFileChooser) e.getSource();
191: final File dir = chooser.getSelectedFile();
192: final Project project = getProject(dir);
193: populateAccessory(project);
194: }
195: }
196:
197: private Project getProject(final File projectDir) {
198:
199: try {
200: final FileObject fo = FileUtil.toFileObject(projectDir);
201:
202: if (fo != null) {
203: return ProjectManager.getDefault().findProject(fo);
204: }
205: } catch (IOException e) {
206: // Return null
207: }
208:
209: return null;
210: }
211:
212: private void populateAccessory(final Project project) {
213:
214: final DefaultListModel model = (DefaultListModel) jListArtifacts
215: .getModel();
216: model.clear();
217: jTextFieldName.setText(project == null ? "" : ProjectUtils
218: .getInformation(project).getDisplayName()); //NOI18N
219: if (project != null) {
220: final AntArtifact artifacts[] = AntArtifactQuery
221: .findArtifactsByType(project, artifactType);
222: for (int i = 0; i < artifacts.length; i++) {
223: final URI uris[] = artifacts[i].getArtifactLocations();
224: for (int j = 0; j < uris.length; j++) {
225: model.addElement(new ArtifactItem(artifacts[i],
226: uris[j]));
227: }
228: }
229: }
230: // jListArtifacts.setSelectedIndex(model.getSize() > 0 ? 0 : -1);
231: }
232:
233: // Variables declaration - do not modify//GEN-BEGIN:variables
234: private javax.swing.JLabel jLabelJarFiles;
235: private javax.swing.JLabel jLabelName;
236: private javax.swing.JList jListArtifacts;
237: private javax.swing.JScrollPane jScrollPane1;
238: private javax.swing.JTextField jTextFieldName;
239:
240: // End of variables declaration//GEN-END:variables
241:
242: private static Container findParent() {
243: return DialogDisplayer.getDefault().createDialog(
244: new DialogDescriptor(new JPanel(), "")).getParent();//NOI18N
245: }
246:
247: /** Shows dialog with the artifact chooser
248: * @return null if canceled selected jars if some jars selected
249: */
250: public static ArtifactItem[] showDialog(final String artifactType) {
251:
252: final JFileChooser chooser = ProjectChooser.projectChooser();
253: chooser
254: .setDialogTitle(NbBundle.getMessage(
255: AntArtifactChooser.class,
256: "LBL_AAChooserSelectProject")); //NOI18N
257: chooser.setApproveButtonText(NbBundle.getMessage(
258: AntArtifactChooser.class, "LBL_AAChooser_AddJARFiles")); //NOI18N
259:
260: final AntArtifactChooser accessory = new AntArtifactChooser(
261: artifactType, chooser);
262: chooser.setAccessory(accessory);
263:
264: final int option = chooser.showOpenDialog(findParent()); // show file chooser
265:
266: if (option == JFileChooser.APPROVE_OPTION) {
267:
268: final Object elements[] = accessory.jListArtifacts
269: .getSelectedValues();
270: final ArtifactItem artifacts[] = new ArtifactItem[elements.length];
271: System
272: .arraycopy(elements, 0, artifacts, 0,
273: elements.length);
274: return artifacts;
275:
276: }
277: return null;
278: }
279:
280: public static class ArtifactItem {
281:
282: final private AntArtifact artifact;
283: final private URI uri;
284:
285: ArtifactItem(AntArtifact artifact, URI uri) {
286: this .artifact = artifact;
287: this .uri = uri;
288: }
289:
290: public AntArtifact getArtifact() {
291: return artifact;
292: }
293:
294: public URI getURI() {
295: return uri;
296: }
297:
298: public String toString() {
299: return uri.getPath();
300: }
301:
302: }
303: }
|