KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > clientproject > ui > customizer > AntArtifactChooser


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.clientproject.ui.customizer;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.beans.PropertyChangeEvent JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import java.io.File JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.net.URI JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Arrays JavaDoc;
31 import java.util.List JavaDoc;
32 import javax.swing.DefaultListModel JavaDoc;
33 import javax.swing.JFileChooser JavaDoc;
34 import javax.swing.JPanel JavaDoc;
35 import org.netbeans.api.project.Project;
36 import org.netbeans.api.project.ProjectManager;
37 import org.netbeans.api.project.ProjectUtils;
38 import org.netbeans.api.project.ant.AntArtifact;
39 import org.netbeans.api.project.ant.AntArtifactQuery;
40 import org.netbeans.spi.project.ui.support.ProjectChooser;
41 import org.netbeans.modules.j2ee.clientproject.ui.FoldersListSettings;
42 import org.openide.DialogDisplayer;
43 import org.openide.ErrorManager;
44 import org.openide.NotifyDescriptor;
45 import org.openide.filesystems.FileObject;
46 import org.openide.filesystems.FileUtil;
47 import org.openide.util.NbBundle;
48
49 /**
50  * Accessory component used in the ProjectChooser for choosing project
51  * artifacts.
52  * @author Petr Hrebejk
53  */

54 public class AntArtifactChooser extends JPanel JavaDoc implements PropertyChangeListener JavaDoc {
55     
56     private String JavaDoc[] artifactTypes;
57     
58     /** Creates new form JarArtifactChooser */
59     public AntArtifactChooser( String JavaDoc[] artifactTypes, JFileChooser JavaDoc chooser ) {
60         this.artifactTypes = artifactTypes;
61         
62         initComponents();
63         jListArtifacts.setModel( new DefaultListModel JavaDoc() );
64         chooser.addPropertyChangeListener( this );
65     }
66     
67     public AntArtifactChooser( String JavaDoc artifactType, JFileChooser JavaDoc chooser ) {
68         this(new String JavaDoc[] {artifactType}, chooser);
69     }
70     
71     
72     /** This method is called from within the constructor to
73      * initialize the form.
74      * WARNING: Do NOT modify this code. The content of this method is
75      * always regenerated by the Form Editor.
76      */

77     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
78
private void initComponents() {
79         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
80
81         jLabelName = new javax.swing.JLabel JavaDoc();
82         jTextFieldName = new javax.swing.JTextField JavaDoc();
83         jLabelJarFiles = new javax.swing.JLabel JavaDoc();
84         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
85         jListArtifacts = new javax.swing.JList JavaDoc();
86
87         setLayout(new java.awt.GridBagLayout JavaDoc());
88
89         jLabelName.setLabelFor(jTextFieldName);
90         org.openide.awt.Mnemonics.setLocalizedText(jLabelName, org.openide.util.NbBundle.getMessage(AntArtifactChooser.class, "LBL_AACH_ProjectName_JLabel"));
91         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
92         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
93         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
94         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 2, 0);
95         add(jLabelName, gridBagConstraints);
96
97         jTextFieldName.setEditable(false);
98         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
99         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
100         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
101         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
102         gridBagConstraints.weightx = 1.0;
103         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 6, 0);
104         add(jTextFieldName, gridBagConstraints);
105         jTextFieldName.getAccessibleContext().setAccessibleDescription(null);
106
107         jLabelJarFiles.setLabelFor(jListArtifacts);
108         org.openide.awt.Mnemonics.setLocalizedText(jLabelJarFiles, org.openide.util.NbBundle.getMessage(AntArtifactChooser.class, "LBL_AACH_ProjectJarFiles_JLabel"));
109         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
110         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
111         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
112         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 2, 0);
113         add(jLabelJarFiles, gridBagConstraints);
114
115         jScrollPane1.setViewportView(jListArtifacts);
116         jListArtifacts.getAccessibleContext().setAccessibleDescription(null);
117
118         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
119         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
120         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
121         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
122         gridBagConstraints.weightx = 1.0;
123         gridBagConstraints.weighty = 1.0;
124         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 0, 0);
125         add(jScrollPane1, gridBagConstraints);
126
127     }// </editor-fold>//GEN-END:initComponents
128

129     
130     public void propertyChange(PropertyChangeEvent JavaDoc e) {
131         if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(e.getPropertyName())) {
132             // We have to update the Accessory
133
JFileChooser JavaDoc chooser = (JFileChooser JavaDoc) e.getSource();
134             File JavaDoc dir = chooser.getSelectedFile(); // may be null (#46744)
135
Project project = getProject(dir); // may be null
136
populateAccessory(project);
137         }
138     }
139     
140     private Project getProject( File JavaDoc projectDir ) {
141         
142         if (projectDir == null) { // #46744
143
return null;
144         }
145         
146         try {
147             File JavaDoc normProjectDir = FileUtil.normalizeFile(projectDir);
148             FileObject fo = FileUtil.toFileObject(normProjectDir);
149             if (fo != null) {
150                 return ProjectManager.getDefault().findProject(fo);
151             }
152         } catch (IOException JavaDoc e) {
153             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
154             // Return null
155
}
156         
157         return null;
158     }
159     
160     /**
161      * Set up GUI fields according to the requested project.
162      * @param project a subproject, or null
163      */

164     private void populateAccessory( Project project ) {
165         
166         DefaultListModel JavaDoc model = (DefaultListModel JavaDoc)jListArtifacts.getModel();
167         model.clear();
168         jTextFieldName.setText(project == null ? "" : ProjectUtils.getInformation(project).getDisplayName()); //NOI18N
169

170         if ( project != null ) {
171             
172             List JavaDoc<AntArtifact> artifacts = new ArrayList JavaDoc<AntArtifact>();
173             for (int i=0; i<artifactTypes.length; i++) {
174                 artifacts.addAll(Arrays.asList(AntArtifactQuery.findArtifactsByType(project, artifactTypes[i])));
175             }
176             
177             for (AntArtifact artifact : artifacts) {
178                 URI JavaDoc uris[] = artifact.getArtifactLocations();
179                 for( int y = 0; y < uris.length; y++ ) {
180                     model.addElement( new ArtifactItem(artifact, uris[y]));
181                 }
182             }
183             jListArtifacts.setSelectionInterval(0, model.size());
184         }
185         
186     }
187     
188     
189     // Variables declaration - do not modify//GEN-BEGIN:variables
190
private javax.swing.JLabel JavaDoc jLabelJarFiles;
191     private javax.swing.JLabel JavaDoc jLabelName;
192     private javax.swing.JList JavaDoc jListArtifacts;
193     private javax.swing.JScrollPane JavaDoc jScrollPane1;
194     private javax.swing.JTextField JavaDoc jTextFieldName;
195     // End of variables declaration//GEN-END:variables
196

197     
198     /** Shows dialog with the artifact chooser
199      * @return null if canceled selected jars if some jars selected
200      */

201     public static ArtifactItem[] showDialog( String JavaDoc[] artifactTypes, Project master, Component JavaDoc parent ) {
202         
203         JFileChooser JavaDoc chooser = ProjectChooser.projectChooser();
204         chooser.setDialogTitle( NbBundle.getMessage( AntArtifactChooser.class, "LBL_AACH_Title" ) ); // NOI18N
205
chooser.setApproveButtonText( NbBundle.getMessage( AntArtifactChooser.class, "LBL_AACH_SelectProject" ) ); // NOI18N
206
chooser.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(AntArtifactChooser.class,"AD_AACH_SelectProject"));
207         AntArtifactChooser accessory = new AntArtifactChooser( artifactTypes, chooser );
208         chooser.setAccessory( accessory );
209         chooser.setPreferredSize( new Dimension JavaDoc( 650, 380 ) );
210         chooser.setCurrentDirectory(FoldersListSettings.getDefault().getLastUsedArtifactFolder());
211         
212         int option = chooser.showOpenDialog( parent ); // Show the chooser
213

214         if ( option == JFileChooser.APPROVE_OPTION ) {
215             
216             File JavaDoc dir = chooser.getSelectedFile();
217             dir = FileUtil.normalizeFile(dir);
218             Project selectedProject = accessory.getProject( dir );
219             
220             if ( selectedProject == null ) {
221                 return null;
222             }
223             
224             if ( selectedProject.getProjectDirectory().equals( master.getProjectDirectory() ) ) {
225                 DialogDisplayer.getDefault().notify( new NotifyDescriptor.Message(
226                         NbBundle.getMessage( AntArtifactChooser.class, "MSG_AACH_RefToItself" ),
227                         NotifyDescriptor.INFORMATION_MESSAGE ) );
228                 return null;
229             }
230             
231             if ( ProjectUtils.hasSubprojectCycles( master, selectedProject ) ) {
232                 DialogDisplayer.getDefault().notify( new NotifyDescriptor.Message(
233                         NbBundle.getMessage( AntArtifactChooser.class, "MSG_AACH_Cycles" ),
234                         NotifyDescriptor.INFORMATION_MESSAGE ) );
235                 return null;
236             }
237             
238             FoldersListSettings.getDefault().setLastUsedArtifactFolder(FileUtil.normalizeFile(chooser.getCurrentDirectory()));
239             
240             Object JavaDoc[] tmp = new Object JavaDoc[accessory.jListArtifacts.getModel().getSize()];
241             int count = 0;
242             for(int i = 0; i < tmp.length; i++) {
243                 if (accessory.jListArtifacts.isSelectedIndex(i)) {
244                     tmp[count] = accessory.jListArtifacts.getModel().getElementAt(i);
245                     count++;
246                 }
247             }
248             ArtifactItem artifactItems[] = new ArtifactItem[count];
249             System.arraycopy(tmp, 0, artifactItems, 0, count);
250             return artifactItems;
251         } else {
252             return null;
253         }
254         
255     }
256     
257     /** Shows dialog with the artifact chooser
258      * @return null if canceled selected jars if some jars selected
259      */

260     public static ArtifactItem[] showDialog( String JavaDoc artifactType, Project master, Component JavaDoc parent ) {
261         
262         JFileChooser JavaDoc chooser = ProjectChooser.projectChooser();
263         chooser.setDialogTitle( NbBundle.getMessage( AntArtifactChooser.class, "LBL_AACH_Title" ) ); // NOI18N
264
chooser.setApproveButtonText( NbBundle.getMessage( AntArtifactChooser.class, "LBL_AACH_SelectProject" ) ); // NOI18N
265

266         AntArtifactChooser accessory = new AntArtifactChooser( artifactType, chooser );
267         chooser.setAccessory( accessory );
268         chooser.setPreferredSize( new Dimension JavaDoc( 650, 380 ) );
269         chooser.setCurrentDirectory(FoldersListSettings.getDefault().getLastUsedArtifactFolder());
270         
271         int option = chooser.showOpenDialog( parent ); // Show the chooser
272

273         if ( option == JFileChooser.APPROVE_OPTION ) {
274             
275             File JavaDoc dir = chooser.getSelectedFile();
276             dir = FileUtil.normalizeFile(dir);
277             Project selectedProject = accessory.getProject( dir );
278             
279             if ( selectedProject == null ) {
280                 return null;
281             }
282             
283             if ( selectedProject.getProjectDirectory().equals( master.getProjectDirectory() ) ) {
284                 DialogDisplayer.getDefault().notify( new NotifyDescriptor.Message(
285                         NbBundle.getMessage( AntArtifactChooser.class, "MSG_AACH_RefToItself" ),
286                         NotifyDescriptor.INFORMATION_MESSAGE ) );
287                 return null;
288             }
289             
290             if ( ProjectUtils.hasSubprojectCycles( master, selectedProject ) ) {
291                 DialogDisplayer.getDefault().notify( new NotifyDescriptor.Message(
292                         NbBundle.getMessage( AntArtifactChooser.class, "MSG_AACH_Cycles" ),
293                         NotifyDescriptor.INFORMATION_MESSAGE ) );
294                 return null;
295             }
296             
297             FoldersListSettings.getDefault().setLastUsedArtifactFolder(FileUtil.normalizeFile(chooser.getCurrentDirectory()));
298             
299             Object JavaDoc[] tmp = new Object JavaDoc[accessory.jListArtifacts.getModel().getSize()];
300             int count = 0;
301             for(int i = 0; i < tmp.length; i++) {
302                 if (accessory.jListArtifacts.isSelectedIndex(i)) {
303                     tmp[count] = accessory.jListArtifacts.getModel().getElementAt(i);
304                     count++;
305                 }
306             }
307             ArtifactItem artifactItems[] = new ArtifactItem[count];
308             System.arraycopy(tmp, 0, artifactItems, 0, count);
309             return artifactItems;
310         } else {
311             return null;
312         }
313         
314     }
315     
316     /**
317      * Pair of AntArtifact and one of jars it produces.
318      */

319     public static class ArtifactItem {
320         
321         private AntArtifact artifact;
322         private URI JavaDoc artifactURI;
323         
324         public ArtifactItem(AntArtifact artifact, URI JavaDoc artifactURI) {
325             this.artifact = artifact;
326             this.artifactURI = artifactURI;
327         }
328         
329         public AntArtifact getArtifact() {
330             return artifact;
331         }
332         
333         public URI JavaDoc getArtifactURI() {
334             return artifactURI;
335         }
336         
337         public String JavaDoc toString() {
338             return artifactURI.toString();
339         }
340         
341     }
342 }
343
Popular Tags