KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbjarproject > ui > customizer > EjbJarClassPathUi


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.ejbjarproject.ui.customizer;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Dialog JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.ActionListener JavaDoc;
26 import java.beans.BeanInfo JavaDoc;
27 import java.io.File JavaDoc;
28 import java.text.MessageFormat JavaDoc;
29 import java.util.Arrays JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.Collections JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.HashSet JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.Set JavaDoc;
37 import javax.swing.ButtonModel JavaDoc;
38 import javax.swing.DefaultListCellRenderer JavaDoc;
39 import javax.swing.DefaultListModel JavaDoc;
40 import javax.swing.Icon JavaDoc;
41 import javax.swing.ImageIcon JavaDoc;
42 import javax.swing.JButton JavaDoc;
43 import javax.swing.JFileChooser JavaDoc;
44 import javax.swing.JList JavaDoc;
45 import javax.swing.JTable JavaDoc;
46 import javax.swing.ListCellRenderer JavaDoc;
47 import javax.swing.ListSelectionModel JavaDoc;
48 import javax.swing.SwingUtilities JavaDoc;
49 import javax.swing.event.ListSelectionEvent JavaDoc;
50 import javax.swing.event.ListSelectionListener JavaDoc;
51 import javax.swing.filechooser.FileFilter JavaDoc;
52 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
53 import javax.swing.table.TableCellRenderer JavaDoc;
54 import org.netbeans.api.java.project.JavaProjectConstants;
55 import org.netbeans.api.project.Project;
56 import org.netbeans.api.project.ProjectInformation;
57 import org.netbeans.api.project.ProjectUtils;
58 import org.netbeans.api.project.libraries.Library;
59 import org.netbeans.modules.j2ee.ejbjarproject.classpath.ClassPathSupport;
60 import org.netbeans.modules.j2ee.ejbjarproject.ui.FoldersListSettings;
61 import org.netbeans.spi.project.support.ant.AntProjectHelper;
62 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
63 import org.netbeans.spi.project.support.ant.ReferenceHelper;
64 import org.openide.DialogDescriptor;
65 import org.openide.DialogDisplayer;
66 import org.openide.filesystems.FileObject;
67 import org.openide.filesystems.FileUtil;
68 import org.openide.filesystems.Repository;
69 import org.openide.loaders.DataFolder;
70 import org.openide.util.NbBundle;
71 import org.openide.util.Utilities;
72
73 /** Classes containing code speciic for handling UI of EJB project classpath
74  *
75  * @author Petr Hrebejk
76  */

77 public class EjbJarClassPathUi {
78     
79     private EjbJarClassPathUi() {}
80            
81     // Innerclasses ------------------------------------------------------------
82

83     /** Renderer which can be used to render the classpath in lists
84      */

85     public static class ClassPathListCellRenderer extends DefaultListCellRenderer JavaDoc {
86         
87         private static String JavaDoc RESOURCE_ICON_JAR = "org/netbeans/modules/j2ee/ejbjarproject/ui/resources/jar.gif"; //NOI18N
88
private static String JavaDoc RESOURCE_ICON_LIBRARY = "org/netbeans/modules/j2ee/ejbjarproject/ui/resources/libraries.gif"; //NOI18N
89
private static String JavaDoc RESOURCE_ICON_ARTIFACT = "org/netbeans/modules/j2ee/ejbjarproject/ui/resources/projectDependencies.gif"; //NOI18N
90
private static String JavaDoc RESOURCE_ICON_CLASSPATH = "org/netbeans/modules/j2ee/ejbjarproject/ui/resources/referencedClasspath.gif"; //NOI18N
91
private static String JavaDoc RESOURCE_ICON_BROKEN_BADGE = "org/netbeans/modules/j2ee/ejbjarproject/ui/resources/brokenProjectBadge.gif"; //NOI18N
92

93         
94         private static ImageIcon JavaDoc ICON_JAR = new ImageIcon JavaDoc( Utilities.loadImage( RESOURCE_ICON_JAR ) );
95         private static ImageIcon JavaDoc ICON_FOLDER = null;
96         private static ImageIcon JavaDoc ICON_LIBRARY = new ImageIcon JavaDoc( Utilities.loadImage( RESOURCE_ICON_LIBRARY ) );
97         private static ImageIcon JavaDoc ICON_ARTIFACT = new ImageIcon JavaDoc( Utilities.loadImage( RESOURCE_ICON_ARTIFACT ) );
98         private static ImageIcon JavaDoc ICON_CLASSPATH = new ImageIcon JavaDoc( Utilities.loadImage( RESOURCE_ICON_CLASSPATH ) );
99         private static ImageIcon JavaDoc ICON_BROKEN_BADGE = new ImageIcon JavaDoc( Utilities.loadImage( RESOURCE_ICON_BROKEN_BADGE ) );
100         
101         private static ImageIcon JavaDoc ICON_BROKEN_JAR;
102         private static ImageIcon JavaDoc ICON_BROKEN_LIBRARY;
103         private static ImageIcon JavaDoc ICON_BROKEN_ARTIFACT;
104                 
105         private PropertyEvaluator evaluator;
106         
107         // Contains well known paths in the EJBProject
108
private static final Map JavaDoc WELL_KNOWN_PATHS_NAMES = new HashMap JavaDoc();
109         static {
110             WELL_KNOWN_PATHS_NAMES.put( EjbJarProjectProperties.JAVAC_CLASSPATH, NbBundle.getMessage( EjbJarProjectProperties.class, "LBL_JavacClasspath_DisplayName" ) );
111             WELL_KNOWN_PATHS_NAMES.put( EjbJarProjectProperties.JAVAC_TEST_CLASSPATH, NbBundle.getMessage( EjbJarProjectProperties.class,"LBL_JavacTestClasspath_DisplayName") );
112             WELL_KNOWN_PATHS_NAMES.put( EjbJarProjectProperties.RUN_TEST_CLASSPATH, NbBundle.getMessage( EjbJarProjectProperties.class, "LBL_RunTestClasspath_DisplayName" ) );
113             WELL_KNOWN_PATHS_NAMES.put( EjbJarProjectProperties.BUILD_CLASSES_DIR, NbBundle.getMessage( EjbJarProjectProperties.class, "LBL_BuildClassesDir_DisplayName" ) );
114             WELL_KNOWN_PATHS_NAMES.put( EjbJarProjectProperties.BUILD_TEST_CLASSES_DIR, NbBundle.getMessage (EjbJarProjectProperties.class,"LBL_BuildTestClassesDir_DisplayName") );
115         };
116                 
117         public ClassPathListCellRenderer( PropertyEvaluator evaluator ) {
118             super();
119             this.evaluator = evaluator;
120         }
121         
122         public Component JavaDoc getListCellRendererComponent( JList JavaDoc list, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
123             
124             ClassPathSupport.Item item = (ClassPathSupport.Item)value;
125             
126             super.getListCellRendererComponent( list, getDisplayName( item ), index, isSelected, cellHasFocus );
127             setIcon( getIcon( item ) );
128             setToolTipText( getToolTipText( item ) );
129             
130             return this;
131         }
132         
133         
134         public String JavaDoc getDisplayName( ClassPathSupport.Item item ) {
135             
136             switch ( item.getType() ) {
137                 
138                 case ClassPathSupport.Item.TYPE_LIBRARY:
139                     if ( item.isBroken() ) {
140                         return NbBundle.getMessage( EjbJarClassPathUi.class, "LBL_MISSING_LIBRARY", getLibraryName( item ) );
141                     }
142                     else {
143                         return item.getLibrary().getDisplayName();
144                     }
145                 case ClassPathSupport.Item.TYPE_CLASSPATH:
146                     String JavaDoc name = (String JavaDoc)WELL_KNOWN_PATHS_NAMES.get( EjbJarProjectProperties.getAntPropertyName( item.getReference() ) );
147                     return name == null ? item.getReference() : name;
148                 case ClassPathSupport.Item.TYPE_ARTIFACT:
149                     if ( item.isBroken() ) {
150                         return NbBundle.getMessage( EjbJarClassPathUi.class, "LBL_MISSING_PROJECT", getProjectName( item ) );
151                     }
152                     else {
153                         Project p = item.getArtifact().getProject();
154                         String JavaDoc projectName;
155                         ProjectInformation pi = ProjectUtils.getInformation(p);
156                         projectName = pi.getDisplayName();
157                         return MessageFormat.format(NbBundle.getMessage(EjbJarClassPathUi.class,"MSG_ProjectArtifactFormat"), new Object JavaDoc[] {
158                             projectName,
159                             item.getArtifactURI().toString()
160                         });
161                     }
162                 case ClassPathSupport.Item.TYPE_JAR:
163                     if ( item.isBroken() ) {
164                         return NbBundle.getMessage( EjbJarClassPathUi.class, "LBL_MISSING_FILE", getFileRefName( item ) );
165                     }
166                     else {
167                         return item.getFile().getPath();
168                     }
169             }
170             
171             return item.getReference(); // XXX
172
}
173         
174         public static Icon JavaDoc getIcon( ClassPathSupport.Item item ) {
175             
176             switch ( item.getType() ) {
177                 
178                 case ClassPathSupport.Item.TYPE_LIBRARY:
179                     if ( item.isBroken() ) {
180                         if ( ICON_BROKEN_LIBRARY == null ) {
181                             ICON_BROKEN_LIBRARY = new ImageIcon JavaDoc( Utilities.mergeImages( ICON_LIBRARY.getImage(), ICON_BROKEN_BADGE.getImage(), 7, 7 ) );
182                         }
183                         return ICON_BROKEN_LIBRARY;
184                     }
185                     else {
186                         return ICON_LIBRARY;
187                     }
188                 case ClassPathSupport.Item.TYPE_ARTIFACT:
189                     if ( item.isBroken() ) {
190                         if ( ICON_BROKEN_ARTIFACT == null ) {
191                             ICON_BROKEN_ARTIFACT = new ImageIcon JavaDoc( Utilities.mergeImages( ICON_ARTIFACT.getImage(), ICON_BROKEN_BADGE.getImage(), 7, 7 ) );
192                         }
193                         return ICON_BROKEN_ARTIFACT;
194                     }
195                     else {
196                         Project p = item.getArtifact().getProject();
197                         if (p != null) {
198                             ProjectInformation pi = ProjectUtils.getInformation(p);
199                             return pi.getIcon();
200                         }
201                         return ICON_ARTIFACT;
202                     }
203                 case ClassPathSupport.Item.TYPE_JAR:
204                     if ( item.isBroken() ) {
205                         if ( ICON_BROKEN_JAR == null ) {
206                             ICON_BROKEN_JAR = new ImageIcon JavaDoc( Utilities.mergeImages( ICON_JAR.getImage(), ICON_BROKEN_BADGE.getImage(), 7, 7 ) );
207                         }
208                         return ICON_BROKEN_JAR;
209                     }
210                     else {
211                         File JavaDoc file = item.getFile();
212                         return file.isDirectory() ? getFolderIcon() : ICON_JAR;
213                     }
214                 case ClassPathSupport.Item.TYPE_CLASSPATH:
215                     return ICON_CLASSPATH;
216                 
217             }
218             
219             return null; // XXX
220
}
221         
222         public String JavaDoc getToolTipText( ClassPathSupport.Item item ) {
223             if ( item.isBroken() &&
224                  ( item.getType() == ClassPathSupport.Item.TYPE_JAR ||
225                    item.getType() == ClassPathSupport.Item.TYPE_ARTIFACT ) ) {
226                 return evaluator.evaluate( item.getReference() );
227             }
228             
229             return getDisplayName( item ); // XXX
230
}
231         
232         private static ImageIcon JavaDoc getFolderIcon() {
233         
234             if ( ICON_FOLDER == null ) {
235                 FileObject root = Repository.getDefault().getDefaultFileSystem().getRoot();
236                 DataFolder dataFolder = DataFolder.findFolder( root );
237                 ICON_FOLDER = new ImageIcon JavaDoc( dataFolder.getNodeDelegate().getIcon( BeanInfo.ICON_COLOR_16x16 ) );
238             }
239
240             return ICON_FOLDER;
241         }
242         
243         private String JavaDoc getProjectName( ClassPathSupport.Item item ) {
244             String JavaDoc ID = item.getReference();
245             // something in the form of "${reference.project-name.id}"
246
return ID.substring(12, ID.indexOf(".", 12)); // NOI18N
247
}
248
249         private String JavaDoc getLibraryName( ClassPathSupport.Item item ) {
250             String JavaDoc ID = item.getReference();
251             // something in the form of "${libs.junit.classpath}"
252
return ID.substring(7, ID.indexOf(".classpath")); // NOI18N
253
}
254
255         private String JavaDoc getFileRefName( ClassPathSupport.Item item ) {
256             String JavaDoc ID = item.getReference();
257             // something in the form of "${file.reference.smth.jar}"
258
return ID.substring(17, ID.length()-1);
259         }
260
261
262         
263     }
264     
265     public static class ClassPathTableCellItemRenderer extends DefaultTableCellRenderer JavaDoc {
266         
267         private ClassPathListCellRenderer renderer;
268         private TableCellRenderer JavaDoc booleanRenderer;
269         
270         public ClassPathTableCellItemRenderer(PropertyEvaluator evaluator) {
271             renderer = new ClassPathListCellRenderer(evaluator);
272         }
273         
274         public Component JavaDoc getTableCellRendererComponent( JTable JavaDoc table, Object JavaDoc value, boolean isSelected, boolean hasFocus, int row, int column ) {
275             
276             if (value instanceof ClassPathSupport.Item) {
277                 ClassPathSupport.Item item = (ClassPathSupport.Item)value;
278                 setIcon( renderer.getIcon( item ) );
279                 setToolTipText( renderer.getToolTipText( item ) );
280                 return super.getTableCellRendererComponent(table, renderer.getDisplayName( item ), isSelected, false, row, column);
281             } else {
282                 if (value instanceof Boolean JavaDoc && booleanRenderer != null)
283                     return booleanRenderer.getTableCellRendererComponent( table, value, isSelected, false, row, column );
284                 else {
285                     setIcon( null );
286                     return super.getTableCellRendererComponent( table, value, isSelected, false, row, column );
287                 }
288             }
289         }
290         
291         public void setBooleanRenderer(TableCellRenderer JavaDoc booleanRenderer) {
292             this.booleanRenderer = booleanRenderer;
293         }
294     }
295     
296     
297     public static class EditMediator implements ActionListener JavaDoc, ListSelectionListener JavaDoc {
298                 
299         private final Project project;
300         private final ListComponent list;
301         private final DefaultListModel JavaDoc listModel;
302         private final ListSelectionModel JavaDoc selectionModel;
303         private final ButtonModel JavaDoc addJar;
304         private final ButtonModel JavaDoc addLibrary;
305         private final ButtonModel JavaDoc addAntArtifact;
306         private final ButtonModel JavaDoc remove;
307         private final ButtonModel JavaDoc moveUp;
308         private final ButtonModel JavaDoc moveDown;
309         private final boolean includeNewFilesInDeployment;
310                     
311         public EditMediator( Project project,
312                              ListComponent list,
313                              ButtonModel JavaDoc addJar,
314                              ButtonModel JavaDoc addLibrary,
315                              ButtonModel JavaDoc addAntArtifact,
316                              ButtonModel JavaDoc remove,
317                              ButtonModel JavaDoc moveUp,
318                              ButtonModel JavaDoc moveDown,
319                              boolean includeNewFilesInDeployment) {
320                              
321             // Remember all buttons
322

323             this.list = list;
324             
325             if ( !( list.getModel() instanceof DefaultListModel JavaDoc ) ) {
326                 throw new IllegalArgumentException JavaDoc( "The list's model has to be of class DefaultListModel" ); // NOI18N
327
}
328             
329             this.listModel = (DefaultListModel JavaDoc)list.getModel();
330             this.selectionModel = list.getSelectionModel();
331             
332             this.addJar = addJar;
333             this.addLibrary = addLibrary;
334             this.addAntArtifact = addAntArtifact;
335             this.remove = remove;
336             this.moveUp = moveUp;
337             this.moveDown = moveDown;
338
339             this.project = project;
340             this.includeNewFilesInDeployment = includeNewFilesInDeployment;
341         }
342
343         public static void register(Project project,
344                                     ListComponent list,
345                                     ButtonModel JavaDoc addJar,
346                                     ButtonModel JavaDoc addLibrary,
347                                     ButtonModel JavaDoc addAntArtifact,
348                                     ButtonModel JavaDoc remove,
349                                     ButtonModel JavaDoc moveUp,
350                                     ButtonModel JavaDoc moveDown,
351                                     boolean includeNewFilesInDeployment) {
352             
353             EditMediator em = new EditMediator( project,
354                                                 list,
355                                                 addJar,
356                                                 addLibrary,
357                                                 addAntArtifact,
358                                                 remove,
359                                                 moveUp,
360                                                 moveDown,
361                                                 includeNewFilesInDeployment );
362             
363             // Register the listener on all buttons
364
addJar.addActionListener( em );
365             addLibrary.addActionListener( em );
366             addAntArtifact.addActionListener( em );
367             remove.addActionListener( em );
368             moveUp.addActionListener( em );
369             moveDown.addActionListener( em );
370             // On list selection
371
em.selectionModel.addListSelectionListener( em );
372             // Set the initial state of the buttons
373
em.valueChanged( null );
374         }
375     
376         // Implementation of ActionListener ------------------------------------
377

378         /** Handles button events
379          */

380         public void actionPerformed( ActionEvent JavaDoc e ) {
381             
382             Object JavaDoc source = e.getSource();
383             
384             if ( source == addJar ) {
385                 // Let user search for the Jar file
386
JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
387                 FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
388                 chooser.setFileSelectionMode( JFileChooser.FILES_AND_DIRECTORIES );
389                 chooser.setMultiSelectionEnabled( true );
390                 chooser.setDialogTitle( NbBundle.getMessage( EjbJarClassPathUi.class, "LBL_AddJar_DialogTitle" ) ); // NOI18N
391
//#61789 on old macosx (jdk 1.4.1) these two method need to be called in this order.
392
chooser.setAcceptAllFileFilterUsed( false );
393                 chooser.setFileFilter( new SimpleFileFilter(
394                     NbBundle.getMessage( EjbJarClassPathUi.class, "LBL_ZipJarFolderFilter" ), // NOI18N
395
new String JavaDoc[] {"ZIP","JAR"} ) ); // NOI18N
396
File JavaDoc curDir = FoldersListSettings.getDefault().getLastUsedClassPathFolder();
397                 chooser.setCurrentDirectory (curDir);
398                 int option = chooser.showOpenDialog( SwingUtilities.getWindowAncestor( list.getComponent() ) ); // Sow the chooser
399

400                 if ( option == JFileChooser.APPROVE_OPTION ) {
401                     
402                     File JavaDoc files[] = chooser.getSelectedFiles();
403                     int[] newSelection = ClassPathUiSupport.addJarFiles( listModel, list.getSelectedIndices(), files, includeNewFilesInDeployment );
404                     list.setSelectedIndices( newSelection );
405                     curDir = FileUtil.normalizeFile(chooser.getCurrentDirectory());
406                     FoldersListSettings.getDefault().setLastUsedClassPathFolder(curDir);
407                 }
408             }
409             else if ( source == addLibrary ) {
410                 Set JavaDoc/*<Library>*/includedLibraries = new HashSet JavaDoc ();
411                 for (int i=0; i< listModel.getSize(); i++) {
412                     ClassPathSupport.Item item = (ClassPathSupport.Item) listModel.get(i);
413                     if (item.getType() == ClassPathSupport.Item.TYPE_LIBRARY && !item.isBroken() ) {
414                         includedLibraries.add( item.getLibrary() );
415                     }
416                 }
417                 Object JavaDoc[] options = new Object JavaDoc[] {
418                     new JButton JavaDoc (NbBundle.getMessage (EjbJarClassPathUi.class,"LBL_AddLibrary")),
419                     DialogDescriptor.CANCEL_OPTION
420                 };
421                 ((JButton JavaDoc)options[0]).setEnabled(false);
422                 ((JButton JavaDoc)options[0]).getAccessibleContext().setAccessibleDescription (NbBundle.getMessage (EjbJarClassPathUi.class,"AD_AddLibrary"));
423                 LibrariesChooser panel = new LibrariesChooser ((JButton JavaDoc)options[0], includedLibraries);
424                 DialogDescriptor desc = new DialogDescriptor(panel,NbBundle.getMessage( EjbJarClassPathUi.class, "LBL_CustomizeCompile_Classpath_AddLibrary" ),
425                     true, options, options[0], DialogDescriptor.DEFAULT_ALIGN,null,null);
426                 Dialog JavaDoc dlg = DialogDisplayer.getDefault().createDialog(desc);
427                 dlg.setVisible(true);
428                 if (desc.getValue() == options[0]) {
429                    int[] newSelection = ClassPathUiSupport.addLibraries( listModel, list.getSelectedIndices(), panel.getSelectedLibraries(), includedLibraries, includeNewFilesInDeployment );
430                    list.setSelectedIndices( newSelection );
431                 }
432                 dlg.dispose();
433             }
434             else if ( source == addAntArtifact ) {
435                 AntArtifactChooser.ArtifactItem artifactItems[] = AntArtifactChooser.showDialog(JavaProjectConstants.ARTIFACT_TYPE_JAR, project, list.getComponent().getParent());
436                 if (artifactItems != null) {
437                     int[] newSelection = ClassPathUiSupport.addArtifacts( listModel, list.getSelectedIndices(), artifactItems, includeNewFilesInDeployment );
438                     list.setSelectedIndices( newSelection );
439                 }
440             }
441             else if ( source == remove ) {
442                 int[] newSelection = ClassPathUiSupport.remove( listModel, list.getSelectedIndices() );
443                 list.setSelectedIndices( newSelection );
444             }
445             else if ( source == moveUp ) {
446                 int[] newSelection = ClassPathUiSupport.moveUp( listModel, list.getSelectedIndices() );
447                 list.setSelectedIndices( newSelection );
448             }
449             else if ( source == moveDown ) {
450                 int[] newSelection = ClassPathUiSupport.moveDown( listModel, list.getSelectedIndices() );
451                 list.setSelectedIndices( newSelection );
452             }
453         }
454         
455         
456         /** Handles changes in the selection
457          */

458         public void valueChanged( ListSelectionEvent JavaDoc e ) {
459             
460             // remove enabled only if selection is not empty
461
boolean canRemove = false;
462             // and when the selection does not contain unremovable item
463
if ( selectionModel.getMinSelectionIndex() != -1 ) {
464                 canRemove = true;
465                 int iMin = selectionModel.getMinSelectionIndex();
466                 int iMax = selectionModel.getMinSelectionIndex();
467                 for ( int i = iMin; i <= iMax; i++ ) {
468                     
469                     if ( selectionModel.isSelectedIndex( i ) ) {
470                         ClassPathSupport.Item item = (ClassPathSupport.Item)listModel.get( i );
471                         if ( item.getType() == ClassPathSupport.Item.TYPE_CLASSPATH ) {
472                             canRemove = false;
473                             break;
474                         }
475                     }
476                 }
477             }
478             
479             // addJar allways enabled
480
// addLibrary allways enabled
481
// addArtifact allways enabled
482
// editButton.setEnabled( edit );
483
remove.setEnabled( canRemove );
484             moveUp.setEnabled( ClassPathUiSupport.canMoveUp( selectionModel ) );
485             moveDown.setEnabled( ClassPathUiSupport.canMoveDown( selectionModel, listModel.getSize() ) );
486             
487         }
488         
489         public interface ListComponent {
490             public Component JavaDoc getComponent();
491             public int[] getSelectedIndices();
492             public void setSelectedIndices(int[] indices);
493             public DefaultListModel JavaDoc getModel();
494             public ListSelectionModel JavaDoc getSelectionModel();
495         }
496         
497         private static final class JListListComponent implements ListComponent {
498             private JList JavaDoc list;
499             private DefaultListModel JavaDoc model;
500             
501             public JListListComponent(JList JavaDoc list) {
502                 this.list = list;
503                 this.model = model;
504             }
505             
506             public Component JavaDoc getComponent() {
507                 return list;
508             }
509             
510             public int[] getSelectedIndices() {
511                 return list.getSelectedIndices();
512             }
513             
514             public void setSelectedIndices(int[] indices) {
515                 list.setSelectedIndices(indices);
516             }
517             
518             public DefaultListModel JavaDoc getModel() {
519                 return (DefaultListModel JavaDoc)list.getModel();
520             }
521             
522             public ListSelectionModel JavaDoc getSelectionModel() {
523                 return list.getSelectionModel();
524             }
525         }
526         
527         private static final class JTableListComponent implements ListComponent {
528             private JTable JavaDoc table;
529             private DefaultListModel JavaDoc model;
530             
531             public JTableListComponent(JTable JavaDoc table, DefaultListModel JavaDoc model) {
532                 this.table = table;
533                 this.model = model;
534             }
535             
536             public Component JavaDoc getComponent() {
537                 return table;
538             }
539             
540             public int[] getSelectedIndices() {
541                 return table.getSelectedRows();
542             }
543             
544             public void setSelectedIndices(int[] indices) {
545                 table.clearSelection();
546                 for (int i = 0; i < indices.length; i++) {
547                     table.addRowSelectionInterval(indices[i], indices[i]);
548                 }
549             }
550             
551             public DefaultListModel JavaDoc getModel() {
552                 return model;
553             }
554             
555             public ListSelectionModel JavaDoc getSelectionModel() {
556                 return table.getSelectionModel();
557             }
558         }
559         
560         public static ListComponent createListComponent(JList JavaDoc list) {
561             return new JListListComponent(list);
562         }
563         
564         public static ListComponent createListComponent(JTable JavaDoc table, DefaultListModel JavaDoc model) {
565             return new JTableListComponent(table, model);
566         }
567     }
568     
569     private static class SimpleFileFilter extends FileFilter JavaDoc {
570
571         private String JavaDoc description;
572         private Collection JavaDoc extensions;
573
574
575         public SimpleFileFilter (String JavaDoc description, String JavaDoc[] extensions) {
576             this.description = description;
577             this.extensions = Arrays.asList(extensions);
578         }
579
580         public boolean accept(File JavaDoc f) {
581             if (f.isDirectory())
582                 return true;
583             String JavaDoc name = f.getName();
584             int index = name.lastIndexOf('.'); //NOI18N
585
if (index <= 0 || index==name.length()-1)
586                 return false;
587             String JavaDoc extension = name.substring (index+1).toUpperCase();
588             return this.extensions.contains(extension);
589         }
590
591         public String JavaDoc getDescription() {
592             return this.description;
593         }
594     }
595         
596 }
597
Popular Tags