KickJava   Java API By Example, From Geeks To Geeks.

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

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

70     private void initComponents() {//GEN-BEGIN: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.setText(org.openide.util.NbBundle.getMessage(AntArtifactChooser.class, "LBL_AACH_ProjectName_JLabel"));
82         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
83         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
84         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
85         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 2, 0);
86         add(jLabelName, gridBagConstraints);
87
88         jTextFieldName.setEditable(false);
89         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
90         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
91         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
92         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
93         gridBagConstraints.weightx = 1.0;
94         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 6, 0);
95         add(jTextFieldName, gridBagConstraints);
96
97         jLabelJarFiles.setText(org.openide.util.NbBundle.getMessage(AntArtifactChooser.class, "LBL_AACH_ProjectJarFiles_JLabel"));
98         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
99         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
100         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
101         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 2, 0);
102         add(jLabelJarFiles, gridBagConstraints);
103
104         jScrollPane1.setViewportView(jListArtifacts);
105
106         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
107         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
108         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
109         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
110         gridBagConstraints.weightx = 1.0;
111         gridBagConstraints.weighty = 1.0;
112         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 0, 0);
113         add(jScrollPane1, gridBagConstraints);
114
115     }//GEN-END:initComponents
116

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

152     private void populateAccessory( Project project ) {
153         
154         DefaultListModel JavaDoc model = (DefaultListModel JavaDoc)jListArtifacts.getModel();
155         model.clear();
156         jTextFieldName.setText(project == null ? "" : ProjectUtils.getInformation(project).getDisplayName()); //NOI18N
157

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

182     
183     /** Shows dialog with the artifact chooser
184      * @return null if canceled selected jars if some jars selected
185      */

186     public static ArtifactItem[] showDialog( String JavaDoc artifactType, Project master, Component JavaDoc parent ) {
187         
188         JFileChooser JavaDoc chooser = ProjectChooser.projectChooser();
189         chooser.setDialogTitle( NbBundle.getMessage( AntArtifactChooser.class, "LBL_AACH_Title" ) ); // NOI18N
190
chooser.setApproveButtonText( NbBundle.getMessage( AntArtifactChooser.class, "LBL_AACH_SelectProject" ) ); // NOI18N
191

192         AntArtifactChooser accessory = new AntArtifactChooser( artifactType, chooser );
193         chooser.setAccessory( accessory );
194         chooser.setPreferredSize( new Dimension JavaDoc( 650, 380 ) );
195         chooser.setCurrentDirectory (FoldersListSettings.getDefault().getLastUsedArtifactFolder());
196
197         int option = chooser.showOpenDialog( parent ); // Show the chooser
198

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

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