KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > ui > customizer > WarIncludesUi


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.web.project.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.util.HashMap JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Set JavaDoc;
33
34 import javax.swing.ButtonModel JavaDoc;
35 import javax.swing.DefaultListSelectionModel JavaDoc;
36 import javax.swing.Icon JavaDoc;
37 import javax.swing.ImageIcon JavaDoc;
38 import javax.swing.JButton JavaDoc;
39 import javax.swing.JFileChooser JavaDoc;
40 import javax.swing.JTable JavaDoc;
41 import javax.swing.ListSelectionModel JavaDoc;
42 import javax.swing.SwingUtilities JavaDoc;
43 import javax.swing.event.ListSelectionEvent JavaDoc;
44 import javax.swing.event.ListSelectionListener JavaDoc;
45 import javax.swing.event.TableModelEvent JavaDoc;
46 import javax.swing.event.TableModelListener JavaDoc;
47 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
48
49 import org.openide.DialogDescriptor;
50 import org.openide.DialogDisplayer;
51 import org.openide.NotifyDescriptor;
52 import org.openide.filesystems.FileObject;
53 import org.openide.filesystems.FileUtil;
54 import org.openide.filesystems.Repository;
55 import org.openide.loaders.DataFolder;
56 import org.openide.util.NbBundle;
57 import org.openide.util.Utilities;
58
59 import org.netbeans.api.java.project.JavaProjectConstants;
60 import org.netbeans.api.project.Project;
61 import org.netbeans.modules.web.api.webmodule.WebModule;
62
63 import org.netbeans.modules.web.project.classpath.ClassPathSupport;
64 import org.netbeans.modules.web.project.ui.FoldersListSettings;
65 import org.netbeans.modules.web.project.ui.customizer.WarIncludesUiSupport.ClasspathTableModel;
66
67 /** Classes containing code speciic for handling UI of J2SE project classpath
68  *
69  * @author Petr Hrebejk, Radko Najman
70  */

71 public class WarIncludesUi {
72     
73     private WarIncludesUi() {}
74            
75     // Innerclasses ------------------------------------------------------------
76

77     public static class EditMediator implements ActionListener JavaDoc, ListSelectionListener JavaDoc, TableModelListener JavaDoc {
78                 
79         private final Project project;
80         private final JTable JavaDoc list;
81         private final ClasspathTableModel listModel;
82         private final ListSelectionModel JavaDoc selectionModel;
83         private final ButtonModel JavaDoc addJar;
84         private final ButtonModel JavaDoc addLibrary;
85         private final ButtonModel JavaDoc addAntArtifact;
86         private final ButtonModel JavaDoc remove;
87                     
88         public EditMediator( Project project,
89                              JTable JavaDoc list,
90                              ButtonModel JavaDoc addJar,
91                              ButtonModel JavaDoc addLibrary,
92                              ButtonModel JavaDoc addAntArtifact,
93                              ButtonModel JavaDoc remove) {
94                              
95             this.list = list;
96             
97             if ( !( list.getModel() instanceof ClasspathTableModel ) ) {
98                 throw new IllegalArgumentException JavaDoc( "The list's model has to be of class DefaultListModel" ); // NOI18N
99
}
100             
101             this.listModel = (ClasspathTableModel) list.getModel();
102             this.selectionModel = list.getSelectionModel();
103             
104             this.addJar = addJar;
105             this.addLibrary = addLibrary;
106             this.addAntArtifact = addAntArtifact;
107             this.remove = remove;
108
109             this.project = project;
110         }
111
112         public static void register(Project project,
113                                     JTable JavaDoc list,
114                                     ButtonModel JavaDoc addJar,
115                                     ButtonModel JavaDoc addLibrary,
116                                     ButtonModel JavaDoc addAntArtifact,
117                                     ButtonModel JavaDoc remove) {
118             
119             EditMediator em = new EditMediator(project, list, addJar, addLibrary, addAntArtifact, remove);
120                         
121             // Register the listener on all buttons
122
addJar.addActionListener( em );
123             addLibrary.addActionListener( em );
124             addAntArtifact.addActionListener( em );
125             remove.addActionListener( em );
126             // On list selection
127
em.selectionModel.addListSelectionListener( em );
128             em.listModel.addTableModelListener(em);
129             // Set the initial state of the buttons
130
em.valueChanged( null );
131         }
132     
133         // Implementation of ActionListener ------------------------------------
134

135         /** Handles button events
136          */

137         public void actionPerformed( ActionEvent JavaDoc e ) {
138             
139             Object JavaDoc source = e.getSource();
140             
141             if ( source == addJar ) {
142                 // Let user search for the Jar file
143
JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
144                 FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
145                 chooser.setFileSelectionMode( JFileChooser.FILES_AND_DIRECTORIES );
146                 chooser.setMultiSelectionEnabled( true );
147                 chooser.setDialogTitle( NbBundle.getMessage( WarIncludesUi.class, "LBL_AddFile_DialogTitle" ) ); // NOI18N
148
chooser.setAcceptAllFileFilterUsed(true);
149                 File JavaDoc curDir = FoldersListSettings.getDefault().getLastUsedClassPathFolder();
150                 chooser.setCurrentDirectory (curDir);
151                 int option = chooser.showOpenDialog( SwingUtilities.getWindowAncestor( list ) ); // Show the chooser
152

153                 if ( option == JFileChooser.APPROVE_OPTION ) {
154                     
155                     File JavaDoc files[] = chooser.getSelectedFiles();
156                     WarIncludesUiSupport.addJarFiles(files, listModel);
157                     curDir = FileUtil.normalizeFile(chooser.getCurrentDirectory());
158                     FoldersListSettings.getDefault().setLastUsedClassPathFolder(curDir);
159                 }
160             }
161             else if ( source == addLibrary ) {
162                 Set JavaDoc/*<Library>*/includedLibraries = new HashSet JavaDoc ();
163                 Iterator JavaDoc it = WarIncludesUiSupport.getIterator(listModel);
164                 while (it.hasNext()) {
165                     ClassPathSupport.Item item = (ClassPathSupport.Item) it.next();
166                     if (item.getType() == ClassPathSupport.Item.TYPE_LIBRARY) {
167                         includedLibraries.add( item.getLibrary() );
168                     }
169                 }
170                 Object JavaDoc[] options = new Object JavaDoc[] {
171                     new JButton JavaDoc (NbBundle.getMessage (WarIncludesUi.class,"LBL_AddLibrary")),
172                     DialogDescriptor.CANCEL_OPTION
173                 };
174                 ((JButton JavaDoc)options[0]).setEnabled(false);
175                 ((JButton JavaDoc)options[0]).getAccessibleContext().setAccessibleDescription (NbBundle.getMessage (WarIncludesUi.class,"AD_AddLibrary"));
176
177                 WebModule wm = WebModule.getWebModule(project.getProjectDirectory());
178                 String JavaDoc j2eeVersion = wm.getJ2eePlatformVersion();
179                 LibrariesChooser panel = new LibrariesChooser ((JButton JavaDoc)options[0], j2eeVersion);
180                 DialogDescriptor desc = new DialogDescriptor(panel,NbBundle.getMessage( WarIncludesUi.class, "LBL_CustomizeCompile_Classpath_AddLibrary" ),
181                     true, options, options[0], DialogDescriptor.DEFAULT_ALIGN,null,null);
182                 Dialog JavaDoc dlg = DialogDisplayer.getDefault().createDialog(desc);
183                 dlg.setVisible(true);
184                 if (desc.getValue() == options[0]) {
185                    WarIncludesUiSupport.addLibraries(panel.getSelectedLibraries(), includedLibraries, list);
186                 }
187                 dlg.dispose();
188             }
189             else if ( source == addAntArtifact ) {
190                 AntArtifactChooser.ArtifactItem artifactItems[] = AntArtifactChooser.showDialog(JavaProjectConstants.ARTIFACT_TYPE_JAR, project, list.getParent());
191                 if (artifactItems != null) {
192                     WarIncludesUiSupport.addArtifacts(artifactItems, listModel);
193                 }
194             }
195             else if ( source == remove ) {
196                 WarIncludesUiSupport.remove( list);
197             }
198         }
199         
200         
201         /** Handles changes in the selection
202          */

203         public void valueChanged( ListSelectionEvent JavaDoc e ) {
204             DefaultListSelectionModel JavaDoc sm = (DefaultListSelectionModel JavaDoc) list.getSelectionModel();
205             int index = sm.getMinSelectionIndex();
206             
207             // remove enabled only if selection is not empty
208
boolean canRemove = index != -1;
209             // and when the selection does not contain unremovable item
210
if (canRemove) {
211                 ClassPathSupport.Item vcpi = (ClassPathSupport.Item) listModel.getValueAt(index, 0);
212                 if (!vcpi.canDelete())
213                     canRemove = false;
214             }
215                         
216             remove.setEnabled(canRemove);
217
218         }
219         
220         // TableModelListener --------------------------------------
221
public void tableChanged(TableModelEvent JavaDoc e) {
222             if (e.getColumn() == 1) {
223                 ClassPathSupport.Item cpItem = (ClassPathSupport.Item) listModel.getValueAt(e.getFirstRow(), 0);
224                 String JavaDoc newPathInWar = (String JavaDoc) listModel.getValueAt(e.getFirstRow(), 1);
225                 String JavaDoc message = null;
226 // if (userInitiatedChange && cpItem.getType() == ClassPathSupport.Item.TYPE_JAR && newPathInWar.startsWith("WEB-INF")) { //NOI18N
227
if (cpItem.getType() == ClassPathSupport.Item.TYPE_JAR && newPathInWar.startsWith("WEB-INF")) { //NOI18N
228
if (newPathInWar.equals("WEB-INF\\lib") || newPathInWar.equals("WEB-INF/lib")) { //NOI18N
229
if (((File JavaDoc) cpItem.getObject()).isDirectory()) {
230                             message = NbBundle.getMessage(WarIncludesUi.class,
231                                 "MSG_NO_FOLDER_IN_WEBINF_LIB", newPathInWar); // NOI18N
232
} else {
233                             message = NbBundle.getMessage(WarIncludesUi.class,
234                                 "MSG_NO_FILE_IN_WEBINF_LIB", newPathInWar); // NOI18N
235
}
236                     } else if (newPathInWar.equals("WEB-INF\\classes") || newPathInWar.equals("WEB-INF/classes")) { //NOI18N
237
message = NbBundle.getMessage(WarIncludesUi.class,
238                                 "MSG_NO_FOLDER_IN_WEBINF_CLASSES", newPathInWar); // NOI18N
239
}
240                 }
241                 if (message != null) {
242                     DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message (message, NotifyDescriptor.WARNING_MESSAGE));
243                 }
244                 cpItem.setPathInWAR((String JavaDoc) listModel.getValueAt(e.getFirstRow(), 1));
245             }
246         }
247     }
248     
249     static class ClassPathCellRenderer extends DefaultTableCellRenderer JavaDoc {
250         
251         private static String JavaDoc RESOURCE_ICON_JAR = "org/netbeans/modules/web/project/ui/resources/jar.gif"; //NOI18N
252
private static String JavaDoc RESOURCE_ICON_LIBRARY = "org/netbeans/modules/web/project/ui/resources/libraries.gif"; //NOI18N
253
private static String JavaDoc RESOURCE_ICON_ARTIFACT = "org/netbeans/modules/web/project/ui/resources/projectDependencies.gif"; //NOI18N
254
private static String JavaDoc RESOURCE_ICON_CLASSPATH = "org/netbeans/modules/web/project/ui/resources/referencedClasspath.gif"; //NOI18N
255
private static String JavaDoc RESOURCE_ICON_BROKEN_BADGE = "org/netbeans/modules/web/project/ui/resources/brokenProjectBadge.gif"; //NOI18N
256

257         private static ImageIcon JavaDoc ICON_JAR = new ImageIcon JavaDoc( Utilities.loadImage( RESOURCE_ICON_JAR ) );
258         private static ImageIcon JavaDoc ICON_FOLDER = null;
259         private static ImageIcon JavaDoc ICON_LIBRARY = new ImageIcon JavaDoc( Utilities.loadImage( RESOURCE_ICON_LIBRARY ) );
260         private static ImageIcon JavaDoc ICON_ARTIFACT = new ImageIcon JavaDoc( Utilities.loadImage( RESOURCE_ICON_ARTIFACT ) );
261         private static ImageIcon JavaDoc ICON_CLASSPATH = new ImageIcon JavaDoc( Utilities.loadImage( RESOURCE_ICON_CLASSPATH ) );
262         private static ImageIcon JavaDoc ICON_BROKEN_BADGE = new ImageIcon JavaDoc( Utilities.loadImage( RESOURCE_ICON_BROKEN_BADGE ) );
263         
264         private static ImageIcon JavaDoc ICON_BROKEN_JAR;
265         private static ImageIcon JavaDoc ICON_BROKEN_LIBRARY;
266         private static ImageIcon JavaDoc ICON_BROKEN_ARTIFACT;
267
268         // Contains well known paths in the WebProject
269
private static final Map JavaDoc WELL_KNOWN_PATHS_NAMES = new HashMap JavaDoc();
270         static {
271             WELL_KNOWN_PATHS_NAMES.put( WebProjectProperties.JAVAC_CLASSPATH, NbBundle.getMessage( WarIncludesUi.class, "LBL_JavacClasspath_DisplayName" ) );
272             WELL_KNOWN_PATHS_NAMES.put( WebProjectProperties.JAVAC_TEST_CLASSPATH, NbBundle.getMessage( WarIncludesUi.class,"LBL_JavacTestClasspath_DisplayName") );
273             WELL_KNOWN_PATHS_NAMES.put( WebProjectProperties.RUN_TEST_CLASSPATH, NbBundle.getMessage( WarIncludesUi.class, "LBL_RunTestClasspath_DisplayName" ) );
274             WELL_KNOWN_PATHS_NAMES.put( WebProjectProperties.BUILD_CLASSES_DIR, NbBundle.getMessage( WarIncludesUi.class, "LBL_BuildClassesDir_DisplayName" ) );
275             WELL_KNOWN_PATHS_NAMES.put( WebProjectProperties.BUILD_TEST_CLASSES_DIR, NbBundle.getMessage (WarIncludesUi.class,"LBL_BuildTestClassesDir_DisplayName") );
276         };
277
278         public Component JavaDoc getTableCellRendererComponent(JTable JavaDoc table, Object JavaDoc value, boolean isSelected, boolean hasFocus, int row, int column) {
279             String JavaDoc s = null;
280             if (value instanceof ClassPathSupport.Item) {
281                 final ClassPathSupport.Item item = (ClassPathSupport.Item) value;
282                 setIcon(getIcon(item));
283                 setToolTipText(getToolTipText(item));
284                 s = getDisplayName(item);
285             }
286             return super.getTableCellRendererComponent(table, s, isSelected, false, row, column);
287         }
288         
289         private String JavaDoc getDisplayName( ClassPathSupport.Item item ) {
290             switch ( item.getType() ) {
291
292                 case ClassPathSupport.Item.TYPE_LIBRARY:
293                     if ( item.isBroken() ) {
294                         return NbBundle.getMessage( WarIncludesUi.class, "LBL_MISSING_LIBRARY", getLibraryName( item ) );
295                     }
296                     else {
297                         return item.getLibrary().getDisplayName();
298                     }
299                 case ClassPathSupport.Item.TYPE_CLASSPATH:
300                     String JavaDoc name = (String JavaDoc)WELL_KNOWN_PATHS_NAMES.get( WebProjectProperties.getAntPropertyName( item.getReference() ) );
301                     return name == null ? item.getReference() : name;
302                 case ClassPathSupport.Item.TYPE_ARTIFACT:
303                     if ( item.isBroken() ) {
304                         return NbBundle.getMessage( WarIncludesUi.class, "LBL_MISSING_PROJECT", getProjectName( item ) );
305                     }
306                     else {
307                         return item.getArtifactURI().toString();
308                     }
309                 case ClassPathSupport.Item.TYPE_JAR:
310                     if ( item.isBroken() ) {
311                         return NbBundle.getMessage( WarIncludesUi.class, "LBL_MISSING_FILE", getFileRefName( item ) );
312                     }
313                     else {
314                         return item.getFile().getPath();
315                     }
316             }
317
318             return item.getReference(); // XXX
319
}
320
321         static Icon JavaDoc getIcon( ClassPathSupport.Item item ) {
322             
323             switch ( item.getType() ) {
324                 
325                 case ClassPathSupport.Item.TYPE_LIBRARY:
326                     if ( item.isBroken() ) {
327                         if ( ICON_BROKEN_LIBRARY == null ) {
328                             ICON_BROKEN_LIBRARY = new ImageIcon JavaDoc( Utilities.mergeImages( ICON_LIBRARY.getImage(), ICON_BROKEN_BADGE.getImage(), 7, 7 ) );
329                         }
330                         return ICON_BROKEN_LIBRARY;
331                     }
332                     else {
333                         return ICON_LIBRARY;
334                     }
335                 case ClassPathSupport.Item.TYPE_ARTIFACT:
336                     if ( item.isBroken() ) {
337                         if ( ICON_BROKEN_ARTIFACT == null ) {
338                             ICON_BROKEN_ARTIFACT = new ImageIcon JavaDoc( Utilities.mergeImages( ICON_ARTIFACT.getImage(), ICON_BROKEN_BADGE.getImage(), 7, 7 ) );
339                         }
340                         return ICON_BROKEN_ARTIFACT;
341                     }
342                     else {
343                         return ICON_ARTIFACT;
344                     }
345                 case ClassPathSupport.Item.TYPE_JAR:
346                     if ( item.isBroken() ) {
347                         if ( ICON_BROKEN_JAR == null ) {
348                             ICON_BROKEN_JAR = new ImageIcon JavaDoc( Utilities.mergeImages( ICON_JAR.getImage(), ICON_BROKEN_BADGE.getImage(), 7, 7 ) );
349                         }
350                         return ICON_BROKEN_JAR;
351                     }
352                     else {
353                         File JavaDoc file = item.getFile();
354                         return file.isDirectory() ? getFolderIcon() : ICON_JAR;
355                     }
356                 case ClassPathSupport.Item.TYPE_CLASSPATH:
357                     return ICON_CLASSPATH;
358                 
359             }
360             
361             return null; // XXX
362
}
363         
364         private String JavaDoc getToolTipText( ClassPathSupport.Item item ) {
365 // if ( item.isBroken() &&
366
// ( item.getType() == ClassPathSupport.Item.TYPE_JAR ||
367
// item.getType() == ClassPathSupport.Item.TYPE_ARTIFACT ) ) {
368
// return evaluator.evaluate( item.getReference() );
369
// }
370

371             return getDisplayName( item ); // XXX
372
}
373         
374         private static ImageIcon JavaDoc getFolderIcon() {
375         
376             if ( ICON_FOLDER == null ) {
377                 FileObject root = Repository.getDefault().getDefaultFileSystem().getRoot();
378                 DataFolder dataFolder = DataFolder.findFolder( root );
379                 ICON_FOLDER = new ImageIcon JavaDoc( dataFolder.getNodeDelegate().getIcon( BeanInfo.ICON_COLOR_16x16 ) );
380             }
381
382             return ICON_FOLDER;
383         }
384
385         private String JavaDoc getProjectName( ClassPathSupport.Item item ) {
386             String JavaDoc ID = item.getReference();
387             // something in the form of "${reference.project-name.id}"
388
return ID.substring(12, ID.indexOf('.', 12)); // NOI18N
389
}
390
391         private String JavaDoc getLibraryName( ClassPathSupport.Item item ) {
392             String JavaDoc ID = item.getReference();
393             // something in the form of "${libs.junit.classpath}"
394
return ID.substring(7, ID.indexOf(".classpath")); // NOI18N
395
}
396
397         private String JavaDoc getFileRefName( ClassPathSupport.Item item ) {
398             String JavaDoc ID = item.getReference();
399             // something in the form of "${file.reference.smth.jar}"
400
return ID.substring(17, ID.length()-1);
401         }
402     }
403 }
404
Popular Tags