KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > freeform > ui > WebClasspathPanel


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.web.freeform.ui;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.io.File JavaDoc;
25 import java.net.MalformedURLException JavaDoc;
26 import java.util.List JavaDoc;
27 import javax.swing.DefaultListModel JavaDoc;
28 import javax.swing.JFileChooser JavaDoc;
29 import javax.swing.filechooser.FileFilter JavaDoc;
30 import org.netbeans.modules.ant.freeform.spi.support.Util;
31 import org.netbeans.modules.web.freeform.WebProjectGenerator;
32 import org.netbeans.spi.project.AuxiliaryConfiguration;
33 import org.netbeans.spi.project.support.ant.AntProjectHelper;
34 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
35 import org.netbeans.spi.project.support.ant.PropertyUtils;
36 import org.openide.ErrorManager;
37 import org.openide.filesystems.FileUtil;
38 import org.openide.util.HelpCtx;
39 import org.openide.util.NbBundle;
40
41 /**
42  *
43  * @author Petr Pisl
44  */

45 public class WebClasspathPanel extends javax.swing.JPanel JavaDoc implements HelpCtx.Provider {
46
47     private DefaultListModel JavaDoc listModel;
48     /** Original project folder (not nbproject folder) */
49     private File JavaDoc projectFolder = null;
50     /** Freeform Project base folder */
51     private File JavaDoc nbProjectFolder;
52     private File JavaDoc lastChosenFile = null;
53     private boolean isSeparateClasspath = true;
54     private boolean ignoreEvent;
55     private static String JavaDoc JAVA_SOURCES_CLASSPATH
56             = org.openide.util.NbBundle.getMessage(WebClasspathPanel.class, "LBL_JAVA_SOURCE_CLASSPATH");
57     //private ProjectModel model;
58

59     /** Creates new form ClasspathPanel */
60     public WebClasspathPanel() {
61         this(true);
62     }
63     
64     public WebClasspathPanel(boolean isWizard) {
65         initComponents();
66         jTextArea1.setBackground(getBackground());
67         listModel = new DefaultListModel JavaDoc();
68         listModel.add(0,JAVA_SOURCES_CLASSPATH);
69         classpath.setModel(listModel);
70         if (!isWizard) {
71             jTextArea1.setText(org.openide.util.NbBundle.getMessage(WebClasspathPanel.class, "LBL_ClasspathPanel_Explanation"));
72         }
73         
74     }
75     
76     public HelpCtx getHelpCtx() {
77         return new HelpCtx( WebClasspathPanel.class );
78     }
79     
80     
81     /** This method is called from within the constructor to
82      * initialize the form.
83      * WARNING: Do NOT modify this code. The content of this method is
84      * always regenerated by the Form Editor.
85      */

86     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
87
private void initComponents() {
88         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
89
90         jLabel3 = new javax.swing.JLabel JavaDoc();
91         addClasspath = new javax.swing.JButton JavaDoc();
92         removeClasspath = new javax.swing.JButton JavaDoc();
93         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
94         classpath = new javax.swing.JList JavaDoc();
95         jPanel1 = new javax.swing.JPanel JavaDoc();
96         moveUp = new javax.swing.JButton JavaDoc();
97         moveDown = new javax.swing.JButton JavaDoc();
98         jTextArea1 = new javax.swing.JTextArea JavaDoc();
99
100         setLayout(new java.awt.GridBagLayout JavaDoc());
101
102         jLabel3.setLabelFor(classpath);
103         org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(WebClasspathPanel.class, "LBL_ClasspathPanel_jLabel3"));
104         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
105         gridBagConstraints.gridx = 0;
106         gridBagConstraints.gridy = 3;
107         gridBagConstraints.gridwidth = 2;
108         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
109         gridBagConstraints.insets = new java.awt.Insets JavaDoc(11, 0, 0, 0);
110         add(jLabel3, gridBagConstraints);
111         jLabel3.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(WebClasspathPanel.class, "ACSD_ClasspathPanel_jLabel3"));
112
113         org.openide.awt.Mnemonics.setLocalizedText(addClasspath, org.openide.util.NbBundle.getMessage(WebClasspathPanel.class, "BTN_ClasspathPanel_addClasspath"));
114         addClasspath.addActionListener(new java.awt.event.ActionListener JavaDoc() {
115             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
116                 addClasspathActionPerformed(evt);
117             }
118         });
119
120         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
121         gridBagConstraints.gridx = 2;
122         gridBagConstraints.gridy = 4;
123         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
124         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 0);
125         add(addClasspath, gridBagConstraints);
126         addClasspath.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(WebClasspathPanel.class, "ACSD_ClasspathPanel_addClasspath"));
127
128         org.openide.awt.Mnemonics.setLocalizedText(removeClasspath, org.openide.util.NbBundle.getMessage(WebClasspathPanel.class, "BTN_ClasspathPanel_removeClasspath"));
129         removeClasspath.addActionListener(new java.awt.event.ActionListener JavaDoc() {
130             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
131                 removeClasspathActionPerformed(evt);
132             }
133         });
134
135         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
136         gridBagConstraints.gridx = 2;
137         gridBagConstraints.gridy = 5;
138         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
139         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
140         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 0);
141         add(removeClasspath, gridBagConstraints);
142         removeClasspath.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(WebClasspathPanel.class, "ACSD_ClasspathPanel_removeClasspath"));
143
144         classpath.addListSelectionListener(new javax.swing.event.ListSelectionListener JavaDoc() {
145             public void valueChanged(javax.swing.event.ListSelectionEvent JavaDoc evt) {
146                 classpathValueChanged(evt);
147             }
148         });
149
150         jScrollPane1.setViewportView(classpath);
151         classpath.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(WebClasspathPanel.class, "ACSD_ClasspathPanel_classpath"));
152
153         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
154         gridBagConstraints.gridx = 0;
155         gridBagConstraints.gridy = 4;
156         gridBagConstraints.gridwidth = 2;
157         gridBagConstraints.gridheight = 4;
158         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
159         gridBagConstraints.weightx = 1.0;
160         gridBagConstraints.weighty = 1.0;
161         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 12);
162         add(jScrollPane1, gridBagConstraints);
163
164         jPanel1.setLayout(new java.awt.GridBagLayout JavaDoc());
165
166         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
167         gridBagConstraints.gridx = 1;
168         gridBagConstraints.gridy = 2;
169         gridBagConstraints.gridwidth = 2;
170         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
171         add(jPanel1, gridBagConstraints);
172
173         moveUp.setMnemonic(org.openide.util.NbBundle.getMessage(WebClasspathPanel.class, "LBL_MoveUp_MNE").charAt(0));
174         moveUp.setText(org.openide.util.NbBundle.getMessage(WebClasspathPanel.class, "LBL_ClasspathPanel_Move_Up"));
175         moveUp.addActionListener(new java.awt.event.ActionListener JavaDoc() {
176             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
177                 moveUpActionPerformed(evt);
178             }
179         });
180
181         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
182         gridBagConstraints.gridx = 2;
183         gridBagConstraints.gridy = 6;
184         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
185         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
186         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 0);
187         add(moveUp, gridBagConstraints);
188         moveUp.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/web/freeform/ui/Bundle").getString("AD_ClasspathPanel_noveUp"));
189
190         moveDown.setMnemonic(org.openide.util.NbBundle.getMessage(WebClasspathPanel.class, "LBL_MoveDown_MNE").charAt(0));
191         moveDown.setText(org.openide.util.NbBundle.getMessage(WebClasspathPanel.class, "LBL_ClasspathPanel_Move_Down"));
192         moveDown.addActionListener(new java.awt.event.ActionListener JavaDoc() {
193             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
194                 moveDownActionPerformed(evt);
195             }
196         });
197
198         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
199         gridBagConstraints.gridx = 2;
200         gridBagConstraints.gridy = 7;
201         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
202         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
203         add(moveDown, gridBagConstraints);
204         moveDown.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/web/freeform/ui/Bundle").getString("AD_ClasspathPanel_moveDown"));
205
206         jTextArea1.setEditable(false);
207         jTextArea1.setForeground(new java.awt.Color JavaDoc(0, 0, 0));
208         jTextArea1.setLineWrap(true);
209         jTextArea1.setText(org.openide.util.NbBundle.getMessage(WebClasspathPanel.class, "MSG_ClasspathPanel_jTextArea"));
210         jTextArea1.setWrapStyleWord(true);
211         jTextArea1.setDisabledTextColor(java.awt.Color.black);
212         jTextArea1.setEnabled(false);
213         jTextArea1.setSelectedTextColor(new java.awt.Color JavaDoc(0, 0, 0));
214         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
215         gridBagConstraints.gridwidth = 3;
216         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
217         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 12);
218         add(jTextArea1, gridBagConstraints);
219         jTextArea1.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(WebClasspathPanel.class, "ACSN_ClasspathPanel_jTextArea"));
220         jTextArea1.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(WebClasspathPanel.class, "ACSD_ClasspathPanel_jTextArea"));
221
222     }// </editor-fold>//GEN-END:initComponents
223

224     private void classpathValueChanged(javax.swing.event.ListSelectionEvent JavaDoc evt) {//GEN-FIRST:event_classpathValueChanged
225
updateButtons();
226     }//GEN-LAST:event_classpathValueChanged
227

228     private void moveDownActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_moveDownActionPerformed
229
int indices[] = classpath.getSelectedIndices();
230         if (indices.length == 0 ||
231                 indices[indices.length - 1] == listModel.getSize() - 1) {
232             return;
233         }
234         for (int i = 0; i < indices.length; i++) {
235             int index = indices[i];
236             Object JavaDoc o = listModel.remove(index);
237             index++;
238             listModel.add(index, o);
239             indices[i] = index;
240         }
241         classpath.setSelectedIndices(indices);
242         updateButtons();
243     }//GEN-LAST:event_moveDownActionPerformed
244

245     private void moveUpActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_moveUpActionPerformed
246
int indices[] = classpath.getSelectedIndices();
247         if (indices.length == 0 || indices[0] == 0) {
248             return;
249         }
250         for (int i = 0; i < indices.length; i++) {
251             int index = indices[i];
252             Object JavaDoc o = listModel.remove(index);
253             index--;
254             listModel.add(index, o);
255             indices[i] = index;
256         }
257         classpath.setSelectedIndices(indices);
258         updateButtons();
259     }//GEN-LAST:event_moveUpActionPerformed
260

261     void updateButtons() {
262         int indices[] = classpath.getSelectedIndices();
263         removeClasspath.setEnabled(listModel.getSize() > 0 && indices.length != 0 && indices[0] != 0);
264         moveUp.setEnabled(indices.length > 0 && indices[0] > 1);
265         moveDown.setEnabled(indices.length > 0 && indices[indices.length - 1] != listModel.getSize() - 1 && indices[0] != 0);
266     }
267     
268     private void removeClasspathActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_removeClasspathActionPerformed
269
int entries[] = classpath.getSelectedIndices();
270         for (int i = 0; i < entries.length; i++) {
271             listModel.remove(entries[i] - i);
272         }
273         updateButtons();
274     }//GEN-LAST:event_removeClasspathActionPerformed
275

276     private void addClasspathActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_addClasspathActionPerformed
277
JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
278         FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
279         chooser.setFileSelectionMode (JFileChooser.FILES_AND_DIRECTORIES);
280         chooser.setMultiSelectionEnabled(true);
281         if (lastChosenFile != null) {
282             chooser.setSelectedFile(lastChosenFile);
283         }
284         else {
285             if (projectFolder!= null)
286                 chooser.setSelectedFile(projectFolder);
287         }
288         chooser.setDialogTitle(NbBundle.getMessage(WebClasspathPanel.class, "LBL_Browse_Classpath"));
289         
290         //#77911: prevent adding a non-folder element on the classpath:
291
FileFilter JavaDoc fileFilter = new SimpleFileFilter(NbBundle.getMessage(WebClasspathPanel.class, "LBL_ZipJarFolderFilter")); // NOI18N
292
chooser.setFileFilter(fileFilter);
293         chooser.setAcceptAllFileFilterUsed( false );
294
295         if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) {
296             File JavaDoc files[] = chooser.getSelectedFiles();
297             for (int i=0; i<files.length; i++) {
298                 File JavaDoc file = FileUtil.normalizeFile(files[i]);
299                 
300                 //Check if the file is acceted by the FileFilter,
301
//user may enter the name of non displayed file into JFileChooser
302
if (!fileFilter.accept(file)) {
303                     continue;
304                 }
305
306                 listModel.addElement(file.getAbsolutePath());
307                 lastChosenFile = file;
308             }
309             updateButtons();
310         }
311     }//GEN-LAST:event_addClasspathActionPerformed
312

313     /** Called from WizardDescriptor.Panel and ProjectCustomizer.Panel
314      * to set base folder. Panel will use this for default position of JFileChooser.
315      * @param baseFolder original project base folder
316      * @param nbProjectFolder Freeform Project base folder
317      */

318     public void setProjectFolders(File JavaDoc baseFolder, File JavaDoc nbProjectFolder) {
319         this.projectFolder = baseFolder;
320         this.nbProjectFolder = nbProjectFolder;
321     }
322     
323     public String JavaDoc getClasspath(){
324         StringBuffer JavaDoc sf = new StringBuffer JavaDoc();
325         for (int i = 1; i < listModel.getSize(); i++){
326             File JavaDoc f = new File JavaDoc((String JavaDoc)listModel.get(i));
327             String JavaDoc path = org.netbeans.modules.ant.freeform.spi.support.Util
328                     .relativizeLocation(projectFolder, nbProjectFolder, f);
329             sf.append(path);
330             if (i < listModel.getSize()-1)
331                 sf.append(File.pathSeparatorChar);
332         }
333         return sf.toString();
334     }
335     
336     void setClasspath(String JavaDoc classpath, PropertyEvaluator evaluator){
337         if (classpath == null)
338             return;
339         listModel.clear();
340         listModel.addElement(JAVA_SOURCES_CLASSPATH);
341         
342         String JavaDoc[] cpa = PropertyUtils.tokenizePath(evaluator.evaluate(classpath));
343         for (int i=0; i<cpa.length; i++) {
344             String JavaDoc path = cpa[i];
345             path = PropertyUtils.resolveFile(nbProjectFolder, path).getAbsolutePath();
346             if (path != null) {
347                 listModel.addElement(path);
348             }
349         }
350     }
351     
352     ActionListener JavaDoc getCustomizerOkListener(final AntProjectHelper projectHelper) {
353         return new ActionListener JavaDoc() {
354             public void actionPerformed(ActionEvent JavaDoc arg0) {
355                 AuxiliaryConfiguration aux = Util.getAuxiliaryConfiguration(projectHelper);
356                 List JavaDoc l = WebProjectGenerator.getWebmodules(projectHelper, aux);
357                 if (l != null){
358                     WebProjectGenerator.WebModule wm = (WebProjectGenerator.WebModule)l.get(0);
359                     wm.classpath = getClasspath();
360                     WebProjectGenerator.putWebModules(projectHelper, aux, l);
361                 }
362                 //mkleint: why updating buttons on saving??
363
updateButtons();
364             }
365         };
366     }
367
368   
369     
370     
371     // Variables declaration - do not modify//GEN-BEGIN:variables
372
private javax.swing.JButton JavaDoc addClasspath;
373     private javax.swing.JList JavaDoc classpath;
374     private javax.swing.JLabel JavaDoc jLabel3;
375     private javax.swing.JPanel JavaDoc jPanel1;
376     private javax.swing.JScrollPane JavaDoc jScrollPane1;
377     private javax.swing.JTextArea JavaDoc jTextArea1;
378     private javax.swing.JButton JavaDoc moveDown;
379     private javax.swing.JButton JavaDoc moveUp;
380     private javax.swing.JButton JavaDoc removeClasspath;
381     // End of variables declaration//GEN-END:variables
382

383     private static class SimpleFileFilter extends FileFilter JavaDoc {
384
385         private String JavaDoc description;
386
387         public SimpleFileFilter (String JavaDoc description) {
388             this.description = description;
389         }
390
391         public boolean accept(File JavaDoc f) {
392             if (f.isDirectory())
393                 return true;
394             try {
395                 return FileUtil.isArchiveFile(f.toURI().toURL());
396             } catch (MalformedURLException JavaDoc mue) {
397                 ErrorManager.getDefault().notify(mue);
398                 return false;
399             }
400         }
401
402         public String JavaDoc getDescription() {
403             return this.description;
404         }
405     }
406
407 }
408
Popular Tags