KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > execution > beaninfo > editors > NbClassPathCustomEditor


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.core.execution.beaninfo.editors;
21
22 import java.beans.PropertyEditor JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28 import javax.swing.DefaultListModel JavaDoc;
29 import javax.swing.JFileChooser JavaDoc;
30 import javax.swing.filechooser.FileFilter JavaDoc;
31 import org.netbeans.beaninfo.editors.FileEditor;
32 import org.openide.execution.NbClassPath;
33 import org.openide.util.HelpCtx;
34 import org.openide.util.NbBundle;
35 import org.openide.util.NbCollections;
36 import org.openide.windows.WindowManager;
37
38 /**
39  * Panel for editing entries in the classpath.
40  * @author David Strupl
41  */

42 class NbClassPathCustomEditor extends javax.swing.JPanel JavaDoc {
43     
44     /** Remember last folder, which was explored in FileChooser.*/
45     private static File JavaDoc lastDirFolder = null;
46     /** Remember last folder, which was explored in FileChooser.*/
47     private static File JavaDoc lastJarFolder = null;
48     /** Property editor associated with. */
49     private PropertyEditor JavaDoc editor;
50     /** Model of list of class path items. */
51     private DefaultListModel JavaDoc listModel = new DefaultListModel JavaDoc();
52     private boolean editable = true;
53
54     
55     /** Creates new form NbClassPathCustomEditor */
56     public NbClassPathCustomEditor() {
57         initComponents ();
58         pathList.setModel(listModel);
59         pathScrollPane.setViewportView(pathList);
60         
61         setMinimumSize (new java.awt.Dimension JavaDoc(400, 200));
62         setPreferredSize(new java.awt.Dimension JavaDoc(400, 200));
63
64         pathList.getAccessibleContext().setAccessibleDescription(getString("ACSD_PathList"));
65         addDirButton.getAccessibleContext().setAccessibleDescription(getString("ACSD_AddDirectory"));
66         addJarButton.getAccessibleContext().setAccessibleDescription(getString("ACSD_AddJAR"));
67         upButton.getAccessibleContext().setAccessibleDescription(getString("ACSD_MoveUp"));
68         downButton.getAccessibleContext().setAccessibleDescription(getString("ACSD_MoveDown"));
69         removeButton.getAccessibleContext().setAccessibleDescription(getString("ACSD_Remove"));
70         
71         getAccessibleContext().setAccessibleDescription(getString("ACSD_CustomNbClassPathEditor"));
72     }
73
74     NbClassPathCustomEditor(PropertyEditor JavaDoc propEd) {
75         this();
76         editor = propEd;
77         Object JavaDoc value = propEd.getValue();
78         if (value instanceof NbClassPath) {
79             setClassPath(((NbClassPath)value).getClassPath());
80         }
81         if ( editor instanceof NbClassPathEditor )
82             if ( ! ((NbClassPathEditor)editor).isEditable() ) {
83                 editable = false;
84                 addDirButton.setEnabled( false );
85                 addJarButton.setEnabled( false );
86             }
87     }
88     
89     /** This method is called from within the constructor to
90      * initialize the form.
91      * WARNING: Do NOT modify this code. The content of this method is
92      * always regenerated by the FormEditor.
93      */

94     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
95
private void initComponents() {
96         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
97
98         innerPanel = new javax.swing.JPanel JavaDoc();
99         addDirButton = new javax.swing.JButton JavaDoc();
100         addJarButton = new javax.swing.JButton JavaDoc();
101         upButton = new javax.swing.JButton JavaDoc();
102         downButton = new javax.swing.JButton JavaDoc();
103         removeButton = new javax.swing.JButton JavaDoc();
104         pathScrollPane = new javax.swing.JScrollPane JavaDoc();
105         pathList = new javax.swing.JList JavaDoc();
106         pathLabel = new javax.swing.JLabel JavaDoc();
107
108         setLayout(new java.awt.BorderLayout JavaDoc());
109
110         innerPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(12, 12, 0, 11));
111         innerPanel.setLayout(new java.awt.GridBagLayout JavaDoc());
112
113         org.openide.awt.Mnemonics.setLocalizedText(addDirButton, getString("CTL_AddDirectory")); // NOI18N
114
addDirButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
115             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
116                 addDirButtonActionPerformed(evt);
117             }
118         });
119         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
120         gridBagConstraints.gridx = 1;
121         gridBagConstraints.gridy = 1;
122         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
123         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
124         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 5, 0);
125         innerPanel.add(addDirButton, gridBagConstraints);
126
127         org.openide.awt.Mnemonics.setLocalizedText(addJarButton, getString("CTL_AddJAR")); // NOI18N
128
addJarButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
129             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
130                 addJarButtonActionPerformed(evt);
131             }
132         });
133         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
134         gridBagConstraints.gridx = 1;
135         gridBagConstraints.gridy = 2;
136         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
137         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
138         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 5, 0);
139         innerPanel.add(addJarButton, gridBagConstraints);
140
141         org.openide.awt.Mnemonics.setLocalizedText(upButton, getString("CTL_MoveUp")); // NOI18N
142
upButton.setEnabled(false);
143         upButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
144             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
145                 upButtonActionPerformed(evt);
146             }
147         });
148         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
149         gridBagConstraints.gridx = 1;
150         gridBagConstraints.gridy = 4;
151         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
152         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
153         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 5, 0);
154         innerPanel.add(upButton, gridBagConstraints);
155
156         org.openide.awt.Mnemonics.setLocalizedText(downButton, getString("CTL_MoveDown")); // NOI18N
157
downButton.setEnabled(false);
158         downButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
159             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
160                 downButtonActionPerformed(evt);
161             }
162         });
163         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
164         gridBagConstraints.gridx = 1;
165         gridBagConstraints.gridy = 5;
166         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
167         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
168         innerPanel.add(downButton, gridBagConstraints);
169
170         org.openide.awt.Mnemonics.setLocalizedText(removeButton, getString("CTL_Remove")); // NOI18N
171
removeButton.setEnabled(false);
172         removeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
173             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
174                 removeButtonActionPerformed(evt);
175             }
176         });
177         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
178         gridBagConstraints.gridx = 1;
179         gridBagConstraints.gridy = 3;
180         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
181         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
182         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 11, 0);
183         innerPanel.add(removeButton, gridBagConstraints);
184
185         pathList.addListSelectionListener(new javax.swing.event.ListSelectionListener JavaDoc() {
186             public void valueChanged(javax.swing.event.ListSelectionEvent JavaDoc evt) {
187                 pathListValueChanged(evt);
188             }
189         });
190         pathList.addMouseListener(new java.awt.event.MouseAdapter JavaDoc() {
191             public void mouseClicked(java.awt.event.MouseEvent JavaDoc evt) {
192                 pathListMouseClicked(evt);
193             }
194         });
195         pathScrollPane.setViewportView(pathList);
196
197         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
198         gridBagConstraints.gridx = 0;
199         gridBagConstraints.gridy = 1;
200         gridBagConstraints.gridheight = 5;
201         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
202         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
203         gridBagConstraints.weightx = 1.0;
204         gridBagConstraints.weighty = 1.0;
205         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 11);
206         innerPanel.add(pathScrollPane, gridBagConstraints);
207
208         pathLabel.setLabelFor(pathList);
209         org.openide.awt.Mnemonics.setLocalizedText(pathLabel, getString("CTL_Classpath.Border_Title")); // NOI18N
210
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
211         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
212         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 2, 0);
213         innerPanel.add(pathLabel, gridBagConstraints);
214
215         add(innerPanel, java.awt.BorderLayout.CENTER);
216     }// </editor-fold>//GEN-END:initComponents
217

218   private void pathListMouseClicked(java.awt.event.MouseEvent JavaDoc evt) {//GEN-FIRST:event_pathListMouseClicked
219
if (evt.getClickCount() != 2) {
220             // trigger the action on double-click
221
return;
222         }
223         triggerEdit(pathList.getSelectedIndex());
224   }//GEN-LAST:event_pathListMouseClicked
225

226   private void pathListValueChanged(javax.swing.event.ListSelectionEvent JavaDoc evt) {//GEN-FIRST:event_pathListValueChanged
227
enableButtons();
228   }//GEN-LAST:event_pathListValueChanged
229

230   private void removeButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_removeButtonActionPerformed
231
int index = pathList.getSelectedIndex();
232
233         Object JavaDoc [] selectedValues = pathList.getSelectedValues();
234         for (int i = 0; i < selectedValues.length; i++) {
235             listModel.removeElement(selectedValues[i]);
236             fireValueChanged();
237         }
238
239         // Select some of remaining item.
240
int size = listModel.getSize();
241         
242         if(index >= 0 && size > 0) {
243             if(size == index) {
244                 pathList.setSelectedIndex(index - 1);
245             } else if(size > index) {
246                 pathList.setSelectedIndex(index);
247             } else {
248                 pathList.setSelectedIndex(0);
249             }
250         }
251         
252         // If empty disable up, down, remve buttons.
253
enableButtons();
254   }//GEN-LAST:event_removeButtonActionPerformed
255

256   private void downButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_downButtonActionPerformed
257
int i = pathList.getSelectedIndex();
258       swap(i);
259       pathList.setSelectedIndex(i+1);
260   }//GEN-LAST:event_downButtonActionPerformed
261

262   private void upButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_upButtonActionPerformed
263
int i = pathList.getSelectedIndex();
264       swap(i-1);
265       pathList.setSelectedIndex(i - 1);
266   }//GEN-LAST:event_upButtonActionPerformed
267

268   private void addJarButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_addJarButtonActionPerformed
269

270         JFileChooser JavaDoc chooser = FileEditor.createHackedFileChooser();
271         setHelpToChooser( chooser );
272         
273         chooser.setFileFilter(new FileFilter JavaDoc() {
274                                   public boolean accept(File JavaDoc f) {
275                                       return (f.isDirectory() || f.getName().endsWith(".jar") || f.getName().endsWith(".zip")); // NOI18N
276
}
277                                   public String JavaDoc getDescription() {
278                                       return getString("CTL_JarArchivesMask");
279                                   }
280                               });
281
282         if (lastJarFolder != null) {
283             chooser.setCurrentDirectory(lastJarFolder);
284         }
285
286         chooser.setDialogTitle(getString("CTL_FileSystemPanel.Jar_Dialog_Title"));
287         chooser.setMultiSelectionEnabled( true );
288         if (chooser.showDialog(WindowManager.getDefault ().getMainWindow (),
289                                getString("CTL_Approve_Button_Title"))
290                 == JFileChooser.APPROVE_OPTION) {
291             File JavaDoc[] files = chooser.getSelectedFiles();
292             boolean found = false;
293             for (int i=0; i<files.length; i++) {
294                 if ((files[i] != null) && (files[i].isFile())) {
295                     found = true;
296                     String JavaDoc path = files[i].getAbsolutePath();
297                     if (! listModel.contains (path)) {
298                         listModel.addElement (path);
299                     }
300                 }
301             }
302             if ( found ) {
303                 lastJarFolder = chooser.getCurrentDirectory();
304                 fireValueChanged();
305             }
306             pathList.setSelectedIndex(listModel.size() - 1);
307         }
308   }//GEN-LAST:event_addJarButtonActionPerformed
309

310   private void addDirButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_addDirButtonActionPerformed
311
JFileChooser JavaDoc chooser = FileEditor.createHackedFileChooser();
312         setHelpToChooser( chooser );
313         chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
314         chooser.setDialogTitle(getString("CTL_FileSystemPanel.Local_Dialog_Title"));
315
316         if (lastDirFolder != null) {
317             chooser.setCurrentDirectory(lastDirFolder);
318         }
319
320         if (chooser.showDialog(WindowManager.getDefault ().getMainWindow (),
321                                getString("CTL_Approve_Button_Title"))
322                 == JFileChooser.APPROVE_OPTION) {
323             File JavaDoc f = chooser.getSelectedFile();
324             if ((f != null) && (f.isDirectory())) {
325                 lastDirFolder = f.getParentFile();
326
327                 String JavaDoc path = f.getAbsolutePath();
328                 if (! listModel.contains (path)) {
329                     listModel.addElement (path);
330                 }
331                 fireValueChanged();
332                 
333                 pathList.setSelectedIndex(listModel.size() - 1);
334             }
335         }
336   }//GEN-LAST:event_addDirButtonActionPerformed
337

338
339     // Variables declaration - do not modify//GEN-BEGIN:variables
340
private javax.swing.JButton JavaDoc addDirButton;
341     private javax.swing.JButton JavaDoc addJarButton;
342     private javax.swing.JButton JavaDoc downButton;
343     private javax.swing.JPanel JavaDoc innerPanel;
344     private javax.swing.JLabel JavaDoc pathLabel;
345     private javax.swing.JList JavaDoc pathList;
346     private javax.swing.JScrollPane JavaDoc pathScrollPane;
347     private javax.swing.JButton JavaDoc removeButton;
348     private javax.swing.JButton JavaDoc upButton;
349     // End of variables declaration//GEN-END:variables
350

351
352     /** Sets value to property editor if exists.
353      * @see #editor */

354     private void fireValueChanged() {
355         if (editor != null) {
356             editor.setValue(getPropertyValue());
357         }
358     }
359   
360     /** Allows the user to edit the selected item. */
361     private void triggerEdit(int index) {
362         if (index < 0) {
363             return;
364         }
365         String JavaDoc selectedItem = (String JavaDoc)listModel.elementAt(index);
366         File JavaDoc selectedF = new File JavaDoc(selectedItem);
367         if (selectedF.isDirectory()) {
368
369             JFileChooser JavaDoc chooser = FileEditor.createHackedFileChooser();
370             setHelpToChooser( chooser );
371             chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
372             chooser.setDialogTitle(getString("CTL_Edit_Local_Dialog_Title"));
373
374             if (selectedF.getParentFile() != null) {
375                 chooser.setCurrentDirectory(selectedF.getParentFile());
376                 chooser.setSelectedFile(selectedF);
377             }
378
379             if (chooser.showDialog(WindowManager.getDefault ().getMainWindow (),
380                                    getString("CTL_Approve_Button_Title"))
381                     == JFileChooser.APPROVE_OPTION) {
382                 File JavaDoc f = chooser.getSelectedFile();
383                 if ((f != null) && (f.isDirectory())) {
384                     lastDirFolder = chooser.getCurrentDirectory();
385                     try {
386                         f = f.getCanonicalFile ();
387                     } catch(IOException JavaDoc ioe) {
388                         // ignore
389
}
390
391                     listModel.set(index, f.getAbsolutePath());
392                     fireValueChanged();
393                 }
394             }
395         } else if (selectedF.isFile()) {
396             JFileChooser JavaDoc chooser = FileEditor.createHackedFileChooser();
397             setHelpToChooser( chooser );
398
399             chooser.setFileFilter(new FileFilter JavaDoc() {
400                   public boolean accept(File JavaDoc f) {
401                       return (f.isDirectory() || f.getName().endsWith(".jar") || f.getName().endsWith(".zip")); // NOI18N
402
}
403                   public String JavaDoc getDescription() {
404                       return getString("CTL_JarArchivesMask");
405                   }
406               });
407
408             chooser.setCurrentDirectory(selectedF.getParentFile());
409             chooser.setSelectedFile(selectedF);
410
411             chooser.setDialogTitle(getString("CTL_Edit_Jar_Dialog_Title"));
412             if (chooser.showDialog(WindowManager.getDefault ().getMainWindow (),
413                                    getString("CTL_Approve_Button_Title"))
414                     == JFileChooser.APPROVE_OPTION) {
415                 File JavaDoc f = chooser.getSelectedFile();
416                 if ((f != null) && (f.isFile())) {
417                     lastJarFolder = chooser.getCurrentDirectory();
418                     listModel.set(index, f.getAbsolutePath());
419                     fireValueChanged();
420                 }
421             }
422         }
423     }
424   
425     /** Swaps item on the the position index with item on the position index+1. */
426     private void swap(int index) {
427         if ((index < 0)||(index >= listModel.size() -1 )) {
428             return;
429         }
430         Object JavaDoc value = listModel.elementAt(index);
431         listModel.removeElement(value);
432         listModel.add(index + 1, value);
433         fireValueChanged();
434     }
435     
436     /** Enables buttons according to the state of the list.*/
437     private void enableButtons() {
438         if ( ! editable )
439             return;
440         removeButton.setEnabled(pathList.getSelectedIndices().length > 0);
441         if (pathList.getSelectedIndices().length == 1) {
442             downButton.setEnabled(pathList.getSelectedIndices()[0] < pathList.getModel().getSize() - 1);
443             upButton.setEnabled(pathList.getSelectedIndices()[0] > 0);
444         } else {
445             downButton.setEnabled(false);
446             upButton.setEnabled(false);
447         }
448     }
449     
450     /** This method parses given classPath and adds the elements to
451      * the listModel.
452      */

453     private void setClassPath(String JavaDoc classPath) {
454         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(classPath, File.pathSeparator);
455         while (tok.hasMoreTokens()) {
456             String JavaDoc s = tok.nextToken();
457             if (s.startsWith("\"")) { // NOI18N
458
s = s.substring(1);
459             }
460             if (s.endsWith("\"")) { // NOI18N
461
s = s.substring(0, s.length() -1 );
462             }
463             if (! listModel.contains (s)) {
464                 listModel.addElement(s);
465             }
466         }
467         
468     }
469     
470     /** Get the customized property value. Implements <code>EnhancedCustomPropertyEditor</code>.
471      * @return the property value
472      * @exception InvalidStateException when the custom property editor does not contain a valid property value
473      * (and thus it should not be set)
474      */

475     public Object JavaDoc getPropertyValue() throws IllegalStateException JavaDoc {
476         List JavaDoc<String JavaDoc> list = Collections.list(NbCollections.checkedEnumerationByFilter(listModel.elements(), String JavaDoc.class, true));
477         String JavaDoc []arr = list.toArray(new String JavaDoc[list.size()]);
478         return new NbClassPath(arr);
479     }
480
481     /** Gets localized string. Helper method. */
482     private static final String JavaDoc getString(String JavaDoc s) {
483         return NbBundle.getMessage(NbClassPathCustomEditor.class, s);
484     }
485     
486     private void setHelpToChooser( JFileChooser JavaDoc chooser ) {
487         HelpCtx help = HelpCtx.findHelp( this );
488         if ( help != null )
489             HelpCtx.setHelpIDString(chooser, help.getHelpID());
490     }
491 }
492
Popular Tags