2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.groovy.groovyproject.ui.customizer;
44
import java.awt.Dimension;
45
import java.beans.PropertyChangeEvent;
46
import java.beans.PropertyChangeListener;
48
import java.io.IOException;
49
import javax.swing.DefaultListModel;
50
import javax.swing.JFileChooser;
51
import javax.swing.JPanel;
52
import org.netbeans.api.project.Project;
53
import org.netbeans.api.project.ProjectManager;
54
import org.netbeans.api.project.ProjectUtils;
55
import org.netbeans.api.project.ant.AntArtifact;
56
import org.netbeans.api.project.ant.AntArtifactQuery;
57
import org.netbeans.spi.project.ui.support.ProjectChooser;
58
import org.netbeans.modules.groovy.groovyproject.ui.FoldersListSettings;
59
import org.openide.DialogDisplayer;
60
import org.openide.ErrorManager;
61
import org.openide.NotifyDescriptor;
62
import org.openide.filesystems.FileObject;
63
import org.openide.filesystems.FileUtil;
64
import org.openide.util.NbBundle;
67
* Accessory component used in the ProjectChooser for choosing project
69
* @author Petr Hrebejk
71
public class AntArtifactChooser extends JPanel implements PropertyChangeListener {
73
// XXX to become an array later
74
private String artifactType;
76
/** Creates new form JarArtifactChooser */
77
public AntArtifactChooser( String artifactType, JFileChooser chooser ) {
78
this.artifactType = artifactType;
81
jListArtifacts.setModel( new DefaultListModel() );
82
chooser.addPropertyChangeListener( this );
85
/** This method is called from within the constructor to
86
* initialize the form.
87
* WARNING: Do NOT modify this code. The content of this method is
88
* always regenerated by the Form Editor.
90
private void initComponents() {//GEN-BEGIN:initComponents
91
java.awt.GridBagConstraints gridBagConstraints;
93
jLabelName = new javax.swing.JLabel();
94
jTextFieldName = new javax.swing.JTextField();
95
jLabelJarFiles = new javax.swing.JLabel();
96
jScrollPane1 = new javax.swing.JScrollPane();
97
jListArtifacts = new javax.swing.JList();
99
setLayout(new java.awt.GridBagLayout());
101
jLabelName.setText(org.openide.util.NbBundle.getMessage(AntArtifactChooser.class, "LBL_AACH_ProjectName_JLabel"));
102
gridBagConstraints = new java.awt.GridBagConstraints();
103
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
104
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
105
gridBagConstraints.insets = new java.awt.Insets(0, 12, 2, 0);
106
add(jLabelName, gridBagConstraints);
108
jTextFieldName.setEditable(false);
109
gridBagConstraints = new java.awt.GridBagConstraints();
110
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
111
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
112
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
113
gridBagConstraints.weightx = 1.0;
114
gridBagConstraints.insets = new java.awt.Insets(0, 12, 6, 0);
115
add(jTextFieldName, gridBagConstraints);
117
jLabelJarFiles.setText(org.openide.util.NbBundle.getMessage(AntArtifactChooser.class, "LBL_AACH_ProjectJarFiles_JLabel"));
118
gridBagConstraints = new java.awt.GridBagConstraints();
119
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
120
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
121
gridBagConstraints.insets = new java.awt.Insets(0, 12, 2, 0);
122
add(jLabelJarFiles, gridBagConstraints);
124
jScrollPane1.setViewportView(jListArtifacts);
126
gridBagConstraints = new java.awt.GridBagConstraints();
127
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
128
gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
129
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
130
gridBagConstraints.weightx = 1.0;
131
gridBagConstraints.weighty = 1.0;
132
gridBagConstraints.insets = new java.awt.Insets(0, 12, 0, 0);
133
add(jScrollPane1, gridBagConstraints);
135
}//GEN-END:initComponents
138
public void propertyChange(PropertyChangeEvent e) {
139
if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(e.getPropertyName())) {
140
// We have to update the Accessory
141
JFileChooser chooser = (JFileChooser) e.getSource();
142
File dir = chooser.getSelectedFile(); // may be null (#46744)
143
Project project = getProject(dir); // may be null
144
populateAccessory(project);
148
private Project getProject( File projectDir ) {
150
if (projectDir == null) { // #46744
155
File normProjectDir = FileUtil.normalizeFile(projectDir);
156
FileObject fo = FileUtil.toFileObject(normProjectDir);
158
return ProjectManager.getDefault().findProject(fo);
160
} catch (IOException e) {
161
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
169
* Set up GUI fields according to the requested project.
170
* @param project a subproject, or null
172
private void populateAccessory( Project project ) {
174
DefaultListModel model = (DefaultListModel)jListArtifacts.getModel();
176
jTextFieldName.setText(project == null ? "" : ProjectUtils.getInformation(project).getDisplayName()); //NOI18N
178
if ( project != null ) {
180
AntArtifact artifacts[] = AntArtifactQuery.findArtifactsByType( project, artifactType );
182
for( int i = 0; i < artifacts.length; i++ ) {
183
model.addElement( new ArtifactItem( artifacts[i]));
190
// Variables declaration - do not modify//GEN-BEGIN:variables
191
private javax.swing.JLabel jLabelJarFiles;
192
private javax.swing.JLabel jLabelName;
193
private javax.swing.JList jListArtifacts;
194
private javax.swing.JScrollPane jScrollPane1;
195
private javax.swing.JTextField jTextFieldName;
196
// End of variables declaration//GEN-END:variables
199
/** Shows dialog with the artifact chooser
200
* @return null if canceled selected jars if some jars selected
202
public static AntArtifact[] showDialog( String artifactType, Project master ) {
204
JFileChooser chooser = ProjectChooser.projectChooser();
205
chooser.setDialogTitle( NbBundle.getMessage( AntArtifactChooser.class, "LBL_AACH_Title" ) ); // NOI18N
206
chooser.setApproveButtonText( NbBundle.getMessage( AntArtifactChooser.class, "LBL_AACH_SelectProject" ) ); // NOI18N
208
AntArtifactChooser accessory = new AntArtifactChooser( artifactType, chooser );
209
chooser.setAccessory( accessory );
210
chooser.setPreferredSize( new Dimension( 650, 380 ) );
211
chooser.setCurrentDirectory (FoldersListSettings.getDefault().getLastUsedArtifactFolder());
213
int option = chooser.showOpenDialog( null ); // Show the chooser
215
if ( option == JFileChooser.APPROVE_OPTION ) {
217
File dir = chooser.getSelectedFile();
218
dir = FileUtil.normalizeFile (dir);
219
Project selectedProject = accessory.getProject( dir );
221
if ( selectedProject == null ) {
225
if ( selectedProject.getProjectDirectory().equals( master.getProjectDirectory() ) ) {
226
DialogDisplayer.getDefault().notify( new NotifyDescriptor.Message(
227
NbBundle.getMessage( AntArtifactChooser.class, "MSG_AACH_RefToItself" ),
228
NotifyDescriptor.INFORMATION_MESSAGE ) );
232
if ( ProjectUtils.hasSubprojectCycles( master, selectedProject ) ) {
233
DialogDisplayer.getDefault().notify( new NotifyDescriptor.Message(
234
NbBundle.getMessage( AntArtifactChooser.class, "MSG_AACH_Cycles" ),
235
NotifyDescriptor.INFORMATION_MESSAGE ) );
239
DefaultListModel model = (DefaultListModel)accessory.jListArtifacts.getModel();
241
AntArtifact artifacts[] = new AntArtifact[ model.size() ];
243
// XXX Adding references twice
244
for( int i = 0; i < artifacts.length; i++ ) {
245
artifacts[i] = ((ArtifactItem)model.getElementAt( i )).getArtifact();
248
FoldersListSettings.getDefault().setLastUsedArtifactFolder (FileUtil.normalizeFile(chooser.getCurrentDirectory()));
258
private static class ArtifactItem {
260
private AntArtifact artifact;
262
ArtifactItem( AntArtifact artifact ) {
263
this.artifact = artifact;
266
AntArtifact getArtifact() {
270
public String toString() {
271
return artifact.getArtifactLocations()[0].toString();