~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to scripting/groovy/groovyproject/src/org/netbeans/modules/groovy/groovyproject/ui/customizer/AntArtifactChooser.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.groovy.groovyproject.ui.customizer;
 
43
 
 
44
import java.awt.Dimension;
 
45
import java.beans.PropertyChangeEvent;
 
46
import java.beans.PropertyChangeListener;
 
47
import java.io.File;
 
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;
 
65
 
 
66
/**
 
67
 * Accessory component used in the ProjectChooser for choosing project
 
68
 * artifacts.
 
69
 * @author Petr Hrebejk
 
70
 */
 
71
public class AntArtifactChooser extends JPanel implements PropertyChangeListener {
 
72
    
 
73
    // XXX to become an array later
 
74
    private String artifactType;
 
75
    
 
76
    /** Creates new form JarArtifactChooser */
 
77
    public AntArtifactChooser( String artifactType, JFileChooser chooser ) {
 
78
        this.artifactType = artifactType;
 
79
        
 
80
        initComponents();
 
81
        jListArtifacts.setModel( new DefaultListModel() );
 
82
        chooser.addPropertyChangeListener( this );
 
83
    }
 
84
    
 
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.
 
89
     */
 
90
    private void initComponents() {//GEN-BEGIN:initComponents
 
91
        java.awt.GridBagConstraints gridBagConstraints;
 
92
 
 
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();
 
98
 
 
99
        setLayout(new java.awt.GridBagLayout());
 
100
 
 
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);
 
107
 
 
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);
 
116
 
 
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);
 
123
 
 
124
        jScrollPane1.setViewportView(jListArtifacts);
 
125
 
 
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);
 
134
 
 
135
    }//GEN-END:initComponents
 
136
    
 
137
    
 
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);
 
145
        }
 
146
    }
 
147
    
 
148
    private Project getProject( File projectDir ) {
 
149
        
 
150
        if (projectDir == null) { // #46744
 
151
            return null;
 
152
        }
 
153
        
 
154
        try {            
 
155
            File normProjectDir = FileUtil.normalizeFile(projectDir);
 
156
            FileObject fo = FileUtil.toFileObject(normProjectDir);
 
157
            if (fo != null) {
 
158
                return ProjectManager.getDefault().findProject(fo);
 
159
            }
 
160
        } catch (IOException e) {
 
161
            ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
 
162
            // Return null
 
163
        }
 
164
        
 
165
        return null;
 
166
    }    
 
167
    
 
168
    /**
 
169
     * Set up GUI fields according to the requested project.
 
170
     * @param project a subproject, or null
 
171
     */
 
172
    private void populateAccessory( Project project ) {
 
173
        
 
174
        DefaultListModel model = (DefaultListModel)jListArtifacts.getModel();
 
175
        model.clear();
 
176
        jTextFieldName.setText(project == null ? "" : ProjectUtils.getInformation(project).getDisplayName()); //NOI18N
 
177
        
 
178
        if ( project != null ) {
 
179
                        
 
180
            AntArtifact artifacts[] = AntArtifactQuery.findArtifactsByType( project, artifactType );
 
181
        
 
182
            for( int i = 0; i < artifacts.length; i++ ) {
 
183
                model.addElement( new ArtifactItem( artifacts[i]));
 
184
            }
 
185
        }
 
186
        
 
187
    }
 
188
        
 
189
    
 
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
 
197
 
 
198
    
 
199
    /** Shows dialog with the artifact chooser 
 
200
     * @return null if canceled selected jars if some jars selected
 
201
     */
 
202
    public static AntArtifact[] showDialog( String artifactType, Project master ) {
 
203
        
 
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
 
207
        
 
208
        AntArtifactChooser accessory = new AntArtifactChooser( artifactType, chooser );
 
209
        chooser.setAccessory( accessory );
 
210
        chooser.setPreferredSize( new Dimension( 650, 380 ) );
 
211
        chooser.setCurrentDirectory (FoldersListSettings.getDefault().getLastUsedArtifactFolder());
 
212
 
 
213
        int option = chooser.showOpenDialog( null ); // Show the chooser
 
214
              
 
215
        if ( option == JFileChooser.APPROVE_OPTION ) {
 
216
 
 
217
            File dir = chooser.getSelectedFile();
 
218
            dir = FileUtil.normalizeFile (dir);
 
219
            Project selectedProject = accessory.getProject( dir );
 
220
 
 
221
            if ( selectedProject == null ) {
 
222
                return null;
 
223
            }
 
224
            
 
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 ) );
 
229
                return null;
 
230
            }
 
231
            
 
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 ) );
 
236
                return null;
 
237
            }
 
238
            
 
239
            DefaultListModel model = (DefaultListModel)accessory.jListArtifacts.getModel();
 
240
            
 
241
            AntArtifact artifacts[] = new AntArtifact[ model.size() ];
 
242
            
 
243
            // XXX Adding references twice            
 
244
            for( int i = 0; i < artifacts.length; i++ ) {
 
245
                artifacts[i] = ((ArtifactItem)model.getElementAt( i )).getArtifact();
 
246
            }
 
247
            
 
248
            FoldersListSettings.getDefault().setLastUsedArtifactFolder (FileUtil.normalizeFile(chooser.getCurrentDirectory()));
 
249
            return artifacts;
 
250
            
 
251
        }
 
252
        else {
 
253
            return null; 
 
254
        }
 
255
                
 
256
    }
 
257
       
 
258
    private static class ArtifactItem {
 
259
        
 
260
        private AntArtifact artifact;
 
261
        
 
262
        ArtifactItem( AntArtifact artifact ) {
 
263
            this.artifact = artifact;
 
264
        }
 
265
        
 
266
        AntArtifact getArtifact() {
 
267
            return artifact;
 
268
        }
 
269
        
 
270
        public String toString() {
 
271
            return artifact.getArtifactLocations()[0].toString();
 
272
        }
 
273
        
 
274
    }
 
275
}