KickJava   Java API By Example, From Geeks To Geeks.

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

50 public class AntArtifactChooser extends JPanel JavaDoc implements PropertyChangeListener JavaDoc {
51     
52     // XXX to become an array later
53
private String JavaDoc artifactType;
54     
55     /** Creates new form JarArtifactChooser */
56     public AntArtifactChooser( String JavaDoc artifactType, JFileChooser JavaDoc chooser ) {
57         this.artifactType = artifactType;
58         
59         initComponents();
60         jListArtifacts.setModel( new DefaultListModel JavaDoc() );
61         chooser.addPropertyChangeListener( this );
62     }
63     
64     /** This method is called from within the constructor to
65      * initialize the form.
66      * WARNING: Do NOT modify this code. The content of this method is
67      * always regenerated by the Form Editor.
68      */

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

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

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

163         if ( project != null ) {
164                         
165             AntArtifact artifacts[] = AntArtifactQuery.findArtifactsByType( project, artifactType );
166         
167             for( int i = 0; i < artifacts.length; i++ ) {
168                 URI JavaDoc uris[] = artifacts[i].getArtifactLocations();
169                 for( int y = 0; y < uris.length; y++ ) {
170                     model.addElement( new ArtifactItem(artifacts[i], uris[y]));
171                 }
172             }
173             jListArtifacts.setSelectionInterval(0, model.size());
174         }
175         
176     }
177         
178     
179     // Variables declaration - do not modify//GEN-BEGIN:variables
180
private javax.swing.JLabel JavaDoc jLabelJarFiles;
181     private javax.swing.JLabel JavaDoc jLabelName;
182     private javax.swing.JList JavaDoc jListArtifacts;
183     private javax.swing.JScrollPane JavaDoc jScrollPane1;
184     private javax.swing.JTextField JavaDoc jTextFieldName;
185     // End of variables declaration//GEN-END:variables
186

187     
188     /** Shows dialog with the artifact chooser
189      * @return null if canceled selected jars if some jars selected
190      */

191     public static ArtifactItem[] showDialog( String JavaDoc artifactType, Project master, Component JavaDoc parent ) {
192         
193         JFileChooser JavaDoc chooser = ProjectChooser.projectChooser();
194         chooser.setDialogTitle( NbBundle.getMessage( AntArtifactChooser.class, "LBL_AACH_Title" ) ); // NOI18N
195
chooser.setApproveButtonText( NbBundle.getMessage( AntArtifactChooser.class, "LBL_AACH_SelectProject" ) ); // NOI18N
196

197         AntArtifactChooser accessory = new AntArtifactChooser( artifactType, chooser );
198         chooser.setAccessory( accessory );
199         chooser.setCurrentDirectory (FoldersListSettings.getDefault().getLastUsedArtifactFolder());
200
201         int option = chooser.showOpenDialog( parent ); // Show the chooser
202

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

250     public static class ArtifactItem {
251         
252         private AntArtifact artifact;
253         private URI JavaDoc artifactURI;
254         
255         public ArtifactItem(AntArtifact artifact, URI JavaDoc artifactURI) {
256             this.artifact = artifact;
257             this.artifactURI = artifactURI;
258         }
259         
260         public AntArtifact getArtifact() {
261             return artifact;
262         }
263         
264         public URI JavaDoc getArtifactURI() {
265             return artifactURI;
266         }
267         
268         public String JavaDoc toString() {
269             return artifactURI.toString();
270         }
271         
272     }
273 }
274
Popular Tags