KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > freeform > ui > ClasspathPanel


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.java.freeform.ui;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.event.ItemEvent JavaDoc;
24 import java.io.File JavaDoc;
25 import java.net.MalformedURLException JavaDoc;
26 import java.text.Collator JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Comparator JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.SortedSet JavaDoc;
34 import java.util.TreeSet JavaDoc;
35 import javax.swing.DefaultComboBoxModel JavaDoc;
36 import javax.swing.DefaultListCellRenderer JavaDoc;
37 import javax.swing.DefaultListModel JavaDoc;
38 import javax.swing.JFileChooser JavaDoc;
39 import javax.swing.JList JavaDoc;
40 import javax.swing.filechooser.FileFilter JavaDoc;
41 import org.netbeans.api.java.platform.JavaPlatform;
42 import org.netbeans.api.java.platform.JavaPlatformManager;
43 import org.netbeans.api.java.platform.PlatformsCustomizer;
44 import org.netbeans.modules.ant.freeform.spi.support.Util;
45 import org.netbeans.modules.java.freeform.JavaProjectGenerator;
46 import org.netbeans.modules.java.freeform.jdkselection.JdkConfiguration;
47 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
48 import org.netbeans.spi.project.support.ant.PropertyUtils;
49 import org.openide.ErrorManager;
50 import org.openide.filesystems.FileUtil;
51 import org.openide.util.HelpCtx;
52 import org.openide.util.NbBundle;
53
54 /**
55  * @author David Konecny, Jesse Glick
56  */

57 public class ClasspathPanel extends javax.swing.JPanel JavaDoc implements HelpCtx.Provider {
58
59     private DefaultListModel JavaDoc listModel;
60     private File JavaDoc lastChosenFile = null;
61     private boolean isSeparateClasspath = true;
62     private List JavaDoc<ProjectModel.CompilationUnitKey> compUnitsKeys;
63     private boolean ignoreEvent;
64     private ProjectModel model;
65     private final JdkConfiguration jdkConf;
66
67     /**
68      * Create new panel in wizard mode.
69      */

70     public ClasspathPanel() {
71         jdkConf = null;
72         basicInit();
73         javaPlatformPanel.setVisible(false);
74     }
75
76     /**
77      * Create new panel in project properties mode.
78      */

79     ClasspathPanel(JdkConfiguration jdkConf) {
80         this.jdkConf = jdkConf;
81         basicInit();
82         jTextArea1.setText(NbBundle.getMessage(ClasspathPanel.class, "LBL_ClasspathPanel_Explanation"));
83         javaPlatformIntro.setBackground(getBackground());
84         javaPlatformIntro.setDisabledTextColor(jLabel2.getForeground());
85         refreshJavaPlatforms();
86         javaPlatform.setRenderer(new DefaultListCellRenderer JavaDoc() {
87             public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
88                 return super.getListCellRendererComponent(list, ((JavaPlatform) value).getDisplayName(), index, isSelected, cellHasFocus);
89             }
90         });
91     }
92
93     private void basicInit() {
94         initComponents();
95         jTextArea1.setBackground(getBackground());
96         jTextArea1.setDisabledTextColor(jLabel2.getForeground());
97         listModel = new DefaultListModel JavaDoc();
98         classpath.setModel(listModel);
99     }
100
101     private void refreshJavaPlatforms() {
102         SortedSet JavaDoc<JavaPlatform> platforms = new TreeSet JavaDoc<JavaPlatform>(new Comparator JavaDoc<JavaPlatform>() {
103             Collator JavaDoc COLL = Collator.getInstance();
104             public int compare(JavaPlatform p1, JavaPlatform p2) {
105                 int res = COLL.compare(p1.getDisplayName(), p2.getDisplayName());
106                 if (res != 0) {
107                     return res;
108                 } else {
109                     return System.identityHashCode(p1) - System.identityHashCode(p2);
110                 }
111             }
112         });
113         platforms.addAll(Arrays.asList(JavaPlatformManager.getDefault().getInstalledPlatforms()));
114         javaPlatform.setModel(new DefaultComboBoxModel JavaDoc(platforms.toArray(new JavaPlatform[platforms.size()])));
115         JavaPlatform pf = jdkConf.getSelectedPlatform();
116         if (pf == null) {
117             pf = JavaPlatformManager.getDefault().getDefaultPlatform();
118         }
119         javaPlatform.setSelectedItem(pf);
120     }
121     
122     public HelpCtx getHelpCtx() {
123         return new HelpCtx( ClasspathPanel.class );
124     }
125     
126     void updateControls() {
127         sourceFolder.removeAllItems();
128         compUnitsKeys = model.createCompilationUnitKeys();
129         isSeparateClasspath = !ProjectModel.isSingleCompilationUnit(compUnitsKeys);
130         List JavaDoc<String JavaDoc> names = createComboContent(compUnitsKeys, model.getEvaluator(), model.getNBProjectFolder());
131         for (String JavaDoc nm : names) {
132             sourceFolder.addItem(nm);
133         }
134         if (names.size() > 0) {
135             ignoreEvent = true;
136             sourceFolder.setSelectedIndex(0);
137             ignoreEvent = false;
138         }
139         loadClasspath();
140         
141         // enable/disable "Separate Classpath" checkbox
142
boolean sepClasspath = model.canHaveSeparateClasspath();
143         separateClasspath.setEnabled(sepClasspath);
144         if (sepClasspath) {
145             // in case there are separate comp units for sources and tests
146
// then disable "Separate Classpath" checkbox because at the
147
// moment it is not possible to create single compilation unit for them
148
if (isSeparateClasspath && !model.canCreateSingleCompilationUnit()) {
149                 separateClasspath.setEnabled(false);
150             }
151         }
152         jLabel2.setEnabled(sepClasspath && isSeparateClasspath);
153         sourceFolder.setEnabled(sepClasspath && isSeparateClasspath);
154         // set initial value of the checkbox
155
ignoreEvent = true;
156         separateClasspath.setSelected(isSeparateClasspath);
157         ignoreEvent = false;
158
159         // disable classpath panel and Add Classpath button if there is
160
// no compilation unit ot be configured
161
addClasspath.setEnabled(compUnitsKeys.size() > 0);
162         classpath.setEnabled(compUnitsKeys.size() > 0);
163     }
164     
165     
166     static List JavaDoc<String JavaDoc> createComboContent(List JavaDoc<ProjectModel.CompilationUnitKey> compilationUnitKeys, PropertyEvaluator evaluator, File JavaDoc nbProjectFolder) {
167         List JavaDoc<String JavaDoc> l = new ArrayList JavaDoc<String JavaDoc>();
168         for (ProjectModel.CompilationUnitKey cul : compilationUnitKeys) {
169             String JavaDoc name;
170             if (cul.locations.size() == 1) {
171                 if (cul.label != null) {
172                     name = cul.label + " [" + SourceFoldersPanel.getLocationDisplayName(evaluator, nbProjectFolder, cul.locations.get(0)) + "]"; // NOI18N
173
} else {
174                     name = convertListToString(cul.locations);
175                 }
176             } else {
177                 name = convertListToString(cul.locations);
178             }
179             l.add(name);
180         }
181         return l;
182     }
183     
184     private static String JavaDoc convertListToString(List JavaDoc<String JavaDoc> l) {
185         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
186         Iterator JavaDoc<String JavaDoc> it = l.iterator();
187         while (it.hasNext()) {
188             String JavaDoc s = it.next();
189             sb.append(s);
190             if (it.hasNext()) {
191                 sb.append(File.pathSeparatorChar+" "); // NOI18N
192
}
193         }
194         return sb.toString();
195     }
196     
197     /** This method is called from within the constructor to
198      * initialize the form.
199      * WARNING: Do NOT modify this code. The content of this method is
200      * always regenerated by the Form Editor.
201      */

202     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
203
private void initComponents() {
204         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
205
206         jLabel2 = new javax.swing.JLabel JavaDoc();
207         jLabel3 = new javax.swing.JLabel JavaDoc();
208         addClasspath = new javax.swing.JButton JavaDoc();
209         removeClasspath = new javax.swing.JButton JavaDoc();
210         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
211         classpath = new javax.swing.JList JavaDoc();
212         jPanel1 = new javax.swing.JPanel JavaDoc();
213         sourceFolder = new javax.swing.JComboBox JavaDoc();
214         moveUp = new javax.swing.JButton JavaDoc();
215         moveDown = new javax.swing.JButton JavaDoc();
216         jTextArea1 = new javax.swing.JTextArea JavaDoc();
217         separateClasspath = new javax.swing.JCheckBox JavaDoc();
218         javaPlatformPanel = new javax.swing.JPanel JavaDoc();
219         javaPlatformIntro = new javax.swing.JTextArea JavaDoc();
220         javaPlatformLabel = new javax.swing.JLabel JavaDoc();
221         javaPlatform = new javax.swing.JComboBox JavaDoc();
222         javaPlatformButton = new javax.swing.JButton JavaDoc();
223
224         setPreferredSize(new java.awt.Dimension JavaDoc(275, 202));
225         setLayout(new java.awt.GridBagLayout JavaDoc());
226
227         jLabel2.setLabelFor(sourceFolder);
228         org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(ClasspathPanel.class, "LBL_ClasspathPanel_jLabel2")); // NOI18N
229
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
230         gridBagConstraints.gridx = 0;
231         gridBagConstraints.gridy = 2;
232         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
233         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 6);
234         add(jLabel2, gridBagConstraints);
235         jLabel2.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ClasspathPanel.class, "ACSD_ClasspathPanel_jLabel2")); // NOI18N
236

237         jLabel3.setLabelFor(classpath);
238         org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(ClasspathPanel.class, "LBL_ClasspathPanel_jLabel3")); // NOI18N
239
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
240         gridBagConstraints.gridx = 0;
241         gridBagConstraints.gridy = 3;
242         gridBagConstraints.gridwidth = 2;
243         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
244         gridBagConstraints.insets = new java.awt.Insets JavaDoc(11, 0, 0, 0);
245         add(jLabel3, gridBagConstraints);
246         jLabel3.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ClasspathPanel.class, "ACSD_ClasspathPanel_jLabel3")); // NOI18N
247

248         org.openide.awt.Mnemonics.setLocalizedText(addClasspath, org.openide.util.NbBundle.getMessage(ClasspathPanel.class, "BTN_ClasspathPanel_addClasspath")); // NOI18N
249
addClasspath.addActionListener(new java.awt.event.ActionListener JavaDoc() {
250             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
251                 addClasspathActionPerformed(evt);
252             }
253         });
254         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
255         gridBagConstraints.gridx = 2;
256         gridBagConstraints.gridy = 4;
257         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
258         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 0);
259         add(addClasspath, gridBagConstraints);
260         addClasspath.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ClasspathPanel.class, "ACSD_ClasspathPanel_addClasspath")); // NOI18N
261

262         org.openide.awt.Mnemonics.setLocalizedText(removeClasspath, org.openide.util.NbBundle.getMessage(ClasspathPanel.class, "BTN_ClasspathPanel_removeClasspath")); // NOI18N
263
removeClasspath.addActionListener(new java.awt.event.ActionListener JavaDoc() {
264             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
265                 removeClasspathActionPerformed(evt);
266             }
267         });
268         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
269         gridBagConstraints.gridx = 2;
270         gridBagConstraints.gridy = 5;
271         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
272         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
273         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 0);
274         add(removeClasspath, gridBagConstraints);
275         removeClasspath.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ClasspathPanel.class, "ACSD_ClasspathPanel_removeClasspath")); // NOI18N
276

277         classpath.addListSelectionListener(new javax.swing.event.ListSelectionListener JavaDoc() {
278             public void valueChanged(javax.swing.event.ListSelectionEvent JavaDoc evt) {
279                 classpathValueChanged(evt);
280             }
281         });
282         jScrollPane1.setViewportView(classpath);
283         classpath.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ClasspathPanel.class, "ACSD_ClasspathPanel_classpath")); // NOI18N
284

285         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
286         gridBagConstraints.gridx = 0;
287         gridBagConstraints.gridy = 4;
288         gridBagConstraints.gridwidth = 2;
289         gridBagConstraints.gridheight = 4;
290         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
291         gridBagConstraints.weightx = 1.0;
292         gridBagConstraints.weighty = 1.0;
293         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 12);
294         add(jScrollPane1, gridBagConstraints);
295
296         jPanel1.setLayout(new java.awt.GridBagLayout JavaDoc());
297
298         sourceFolder.addItemListener(new java.awt.event.ItemListener JavaDoc() {
299             public void itemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {
300                 sourceFolderItemStateChanged(evt);
301             }
302         });
303         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
304         gridBagConstraints.gridx = 0;
305         gridBagConstraints.gridy = 0;
306         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
307         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
308         gridBagConstraints.weightx = 1.0;
309         jPanel1.add(sourceFolder, gridBagConstraints);
310
311         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
312         gridBagConstraints.gridx = 1;
313         gridBagConstraints.gridy = 2;
314         gridBagConstraints.gridwidth = 2;
315         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
316         add(jPanel1, gridBagConstraints);
317
318         org.openide.awt.Mnemonics.setLocalizedText(moveUp, org.openide.util.NbBundle.getMessage(ClasspathPanel.class, "LBL_ClasspathPanel_Move_Up")); // NOI18N
319
moveUp.addActionListener(new java.awt.event.ActionListener JavaDoc() {
320             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
321                 moveUpActionPerformed(evt);
322             }
323         });
324         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
325         gridBagConstraints.gridx = 2;
326         gridBagConstraints.gridy = 6;
327         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
328         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
329         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 0);
330         add(moveUp, gridBagConstraints);
331         java.util.ResourceBundle JavaDoc bundle = java.util.ResourceBundle.getBundle("org/netbeans/modules/java/freeform/ui/Bundle"); // NOI18N
332
moveUp.getAccessibleContext().setAccessibleDescription(bundle.getString("AD_ClasspathPanel_noveUp")); // NOI18N
333

334         org.openide.awt.Mnemonics.setLocalizedText(moveDown, org.openide.util.NbBundle.getMessage(ClasspathPanel.class, "LBL_ClasspathPanel_Move_Down")); // NOI18N
335
moveDown.addActionListener(new java.awt.event.ActionListener JavaDoc() {
336             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
337                 moveDownActionPerformed(evt);
338             }
339         });
340         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
341         gridBagConstraints.gridx = 2;
342         gridBagConstraints.gridy = 7;
343         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
344         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
345         add(moveDown, gridBagConstraints);
346         moveDown.getAccessibleContext().setAccessibleDescription(bundle.getString("AD_ClasspathPanel_moveDown")); // NOI18N
347

348         jTextArea1.setEditable(false);
349         jTextArea1.setLineWrap(true);
350         jTextArea1.setText(org.openide.util.NbBundle.getMessage(ClasspathPanel.class, "MSG_ClasspathPanel_jTextArea")); // NOI18N
351
jTextArea1.setWrapStyleWord(true);
352         jTextArea1.setEnabled(false);
353         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
354         gridBagConstraints.gridwidth = 3;
355         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
356         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 12);
357         add(jTextArea1, gridBagConstraints);
358         jTextArea1.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(ClasspathPanel.class, "ACSN_ClasspathPanel_jTextArea")); // NOI18N
359
jTextArea1.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ClasspathPanel.class, "ACSD_ClasspathPanel_jTextArea")); // NOI18N
360

361         separateClasspath.setSelected(true);
362         org.openide.awt.Mnemonics.setLocalizedText(separateClasspath, org.openide.util.NbBundle.getMessage(ClasspathPanel.class, "LBL_ClasspathPanel_sepatateClasspath")); // NOI18N
363
separateClasspath.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
364         separateClasspath.addActionListener(new java.awt.event.ActionListener JavaDoc() {
365             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
366                 separateClasspathActionPerformed(evt);
367             }
368         });
369         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
370         gridBagConstraints.gridx = 0;
371         gridBagConstraints.gridy = 1;
372         gridBagConstraints.gridwidth = 3;
373         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
374         gridBagConstraints.insets = new java.awt.Insets JavaDoc(11, 0, 0, 0);
375         add(separateClasspath, gridBagConstraints);
376         separateClasspath.getAccessibleContext().setAccessibleDescription(bundle.getString("AD_ClasspathPanel_separateClasspath")); // NOI18N
377

378         javaPlatformPanel.setLayout(new java.awt.GridBagLayout JavaDoc());
379
380         javaPlatformIntro.setEditable(false);
381         javaPlatformIntro.setLineWrap(true);
382         javaPlatformIntro.setText(org.openide.util.NbBundle.getMessage(ClasspathPanel.class, "ClasspathPanel.javaPlatformIntro")); // NOI18N
383
javaPlatformIntro.setWrapStyleWord(true);
384         javaPlatformIntro.setEnabled(false);
385         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
386         gridBagConstraints.gridx = 0;
387         gridBagConstraints.gridy = 0;
388         gridBagConstraints.gridwidth = 3;
389         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
390         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 0);
391         javaPlatformPanel.add(javaPlatformIntro, gridBagConstraints);
392
393         javaPlatformLabel.setLabelFor(javaPlatform);
394         org.openide.awt.Mnemonics.setLocalizedText(javaPlatformLabel, org.openide.util.NbBundle.getMessage(ClasspathPanel.class, "ClasspathPanel.javaPlatformLabel")); // NOI18N
395
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
396         gridBagConstraints.gridx = 0;
397         gridBagConstraints.gridy = 1;
398         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
399         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 6);
400         javaPlatformPanel.add(javaPlatformLabel, gridBagConstraints);
401         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
402         gridBagConstraints.gridx = 1;
403         gridBagConstraints.gridy = 1;
404         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
405         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
406         gridBagConstraints.weightx = 1.0;
407         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 6);
408         javaPlatformPanel.add(javaPlatform, gridBagConstraints);
409
410         org.openide.awt.Mnemonics.setLocalizedText(javaPlatformButton, org.openide.util.NbBundle.getMessage(ClasspathPanel.class, "ClasspathPanel.javaPlatformButton")); // NOI18N
411
javaPlatformButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
412             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
413                 javaPlatformButtonActionPerformed(evt);
414             }
415         });
416         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
417         gridBagConstraints.gridx = 2;
418         gridBagConstraints.gridy = 1;
419         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
420         javaPlatformPanel.add(javaPlatformButton, gridBagConstraints);
421
422         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
423         gridBagConstraints.gridx = 0;
424         gridBagConstraints.gridy = 8;
425         gridBagConstraints.gridwidth = 3;
426         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
427         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 0);
428         add(javaPlatformPanel, gridBagConstraints);
429     }// </editor-fold>//GEN-END:initComponents
430

431     private void javaPlatformButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_javaPlatformButtonActionPerformed
432
PlatformsCustomizer.showCustomizer(jdkConf.getSelectedPlatform());
433         refreshJavaPlatforms();
434     }//GEN-LAST:event_javaPlatformButtonActionPerformed
435

436     private void classpathValueChanged(javax.swing.event.ListSelectionEvent JavaDoc evt) {//GEN-FIRST:event_classpathValueChanged
437
updateButtons();
438     }//GEN-LAST:event_classpathValueChanged
439

440     private void separateClasspathActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_separateClasspathActionPerformed
441
if (ignoreEvent) {
442             return;
443         }
444         applyChanges();
445         isSeparateClasspath = separateClasspath.isSelected();
446         model.updateCompilationUnits(isSeparateClasspath);
447         updateControls();
448     }//GEN-LAST:event_separateClasspathActionPerformed
449

450     private void moveDownActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_moveDownActionPerformed
451
int indices[] = classpath.getSelectedIndices();
452         if (indices.length == 0 ||
453                 indices[indices.length - 1] == listModel.getSize() - 1) {
454             return;
455         }
456         for (int i = 0; i < indices.length; i++) {
457             int index = indices[i];
458             Object JavaDoc o = listModel.remove(index);
459             index++;
460             listModel.add(index, o);
461             indices[i] = index;
462         }
463         classpath.setSelectedIndices(indices);
464         applyChanges();
465         updateButtons();
466     }//GEN-LAST:event_moveDownActionPerformed
467

468     private void moveUpActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_moveUpActionPerformed
469
int indices[] = classpath.getSelectedIndices();
470         if (indices.length == 0 || indices[0] == 0) {
471             return;
472         }
473         for (int i = 0; i < indices.length; i++) {
474             int index = indices[i];
475             Object JavaDoc o = listModel.remove(index);
476             index--;
477             listModel.add(index, o);
478             indices[i] = index;
479         }
480         classpath.setSelectedIndices(indices);
481         applyChanges();
482         updateButtons();
483     }//GEN-LAST:event_moveUpActionPerformed
484

485     private void sourceFolderItemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {//GEN-FIRST:event_sourceFolderItemStateChanged
486
if (ignoreEvent) {
487             return;
488         }
489         if (evt.getStateChange() == ItemEvent.DESELECTED) {
490             int index = findIndex(evt.getItem());
491             // if index == -1 then item was removed and will not be saved
492
if (index != -1) {
493                 saveClasspath(index);
494             }
495         } else {
496             loadClasspath();
497         }
498         updateButtons();
499     }//GEN-LAST:event_sourceFolderItemStateChanged
500

501     private int findIndex(Object JavaDoc o) {
502         for (int i=0; i<sourceFolder.getModel().getSize(); i++) {
503             if (sourceFolder.getModel().getElementAt(i).equals(o)) {
504                 return i;
505             }
506         }
507         return -1;
508     }
509
510     /** Source package combo is changing - take classpath from the listbox and
511      * store it in compilaiton unit identified by the index.*/

512     private void saveClasspath(int index) {
513         ProjectModel.CompilationUnitKey key = compUnitsKeys.get(index);
514         JavaProjectGenerator.JavaCompilationUnit cu = model.getCompilationUnit(key, model.isTestSourceFolder(index));
515         updateCompilationUnitCompileClasspath(cu);
516     }
517
518     /** Source package has changed - find current source package and read its classpath and
519      * update classpath listbox with it.*/

520     private void loadClasspath() {
521         int index;
522         if (isSeparateClasspath) {
523             index = sourceFolder.getSelectedIndex();
524             if (index == -1) {
525                 return;
526             }
527         } else {
528             index = 0;
529         }
530         ProjectModel.CompilationUnitKey key = compUnitsKeys.get(index);
531         JavaProjectGenerator.JavaCompilationUnit cu = model.getCompilationUnit(key, model.isTestSourceFolder(index));
532         updateJListClassPath(cu.classpath);
533     }
534
535     /** Update compilation unit classpath list with the classpath specified
536      * in classpath list box. */

537     private void updateCompilationUnitCompileClasspath(JavaProjectGenerator.JavaCompilationUnit cu) {
538         List JavaDoc<JavaProjectGenerator.JavaCompilationUnit.CP> cps = cu.classpath;
539         if (cps != null) {
540             Iterator JavaDoc<JavaProjectGenerator.JavaCompilationUnit.CP> it = cps.iterator();
541             while (it.hasNext()) {
542                 JavaProjectGenerator.JavaCompilationUnit.CP cp = it.next();
543                 if (cp.mode.equals(ProjectModel.CLASSPATH_MODE_COMPILE)) {
544                     it.remove();
545                     // there should be only one, but go on
546
// break;
547
}
548             }
549         }
550         if (classpath.getModel().getSize() == 0) {
551             return;
552         }
553         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
554         for (int i=0; i<classpath.getModel().getSize(); i++) {
555             File JavaDoc f = new File JavaDoc((String JavaDoc)classpath.getModel().getElementAt(i));
556             String JavaDoc path = Util.relativizeLocation(model.getBaseFolder(), model.getNBProjectFolder(), f);
557             sb.append(path);
558             if (i+1<classpath.getModel().getSize()) {
559                 sb.append(File.pathSeparatorChar);
560             }
561         }
562         if (sb.length() > 0) {
563             if (cps == null) {
564                 cps = new ArrayList JavaDoc<JavaProjectGenerator.JavaCompilationUnit.CP>();
565                 cu.classpath = cps;
566             }
567             JavaProjectGenerator.JavaCompilationUnit.CP cp = new JavaProjectGenerator.JavaCompilationUnit.CP();
568             cp.mode = ProjectModel.CLASSPATH_MODE_COMPILE;
569             cp.classpath = sb.toString();
570             cps.add(cp);
571         }
572     }
573
574     /** Reads "compile" mode classpath and updates panel's list box.*/
575     private void updateJListClassPath(List JavaDoc<JavaProjectGenerator.JavaCompilationUnit.CP> cps) {
576         listModel.removeAllElements();
577         if (cps == null) {
578             return;
579         }
580         for (JavaProjectGenerator.JavaCompilationUnit.CP cp : cps) {
581             if (cp.mode.equals(ProjectModel.CLASSPATH_MODE_COMPILE)) {
582                 for (String JavaDoc path : PropertyUtils.tokenizePath(model.getEvaluator().evaluate(cp.classpath))) {
583                     path = PropertyUtils.resolveFile(model.getNBProjectFolder(), path).getAbsolutePath();
584                     if (path != null) {
585                         listModel.addElement(path);
586                     }
587                 }
588             }
589         }
590         updateButtons();
591     }
592     
593     private void updateButtons() {
594         int indices[] = classpath.getSelectedIndices();
595         removeClasspath.setEnabled(listModel.getSize() > 0 && indices.length != 0);
596         moveUp.setEnabled(indices.length > 0 && indices[0] != 0);
597         moveDown.setEnabled(indices.length > 0 && indices[indices.length - 1] != listModel.getSize() - 1);
598     }
599     
600     private void removeClasspathActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_removeClasspathActionPerformed
601
int entries[] = classpath.getSelectedIndices();
602         for (int i = 0; i < entries.length; i++) {
603             listModel.remove(entries[i] - i);
604         }
605         applyChanges();
606         updateButtons();
607     }//GEN-LAST:event_removeClasspathActionPerformed
608

609     private void addClasspathActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_addClasspathActionPerformed
610
JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
611         FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
612         chooser.setFileSelectionMode (JFileChooser.FILES_AND_DIRECTORIES);
613         chooser.setMultiSelectionEnabled(true);
614         if (lastChosenFile != null) {
615             chooser.setSelectedFile(lastChosenFile);
616         } else {
617             File JavaDoc files[] = model.getBaseFolder().listFiles();
618             if (files != null && files.length > 0) {
619                 chooser.setSelectedFile(files[0]);
620             } else {
621                 chooser.setSelectedFile(model.getBaseFolder());
622             }
623         }
624         chooser.setDialogTitle(NbBundle.getMessage(ClasspathPanel.class, "LBL_Browse_Classpath"));
625         
626         //#65354: prevent adding a non-folder element on the classpath:
627
FileFilter JavaDoc fileFilter = new SimpleFileFilter (
628             NbBundle.getMessage( ClasspathPanel.class, "LBL_ZipJarFolderFilter" ), // NOI18N
629
new String JavaDoc[] {"ZIP","JAR"} ); // NOI18N
630
//#61789 on old macosx (jdk 1.4.1) these two method need to be called in this order.
631
chooser.setAcceptAllFileFilterUsed( false );
632         chooser.setFileFilter(fileFilter);
633             
634         if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) {
635             for (File JavaDoc file : chooser.getSelectedFiles()) {
636                 file = FileUtil.normalizeFile(file);
637                 
638                 //Check if the file is acceted by the FileFilter,
639
//user may enter the name of non displayed file into JFileChooser
640
if (!fileFilter.accept(file)) {
641                     continue;
642                 }
643                 
644                 listModel.addElement(file.getAbsolutePath());
645                 lastChosenFile = file;
646             }
647             applyChanges();
648             updateButtons();
649         }
650     }//GEN-LAST:event_addClasspathActionPerformed
651

652     private void applyChanges() {
653         if (isSeparateClasspath) {
654             if (sourceFolder.getSelectedIndex() != -1) {
655                 saveClasspath(sourceFolder.getSelectedIndex());
656             }
657         } else {
658             saveClasspath(0);
659         }
660     }
661
662     public void setModel(ProjectModel model) {
663         this.model = model;
664         updateControls();
665     }
666     
667     // Variables declaration - do not modify//GEN-BEGIN:variables
668
private javax.swing.JButton JavaDoc addClasspath;
669     private javax.swing.JList JavaDoc classpath;
670     private javax.swing.JLabel JavaDoc jLabel2;
671     private javax.swing.JLabel JavaDoc jLabel3;
672     private javax.swing.JPanel JavaDoc jPanel1;
673     private javax.swing.JScrollPane JavaDoc jScrollPane1;
674     private javax.swing.JTextArea JavaDoc jTextArea1;
675     javax.swing.JComboBox JavaDoc javaPlatform;
676     private javax.swing.JButton JavaDoc javaPlatformButton;
677     private javax.swing.JTextArea JavaDoc javaPlatformIntro;
678     private javax.swing.JLabel JavaDoc javaPlatformLabel;
679     private javax.swing.JPanel JavaDoc javaPlatformPanel;
680     private javax.swing.JButton JavaDoc moveDown;
681     private javax.swing.JButton JavaDoc moveUp;
682     private javax.swing.JButton JavaDoc removeClasspath;
683     private javax.swing.JCheckBox JavaDoc separateClasspath;
684     private javax.swing.JComboBox JavaDoc sourceFolder;
685     // End of variables declaration//GEN-END:variables
686

687     private static class SimpleFileFilter extends FileFilter JavaDoc {
688
689         private String JavaDoc description;
690         private Collection JavaDoc extensions;
691
692
693         public SimpleFileFilter (String JavaDoc description, String JavaDoc[] extensions) {
694             this.description = description;
695             this.extensions = Arrays.asList(extensions);
696         }
697
698         public boolean accept(File JavaDoc f) {
699             if (f.isDirectory())
700                 return true;
701             try {
702                 return FileUtil.isArchiveFile(f.toURI().toURL());
703             } catch (MalformedURLException JavaDoc mue) {
704                 ErrorManager.getDefault().notify(mue);
705                 return false;
706             }
707         }
708
709         public String JavaDoc getDescription() {
710             return this.description;
711         }
712     }
713 }
714
Popular Tags