KickJava   Java API By Example, From Geeks To Geeks.

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

19
20 package org.netbeans.modules.j2ee.clientproject.ui;
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.util.lookup.Lookups;
66 import org.openide.windows.WindowManager;
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.api.project.FileOwnerQuery;
75 import org.netbeans.spi.project.support.ant.AntProjectHelper;
76 import org.netbeans.spi.project.support.ant.EditableProperties;
77 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
78 import org.netbeans.spi.project.support.ant.PropertyUtils;
79 import org.netbeans.spi.project.support.ant.ReferenceHelper;
80 import org.netbeans.spi.java.project.support.ui.PackageView;
81 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
82 import org.netbeans.modules.j2ee.clientproject.UpdateHelper;
83 import org.netbeans.modules.j2ee.clientproject.classpath.AppClientProjectClassPathExtender;
84 import org.netbeans.modules.j2ee.clientproject.ui.customizer.AntArtifactChooser;
85 import org.netbeans.modules.j2ee.clientproject.ui.customizer.AppClientClassPathUi;
86 import org.netbeans.modules.j2ee.clientproject.ui.customizer.LibrariesChooser;
87
88
89
90
91 /**
92  * LibrariesNode displays the content of classpath and optionaly Java platform.
93  * @author Tomas Zezula
94  */

95 final class LibrariesNode extends AbstractNode {
96
97     private static final Image JavaDoc ICON_BADGE = Utilities.loadImage("org/netbeans/modules/j2ee/clientproject/ui/resources/libraries-badge.png"); //NOI18N
98
static final RequestProcessor rp = new RequestProcessor ();
99     private static Icon JavaDoc folderIconCache;
100     private static Icon JavaDoc openedFolderIconCache;
101
102     private final String JavaDoc displayName;
103     private final Action JavaDoc[] librariesNodeActions;
104
105
106     /**
107      * Creates new LibrariesNode named displayName displaying classPathProperty classpath
108      * and optionaly Java platform.
109      * @param displayName the display name of the node
110      * @param project the project owning this node, the proejct is placed into LibrariesNode's lookup
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 J2SE 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     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 = ((ImageIcon JavaDoc)icon).getImage();
188         image = Utilities.mergeImages(image, ICON_BADGE, 7, 7 );
189         return image;
190     }
191
192     //Static inner classes
193
private static class LibrariesChildren extends Children.Keys implements PropertyChangeListener JavaDoc {
194
195         
196         /**
197          * Constant represneting a prefix of library reference generated by {@link org.netbeans.modules.java.j2seplatform.libraries.J2SELibraryTypeProvider}
198          */

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

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

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

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

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

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

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