KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > oc4j > customizer > OC4JCustomizerSupport


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.oc4j.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.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
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 Michal Mocnak
60  */

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

79     public static Component JavaDoc createUserCustomizer(InstanceProperties ip) {
80         if (ip == null) {
81             throw new NullPointerException JavaDoc();
82         }
83         
84         return new OC4JCustomizerUserPanel(ip);
85     }
86     
87     /**
88      * Creates non-editable customizer classes pane.
89      *
90      * @param model A model prepresenting the class path entries.
91      *
92      * @return A Component representing the classes pane.
93      *
94      * @throws NullPointerException If null model is passed in.
95      */

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

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

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

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

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

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

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

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

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

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

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

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

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

368         public List JavaDoc<URL JavaDoc> getData() {
369             return data;
370         }
371     }
372     
373     private static class PathView extends JPanel JavaDoc {
374         
375         private JList JavaDoc resources;
376         private JButton JavaDoc addButton;
377         private JButton JavaDoc addURLButton;
378         private JButton JavaDoc removeButton;
379         private JButton JavaDoc moveUpButton;
380         private JButton JavaDoc moveDownButton;
381         private File JavaDoc currentDir;
382         private String JavaDoc type;
383         
384         public PathView(PathModel model, String JavaDoc type, File JavaDoc currentDir) {
385             this.type = type;
386             this.currentDir = currentDir;
387             initComponents(model);
388         }
389         
390         private void initComponents(PathModel model) {
391             setLayout(new GridBagLayout JavaDoc());
392             JLabel JavaDoc label = new JLabel JavaDoc();
393             String JavaDoc key = null;
394             String JavaDoc mneKey = null;
395             String JavaDoc ad = null;
396             if (type.equals(CLASSPATH)) {
397                 key = "TXT_Classes"; // NOI18N
398
mneKey = "MNE_Classes"; // NOI18N
399
ad = "AD_Classes"; // NOI18N
400
} else if (type.equals(JAVADOC)) {
401                 key = "TXT_Javadoc"; // NOI18N
402
mneKey = "MNE_Javadoc"; // NOI18N
403
ad = "AD_Javadoc"; // NOI18N
404
} else {
405                 assert false : "Illegal type of panel"; //NOI18N
406
return;
407             }
408             label.setText(NbBundle.getMessage(OC4JCustomizerSupport.class,key));
409             label.setDisplayedMnemonic(NbBundle.getMessage(OC4JCustomizerSupport.class,mneKey).charAt(0));
410             GridBagConstraints JavaDoc c = new GridBagConstraints JavaDoc();
411             c.gridx = GridBagConstraints.RELATIVE;
412             c.gridy = GridBagConstraints.RELATIVE;
413             c.gridwidth = GridBagConstraints.REMAINDER;
414             c.insets = new Insets JavaDoc(6,12,2,0);
415             c.fill = GridBagConstraints.HORIZONTAL;
416             c.weightx = 1.0;
417             ((GridBagLayout JavaDoc)getLayout()).setConstraints(label,c);
418             add(label);
419             resources = new JList JavaDoc(model);
420             label.setLabelFor(resources);
421             resources.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(OC4JCustomizerSupport.class,ad));
422             resources.addListSelectionListener(new ListSelectionListener JavaDoc() {
423                 public void valueChanged(ListSelectionEvent JavaDoc e) {
424                     selectionChanged();
425                 }
426             });
427             JScrollPane JavaDoc spane = new JScrollPane JavaDoc(this.resources);
428             // set the preferred size so that the size won't be set according to
429
// the longest row in the list by default
430
spane.setPreferredSize(new java.awt.Dimension JavaDoc(200, 100));
431             c = new GridBagConstraints JavaDoc();
432             c.gridx = GridBagConstraints.RELATIVE;
433             c.gridy = GridBagConstraints.RELATIVE;
434             c.gridwidth = 1;
435             c.gridheight = 5;
436             c.insets = new Insets JavaDoc(0,12,12,6);
437             c.fill = GridBagConstraints.BOTH;
438             c.weightx = 1.0;
439             c.weighty = 1.0;
440             ((GridBagLayout JavaDoc)this.getLayout()).setConstraints(spane,c);
441             add(spane);
442             if (type == JAVADOC) {
443                 this.addButton = new JButton JavaDoc();
444                 String JavaDoc text = NbBundle.getMessage(OC4JCustomizerSupport.class, "CTL_AddZip");
445                 char mne = NbBundle.getMessage(OC4JCustomizerSupport.class, "MNE_AddZip").charAt(0);
446                 ad = NbBundle.getMessage(OC4JCustomizerSupport.class, "AD_AddZip");
447                 this.addButton.setText(text);
448                 this.addButton.setMnemonic(mne);
449                 this.addButton.getAccessibleContext().setAccessibleDescription(ad);
450                 addButton.addActionListener( new ActionListener JavaDoc() {
451                     public void actionPerformed(ActionEvent JavaDoc e) {
452                         addPathElement();
453                     }
454                 });
455                 c = new GridBagConstraints JavaDoc();
456                 c.gridx = 1;
457                 c.gridy = 1;
458                 c.gridwidth = GridBagConstraints.REMAINDER;
459                 c.fill = GridBagConstraints.HORIZONTAL;
460                 c.anchor = GridBagConstraints.NORTHWEST;
461                 c.insets = new Insets JavaDoc(0,6,0,6);
462                 ((GridBagLayout JavaDoc)this.getLayout()).setConstraints(addButton,c);
463                 this.add(addButton);
464                 
465                 removeButton = new JButton JavaDoc(NbBundle.getMessage(OC4JCustomizerSupport.class, "CTL_Remove"));
466                 removeButton.setMnemonic(NbBundle.getMessage(OC4JCustomizerSupport.class, "MNE_Remove").charAt(0));
467                 removeButton.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(OC4JCustomizerSupport.class,"AD_Remove"));
468                 removeButton.addActionListener( new ActionListener JavaDoc() {
469                     public void actionPerformed(ActionEvent JavaDoc e) {
470                         removePathElement();
471                     }
472                 });
473                 removeButton.setEnabled(false);
474                 c = new GridBagConstraints JavaDoc();
475                 c.gridx = 1;
476                 c.gridy = 3;
477                 c.gridwidth = GridBagConstraints.REMAINDER;
478                 c.fill = GridBagConstraints.HORIZONTAL;
479                 c.anchor = GridBagConstraints.NORTHWEST;
480                 c.insets = new Insets JavaDoc(12,6,0,6);
481                 ((GridBagLayout JavaDoc)this.getLayout()).setConstraints(removeButton,c);
482                 this.add(removeButton);
483                 moveUpButton = new JButton JavaDoc(NbBundle.getMessage(OC4JCustomizerSupport.class, "CTL_Up"));
484                 moveUpButton.setMnemonic(NbBundle.getMessage(OC4JCustomizerSupport.class, "MNE_Up").charAt(0));
485                 moveUpButton.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(OC4JCustomizerSupport.class,"AD_Up"));
486                 moveUpButton.addActionListener( new ActionListener JavaDoc() {
487                     public void actionPerformed(ActionEvent JavaDoc e) {
488                         moveUpPathElement();
489                     }
490                 });
491                 moveUpButton.setEnabled(false);
492                 c = new GridBagConstraints JavaDoc();
493                 c.gridx = 1;
494                 c.gridy = 4;
495                 c.gridwidth = GridBagConstraints.REMAINDER;
496                 c.fill = GridBagConstraints.HORIZONTAL;
497                 c.anchor = GridBagConstraints.NORTHWEST;
498                 c.insets = new Insets JavaDoc(12,6,0,6);
499                 ((GridBagLayout JavaDoc)this.getLayout()).setConstraints(moveUpButton,c);
500                 this.add(moveUpButton);
501                 moveDownButton = new JButton JavaDoc(NbBundle.getMessage(OC4JCustomizerSupport.class, "CTL_Down"));
502                 moveDownButton.setMnemonic(NbBundle.getMessage(OC4JCustomizerSupport.class, "MNE_Down").charAt(0));
503                 moveDownButton.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(OC4JCustomizerSupport.class,"AD_Down"));
504                 moveDownButton.addActionListener( new ActionListener JavaDoc() {
505                     public void actionPerformed(ActionEvent JavaDoc e) {
506                         moveDownPathElement();
507                     }
508                 });
509                 moveDownButton.setEnabled(false);
510                 c = new GridBagConstraints JavaDoc();
511                 c.gridx = 1;
512                 c.gridy = 5;
513                 c.gridwidth = GridBagConstraints.REMAINDER;
514                 c.fill = GridBagConstraints.HORIZONTAL;
515                 c.anchor = GridBagConstraints.NORTHWEST;
516                 c.insets = new Insets JavaDoc(5,6,6,6);
517                 ((GridBagLayout JavaDoc)this.getLayout()).setConstraints(moveDownButton,c);
518                 this.add(moveDownButton);
519             }
520         }
521         
522         private void addPathElement() {
523             JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
524             FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
525             chooser.setMultiSelectionEnabled(true);
526             String JavaDoc title = null;
527             String JavaDoc message = null;
528             String JavaDoc approveButtonName = null;
529             String JavaDoc approveButtonNameMne = null;
530             if (this.type == JAVADOC) {
531                 title = NbBundle.getMessage(OC4JCustomizerSupport.class,"TXT_OpenJavadoc");
532                 message = NbBundle.getMessage(OC4JCustomizerSupport.class,"TXT_Javadoc");
533                 approveButtonName = NbBundle.getMessage(OC4JCustomizerSupport.class,"TXT_OpenJavadoc");
534                 approveButtonNameMne = NbBundle.getMessage(OC4JCustomizerSupport.class,"MNE_OpenJavadoc");
535             }
536             chooser.setDialogTitle(title);
537             chooser.setApproveButtonText(approveButtonName);
538             chooser.setApproveButtonMnemonic(approveButtonNameMne.charAt(0));
539             chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
540             //#61789 on old macosx (jdk 1.4.1) these two method need to be called in this order.
541
chooser.setAcceptAllFileFilterUsed( false );
542             chooser.setFileFilter(new SimpleFileFilter(message,new String JavaDoc[] {"ZIP","JAR"})); //NOI18N
543
if (this.currentDir != null && currentDir.exists()) {
544                 chooser.setCurrentDirectory(this.currentDir);
545             }
546             if (chooser.showOpenDialog(this)==JFileChooser.APPROVE_OPTION) {
547                 File JavaDoc[] fs = chooser.getSelectedFiles();
548                 PathModel model = (PathModel) this.resources.getModel();
549                 boolean addingFailed = false;
550                 int firstIndex = this.resources.getModel().getSize();
551                 for (int i = 0; i < fs.length; i++) {
552                     File JavaDoc f = fs[i];
553                     //XXX: JFileChooser workaround (JDK bug #5075580), double click on folder returns wrong file
554
// E.g. for /foo/src it returns /foo/src/src
555
// Try to convert it back by removing last invalid name component
556
if (!f.exists()) {
557                         File JavaDoc parent = f.getParentFile();
558                         if (parent != null && f.getName().equals(parent.getName()) && parent.exists()) {
559                             f = parent;
560                         }
561                     }
562                     addingFailed|=!model.addPath(f);
563                 }
564                 if (addingFailed) {
565                     new NotifyDescriptor.Message(NbBundle.getMessage(OC4JCustomizerSupport.class,"TXT_CanNotAddResolve"),
566                             NotifyDescriptor.ERROR_MESSAGE);
567                 }
568                 int lastIndex = this.resources.getModel().getSize()-1;
569                 if (firstIndex<=lastIndex) {
570                     int[] toSelect = new int[lastIndex-firstIndex+1];
571                     for (int i = 0; i < toSelect.length; i++) {
572                         toSelect[i] = firstIndex+i;
573                     }
574                     this.resources.setSelectedIndices(toSelect);
575                 }
576                 this.currentDir = FileUtil.normalizeFile(chooser.getCurrentDirectory());
577             }
578         }
579         
580         private void removePathElement() {
581             int[] indices = this.resources.getSelectedIndices();
582             if (indices.length == 0) {
583                 return;
584             }
585             PathModel model = (PathModel) this.resources.getModel();
586             model.removePath(indices);
587             if ( indices[indices.length-1]-indices.length+1 < this.resources.getModel().getSize()) {
588                 this.resources.setSelectedIndex(indices[indices.length-1]-indices.length+1);
589             } else if (indices[0]>0) {
590                 this.resources.setSelectedIndex(indices[0]-1);
591             }
592         }
593         
594         private void moveDownPathElement() {
595             int[] indices = this.resources.getSelectedIndices();
596             if (indices.length == 0) {
597                 return;
598             }
599             PathModel model = (PathModel) this.resources.getModel();
600             model.moveDownPath(indices);
601             for (int i=0; i< indices.length; i++) {
602                 indices[i] = indices[i] + 1;
603             }
604             this.resources.setSelectedIndices(indices);
605         }
606         
607         private void moveUpPathElement() {
608             int[] indices = this.resources.getSelectedIndices();
609             if (indices.length == 0) {
610                 return;
611             }
612             PathModel model = (PathModel) this.resources.getModel();
613             model.moveUpPath(indices);
614             for (int i=0; i< indices.length; i++) {
615                 indices[i] = indices[i] - 1;
616             }
617             this.resources.setSelectedIndices(indices);
618         }
619         
620         private void selectionChanged() {
621             if (this.type == CLASSPATH) {
622                 return;
623             }
624             int indices[] = this.resources.getSelectedIndices();
625             this.removeButton.setEnabled(indices.length > 0);
626             this.moveUpButton.setEnabled(indices.length > 0 && indices[0]>0);
627             this.moveDownButton.setEnabled(indices.length > 0 && indices[indices.length-1]<this.resources.getModel().getSize()-1);
628         }
629     }
630     
631     private static class SimpleFileFilter extends FileFilter JavaDoc {
632         
633         private String JavaDoc description;
634         private Collection JavaDoc extensions;
635         
636         
637         public SimpleFileFilter(String JavaDoc description, String JavaDoc[] extensions) {
638             this.description = description;
639             this.extensions = Arrays.asList(extensions);
640         }
641         
642         public boolean accept(File JavaDoc f) {
643             if (f.isDirectory())
644                 return true;
645             String JavaDoc name = f.getName();
646             int index = name.lastIndexOf('.'); //NOI18N
647
if (index <= 0 || index==name.length()-1)
648                 return false;
649             String JavaDoc extension = name.substring(index+1).toUpperCase();
650             return this.extensions.contains(extension);
651         }
652         
653         public String JavaDoc getDescription() {
654             return this.description;
655         }
656     }
657 }
Popular Tags