KickJava   Java API By Example, From Geeks To Geeks.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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