KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tomcat5 > customizer > CustomizerSupport


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.tomcat5.customizer;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.GridBagConstraints JavaDoc;
24 import java.awt.GridBagLayout JavaDoc;
25 import java.awt.Insets JavaDoc;
26 import java.awt.event.ActionEvent JavaDoc;
27 import java.awt.event.ActionListener JavaDoc;
28 import java.io.File JavaDoc;
29 import java.net.MalformedURLException JavaDoc;
30 import java.net.URI JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Arrays JavaDoc;
34 import java.util.Collection JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.StringTokenizer JavaDoc;
38 import javax.swing.AbstractListModel JavaDoc;
39 import javax.swing.JButton JavaDoc;
40 import javax.swing.JFileChooser JavaDoc;
41 import javax.swing.JLabel JavaDoc;
42 import javax.swing.JList JavaDoc;
43 import javax.swing.JPanel JavaDoc;
44 import javax.swing.JScrollPane JavaDoc;
45 import javax.swing.event.ListSelectionEvent JavaDoc;
46 import javax.swing.event.ListSelectionListener JavaDoc;
47 import javax.swing.filechooser.FileFilter JavaDoc;
48 import org.netbeans.modules.j2ee.deployment.common.api.J2eeLibraryTypeProvider;
49 import org.openide.ErrorManager;
50 import org.openide.NotifyDescriptor;
51 import org.openide.filesystems.FileUtil;
52 import org.openide.util.NbBundle;
53
54 /**
55  * Server customizer support class. Provides default implementations of some
56  * common server manager customizer panes.
57  *
58  * @author sherold
59  *
60  * @since 1.19
61  */

62 public final class CustomizerSupport {
63     
64     private static final String JavaDoc CLASSPATH = J2eeLibraryTypeProvider.VOLUME_TYPE_CLASSPATH;
65     private static final String JavaDoc SOURCES = J2eeLibraryTypeProvider.VOLUME_TYPE_SRC;
66     private static final String JavaDoc JAVADOC = J2eeLibraryTypeProvider.VOLUME_TYPE_JAVADOC;
67     
68     /** Do not allow to create instances of this class */
69     private CustomizerSupport() {
70     }
71     
72     /**
73      * Creates non-editable customizer classes pane.
74      *
75      * @param model A model prepresenting the class path entries.
76      *
77      * @return A Component representing the classes pane.
78      *
79      * @throws NullPointerException If null model is passed in.
80      */

81     public static Component JavaDoc createClassesCustomizer(PathModel model) {
82         if (model == null) {
83             throw new NullPointerException JavaDoc();
84         }
85         return new PathView(model, CLASSPATH, null);
86     }
87     
88     /**
89      * Creates an editable customizer sources pane.
90      *
91      * @param model A model prepresenting the source path entries.
92      * @param currentDir Add sources file chooser current directory. Passing in
93      * a null represents the user's default directory.
94      *
95      * @return A Component representing the sources pane.
96      *
97      * @throws NullPointerException If null model is passed in.
98      */

99     public static Component JavaDoc createSourcesCustomizer(PathModel model, File JavaDoc currentDir) {
100         if (model == null) {
101             throw new NullPointerException JavaDoc();
102         }
103         return new PathView(model, SOURCES, currentDir);
104     }
105     
106     /**
107      * Creates an editable customizer javadoc pane.
108      *
109      * @param model A model prepresenting the javadoc entries.
110      * @param currentDir Add javadoc file chooser current directory. Passing in
111      * a null represents the user's default directory.
112      *
113      * @return A Component representing the javadoc pane.
114      *
115      * @throws NullPointerException If null model is passed in.
116      */

117     public static Component JavaDoc createJavadocCustomizer(PathModel model, File JavaDoc currentDir) {
118         if (model == null) {
119             throw new NullPointerException JavaDoc();
120         }
121         return new PathView(model, JAVADOC, currentDir);
122     }
123     
124     /**
125      * Creates an Ant-style path specification from the specified list of URLs.
126      *
127      * @param The list of URLs.
128      *
129      * @return An Ant-style path specification.
130      */

131     public static String JavaDoc buildPath(List JavaDoc<URL JavaDoc> path) {
132         String JavaDoc PATH_SEPARATOR = System.getProperty("path.separator"); // NOI18N
133
StringBuffer JavaDoc sb = new StringBuffer JavaDoc(path.size() * 16);
134         for (Iterator JavaDoc<URL JavaDoc> i = path.iterator(); i.hasNext(); ) {
135             sb.append(urlToString(i.next()));
136             if (i.hasNext()) {
137                 sb.append(PATH_SEPARATOR);
138             }
139         }
140         return sb.toString();
141     }
142     
143     /**
144      * Splits an Ant-style path specification into the list of URLs. Tokenizes on
145      * <code>:</code> and <code>;</code>, paying attention to DOS-style components
146      * such as <samp>C:\FOO</samp>. Also removes any empty components.
147      *
148      * @param path An Ant-style path (elements arbitrary) using DOS or Unix separators
149      *
150      * @return A tokenization of the specified path into the list of URLs.
151      */

152     public static List JavaDoc<URL JavaDoc> tokenizePath(String JavaDoc path) {
153         try {
154             List JavaDoc<URL JavaDoc> l = new ArrayList JavaDoc();
155             StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(path, ":;", true); // NOI18N
156
char dosHack = '\0';
157             char lastDelim = '\0';
158             int delimCount = 0;
159             while (tok.hasMoreTokens()) {
160                 String JavaDoc s = tok.nextToken();
161                 if (s.length() == 0) {
162                     // Strip empty components.
163
continue;
164                 }
165                 if (s.length() == 1) {
166                     char c = s.charAt(0);
167                     if (c == ':' || c == ';') {
168                         // Just a delimiter.
169
lastDelim = c;
170                         delimCount++;
171                         continue;
172                     }
173                 }
174                 if (dosHack != '\0') {
175                     // #50679 - "C:/something" is also accepted as DOS path
176
if (lastDelim == ':' && delimCount == 1 && (s.charAt(0) == '\\' || s.charAt(0) == '/')) {
177                         // We had a single letter followed by ':' now followed by \something or /something
178
s = "" + dosHack + ':' + s;
179                         // and use the new token with the drive prefix...
180
} else {
181                         // Something else, leave alone.
182
l.add(fileToUrl(new File JavaDoc(Character.toString(dosHack))));
183                         // and continue with this token too...
184
}
185                     dosHack = '\0';
186                 }
187                 // Reset count of # of delimiters in a row.
188
delimCount = 0;
189                 if (s.length() == 1) {
190                     char c = s.charAt(0);
191                     if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
192                         // Probably a DOS drive letter. Leave it with the next component.
193
dosHack = c;
194                         continue;
195                     }
196                 }
197                 l.add(fileToUrl(new File JavaDoc(s)));
198             }
199             if (dosHack != '\0') {
200                 //the dosHack was the last letter in the input string (not followed by the ':')
201
//so obviously not a drive letter.
202
//Fix for issue #57304
203
l.add(fileToUrl(new File JavaDoc(Character.toString(dosHack))));
204             }
205             return l;
206         } catch (MalformedURLException JavaDoc e) {
207             ErrorManager.getDefault().notify(e);
208             return new ArrayList JavaDoc();
209         }
210     }
211     
212     /** Return URL representation of the specified file. */
213     private static URL JavaDoc fileToUrl(File JavaDoc file) throws MalformedURLException JavaDoc {
214         URL JavaDoc url = file.toURI().toURL();
215         if (FileUtil.isArchiveFile(url)) {
216             url = FileUtil.getArchiveRoot(url);
217         }
218         return url;
219     }
220     
221     /** Return string representation of the specified URL. */
222     private static String JavaDoc urlToString(URL JavaDoc url) {
223         if ("jar".equals(url.getProtocol())) { // NOI18N
224
URL JavaDoc fileURL = FileUtil.getArchiveFile(url);
225             if (FileUtil.getArchiveRoot(fileURL).equals(url)) {
226                 // really the root
227
url = fileURL;
228             } else {
229                 // some subdir, just show it as is
230
return url.toExternalForm();
231             }
232         }
233         if ("file".equals(url.getProtocol())) { // NOI18N
234
File JavaDoc f = new File JavaDoc(URI.create(url.toExternalForm()));
235             return f.getAbsolutePath();
236         }
237         else {
238             return url.toExternalForm();
239         }
240     }
241     
242     /**
243      * Path list model, supports adding, removing and moving URL entries in the list.
244      */

245     public static final class PathModel extends AbstractListModel JavaDoc {
246
247         private final List JavaDoc<URL JavaDoc> data;
248
249         /**
250          * Creates a new PathModel initialized with a list of URL entries.
251          *
252          * @param data The list of URL entries.
253          *
254          * @throws NullPointerException If null data attribute is passed in.
255          */

256         public PathModel(List JavaDoc<URL JavaDoc> data) {
257             if (data == null) {
258                 throw new NullPointerException JavaDoc("The data attribute must not be null."); // NOI18N
259
}
260             this.data = data;
261         }
262
263         /**
264          * Returns the number of URL entries in the list.
265          *
266          * return The number of URL entries in the list.
267          */

268         public int getSize() {
269             return data.size();
270         }
271
272         /**
273          * Returns the element at the specified position in this list.
274          *
275          * @param index The element position in the list.
276          *
277          * @return The element at the specified position in this list.
278          */

279         public Object JavaDoc getElementAt(int index) {
280             URL JavaDoc url = data.get(index);
281             if ("jar".equals(url.getProtocol())) { // NOI18N
282
URL JavaDoc fileURL = FileUtil.getArchiveFile(url);
283                 if (FileUtil.getArchiveRoot(fileURL).equals(url)) {
284                     // really the root
285
url = fileURL;
286                 } else {
287                     // some subdir, just show it as is
288
return url.toExternalForm();
289                 }
290             }
291             if ("file".equals(url.getProtocol())) { // NOI18N
292
File JavaDoc f = new File JavaDoc(URI.create(url.toExternalForm()));
293                 return f.getAbsolutePath();
294             }
295             else {
296                 return url.toExternalForm();
297             }
298         }
299
300         /**
301          * Removes the URL entries denotated with their respective indices from the list.
302          */

303         public void removePath(int[] indices) {
304             for (int i = indices.length - 1; i >= 0; i--) {
305                 data.remove(indices[i]);
306             }
307             fireIntervalRemoved(this, indices[0], indices[indices.length - 1]);
308         }
309
310         /**
311          * Moves the URL entries denotated with their respective indices up in the list.
312          */

313         public void moveUpPath(int[] indices) {
314             for (int i = 0; i < indices.length; i++) {
315                 URL JavaDoc p2 = data.get(indices[i]);
316                 URL JavaDoc p1 = data.set(indices[i] - 1, p2);
317                 data.set(indices[i], p1);
318             }
319             fireContentsChanged(this, indices[0] - 1, indices[indices.length - 1]);
320         }
321
322         /**
323          * Moves the URL entries denotated with their respective indices down in the list.
324          */

325         public void moveDownPath(int[] indices) {
326             for (int i = indices.length - 1; i >= 0; i--) {
327                 URL JavaDoc p1 = data.get(indices[i]);
328                 URL JavaDoc p2 = data.set(indices[i] + 1, p1);
329                 data.set(indices[i], p2);
330             }
331             fireContentsChanged(this, indices[0], indices[indices.length - 1] + 1);
332         }
333
334         /**
335          * Appends the URL representing the specified file to the end of the list.
336          *
337          * @return true if the URL was appended, false otherwise.
338          */

339         public boolean addPath(File JavaDoc f) {
340             try {
341                 URL JavaDoc url = f.toURI().toURL();
342                 return this.addPath(url);
343             } catch (MalformedURLException JavaDoc mue) {
344                 return false;
345             }
346         }
347
348         /**
349          * Appends the specified URL to the end of the list.
350          *
351          * @return true if the URL was appended, false otherwise.
352          */

353         public boolean addPath(URL JavaDoc url) {
354             if (FileUtil.isArchiveFile(url)) {
355                 url = FileUtil.getArchiveRoot(url);
356             } else if (!url.toExternalForm().endsWith("/")) { // NOI18N
357
try {
358                     url = new URL JavaDoc(url.toExternalForm() + "/"); // NOI18N
359
} catch (MalformedURLException JavaDoc mue) {
360                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, mue);
361                 }
362             }
363             int oldSize = data.size();
364             data.add(url);
365             fireIntervalAdded(this, oldSize, oldSize);
366             return true;
367         }
368
369         /**
370          * Returns the list of URL entries.
371          * @return The list of URL entries.
372          */

373         public List JavaDoc<URL JavaDoc> getData() {
374             return data;
375         }
376     }
377     
378     // private helper classes -------------------------------------------------
379

380     private static class PathView extends JPanel JavaDoc {
381         
382         private JList JavaDoc resources;
383         private JButton JavaDoc addButton;
384         private JButton JavaDoc addURLButton;
385         private JButton JavaDoc removeButton;
386         private JButton JavaDoc moveUpButton;
387         private JButton JavaDoc moveDownButton;
388         private File JavaDoc currentDir;
389         private String JavaDoc type;
390
391         public PathView (PathModel model, String JavaDoc type, File JavaDoc currentDir) {
392             this.type = type;
393             this.currentDir = currentDir;
394             initComponents(model);
395         }
396
397         private void initComponents(PathModel model) {
398             setLayout(new GridBagLayout JavaDoc());
399             JLabel JavaDoc label = new JLabel JavaDoc ();
400             String JavaDoc key = null;
401             String JavaDoc mneKey = null;
402             String JavaDoc ad = null;
403             if (type.equals(CLASSPATH)) {
404                 key = "TXT_Classes"; // NOI18N
405
mneKey = "MNE_Classes"; // NOI18N
406
ad = "AD_Classes"; // NOI18N
407
} else if (type.equals(SOURCES)) {
408                 key = "TXT_Sources"; // NOI18N
409
mneKey = "MNE_Sources"; // NOI18N
410
ad = "AD_Sources"; // NOI18N
411
} else if (type.equals(JAVADOC)) {
412                 key = "TXT_Javadoc"; // NOI18N
413
mneKey = "MNE_Javadoc"; // NOI18N
414
ad = "AD_Javadoc"; // NOI18N
415
} else {
416                 assert false : "Illegal type of panel"; //NOI18N
417
return;
418             }
419             label.setText(NbBundle.getMessage(CustomizerSupport.class,key));
420             label.setDisplayedMnemonic(NbBundle.getMessage(CustomizerSupport.class,mneKey).charAt(0));
421             GridBagConstraints JavaDoc c = new GridBagConstraints JavaDoc();
422             c.gridx = GridBagConstraints.RELATIVE;
423             c.gridy = GridBagConstraints.RELATIVE;
424             c.gridwidth = GridBagConstraints.REMAINDER;
425             c.insets = new Insets JavaDoc (6,12,2,0);
426             c.fill = GridBagConstraints.HORIZONTAL;
427             c.weightx = 1.0;
428             ((GridBagLayout JavaDoc)getLayout()).setConstraints(label,c);
429             add(label);
430             resources = new JList JavaDoc(model);
431             label.setLabelFor(resources);
432             resources.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(CustomizerSupport.class,ad));
433             resources.addListSelectionListener(new ListSelectionListener JavaDoc() {
434                 public void valueChanged(ListSelectionEvent JavaDoc e) {
435                     selectionChanged ();
436                 }
437             });
438             JScrollPane JavaDoc spane = new JScrollPane JavaDoc (this.resources);
439             // set the preferred size so that the size won't be set according to
440
// the longest row in the list by default
441
spane.setPreferredSize(new java.awt.Dimension JavaDoc(200, 100));
442             c = new GridBagConstraints JavaDoc();
443             c.gridx = GridBagConstraints.RELATIVE;
444             c.gridy = GridBagConstraints.RELATIVE;
445             c.gridwidth = 1;
446             c.gridheight = 5;
447             c.insets = new Insets JavaDoc (0,12,12,6);
448             c.fill = GridBagConstraints.BOTH;
449             c.weightx = 1.0;
450             c.weighty = 1.0;
451             ((GridBagLayout JavaDoc)this.getLayout()).setConstraints(spane,c);
452             add(spane);
453             if (type == SOURCES || type == JAVADOC) {
454                 this.addButton = new JButton JavaDoc ();
455                 String JavaDoc text;
456                 char mne;
457                 if (type == SOURCES) {
458                     text = NbBundle.getMessage(CustomizerSupport.class, "CTL_Add");
459                     mne = NbBundle.getMessage(CustomizerSupport.class, "MNE_Add").charAt(0);
460                     ad = NbBundle.getMessage(CustomizerSupport.class, "AD_Add");
461                 }
462                 else {
463                     text = NbBundle.getMessage(CustomizerSupport.class, "CTL_AddZip");
464                     mne = NbBundle.getMessage(CustomizerSupport.class, "MNE_AddZip").charAt(0);
465                     ad = NbBundle.getMessage(CustomizerSupport.class, "AD_AddZip");
466                 }
467                 this.addButton.setText(text);
468                 this.addButton.setMnemonic(mne);
469                 this.addButton.getAccessibleContext().setAccessibleDescription (ad);
470                 addButton.addActionListener( new ActionListener JavaDoc () {
471                     public void actionPerformed(ActionEvent JavaDoc e) {
472                         addPathElement ();
473                     }
474                 });
475                 c = new GridBagConstraints JavaDoc();
476                 c.gridx = 1;
477                 c.gridy = 1;
478                 c.gridwidth = GridBagConstraints.REMAINDER;
479                 c.fill = GridBagConstraints.HORIZONTAL;
480                 c.anchor = GridBagConstraints.NORTHWEST;
481                 c.insets = new Insets JavaDoc (0,6,0,6);
482                 ((GridBagLayout JavaDoc)this.getLayout()).setConstraints(addButton,c);
483                 this.add (addButton);
484 // if (this.type == JAVADOC) {
485
// addURLButton = new JButton (NbBundle.getMessage(CustomizerSupport.class, "CTL_AddURL"));
486
// addURLButton.setMnemonic(NbBundle.getMessage(CustomizerSupport.class, "MNE_AddURL").charAt(0));
487
// addURLButton.addActionListener(new ActionListener () {
488
// public void actionPerformed(ActionEvent e) {
489
// addURLElement ();
490
// }
491
// });
492
// c = new GridBagConstraints();
493
// c.gridx = 1;
494
// c.gridy = 2;
495
// c.gridwidth = GridBagConstraints.REMAINDER;
496
// c.fill = GridBagConstraints.HORIZONTAL;
497
// c.anchor = GridBagConstraints.NORTHWEST;
498
// c.insets = new Insets (0,6,6,12);
499
// ((GridBagLayout)this.getLayout()).setConstraints(addURLButton,c);
500
// this.add (addURLButton);
501
// }
502
removeButton = new JButton JavaDoc (NbBundle.getMessage(CustomizerSupport.class, "CTL_Remove"));
503                 removeButton.setMnemonic(NbBundle.getMessage(CustomizerSupport.class, "MNE_Remove").charAt(0));
504                 removeButton.getAccessibleContext().setAccessibleDescription (NbBundle.getMessage(CustomizerSupport.class,"AD_Remove"));
505                 removeButton.addActionListener( new ActionListener JavaDoc () {
506                     public void actionPerformed(ActionEvent JavaDoc e) {
507                         removePathElement ();
508                     }
509                 });
510                 removeButton.setEnabled(false);
511                 c = new GridBagConstraints JavaDoc();
512                 c.gridx = 1;
513                 c.gridy = 3;
514                 c.gridwidth = GridBagConstraints.REMAINDER;
515                 c.fill = GridBagConstraints.HORIZONTAL;
516                 c.anchor = GridBagConstraints.NORTHWEST;
517                 c.insets = new Insets JavaDoc (12,6,0,6);
518                 ((GridBagLayout JavaDoc)this.getLayout()).setConstraints(removeButton,c);
519                 this.add (removeButton);
520                 moveUpButton = new JButton JavaDoc (NbBundle.getMessage(CustomizerSupport.class, "CTL_Up"));
521                 moveUpButton.setMnemonic(NbBundle.getMessage(CustomizerSupport.class, "MNE_Up").charAt(0));
522                 moveUpButton.getAccessibleContext().setAccessibleDescription (NbBundle.getMessage(CustomizerSupport.class,"AD_Up"));
523                 moveUpButton.addActionListener( new ActionListener JavaDoc () {
524                     public void actionPerformed(ActionEvent JavaDoc e) {
525                         moveUpPathElement ();
526                     }
527                 });
528                 moveUpButton.setEnabled(false);
529                 c = new GridBagConstraints JavaDoc();
530                 c.gridx = 1;
531                 c.gridy = 4;
532                 c.gridwidth = GridBagConstraints.REMAINDER;
533                 c.fill = GridBagConstraints.HORIZONTAL;
534                 c.anchor = GridBagConstraints.NORTHWEST;
535                 c.insets = new Insets JavaDoc (12,6,0,6);
536                 ((GridBagLayout JavaDoc)this.getLayout()).setConstraints(moveUpButton,c);
537                 this.add (moveUpButton);
538                 moveDownButton = new JButton JavaDoc (NbBundle.getMessage(CustomizerSupport.class, "CTL_Down"));
539                 moveDownButton.setMnemonic (NbBundle.getMessage(CustomizerSupport.class, "MNE_Down").charAt(0));
540                 moveDownButton.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(CustomizerSupport.class,"AD_Down"));
541                 moveDownButton.addActionListener( new ActionListener JavaDoc () {
542                     public void actionPerformed(ActionEvent JavaDoc e) {
543                         moveDownPathElement ();
544                     }
545                 });
546                 moveDownButton.setEnabled(false);
547                 c = new GridBagConstraints JavaDoc();
548                 c.gridx = 1;
549                 c.gridy = 5;
550                 c.gridwidth = GridBagConstraints.REMAINDER;
551                 c.fill = GridBagConstraints.HORIZONTAL;
552                 c.anchor = GridBagConstraints.NORTHWEST;
553                 c.insets = new Insets JavaDoc (5,6,6,6);
554                 ((GridBagLayout JavaDoc)this.getLayout()).setConstraints(moveDownButton,c);
555                 this.add (moveDownButton);
556             }
557         }
558         
559 // private void addURLElement() {
560
// JPanel p = new JPanel ();
561
// GridBagLayout lm = new GridBagLayout();
562
// p.setLayout (lm);
563
// GridBagConstraints c = new GridBagConstraints ();
564
// c.gridx = c.gridy = GridBagConstraints.RELATIVE;
565
// c.insets = new Insets (12,12,12,6);
566
// c.anchor = GridBagConstraints.NORTHWEST;
567
// JLabel label = new JLabel (NbBundle.getMessage(CustomizerSupport.class,"CTL_AddJavadocURLMessage"));
568
// label.setDisplayedMnemonic ('U');
569
// lm.setConstraints(label,c);
570
// p.add (label);
571
// c = new GridBagConstraints ();
572
// c.gridx = c.gridy = GridBagConstraints.RELATIVE;
573
// c.gridwidth = GridBagConstraints.REMAINDER;
574
// c.insets = new Insets (12,0,12,6);
575
// c.fill = GridBagConstraints.HORIZONTAL;
576
// c.anchor = GridBagConstraints.NORTHWEST;
577
// JTextField text = new JTextField ();
578
// text.setColumns(30);
579
// text.setText (NbBundle.getMessage(CustomizerSupport.class,"TXT_DefaultProtocol"));
580
// text.selectAll();
581
// label.setLabelFor(text);
582
// lm.setConstraints(text,c);
583
// p.add (text);
584
// JButton[] options = new JButton[] {
585
// new JButton (NbBundle.getMessage(CustomizerSupport.class,"CTL_AddJavadocURLTitle")),
586
// new JButton (NbBundle.getMessage(CustomizerSupport.class,"CTL_Cancel"))
587
// };
588
// options[0].setMnemonic(NbBundle.getMessage(CustomizerSupport.class,"MNE_Add").charAt(0));
589
// options[1].setMnemonic(NbBundle.getMessage(CustomizerSupport.class,"MNE_Cancel").charAt(0));
590
// DialogDescriptor input = new DialogDescriptor (
591
// p,
592
// NbBundle.getMessage(CustomizerSupport.class,"CTL_AddJavadocURLTitle"),
593
// true, options, options[0], DialogDescriptor.DEFAULT_ALIGN, null, null);
594
// if (DialogDisplayer.getDefault().notify(input) == options[0]) {
595
// try {
596
// String value = text.getText();
597
// URL url = new URL (value);
598
// ((PathModel)this.resources.getModel()).addPath(url);
599
// this.resources.setSelectedIndex (this.resources.getModel().getSize()-1);
600
// } catch (MalformedURLException mue) {
601
// DialogDescriptor.Message message = new DialogDescriptor.Message (
602
// NbBundle.getMessage(CustomizerSupport.class,"CTL_InvalidURLFormat"),
603
// DialogDescriptor.ERROR_MESSAGE);
604
// DialogDisplayer.getDefault().notify(message);
605
// }
606
// }
607
// }
608

609         private void addPathElement () {
610             JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
611             FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
612             chooser.setMultiSelectionEnabled (true);
613             String JavaDoc title = null;
614             String JavaDoc message = null;
615             String JavaDoc approveButtonName = null;
616             String JavaDoc approveButtonNameMne = null;
617             if (this.type == SOURCES) {
618                 title = NbBundle.getMessage (CustomizerSupport.class,"TXT_OpenSources");
619                 message = NbBundle.getMessage (CustomizerSupport.class,"TXT_Sources");
620                 approveButtonName = NbBundle.getMessage (CustomizerSupport.class,"TXT_OpenSources");
621                 approveButtonNameMne = NbBundle.getMessage (CustomizerSupport.class,"MNE_OpenSources");
622             }
623             else if (this.type == JAVADOC) {
624                 title = NbBundle.getMessage (CustomizerSupport.class,"TXT_OpenJavadoc");
625                 message = NbBundle.getMessage (CustomizerSupport.class,"TXT_Javadoc");
626                 approveButtonName = NbBundle.getMessage (CustomizerSupport.class,"TXT_OpenJavadoc");
627                 approveButtonNameMne = NbBundle.getMessage (CustomizerSupport.class,"MNE_OpenJavadoc");
628             }
629             chooser.setDialogTitle(title);
630             chooser.setApproveButtonText(approveButtonName);
631             chooser.setApproveButtonMnemonic (approveButtonNameMne.charAt(0));
632             chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
633             //#61789 on old macosx (jdk 1.4.1) these two method need to be called in this order.
634
chooser.setAcceptAllFileFilterUsed( false );
635             chooser.setFileFilter (new SimpleFileFilter(message,new String JavaDoc[] {"ZIP","JAR"})); //NOI18N
636
if (this.currentDir != null && currentDir.exists()) {
637                 chooser.setCurrentDirectory(this.currentDir);
638             }
639             if (chooser.showOpenDialog(this)==JFileChooser.APPROVE_OPTION) {
640                 File JavaDoc[] fs = chooser.getSelectedFiles();
641                 PathModel model = (PathModel) this.resources.getModel();
642                 boolean addingFailed = false;
643                 int firstIndex = this.resources.getModel().getSize();
644                 for (int i = 0; i < fs.length; i++) {
645                     File JavaDoc f = fs[i];
646                     //XXX: JFileChooser workaround (JDK bug #5075580), double click on folder returns wrong file
647
// E.g. for /foo/src it returns /foo/src/src
648
// Try to convert it back by removing last invalid name component
649
if (!f.exists()) {
650                         File JavaDoc parent = f.getParentFile();
651                         if (parent != null && f.getName().equals(parent.getName()) && parent.exists()) {
652                             f = parent;
653                         }
654                     }
655                     addingFailed|=!model.addPath (f);
656                 }
657                 if (addingFailed) {
658                     new NotifyDescriptor.Message (NbBundle.getMessage(CustomizerSupport.class,"TXT_CanNotAddResolve"),
659                             NotifyDescriptor.ERROR_MESSAGE);
660                 }
661                 int lastIndex = this.resources.getModel().getSize()-1;
662                 if (firstIndex<=lastIndex) {
663                     int[] toSelect = new int[lastIndex-firstIndex+1];
664                     for (int i = 0; i < toSelect.length; i++) {
665                         toSelect[i] = firstIndex+i;
666                     }
667                     this.resources.setSelectedIndices(toSelect);
668                 }
669                 this.currentDir = FileUtil.normalizeFile(chooser.getCurrentDirectory());
670             }
671         }
672
673         private void removePathElement () {
674             int[] indices = this.resources.getSelectedIndices();
675             if (indices.length == 0) {
676                 return;
677             }
678             PathModel model = (PathModel) this.resources.getModel();
679             model.removePath (indices);
680             if ( indices[indices.length-1]-indices.length+1 < this.resources.getModel().getSize()) {
681                 this.resources.setSelectedIndex (indices[indices.length-1]-indices.length+1);
682             }
683             else if (indices[0]>0) {
684                 this.resources.setSelectedIndex (indices[0]-1);
685             }
686         }
687
688         private void moveDownPathElement () {
689             int[] indices = this.resources.getSelectedIndices();
690             if (indices.length == 0) {
691                 return;
692             }
693             PathModel model = (PathModel) this.resources.getModel();
694             model.moveDownPath (indices);
695             for (int i=0; i< indices.length; i++) {
696                 indices[i] = indices[i] + 1;
697             }
698             this.resources.setSelectedIndices (indices);
699         }
700
701         private void moveUpPathElement () {
702             int[] indices = this.resources.getSelectedIndices();
703             if (indices.length == 0) {
704                 return;
705             }
706             PathModel model = (PathModel) this.resources.getModel();
707             model.moveUpPath (indices);
708             for (int i=0; i< indices.length; i++) {
709                 indices[i] = indices[i] - 1;
710             }
711             this.resources.setSelectedIndices (indices);
712         }
713
714         private void selectionChanged () {
715             if (this.type == CLASSPATH) {
716                 return;
717             }
718             int indices[] = this.resources.getSelectedIndices();
719             this.removeButton.setEnabled (indices.length > 0);
720             this.moveUpButton.setEnabled (indices.length > 0 && indices[0]>0);
721             this.moveDownButton.setEnabled(indices.length > 0 && indices[indices.length-1]<this.resources.getModel().getSize()-1);
722         }
723     }
724     
725     private static class SimpleFileFilter extends FileFilter JavaDoc {
726
727         private String JavaDoc description;
728         private Collection JavaDoc extensions;
729
730
731         public SimpleFileFilter (String JavaDoc description, String JavaDoc[] extensions) {
732             this.description = description;
733             this.extensions = Arrays.asList(extensions);
734         }
735
736         public boolean accept(File JavaDoc f) {
737             if (f.isDirectory())
738                 return true;
739             String JavaDoc name = f.getName();
740             int index = name.lastIndexOf('.'); //NOI18N
741
if (index <= 0 || index==name.length()-1)
742                 return false;
743             String JavaDoc extension = name.substring (index+1).toUpperCase();
744             return this.extensions.contains(extension);
745         }
746
747         public String JavaDoc getDescription() {
748             return this.description;
749         }
750     }
751 }
752
Popular Tags