KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > earproject > 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.earproject.ui.customizer;
21
22 import java.awt.Dimension 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 javax.swing.DefaultListModel JavaDoc;
28 import javax.swing.JFileChooser JavaDoc;
29 import org.netbeans.api.project.Project;
30 import org.netbeans.api.project.ProjectManager;
31 import org.netbeans.api.project.ProjectUtils;
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.FileUtil;
34 import org.openide.util.NbBundle;
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.openide.DialogDisplayer;
39 import org.openide.NotifyDescriptor;
40
41 /** Accessory component used in the ProjectChooser for choosing project
42  * artifacts.
43  *
44  * @author phrebejk
45  */

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

65     private void initComponents() {//GEN-BEGIN:initComponents
66
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
67
68         jLabelName = new javax.swing.JLabel JavaDoc();
69         jTextFieldName = new javax.swing.JTextField JavaDoc();
70         jLabelJarFiles = new javax.swing.JLabel JavaDoc();
71         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
72         jListArtifacts = new javax.swing.JList JavaDoc();
73
74         setLayout(new java.awt.GridBagLayout JavaDoc());
75
76         jLabelName.setText(NbBundle.getMessage(AntArtifactChooser.class, "LBL_AACH_ProjectName_JLabel"));
77         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
78         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
79         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
80         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 2, 0);
81         add(jLabelName, gridBagConstraints);
82
83         jTextFieldName.setEditable(false);
84         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
85         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
86         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
87         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
88         gridBagConstraints.weightx = 1.0;
89         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 6, 0);
90         add(jTextFieldName, gridBagConstraints);
91
92         jLabelJarFiles.setText(NbBundle.getMessage(AntArtifactChooser.class, "LBL_AACH_ProjectJarFiles_JLabel"));
93         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
94         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
95         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
96         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 2, 0);
97         add(jLabelJarFiles, gridBagConstraints);
98
99         jScrollPane1.setViewportView(jListArtifacts);
100
101         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
102         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
103         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
104         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
105         gridBagConstraints.weightx = 1.0;
106         gridBagConstraints.weighty = 1.0;
107         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 0, 0);
108         add(jScrollPane1, gridBagConstraints);
109
110     }//GEN-END:initComponents
111

112     
113     public void propertyChange( PropertyChangeEvent JavaDoc e ) {
114         
115         if ( JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals( e.getPropertyName() ) ) {
116             // We have to update the Accessory
117
JFileChooser JavaDoc chooser = (JFileChooser JavaDoc)e.getSource();
118             File JavaDoc dir = chooser.getSelectedFile();
119             Project project = dir == null ? null : getProject(FileUtil.normalizeFile(dir));
120             populateAccessory( project );
121         }
122     }
123     
124     private Project getProject( File JavaDoc projectDir ) {
125         
126         try {
127             projectDir = FileUtil.normalizeFile (projectDir);
128             FileObject fo = FileUtil.toFileObject(projectDir);
129             
130             if (fo != null) {
131                 Project project = ProjectManager.getDefault().findProject(fo);
132                 return project;
133             }
134         }
135         catch ( IOException JavaDoc e ) {
136             // Return null
137
}
138         
139         return null;
140     }
141     
142     
143     private void populateAccessory( Project project ) {
144         
145         DefaultListModel JavaDoc model = (DefaultListModel JavaDoc)jListArtifacts.getModel();
146         model.clear();
147         jTextFieldName.setText(project == null ? "" : ProjectUtils.getInformation(project).getDisplayName()); //NOI18N
148

149         if ( project != null ) {
150             
151             for (int j = 0; j < artifactType.length; j++) {
152                         
153             AntArtifact artifacts[] = AntArtifactQuery.findArtifactsByType( project, artifactType[j] );
154         
155             for( int i = 0; i < artifacts.length; i++ ) {
156                 model.addElement( new ArtifactItem( artifacts[i]));
157             }
158             if (artifacts.length > 0)
159                 return;
160             }
161         }
162         
163     }
164         
165     
166     // Variables declaration - do not modify//GEN-BEGIN:variables
167
private javax.swing.JLabel JavaDoc jLabelJarFiles;
168     private javax.swing.JLabel JavaDoc jLabelName;
169     private javax.swing.JList JavaDoc jListArtifacts;
170     private javax.swing.JScrollPane JavaDoc jScrollPane1;
171     private javax.swing.JTextField JavaDoc jTextFieldName;
172     // End of variables declaration//GEN-END:variables
173

174     
175     /** Shows dialog with the artifact chooser
176      * @return null if canceled selected jars if some jars selected
177      */

178     public static AntArtifact[] showDialog( String JavaDoc artifactType, Project master ) {
179         return showDialog(master, new String JavaDoc[] { artifactType });
180     }
181     
182     public static AntArtifact[] showDialog(Project master, String JavaDoc [] artifactTypes) {
183         JFileChooser JavaDoc chooser = ProjectChooser.projectChooser();
184         chooser.setDialogTitle( NbBundle.getMessage( AntArtifactChooser.class, "LBL_AACH_Title" ) ); // NOI18N
185
chooser.setApproveButtonText( NbBundle.getMessage( AntArtifactChooser.class, "LBL_AACH_SelectProject" ) ); // NOI18N
186

187         AntArtifactChooser accessory = new AntArtifactChooser( artifactTypes, chooser );
188         chooser.setAccessory( accessory );
189         
190         chooser.setPreferredSize( new Dimension JavaDoc( 650, 360 ) );
191         
192         int option = chooser.showOpenDialog( null ); // Show the chooser
193

194         if ( option == JFileChooser.APPROVE_OPTION ) {
195             
196             File JavaDoc dir = chooser.getSelectedFile();
197             dir = FileUtil.normalizeFile (dir);
198             Project selectedProject = accessory.getProject( dir );
199
200             if ( selectedProject == null ) {
201                 return null;
202             }
203             
204             if ( selectedProject.getProjectDirectory().equals( master.getProjectDirectory() ) ) {
205                 DialogDisplayer.getDefault().notify( new NotifyDescriptor.Message(
206                     NbBundle.getMessage( AntArtifactChooser.class, "MSG_AACH_RefToItself" ),
207                     NotifyDescriptor.INFORMATION_MESSAGE ) );
208                 return null;
209             }
210             
211             if ( ProjectUtils.hasSubprojectCycles( master, selectedProject ) ) {
212                 DialogDisplayer.getDefault().notify( new NotifyDescriptor.Message(
213                     NbBundle.getMessage( AntArtifactChooser.class, "MSG_AACH_Cycles" ),
214                     NotifyDescriptor.INFORMATION_MESSAGE ) );
215                 return null;
216             }
217             
218             DefaultListModel JavaDoc model = (DefaultListModel JavaDoc)accessory.jListArtifacts.getModel();
219             
220             AntArtifact artifacts[] = new AntArtifact[ model.size() ];
221             
222             // XXX Adding references twice
223
for( int i = 0; i < artifacts.length; i++ ) {
224                 artifacts[i] = ((ArtifactItem)model.getElementAt( i )).getArtifact();
225             }
226             
227             return artifacts;
228             
229         }
230         else {
231             return null;
232         }
233                 
234     }
235        
236     private static class ArtifactItem {
237         
238         private AntArtifact artifact;
239         
240         ArtifactItem( AntArtifact artifact ) {
241             this.artifact = artifact;
242         }
243         
244         AntArtifact getArtifact() {
245             return artifact;
246         }
247         
248         public String JavaDoc toString() {
249             return artifact.getArtifactLocations()[0].toString();
250         }
251         
252     }
253 }
254
Popular Tags