KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > ui > LibrariesNode


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.web.project.ui;
21
22 import java.awt.Dialog JavaDoc;
23 import java.awt.Image JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.beans.BeanInfo JavaDoc;
26 import java.beans.PropertyChangeEvent JavaDoc;
27 import java.beans.PropertyChangeListener JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.File JavaDoc;
30 import java.net.MalformedURLException JavaDoc;
31 import java.net.URI JavaDoc;
32 import java.net.URL JavaDoc;
33 import java.text.MessageFormat JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.Arrays JavaDoc;
36 import java.util.Collections JavaDoc;
37 import java.util.HashSet JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.LinkedList JavaDoc;
40 import java.util.List JavaDoc;
41 import java.util.Set JavaDoc;
42 import javax.swing.AbstractAction JavaDoc;
43 import javax.swing.Action JavaDoc;
44 import javax.swing.Icon JavaDoc;
45 import javax.swing.ImageIcon JavaDoc;
46 import javax.swing.JButton JavaDoc;
47 import javax.swing.JFileChooser JavaDoc;
48 import javax.swing.filechooser.FileFilter JavaDoc;
49 import org.netbeans.modules.web.project.ui.customizer.AntArtifactChooser.ArtifactItem;
50 import org.netbeans.modules.web.project.ui.customizer.WebClassPathUi;
51 import org.openide.DialogDescriptor;
52 import org.openide.DialogDisplayer;
53 import org.openide.ErrorManager;
54 import org.openide.filesystems.FileObject;
55 import org.openide.filesystems.FileUtil;
56 import org.openide.filesystems.Repository;
57 import org.openide.filesystems.URLMapper;
58 import org.openide.loaders.DataFolder;
59 import org.openide.nodes.AbstractNode;
60 import org.openide.nodes.Children;
61 import org.openide.nodes.Node;
62 import org.openide.util.NbBundle;
63 import org.openide.util.RequestProcessor;
64 import org.openide.util.Utilities;
65 import org.openide.windows.WindowManager;
66 import org.netbeans.api.project.FileOwnerQuery;
67 import org.netbeans.api.project.SourceGroup;
68 import org.netbeans.api.project.Project;
69 import org.netbeans.api.project.ant.AntArtifact;
70 import org.netbeans.api.project.libraries.Library;
71 import org.netbeans.api.project.libraries.LibraryManager;
72 import org.netbeans.api.java.classpath.ClassPath;
73 import org.netbeans.api.java.project.JavaProjectConstants;
74 import org.netbeans.spi.project.support.ant.AntProjectHelper;
75 import org.netbeans.spi.project.support.ant.EditableProperties;
76 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
77 import org.netbeans.spi.project.support.ant.PropertyUtils;
78 import org.netbeans.spi.project.support.ant.ReferenceHelper;
79 import org.netbeans.spi.java.project.support.ui.PackageView;
80 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
81 import org.netbeans.modules.web.project.UpdateHelper;
82 import org.netbeans.modules.web.api.webmodule.WebModule;
83 import org.netbeans.modules.web.project.classpath.WebProjectClassPathExtender;
84 import org.netbeans.modules.web.project.ui.customizer.AntArtifactChooser;
85 import org.netbeans.modules.web.project.ui.customizer.WebProjectProperties;
86 import org.netbeans.modules.web.project.ui.customizer.LibrariesChooser;
87 import org.openide.util.lookup.Lookups;
88
89 /**
90  * LibrariesNode displays the content of classpath and optionaly Java platform.
91  * @author Tomas Zezula
92  */

93 final class LibrariesNode extends AbstractNode {
94
95     private static final Image JavaDoc ICON_BADGE = Utilities.loadImage("org/netbeans/modules/web/project/ui/resources/libraries-badge.png"); //NOI18N
96
static final RequestProcessor rp = new RequestProcessor ();
97     private static Icon JavaDoc folderIconCache;
98     private static Icon JavaDoc openedFolderIconCache;
99
100     private final String JavaDoc displayName;
101     private final Action JavaDoc[] librariesNodeActions;
102
103
104     /**
105      * Creates new LibrariesNode named displayName displaying classPathProperty classpath
106      * and optionaly Java platform.
107      * @param displayName the display name of the node
108      * @param eval {@link PropertyEvaluator} used for listening
109      * @param helper {@link UpdateHelper} used for reading and updating project's metadata
110      * @param refHelper {@link ReferenceHelper} used for destroying unused references
111      * @param classPathProperty the ant property name of classpath which should be visualized
112      * @param classPathIgnoreRef the array of ant property names which should not be displayed, may be
113      * an empty array but not null
114      * @param platformProperty the ant name property holding the Web platform system name or null
115      * if the platform should not be displayed
116      * @param librariesNodeActions actions which should be available on the created node.
117      */

118     LibrariesNode (String JavaDoc displayName, Project project, PropertyEvaluator eval, UpdateHelper helper, ReferenceHelper refHelper,
119                    String JavaDoc classPathProperty, String JavaDoc[] classPathIgnoreRef, String JavaDoc platformProperty, String JavaDoc j2eePlatformProperty,
120                    Action JavaDoc[] librariesNodeActions, String JavaDoc webModuleElementName) {
121         super (new LibrariesChildren (eval, helper, refHelper, classPathProperty, classPathIgnoreRef, platformProperty, j2eePlatformProperty, webModuleElementName), Lookups.singleton(project));
122         this.displayName = displayName;
123         this.librariesNodeActions = librariesNodeActions;
124     }
125
126     public String JavaDoc getDisplayName () {
127         return this.displayName;
128     }
129
130     public String JavaDoc getName () {
131         return this.getDisplayName();
132     }
133
134     public Image JavaDoc getIcon( int type ) {
135         return computeIcon( false, type );
136     }
137         
138     public Image JavaDoc getOpenedIcon( int type ) {
139         return computeIcon( true, type );
140     }
141
142     public Action JavaDoc[] getActions(boolean context) {
143         return this.librariesNodeActions;
144     }
145
146     public boolean canCopy() {
147         return false;
148     }
149
150     //Static Action Factory Methods
151
public static Action JavaDoc createAddProjectAction (Project p, String JavaDoc classPathId, String JavaDoc webModuleElementName) {
152         return new AddProjectAction (p, classPathId, webModuleElementName);
153     }
154
155     public static Action JavaDoc createAddLibraryAction (Project p, String JavaDoc classPathId, String JavaDoc webModuleElementName) {
156         return new AddLibraryAction (p, classPathId, webModuleElementName);
157     }
158
159     public static Action JavaDoc createAddFolderAction (Project p, String JavaDoc classPathId, String JavaDoc webModuleElementName) {
160         return new AddFolderAction (p, classPathId, webModuleElementName);
161     }
162     
163     /**
164      * Returns Icon of folder on active platform
165      * @param opened should the icon represent opened folder
166      * @return the folder icon
167      */

168     static synchronized Icon JavaDoc getFolderIcon (boolean opened) {
169         if (openedFolderIconCache == null) {
170             Node n = DataFolder.findFolder(Repository.getDefault().getDefaultFileSystem().getRoot()).getNodeDelegate();
171             openedFolderIconCache = new ImageIcon JavaDoc(n.getOpenedIcon(BeanInfo.ICON_COLOR_16x16));
172             folderIconCache = new ImageIcon JavaDoc(n.getIcon(BeanInfo.ICON_COLOR_16x16));
173         }
174         if (opened) {
175             return openedFolderIconCache;
176         }
177         else {
178             return folderIconCache;
179         }
180     }
181
182     private Image JavaDoc computeIcon( boolean opened, int type ) {
183         Icon JavaDoc icon = getFolderIcon(opened);
184         Image JavaDoc image = ((ImageIcon JavaDoc)icon).getImage();
185         image = Utilities.mergeImages(image, ICON_BADGE, 7, 7 );
186         return image;
187     }
188
189     //Static inner classes
190
private static class LibrariesChildren extends Children.Keys implements PropertyChangeListener JavaDoc {
191
192         
193         /**
194          * Constant represneting a prefix of library reference generated by {@link org.netbeans.modules.java.j2seplatform.libraries.J2SELibraryTypeProvider}
195          */

196         private static final String JavaDoc LIBRARY_PREFIX = "${libs."; // NOI18N
197

198         /**
199          * Constant representing a prefix of artifact reference generated by {@link ReferenceHelper}
200          */

201         private static final String JavaDoc ANT_ARTIFACT_PREFIX = "${reference."; // NOI18N
202
/**
203          * Constant representing a prefix of file reference generated by {@link ReferenceHelper}
204          */

205         private static final String JavaDoc FILE_REF_PREFIX = "${file.reference."; //NOI18N
206
/**
207          * Constant representing a prefix of ant property reference
208          */

209         private static final String JavaDoc REF_PREFIX = "${"; //NOI18N
210

211         private static final String JavaDoc LIBRARIES_ICON = "org/netbeans/modules/web/project/ui/resources/libraries.gif"; //NOI18N
212
private static final String JavaDoc ARCHIVE_ICON = "org/netbeans/modules/web/project/ui/resources/jar.gif";//NOI18N
213

214         private final PropertyEvaluator eval;
215         private final UpdateHelper helper;
216         private final ReferenceHelper refHelper;
217         private final String JavaDoc classPathProperty;
218         private final String JavaDoc platformProperty;
219         private final String JavaDoc j2eePlatformProperty;
220         private final Set JavaDoc classPathIgnoreRef;
221         private final String JavaDoc webModuleElementName;
222
223         //XXX: Workaround: classpath is used only to listen on non existent files.
224
// This should be removed when there will be API for it
225
// See issue: http://www.netbeans.org/issues/show_bug.cgi?id=33162
226
private ClassPath fsListener;
227
228
229         LibrariesChildren (PropertyEvaluator eval, UpdateHelper helper, ReferenceHelper refHelper,
230                            String JavaDoc classPathProperty, String JavaDoc[] classPathIgnoreRef, String JavaDoc platformProperty, String JavaDoc j2eePlatformProperty, String JavaDoc webModuleElementName) {
231             this.eval = eval;
232             this.helper = helper;
233             this.refHelper = refHelper;
234             this.classPathProperty = classPathProperty;
235             this.classPathIgnoreRef = new HashSet JavaDoc(Arrays.asList(classPathIgnoreRef));
236             this.platformProperty = platformProperty;
237             this.j2eePlatformProperty = j2eePlatformProperty;
238             this.webModuleElementName = webModuleElementName;
239         }
240
241         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
242             String JavaDoc propName = evt.getPropertyName();
243             if (classPathProperty.equals(propName) || ClassPath.PROP_ROOTS.equals(propName)) {
244                 synchronized (this) {
245                     if (fsListener!=null) {
246                         fsListener.removePropertyChangeListener (this);
247                     }
248                 }
249                 rp.post (new Runnable JavaDoc () {
250                     public void run () {
251                         setKeys(getKeys());
252                     }
253                 });
254             }
255         }
256
257         protected void addNotify() {
258             this.eval.addPropertyChangeListener (this);
259             this.setKeys(getKeys ());
260         }
261
262         protected void removeNotify() {
263             this.eval.removePropertyChangeListener(this);
264             synchronized (this) {
265                 if (fsListener!=null) {
266                     fsListener.removePropertyChangeListener (this);
267                     fsListener = null;
268                 }
269             }
270             this.setKeys(Collections.EMPTY_SET);
271         }
272
273         protected Node[] createNodes(Object JavaDoc obj) {
274             Node[] result = null;
275             if (obj instanceof Key) {
276                 Key key = (Key) obj;
277                 switch (key.getType()) {
278                     case Key.TYPE_PLATFORM:
279                         result = new Node[] {PlatformNode.create(eval, platformProperty)};
280                         break;
281                     case Key.TYPE_J2EE_PLATFORM:
282                         Project p = FileOwnerQuery.getOwner(helper.getAntProjectHelper().getProjectDirectory());
283                         result = new Node[] {J2eePlatformNode.create(p, eval, j2eePlatformProperty)};
284                         break;
285                     case Key.TYPE_PROJECT:
286                         result = new Node[] {new ProjectNode(key.getProject(), key.getArtifactLocation(), helper, eval, refHelper, key.getClassPathId(),
287                             key.getEntryId(), webModuleElementName)};
288                         break;
289                     case Key.TYPE_LIBRARY:
290                         result = new Node[] {ActionFilterNode.create(PackageView.createPackageView(key.getSourceGroup()),
291                             helper, eval, refHelper, key.getClassPathId(), key.getEntryId(), webModuleElementName)};
292                         break;
293                 }
294             }
295             if (result == null) {
296                 assert false : "Unknown key type"; //NOI18N
297
result = new Node[0];
298             }
299             return result;
300         }
301         
302         private List JavaDoc getKeys () {
303             EditableProperties projectSharedProps = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
304             EditableProperties projectPrivateProps = helper.getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH);
305             EditableProperties privateProps = PropertyUtils.getGlobalProperties();
306             List JavaDoc/*<URL>*/ rootsList = new ArrayList JavaDoc ();
307             List JavaDoc result = getKeys (projectSharedProps, projectPrivateProps, privateProps, classPathProperty, rootsList);
308             if (platformProperty!=null) {
309                 result.add (new Key());
310             }
311             if (j2eePlatformProperty != null) {
312                 result.add(new Key(true));
313             }
314             //XXX: Workaround: Remove this when there will be API for listening on nonexistent files
315
// See issue: http://www.netbeans.org/issues/show_bug.cgi?id=33162
316
ClassPath cp = ClassPathSupport.createClassPath ((URL JavaDoc[])rootsList.toArray(new URL JavaDoc[rootsList.size()]));
317             cp.addPropertyChangeListener (this);
318             cp.getRoots();
319             synchronized (this) {
320                 fsListener = cp;
321             }
322             return result;
323         }
324
325         private List JavaDoc getKeys (EditableProperties projectSharedProps, EditableProperties projectPrivateProps,
326                               EditableProperties privateProps, String JavaDoc currentClassPath, List JavaDoc/*<URL>*/ rootsList) {
327             List JavaDoc result = new ArrayList JavaDoc ();
328             String JavaDoc raw = projectSharedProps.getProperty (currentClassPath);
329             if (raw == null) {
330                 raw = projectPrivateProps.getProperty(currentClassPath);
331             }
332             if (raw == null) {
333                 raw = privateProps.getProperty(currentClassPath);
334             }
335             if (raw == null) {
336                 return result;
337             }
338             List JavaDoc pe = new ArrayList JavaDoc(Arrays.asList(PropertyUtils.tokenizePath( raw )));
339             while (pe.size()>0){
340                 String JavaDoc prop = (String JavaDoc) pe.remove(0);
341                 String JavaDoc propName = WebProjectProperties.getAntPropertyName (prop);
342                 if (classPathIgnoreRef.contains(propName)) {
343                     continue;
344                 }
345                 else if (prop.startsWith( LIBRARY_PREFIX )) {
346                     //Library reference
347
String JavaDoc eval = prop.substring( LIBRARY_PREFIX.length(), prop.lastIndexOf('.') ); //NOI18N
348
Library lib = LibraryManager.getDefault().getLibrary (eval);
349                     if (lib != null) {
350                         List JavaDoc/*<URL>*/ roots = lib.getContent("classpath"); //NOI18N
351
Icon JavaDoc libIcon = new ImageIcon JavaDoc (Utilities.loadImage(LIBRARIES_ICON));
352                         for (Iterator JavaDoc it = roots.iterator(); it.hasNext();) {
353                             URL JavaDoc rootUrl = (URL JavaDoc) it.next();
354                             rootsList.add (rootUrl);
355                             FileObject root = URLMapper.findFileObject (rootUrl);
356                             if (root != null) {
357                                 String JavaDoc displayName;
358                                 if ("jar".equals(rootUrl.getProtocol())) { //NOI18N
359
FileObject file = FileUtil.getArchiveFile (root);
360                                     displayName = file.getNameExt();
361                                 }
362                                 else {
363                                     File JavaDoc file = FileUtil.toFile (root);
364                                     if (file != null) {
365                                         displayName = file.getAbsolutePath();
366                                     }
367                                     else {
368                                         displayName = root.getNameExt();
369                                     }
370                                 }
371                                 displayName = MessageFormat.format (
372                                     NbBundle.getMessage (LibrariesNode.class,"TXT_LibraryPartFormat"),
373                                     new Object JavaDoc[] {lib.getDisplayName(), displayName});
374                                 SourceGroup sg = new LibrariesSourceGroup (root, displayName, libIcon, libIcon);
375                                 result.add (new Key(sg,currentClassPath, propName));
376                             }
377                         }
378                     }
379                     //Todo: May try to resolve even broken library
380
}
381                 else if (prop.startsWith(ANT_ARTIFACT_PREFIX)) {
382                     //Project reference
383
Object JavaDoc[] ref = refHelper.findArtifactAndLocation(prop);
384                     if (ref[0] != null && ref[1] != null) {
385                         AntArtifact artifact = (AntArtifact)ref[0];
386                         URI JavaDoc uri = (URI JavaDoc)ref[1];
387                         result.add(new Key(artifact, uri, currentClassPath, propName));
388                     }
389                 }
390                 else if (prop.startsWith(FILE_REF_PREFIX)) {
391                     //File reference
392
String JavaDoc evaluatedRef = eval.getProperty(propName);
393                     File JavaDoc file = helper.getAntProjectHelper().resolveFile(evaluatedRef);
394                     SourceGroup sg = createFileSourceGroup(file,rootsList);
395                     if (sg !=null) {
396                         result.add (new Key(sg,currentClassPath, propName));
397                     }
398                 }
399                 else if (prop.startsWith(REF_PREFIX)) {
400                     //Path reference
401
result.addAll(getKeys(projectSharedProps, projectPrivateProps, privateProps,propName, rootsList));
402                 }
403                 else {
404                     //file
405
File JavaDoc file = helper.getAntProjectHelper().resolveFile(prop);
406                     SourceGroup sg = createFileSourceGroup(file,rootsList);
407                     if (sg !=null) {
408                         result.add ( new Key(sg,currentClassPath, propName));
409                     }
410                 }
411             }
412             return result;
413         }
414
415         private static SourceGroup createFileSourceGroup (File JavaDoc file, List JavaDoc/*<URL>*/ rootsList) {
416             Icon JavaDoc icon;
417             Icon JavaDoc openedIcon;
418             String JavaDoc displayName;
419             try {
420                 URL JavaDoc url = file.toURI().toURL();
421                 if (FileUtil.isArchiveFile(url)) {
422                     url = FileUtil.getArchiveRoot(url);
423                     icon = openedIcon = new ImageIcon JavaDoc (Utilities.loadImage(ARCHIVE_ICON));
424                     displayName = file.getName();
425                 }
426                 else {
427                     String JavaDoc sURL = url.toExternalForm();
428                     if (!sURL.endsWith("/")) { //NOI18N
429
url = new URL JavaDoc (sURL+"/"); //NOI18N
430
}
431                     icon = getFolderIcon (false);
432                     openedIcon = getFolderIcon (true);
433                     displayName = file.getAbsolutePath();
434                 }
435                 rootsList.add (url);
436                 FileObject root = URLMapper.findFileObject (url);
437                 if (root != null) {
438                     return new LibrariesSourceGroup (root,displayName,icon,openedIcon);
439                 }
440             } catch (MalformedURLException JavaDoc e) {
441                 ErrorManager.getDefault().notify(e);
442             }
443             return null;
444         }
445     }
446
447     private static class Key {
448         static final int TYPE_PLATFORM = 0;
449         static final int TYPE_LIBRARY = 1;
450         static final int TYPE_PROJECT = 2;
451         static final int TYPE_J2EE_PLATFORM = 3;
452
453         private int type;
454         private String JavaDoc classPathId;
455         private String JavaDoc entryId;
456         private SourceGroup sg;
457         private AntArtifact antArtifact;
458         private URI JavaDoc uri;
459         
460         Key () {
461             this(false);
462         }
463
464         Key (boolean j2ee) {
465             this.type = j2ee ? TYPE_J2EE_PLATFORM : TYPE_PLATFORM;
466         }
467
468         Key (SourceGroup sg, String JavaDoc classPathId, String JavaDoc entryId) {
469             this.type = TYPE_LIBRARY;
470             this.sg = sg;
471             this.classPathId = classPathId;
472             this.entryId = entryId;
473         }
474
475         Key (AntArtifact a, URI JavaDoc uri, String JavaDoc classPathId, String JavaDoc entryId) {
476             this.type = TYPE_PROJECT;
477             this.antArtifact = a;
478             this.uri = uri;
479             this.classPathId = classPathId;
480             this.entryId = entryId;
481         }
482
483         public int getType () {
484             return this.type;
485         }
486
487         public String JavaDoc getClassPathId () {
488             return this.classPathId;
489         }
490
491         public String JavaDoc getEntryId () {
492             return this.entryId;
493         }
494
495         public SourceGroup getSourceGroup () {
496             return this.sg;
497         }
498
499         public AntArtifact getProject() {
500             return this.antArtifact;
501         }
502         
503         public URI JavaDoc getArtifactLocation() {
504             return this.uri;
505         }
506         
507         public int hashCode() {
508             int hashCode = this.type<<16;
509             switch (this.type) {
510                 case TYPE_LIBRARY:
511                     hashCode ^= this.sg == null ? 0 : this.sg.hashCode();
512                     break;
513                 case TYPE_PROJECT:
514                     hashCode ^= this.antArtifact == null ? 0 : this.antArtifact.hashCode();
515                     break;
516             }
517             return hashCode;
518         }
519
520         public boolean equals(Object JavaDoc obj) {
521             if (!(obj instanceof Key)) {
522                 return false;
523             }
524             Key other = (Key) obj;
525             if (other.type != type) {
526                 return false;
527             }
528             switch (type) {
529                 case TYPE_LIBRARY:
530                     return (this.sg == null ? other.sg == null : this.sg.equals(other.sg)) &&
531                         (this.classPathId == null ? other.classPathId == null : this.classPathId.equals (other.classPathId)) &&
532                         (this.entryId == null ? other.entryId == null : this.entryId.equals (other.entryId));
533                 case TYPE_PROJECT:
534                     return (this.antArtifact == null ? other.antArtifact == null : this.antArtifact.equals(other.antArtifact)) &&
535                         (this.classPathId == null ? other.classPathId == null : this.classPathId.equals (other.classPathId)) &&
536                         (this.entryId == null ? other.entryId == null : this.entryId.equals (other.entryId));
537                 case TYPE_PLATFORM:
538                 case TYPE_J2EE_PLATFORM:
539                     return true;
540                 default:
541                     throw new IllegalStateException JavaDoc();
542             }
543         }
544     }
545
546     private static class AddProjectAction extends AbstractAction JavaDoc {
547
548         private final Project project;
549         private final String JavaDoc classPathId;
550         private final String JavaDoc webModuleElementName;
551
552         public AddProjectAction (Project project, String JavaDoc classPathId, String JavaDoc webModuleElementName) {
553             super( NbBundle.getMessage( LibrariesNode.class, "LBL_AddProject_Action" ) );
554             this.project = project;
555             this.classPathId = classPathId;
556             this.webModuleElementName = webModuleElementName;
557         }
558
559         public void actionPerformed(ActionEvent JavaDoc e) {
560             ArtifactItem artifacts[] = AntArtifactChooser.showDialog(JavaProjectConstants.ARTIFACT_TYPE_JAR, project, null);
561                 if ( artifacts != null ) {
562                     addArtifacts( artifacts );
563                 }
564         }
565
566         private void addArtifacts (ArtifactItem[] artifactItems) {
567             WebProjectClassPathExtender cpExtender = (WebProjectClassPathExtender) project.getLookup().lookup(WebProjectClassPathExtender.class);
568             if (cpExtender != null) {
569                 try {
570                     cpExtender.addAntArtifacts(classPathId, artifactItems, webModuleElementName);
571                 } catch (IOException JavaDoc ioe) {
572                     ErrorManager.getDefault().notify(ioe);
573                 }
574             }
575             else {
576                 ErrorManager.getDefault().log ("WebProjectClassPathExtender not found in the project lookup of project: "+project.getProjectDirectory().getPath()); //NOI18N
577
}
578         }
579     }
580
581     private static class AddLibraryAction extends AbstractAction JavaDoc {
582
583         private final Project project;
584         private final String JavaDoc classPathId;
585         private final String JavaDoc webModuleElementName;
586
587         public AddLibraryAction (Project project, String JavaDoc classPathId, String JavaDoc webModuleElementName) {
588             super( NbBundle.getMessage( LibrariesNode.class, "LBL_AddLibrary_Action" ) );
589             this.project = project;
590             this.classPathId = classPathId;
591             this.webModuleElementName = webModuleElementName;
592         }
593
594         public void actionPerformed(ActionEvent JavaDoc e) {
595             Object JavaDoc[] options = new Object JavaDoc[] {
596                 new JButton JavaDoc (NbBundle.getMessage (LibrariesNode.class,"LBL_AddLibrary")),
597                 DialogDescriptor.CANCEL_OPTION
598             };
599             ((JButton JavaDoc)options[0]).setEnabled(false);
600             ((JButton JavaDoc)options[0]).getAccessibleContext().setAccessibleDescription (NbBundle.getMessage (WebClassPathUi.class,"AD_AddLibrary"));
601                         
602             WebModule wm = WebModule.getWebModule(project.getProjectDirectory());
603             String JavaDoc j2eeVersion = wm.getJ2eePlatformVersion();
604             LibrariesChooser panel = new LibrariesChooser ((JButton JavaDoc)options[0], j2eeVersion);
605             DialogDescriptor desc = new DialogDescriptor(panel,NbBundle.getMessage( LibrariesNode.class, "LBL_CustomizeCompile_Classpath_AddLibrary" ),
606                     true, options, options[0], DialogDescriptor.DEFAULT_ALIGN,null,null);
607             Dialog JavaDoc dlg = DialogDisplayer.getDefault().createDialog(desc);
608             dlg.setVisible(true);
609             if (desc.getValue() == options[0]) {
610                 addLibraries (panel.getSelectedLibraries());
611             }
612             dlg.dispose();
613         }
614
615         private void addLibraries (Library[] libraries) {
616             WebProjectClassPathExtender cpExtender = (WebProjectClassPathExtender) project.getLookup().lookup(WebProjectClassPathExtender.class);
617             if (cpExtender != null) {
618                 try {
619                     cpExtender.addLibraries(classPathId, libraries, webModuleElementName);
620                 } catch (IOException JavaDoc ioe) {
621                     ErrorManager.getDefault().notify(ioe);
622                 }
623             }
624             else {
625                 ErrorManager.getDefault().log ("WebProjectClassPathExtender not found in the project lookup of project: "+project.getProjectDirectory().getPath()); //NOI18N
626
}
627         }
628         
629     }
630
631     private static class AddFolderAction extends AbstractAction JavaDoc {
632
633         private final Project project;
634         private final String JavaDoc classPathId;
635         private final String JavaDoc webModuleElementName;
636
637         public AddFolderAction (Project project, String JavaDoc classPathId, String JavaDoc webModuleElementName) {
638             super( NbBundle.getMessage( LibrariesNode.class, "LBL_AddFolder_Action" ) );
639             this.project = project;
640             this.classPathId = classPathId;
641             this.webModuleElementName = webModuleElementName;
642         }
643
644         public void actionPerformed(ActionEvent JavaDoc e) {
645             JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
646             FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
647             chooser.setFileSelectionMode( JFileChooser.FILES_AND_DIRECTORIES );
648             chooser.setMultiSelectionEnabled( true );
649             chooser.setDialogTitle( NbBundle.getMessage( LibrariesNode.class, "LBL_AddJar_DialogTitle" ) ); // NOI18N
650
//#61789 on old macosx (jdk 1.4.1) these two method need to be called in this order.
651
chooser.setAcceptAllFileFilterUsed( false );
652             FileFilter JavaDoc fileFilter = new SimpleFileFilter (
653                     NbBundle.getMessage( WebClassPathUi.class, "LBL_ZipJarFolderFilter" )); // NOI18N
654
chooser.setFileFilter(fileFilter);
655             File JavaDoc curDir = FoldersListSettings.getDefault().getLastUsedClassPathFolder();
656             chooser.setCurrentDirectory (curDir);
657             int option = chooser.showOpenDialog( WindowManager.getDefault().getMainWindow() );
658             if ( option == JFileChooser.APPROVE_OPTION ) {
659                 File JavaDoc files[] = chooser.getSelectedFiles();
660                 addJarFiles( files, fileFilter );
661                 curDir = FileUtil.normalizeFile(chooser.getCurrentDirectory());
662                 FoldersListSettings.getDefault().setLastUsedClassPathFolder(curDir);
663             }
664         }
665
666         private void addJarFiles (File JavaDoc[] files, FileFilter JavaDoc fileFilter) {
667             WebProjectClassPathExtender cpExtender = (WebProjectClassPathExtender) project.getLookup().lookup(WebProjectClassPathExtender.class);
668             if (cpExtender != null) {
669                 List JavaDoc fileObjects = new LinkedList JavaDoc();
670                 for (int i = 0; i < files.length; i++) {
671                     if (fileFilter.accept(files[i])) {
672                         FileObject fo = FileUtil.toFileObject (files[i]);
673                         assert fo != null : files[i];
674                         fileObjects.add(fo);
675                     }
676                 }
677                 try {
678                     FileObject[] fileObjectArray = new FileObject[fileObjects.size()];
679                     fileObjects.toArray(fileObjectArray);
680                     cpExtender.addArchiveFiles(classPathId, fileObjectArray, webModuleElementName);
681                 } catch (IOException JavaDoc ioe) {
682                     ErrorManager.getDefault().notify(ioe);
683                 }
684             }
685             else {
686                 ErrorManager.getDefault().log ("WebProjectClassPathExtender not found in the project lookup of project: "+project.getProjectDirectory().getPath()); //NOI18N
687
}
688         }
689
690     }
691
692     private static class SimpleFileFilter extends FileFilter JavaDoc {
693
694         private String JavaDoc description;
695
696
697         public SimpleFileFilter (String JavaDoc description) {
698             this.description = description;
699         }
700
701         public boolean accept(File JavaDoc f) {
702             if (f.isDirectory())
703                 return true;
704             try {
705                 return FileUtil.isArchiveFile(f.toURI().toURL());
706             } catch (MalformedURLException JavaDoc mue) {
707                 ErrorManager.getDefault().notify(mue);
708                 return false;
709             }
710         }
711
712         public String JavaDoc getDescription() {
713             return this.description;
714         }
715     }
716 }
717
Popular Tags