KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seproject > 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.java.j2seproject.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.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import javax.swing.DefaultListModel JavaDoc;
34 import javax.swing.JFileChooser JavaDoc;
35 import javax.swing.JPanel JavaDoc;
36 import org.netbeans.api.project.Project;
37 import org.netbeans.api.project.ProjectManager;
38 import org.netbeans.api.project.ProjectUtils;
39 import org.netbeans.api.project.ant.AntArtifact;
40 import org.netbeans.api.project.ant.AntArtifactQuery;
41 import org.netbeans.spi.project.ui.support.ProjectChooser;
42 import org.netbeans.modules.java.j2seproject.ui.FoldersListSettings;
43 import org.openide.DialogDisplayer;
44 import org.openide.ErrorManager;
45 import org.openide.NotifyDescriptor;
46 import org.openide.filesystems.FileObject;
47 import org.openide.filesystems.FileUtil;
48 import org.openide.util.NbBundle;
49
50 /**
51  * Accessory component used in the ProjectChooser for choosing project
52  * artifacts.
53  * @author Petr Hrebejk
54  */

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

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

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

160     private void populateAccessory( Project project ) {
161         
162         DefaultListModel JavaDoc model = (DefaultListModel JavaDoc)jListArtifacts.getModel();
163         model.clear();
164         jTextFieldName.setText(project == null ? "" : ProjectUtils.getInformation(project).getDisplayName()); //NOI18N
165

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

194     
195     /** Shows dialog with the artifact chooser
196      * @return null if canceled selected jars if some jars selected
197      */

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

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

258     public static class ArtifactItem {
259         
260         private AntArtifact artifact;
261         private URI JavaDoc artifactURI;
262         
263         public ArtifactItem(AntArtifact artifact, URI JavaDoc artifactURI) {
264             this.artifact = artifact;
265             this.artifactURI = artifactURI;
266         }
267         
268         public AntArtifact getArtifact() {
269             return artifact;
270         }
271         
272         public URI JavaDoc getArtifactURI() {
273             return artifactURI;
274         }
275         
276         public String JavaDoc toString() {
277             return artifactURI.toString();
278         }
279         
280     }
281 }
282
Popular Tags