KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbjarproject > ui > logicalview > libraries > 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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.ejbjarproject.ui.logicalview.libraries;
21
22
23 import java.awt.Dialog JavaDoc;
24 import java.awt.Image JavaDoc;
25 import java.awt.event.ActionEvent JavaDoc;
26 import java.beans.BeanInfo JavaDoc;
27 import java.beans.PropertyChangeEvent JavaDoc;
28 import java.beans.PropertyChangeListener JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.File JavaDoc;
31 import java.net.MalformedURLException JavaDoc;
32 import java.net.URI JavaDoc;
33 import java.net.URL JavaDoc;
34 import java.text.MessageFormat JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Arrays JavaDoc;
37 import java.util.Collection JavaDoc;
38 import java.util.Collections JavaDoc;
39 import java.util.HashSet JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.LinkedList JavaDoc;
42 import java.util.List JavaDoc;
43 import java.util.Set JavaDoc;
44 import javax.swing.AbstractAction JavaDoc;
45 import javax.swing.Action JavaDoc;
46 import javax.swing.Icon JavaDoc;
47 import javax.swing.ImageIcon JavaDoc;
48 import javax.swing.JButton JavaDoc;
49 import javax.swing.JFileChooser JavaDoc;
50 import javax.swing.filechooser.FileFilter JavaDoc;
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.modules.j2ee.ejbjarproject.ui.FoldersListSettings;
81 import org.netbeans.modules.j2ee.ejbjarproject.UpdateHelper;
82 import org.netbeans.modules.j2ee.ejbjarproject.classpath.EjbJarProjectClassPathExtender;
83 import org.netbeans.modules.j2ee.ejbjarproject.ui.customizer.AntArtifactChooser;
84 import org.netbeans.modules.j2ee.ejbjarproject.ui.customizer.EjbJarClassPathUi;
85 import org.netbeans.modules.j2ee.ejbjarproject.ui.customizer.EjbJarProjectProperties;
86 import org.netbeans.modules.j2ee.ejbjarproject.ui.customizer.LibrariesChooser;
87 import org.openide.util.lookup.Lookups;
88
89
90
91
92
93 /**
94  * LibrariesNode displays the content of classpath and optionaly Java platform.
95  * @author Tomas Zezula
96  */

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

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

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

198         private static final String JavaDoc LIBRARY_PREFIX = "${libs."; // NOI18N
199

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

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

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

211         private static final String JavaDoc REF_PREFIX = "${"; //NOI18N
212

213         private static final String JavaDoc LIBRARIES_ICON = "org/netbeans/modules/j2ee/ejbjarproject/ui/resources/libraries.gif"; //NOI18N
214
private static final String JavaDoc ARCHIVE_ICON = "org/netbeans/modules/j2ee/ejbjarproject/ui/resources/jar.gif";//NOI18N
215

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