KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > ide > j2ee > ui > 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.sun.ide.j2ee.ui;
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                 ErrorManager.getDefault().log(ErrorManager.WARNING, f.getAbsolutePath());
345                 ErrorManager.getDefault().notify(ErrorManager.WARNING, mue);
346                 return false;
347             }
348         }
349
350         /**
351          * Appends the specified URL to the end of the list.
352          *
353          * @return true if the URL was appended, false otherwise.
354          */

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

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

382     private static class PathView extends JPanel JavaDoc {
383         
384         private JList JavaDoc resources;
385         private JButton JavaDoc addButton;
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;
402             String JavaDoc mneKey;
403             String JavaDoc ad;
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             org.openide.awt.Mnemonics.setLocalizedText(label, org.openide.util.NbBundle.getBundle(CustomizerSupport.class).getString(key)); // NOI18N
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 (SOURCES.equals(type) || JAVADOC.equals(type)) {
454                 this.addButton = new JButton JavaDoc ();
455                 String JavaDoc text;
456                 char mne;
457                 if (SOURCES.equals(type)) {
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                 org.openide.awt.Mnemonics.setLocalizedText(this.addButton, text); // NOI18N
468
this.addButton.getAccessibleContext().setAccessibleDescription (ad);
469                 addButton.addActionListener( new ActionListener JavaDoc () {
470                     public void actionPerformed(ActionEvent JavaDoc e) {
471                         addPathElement ();
472                     }
473                 });
474                 c = new GridBagConstraints JavaDoc();
475                 c.gridx = 1;
476                 c.gridy = 1;
477                 c.gridwidth = GridBagConstraints.REMAINDER;
478                 c.fill = GridBagConstraints.HORIZONTAL;
479                 c.anchor = GridBagConstraints.NORTHWEST;
480                 c.insets = new Insets JavaDoc (0,6,0,6);
481                 ((GridBagLayout JavaDoc)this.getLayout()).setConstraints(addButton,c);
482                 this.add (addButton);
483                 removeButton = new JButton JavaDoc ();
484                 org.openide.awt.Mnemonics.setLocalizedText(this.removeButton,
485                         NbBundle.getMessage(CustomizerSupport.class, "CTL_Remove")); // NOI18N
486
removeButton.getAccessibleContext().setAccessibleDescription (NbBundle.getMessage(CustomizerSupport.class,"AD_Remove"));
487                 removeButton.addActionListener( new ActionListener JavaDoc () {
488                     public void actionPerformed(ActionEvent JavaDoc e) {
489                         removePathElement ();
490                     }
491                 });
492                 removeButton.setEnabled(false);
493                 c = new GridBagConstraints JavaDoc();
494                 c.gridx = 1;
495                 c.gridy = 3;
496                 c.gridwidth = GridBagConstraints.REMAINDER;
497                 c.fill = GridBagConstraints.HORIZONTAL;
498                 c.anchor = GridBagConstraints.NORTHWEST;
499                 c.insets = new Insets JavaDoc (12,6,0,6);
500                 ((GridBagLayout JavaDoc)this.getLayout()).setConstraints(removeButton,c);
501                 this.add (removeButton);
502                 moveUpButton = new JButton JavaDoc ();
503                 org.openide.awt.Mnemonics.setLocalizedText(this.moveUpButton,
504                         NbBundle.getMessage(CustomizerSupport.class, "CTL_Up")); // NOI18N
505
moveUpButton.getAccessibleContext().setAccessibleDescription (NbBundle.getMessage(CustomizerSupport.class,"AD_Up"));
506                 moveUpButton.addActionListener( new ActionListener JavaDoc () {
507                     public void actionPerformed(ActionEvent JavaDoc e) {
508                         moveUpPathElement ();
509                     }
510                 });
511                 moveUpButton.setEnabled(false);
512                 c = new GridBagConstraints JavaDoc();
513                 c.gridx = 1;
514                 c.gridy = 4;
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(moveUpButton,c);
520                 this.add (moveUpButton);
521                 moveDownButton = new JButton JavaDoc ();
522                 org.openide.awt.Mnemonics.setLocalizedText(this.moveDownButton,
523                         NbBundle.getMessage(CustomizerSupport.class, "CTL_Down")); // NOI18N
524
moveDownButton.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(CustomizerSupport.class,"AD_Down"));
525                 moveDownButton.addActionListener( new ActionListener JavaDoc () {
526                     public void actionPerformed(ActionEvent JavaDoc e) {
527                         moveDownPathElement ();
528                     }
529                 });
530                 moveDownButton.setEnabled(false);
531                 c = new GridBagConstraints JavaDoc();
532                 c.gridx = 1;
533                 c.gridy = 5;
534                 c.gridwidth = GridBagConstraints.REMAINDER;
535                 c.fill = GridBagConstraints.HORIZONTAL;
536                 c.anchor = GridBagConstraints.NORTHWEST;
537                 c.insets = new Insets JavaDoc (5,6,6,6);
538                 ((GridBagLayout JavaDoc)this.getLayout()).setConstraints(moveDownButton,c);
539                 this.add (moveDownButton);
540             }
541         }
542         
543
544         private void addPathElement () {
545             JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
546             FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
547             chooser.setMultiSelectionEnabled (true);
548             String JavaDoc title = null;
549             String JavaDoc message = null;
550             String JavaDoc approveButtonName = null;
551             String JavaDoc approveButtonNameMne = null;
552             if (SOURCES.equals(this.type)) {
553                 title = NbBundle.getMessage (CustomizerSupport.class,"TXT_OpenSources");
554                 message = NbBundle.getMessage (CustomizerSupport.class,"TXT_Sources");
555                 approveButtonName = NbBundle.getMessage (CustomizerSupport.class,"TXT_OpenSources");
556                 approveButtonNameMne = NbBundle.getMessage (CustomizerSupport.class,"MNE_OpenSources");
557             }
558             else if (JAVADOC.equals(this.type)) {
559                 title = NbBundle.getMessage (CustomizerSupport.class,"TXT_OpenJavadoc");
560                 message = NbBundle.getMessage (CustomizerSupport.class,"TXT_Javadoc");
561                 approveButtonName = NbBundle.getMessage (CustomizerSupport.class,"TXT_OpenJavadoc");
562                 approveButtonNameMne = NbBundle.getMessage (CustomizerSupport.class,"MNE_OpenJavadoc");
563             }
564             chooser.setDialogTitle(title);
565             chooser.setApproveButtonText(approveButtonName);
566             if (null != approveButtonNameMne) {
567                 chooser.setApproveButtonMnemonic (approveButtonNameMne.charAt(0));
568             }
569             chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
570             //#61789 on old macosx (jdk 1.4.1) these two method need to be called in this order.
571
chooser.setAcceptAllFileFilterUsed( false );
572             chooser.setFileFilter (new SimpleFileFilter(message,new String JavaDoc[] {"ZIP","JAR"})); //NOI18N
573
if (this.currentDir != null && currentDir.exists()) {
574                 chooser.setCurrentDirectory(this.currentDir);
575             }
576             if (chooser.showOpenDialog(this)==JFileChooser.APPROVE_OPTION) {
577                 File JavaDoc[] fs = chooser.getSelectedFiles();
578                 PathModel model = (PathModel) this.resources.getModel();
579                 boolean addingFailed = false;
580                 int firstIndex = this.resources.getModel().getSize();
581                 for (int i = 0; i < fs.length; i++) {
582                     File JavaDoc f = fs[i];
583                     //XXX: JFileChooser workaround (JDK bug #5075580), double click on folder returns wrong file
584
// E.g. for /foo/src it returns /foo/src/src
585
// Try to convert it back by removing last invalid name component
586
if (!f.exists()) {
587                         File JavaDoc parent = f.getParentFile();
588                         if (parent != null && f.getName().equals(parent.getName()) && parent.exists()) {
589                             f = parent;
590                         }
591                     }
592                     addingFailed|=!model.addPath (f);
593                 }
594                 if (addingFailed) {
595                     new NotifyDescriptor.Message (NbBundle.getMessage(CustomizerSupport.class,"TXT_CanNotAddResolve"),
596                             NotifyDescriptor.ERROR_MESSAGE);
597                 }
598                 int lastIndex = this.resources.getModel().getSize()-1;
599                 if (firstIndex<=lastIndex) {
600                     int[] toSelect = new int[lastIndex-firstIndex+1];
601                     for (int i = 0; i < toSelect.length; i++) {
602                         toSelect[i] = firstIndex+i;
603                     }
604                     this.resources.setSelectedIndices(toSelect);
605                 }
606                 this.currentDir = FileUtil.normalizeFile(chooser.getCurrentDirectory());
607             }
608         }
609
610         private void removePathElement () {
611             int[] indices = this.resources.getSelectedIndices();
612             if (indices.length == 0) {
613                 return;
614             }
615             PathModel model = (PathModel) this.resources.getModel();
616             model.removePath (indices);
617             if ( indices[indices.length-1]-indices.length+1 < this.resources.getModel().getSize()) {
618                 this.resources.setSelectedIndex (indices[indices.length-1]-indices.length+1);
619             }
620             else if (indices[0]>0) {
621                 this.resources.setSelectedIndex (indices[0]-1);
622             }
623         }
624
625         private void moveDownPathElement () {
626             int[] indices = this.resources.getSelectedIndices();
627             if (indices.length == 0) {
628                 return;
629             }
630             PathModel model = (PathModel) this.resources.getModel();
631             model.moveDownPath (indices);
632             for (int i=0; i< indices.length; i++) {
633                 indices[i] = indices[i] + 1;
634             }
635             this.resources.setSelectedIndices (indices);
636         }
637
638         private void moveUpPathElement () {
639             int[] indices = this.resources.getSelectedIndices();
640             if (indices.length == 0) {
641                 return;
642             }
643             PathModel model = (PathModel) this.resources.getModel();
644             model.moveUpPath (indices);
645             for (int i=0; i< indices.length; i++) {
646                 indices[i] = indices[i] - 1;
647             }
648             this.resources.setSelectedIndices (indices);
649         }
650
651         private void selectionChanged () {
652             if (CLASSPATH.equals(this.type)) {
653                 return;
654             }
655             int indices[] = this.resources.getSelectedIndices();
656             this.removeButton.setEnabled (indices.length > 0);
657             this.moveUpButton.setEnabled (indices.length > 0 && indices[0]>0);
658             this.moveDownButton.setEnabled(indices.length > 0 && indices[indices.length-1]<this.resources.getModel().getSize()-1);
659         }
660     }
661     
662     private static class SimpleFileFilter extends FileFilter JavaDoc {
663
664         private String JavaDoc description;
665         private Collection JavaDoc extensions;
666
667
668         public SimpleFileFilter (String JavaDoc description, String JavaDoc[] extensions) {
669             this.description = description;
670             this.extensions = Arrays.asList(extensions);
671         }
672
673         public boolean accept(File JavaDoc f) {
674             if (f.isDirectory()) {
675                 return true;
676             }
677             String JavaDoc name = f.getName();
678             int index = name.lastIndexOf('.'); //NOI18N
679
if (index <= 0 || index==name.length()-1) {
680                 return false;
681             }
682             String JavaDoc extension = name.substring (index+1).toUpperCase();
683             return this.extensions.contains(extension);
684         }
685
686         public String JavaDoc getDescription() {
687             return this.description;
688         }
689     }
690 }
691
Popular Tags