KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > earproject > ui > J2eeArchiveLogicalViewProvider


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.earproject.ui;
21
22 import java.awt.Image JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.beans.PropertyChangeEvent JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import java.io.CharConversionException JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.HashSet JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.ResourceBundle JavaDoc;
37 import java.util.Set JavaDoc;
38 import java.util.StringTokenizer JavaDoc;
39 import javax.swing.AbstractAction JavaDoc;
40 import javax.swing.Action JavaDoc;
41 import javax.swing.JSeparator JavaDoc;
42 import javax.swing.event.ChangeEvent JavaDoc;
43 import javax.swing.event.ChangeListener JavaDoc;
44 import org.netbeans.api.java.platform.JavaPlatformManager;
45 import org.netbeans.api.project.FileOwnerQuery;
46 import org.netbeans.api.project.Project;
47 import org.netbeans.api.project.ProjectUtils;
48 import org.netbeans.api.project.SourceGroup;
49 import org.netbeans.api.project.Sources;
50 import org.netbeans.modules.j2ee.api.ejbjar.EjbProjectConstants;
51 import org.netbeans.modules.j2ee.common.ui.BrokenServerSupport;
52 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
53 import org.netbeans.modules.j2ee.deployment.devmodules.spi.InstanceListener;
54 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
55 import org.netbeans.modules.j2ee.earproject.BrokenProjectSupport;
56 import org.netbeans.modules.j2ee.earproject.EarProject;
57 import org.netbeans.modules.j2ee.earproject.UpdateHelper;
58 import org.netbeans.modules.j2ee.earproject.ui.customizer.EarProjectProperties;
59 import org.netbeans.spi.java.project.support.ui.BrokenReferencesSupport;
60 import org.netbeans.spi.java.project.support.ui.PackageView;
61 import org.netbeans.spi.project.ActionProvider;
62 import org.netbeans.spi.project.support.ant.AntBasedProjectType;
63 import org.netbeans.spi.project.support.ant.AntProjectHelper;
64 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
65 import org.netbeans.spi.project.support.ant.ReferenceHelper;
66 import org.netbeans.spi.project.ui.LogicalViewProvider;
67 import org.netbeans.spi.project.ui.support.CommonProjectActions;
68 import org.netbeans.spi.project.ui.support.DefaultProjectOperations;
69 import org.netbeans.spi.project.ui.support.ProjectSensitiveActions;
70 import org.openide.ErrorManager;
71 import org.openide.actions.FindAction;
72 import org.openide.actions.ToolsAction;
73 import org.openide.filesystems.FileObject;
74 import org.openide.filesystems.FileStateInvalidException;
75 import org.openide.filesystems.FileStatusEvent;
76 import org.openide.filesystems.FileStatusListener;
77 import org.openide.filesystems.FileSystem;
78 import org.openide.filesystems.FileUtil;
79 import org.openide.filesystems.Repository;
80 import org.openide.loaders.DataFolder;
81 import org.openide.loaders.DataObject;
82 import org.openide.loaders.DataObjectNotFoundException;
83 import org.openide.loaders.FolderLookup;
84 import org.openide.nodes.AbstractNode;
85 import org.openide.nodes.Node;
86 import org.openide.nodes.NodeOp;
87 import org.openide.util.ContextAwareAction;
88 import org.openide.util.HelpCtx;
89 import org.openide.util.Lookup;
90 import org.openide.util.NbBundle;
91 import org.openide.util.RequestProcessor;
92 import org.openide.util.Utilities;
93 import org.openide.util.WeakListeners;
94 import org.openide.util.actions.SystemAction;
95 import org.openide.util.lookup.Lookups;
96 import org.openide.xml.XMLUtil;
97
98 /**
99  * Support for creating logical views.
100  */

101 public class J2eeArchiveLogicalViewProvider implements LogicalViewProvider {
102     
103     private final EarProject project;
104     protected final UpdateHelper helper;
105     private final PropertyEvaluator evaluator;
106     protected final ReferenceHelper resolver;
107     private final List JavaDoc<? extends Action JavaDoc> specialActions;
108     private final AntBasedProjectType abpt;
109     
110     public J2eeArchiveLogicalViewProvider(EarProject project, UpdateHelper helper,
111             PropertyEvaluator evaluator, ReferenceHelper resolver,
112             List JavaDoc<? extends Action JavaDoc> specialActions, AntBasedProjectType abpt) {
113         this.project = project;
114         assert project != null;
115         this.helper = helper;
116         assert helper != null;
117         this.evaluator = evaluator;
118         assert evaluator != null;
119         this.resolver = resolver;
120         this.specialActions = specialActions;
121         this.abpt = abpt;
122     }
123     
124     public Node createLogicalView() {
125         return new ArchiveLogicalViewRootNode();
126     }
127     
128     public Node findPath(Node root, Object JavaDoc target) {
129         Project project = (Project) root.getLookup().lookup(Project.class);
130         if (project == null) {
131             return null;
132         }
133         
134         // XXX this is incorrect - must handle case that target instanceof DataObject
135
// (since that is likely to be what is in lookup)
136
if (target instanceof FileObject) {
137             FileObject fo = (FileObject) target;
138             Project owner = FileOwnerQuery.getOwner(fo);
139             if (!project.equals(owner)) {
140                 return null; // Don't waste time if project does not own the fo
141
}
142             // trying to find node in docbase
143
Node result = findNodeUnderConfiguration(root, fo);
144             if (result != null) {
145                 return result;
146             }
147             // trying to find node in sources
148
Node[] nodes = root.getChildren().getNodes(true);
149             for (int i = nodes.length-1; i >= 0; i--) {
150                 result = PackageView.findPath(nodes[i], target);
151                 if (result!=null) {
152                     return result;
153                 }
154             }
155         }
156         return null;
157     }
158     
159     private Node findNodeUnderConfiguration(Node root, FileObject fo) {
160         FileObject rootfo = helper.getAntProjectHelper().resolveFileObject(evaluator.getProperty(EarProjectProperties.META_INF));
161         String JavaDoc relPath = FileUtil.getRelativePath(rootfo, fo);
162         if (relPath == null) {
163             return null;
164         }
165         int idx = relPath.indexOf('.'); //NOI18N
166
if (idx != -1) {
167             relPath = relPath.substring(0, idx);
168         }
169         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(relPath, "/"); //NOI18N
170
Node result = NodeOp.findChild(root,rootfo.getName());
171         while (st.hasMoreTokens()) {
172             result = NodeOp.findChild(result, st.nextToken());
173         }
174         
175         return result;
176     }
177     
178     private static Lookup createLookup( Project project, AntProjectHelper c ) {
179         DataFolder rootFolder = DataFolder.findFolder( project.getProjectDirectory() );
180         // XXX Remove root folder after FindAction rewrite
181
Lookup ret = null;
182         if (null == c) {
183             ret = Lookups.fixed( new Object JavaDoc[] { project, rootFolder });
184         } else {
185             ret = Lookups.fixed( new Object JavaDoc[] { project, rootFolder, c } );
186         }
187         return ret;
188     }
189     
190     // Private innerclasses ----------------------------------------------------
191

192     private static final String JavaDoc[] BREAKABLE_PROPERTIES = new String JavaDoc[] {
193         EarProjectProperties.JAVAC_CLASSPATH,
194         EarProjectProperties.DEBUG_CLASSPATH,
195         EarProjectProperties.JAR_CONTENT_ADDITIONAL,
196     };
197     
198     public static boolean hasBrokenLinks(AntProjectHelper helper, ReferenceHelper resolver) {
199         return BrokenReferencesSupport.isBroken(helper, resolver, BREAKABLE_PROPERTIES,
200                 new String JavaDoc[] { EarProjectProperties.JAVA_PLATFORM});
201     }
202     
203     private String JavaDoc getIconBase() {
204         IconBaseProvider ibp = (IconBaseProvider) project.getLookup().lookup(IconBaseProvider.class);
205         return (null == ibp)
206                 ? "org/netbeans/modules/j2ee/earproject/ui/resources/" // NOI18N
207
: ibp.getIconBase();
208     }
209     
210     /** Package private for unit test only. */
211     final class ArchiveLogicalViewRootNode extends AbstractNode implements Runnable JavaDoc, FileStatusListener, ChangeListener JavaDoc, PropertyChangeListener JavaDoc {
212         
213         private static final String JavaDoc BROKEN_PROJECT_BADGE = "org/netbeans/modules/j2ee/earproject/ui/resources/brokenProjectBadge.gif"; // NOI18N
214

215         private Action JavaDoc brokenLinksAction;
216         private final BrokenServerAction brokenServerAction;
217         private final BrokenProjectSupport brokenProjectSupport;
218         private boolean broken;
219         
220         // icon badging >>>
221
private Set JavaDoc<FileObject> files;
222         private Map JavaDoc<FileSystem, FileStatusListener> fileSystemListeners;
223         private RequestProcessor.Task task;
224         private final Object JavaDoc privateLock = new Object JavaDoc();
225         private boolean iconChange;
226         private boolean nameChange;
227         private ChangeListener JavaDoc sourcesListener;
228         private Map JavaDoc<SourceGroup, PropertyChangeListener JavaDoc> groupsListeners;
229         // icon badging <<<
230

231         public ArchiveLogicalViewRootNode() {
232             super(new ArchiveViews.LogicalViewChildren(project, helper.getAntProjectHelper(), evaluator),
233                     createLookup(project, helper.getAntProjectHelper()));
234             setIconBaseWithExtension(getIconBase() + "projectIcon.gif"); // NOI18N
235
super.setName( ProjectUtils.getInformation( project ).getDisplayName() );
236             if (hasBrokenLinks(helper.getAntProjectHelper(), resolver)) {
237                 broken = true;
238             }
239             brokenServerAction = new BrokenServerAction();
240             J2eeModuleProvider moduleProvider = (J2eeModuleProvider)project.getLookup().lookup(J2eeModuleProvider.class);
241             moduleProvider.addInstanceListener((InstanceListener)WeakListeners.create(
242                     InstanceListener.class, brokenServerAction, moduleProvider));
243             refreshProjectFiles();
244             this.brokenProjectSupport = (BrokenProjectSupport)
245                     project.getLookup().lookup(BrokenProjectSupport.class);
246             this.brokenProjectSupport.addChangeListener(new ChangeListener JavaDoc() {
247                 public void stateChanged(ChangeEvent JavaDoc e) {
248                     checkProjectValidity();
249                 }
250             });
251         }
252         
253         private void refreshProjectFiles() {
254             setFiles(getProjectFiles());
255         }
256         
257         /** Package private for unit test only. */
258         Set JavaDoc<FileObject> getProjectFiles() {
259             Sources sources = ProjectUtils.getSources(project); // returns singleton
260
if (sourcesListener == null) {
261                 sourcesListener = WeakListeners.change(this, sources);
262                 sources.addChangeListener(sourcesListener);
263             }
264             return getProjectFiles(Arrays.asList(sources.getSourceGroups(Sources.TYPE_GENERIC)));
265         }
266         
267         private Set JavaDoc<FileObject> getProjectFiles(Collection JavaDoc<SourceGroup> groups) {
268             if (groupsListeners != null) {
269                 for (SourceGroup group : groupsListeners.keySet()) {
270                     PropertyChangeListener JavaDoc pcl = groupsListeners.get(group);
271                     group.removePropertyChangeListener(pcl);
272                 }
273             }
274             groupsListeners = new HashMap JavaDoc<SourceGroup, PropertyChangeListener JavaDoc>();
275             Set JavaDoc<FileObject> files = new HashSet JavaDoc<FileObject>();
276             for (SourceGroup group : groups) {
277                 PropertyChangeListener JavaDoc pcl = WeakListeners.propertyChange(this, group);
278                 groupsListeners.put(group, pcl);
279                 group.addPropertyChangeListener(pcl);
280                 FileObject groupRoot = group.getRootFolder();
281                 if (project.getProjectDirectory().equals(groupRoot)) {
282                     // add rather children of project's root folder than the
283
// folder itself (cf. #78994)
284
Enumeration JavaDoc en = project.getProjectDirectory().getChildren(false);
285                     while (en.hasMoreElements()) {
286                         FileObject child = (FileObject) en.nextElement();
287                         if (FileOwnerQuery.getOwner(child) == project) {
288                             files.add(child);
289                         }
290                     }
291                 } else { // add group's root (may be tweaked if needed)
292
files.add(groupRoot);
293                 }
294             }
295             return files;
296         }
297         
298         private final void setFiles(Set JavaDoc<FileObject> files) {
299             if (fileSystemListeners != null) {
300                 for (FileSystem fs : fileSystemListeners.keySet()) {
301                     FileStatusListener fsl = fileSystemListeners.get(fs);
302                     fs.removeFileStatusListener(fsl);
303                 }
304             }
305             
306             fileSystemListeners = new HashMap JavaDoc<FileSystem, FileStatusListener>();
307             this.files = files;
308             if (files == null) {
309                 return;
310             }
311             
312             Set JavaDoc<FileSystem> hookedFileSystems = new HashSet JavaDoc<FileSystem>();
313             for (FileObject fo : files) {
314                 try {
315                     FileSystem fs = fo.getFileSystem();
316                     if (hookedFileSystems.contains(fs)) {
317                         continue;
318                     }
319                     hookedFileSystems.add(fs);
320                     FileStatusListener fsl = FileUtil.weakFileStatusListener(this, fs);
321                     fs.addFileStatusListener(fsl);
322                     fileSystemListeners.put(fs, fsl);
323                 } catch (FileStateInvalidException e) {
324                     ErrorManager err = ErrorManager.getDefault();
325                     err.annotate(e, "Can not get " + fo + " filesystem, ignoring..."); // NO18N // NOI18N
326
err.notify(ErrorManager.INFORMATIONAL, e);
327                 }
328             }
329         }
330         
331         private synchronized void checkProjectValidity() {
332             boolean old = broken;
333             broken = brokenProjectSupport.hasBrokenArtifacts();
334             if (!broken) {
335                 broken = hasBrokenLinks(helper.getAntProjectHelper(), resolver);
336             }
337             if (old != broken) {
338                 getBrokenLinksAction().setEnabled(broken);
339                 fireIconChange();
340                 fireOpenedIconChange();
341                 fireDisplayNameChange(null, null);
342             }
343         }
344         
345         public Action JavaDoc getBrokenLinksAction() {
346             if (broken && brokenLinksAction == null) {
347                 brokenLinksAction = new BrokenLinksAction();
348             }
349             return brokenLinksAction;
350         }
351         
352         public String JavaDoc getHtmlDisplayName() {
353             String JavaDoc dispName = super.getDisplayName();
354             try {
355                 dispName = XMLUtil.toElementContent(dispName);
356             } catch (CharConversionException JavaDoc ex) {
357                 // ignore
358
}
359             return broken || brokenServerAction.isEnabled() ? "<font color=\"#A40000\">" + dispName + "</font>" : null; //NOI18N
360
}
361         
362         public Image JavaDoc getIcon(int type) {
363             Image JavaDoc img = getMyIcon(type);
364             
365             if (files != null && files.iterator().hasNext()) {
366                 try {
367                     FileObject fo = (FileObject) files.iterator().next();
368                     img = fo.getFileSystem().getStatus().annotateIcon(img, type, files);
369                 } catch (FileStateInvalidException e) {
370                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
371                 }
372             }
373             
374             return img;
375         }
376         
377         public Image JavaDoc getOpenedIcon(int type) {
378             Image JavaDoc img = getMyOpenedIcon(type);
379             
380             if (files != null && files.iterator().hasNext()) {
381                 try {
382                     FileObject fo = (FileObject) files.iterator().next();
383                     img = fo.getFileSystem().getStatus().annotateIcon(img, type, files);
384                 } catch (FileStateInvalidException e) {
385                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
386                 }
387             }
388             
389             return img;
390         }
391         
392         public Action JavaDoc[] getActions( boolean context ) {
393             return context ? super.getActions(true) : getAdditionalActions();
394         }
395         
396         public boolean canRename() {
397             return true;
398         }
399         
400         public void setName(String JavaDoc s) {
401             DefaultProjectOperations.performDefaultRenameOperation(project, s);
402         }
403         
404         public void run() {
405             boolean fireIcon;
406             boolean fireName;
407             synchronized (privateLock) {
408                 fireIcon = iconChange;
409                 fireName = nameChange;
410                 iconChange = false;
411                 nameChange = false;
412             }
413             if (fireIcon) {
414                 fireIconChange();
415                 fireOpenedIconChange();
416             }
417             if (fireName) {
418                 fireDisplayNameChange(null, null);
419             }
420         }
421         
422         public void annotationChanged(FileStatusEvent event) {
423             if (task == null) {
424                 task = RequestProcessor.getDefault().create(this);
425             }
426             
427             synchronized (privateLock) {
428                 if ((iconChange == false && event.isIconChange()) || (nameChange == false && event.isNameChange())) {
429                     for (FileObject fo : files) {
430                         if (event.hasChanged(fo)) {
431                             iconChange |= event.isIconChange();
432                             nameChange |= event.isNameChange();
433                         }
434                     }
435                 }
436             }
437             
438             task.schedule(50); // batch by 50 ms
439
}
440         
441         // sources change
442
public void stateChanged(ChangeEvent JavaDoc e) {
443             refreshProjectFiles();
444         }
445         
446         // group change
447
public void propertyChange(PropertyChangeEvent JavaDoc evt) {
448             refreshProjectFiles();
449         }
450         
451         public Image JavaDoc getMyIcon(int type) {
452             Image JavaDoc original = super.getIcon( type );
453             return broken || brokenServerAction.isEnabled()
454             ? Utilities.mergeImages(original, Utilities.loadImage(BROKEN_PROJECT_BADGE), 8, 0)
455             : original;
456         }
457         
458         public Image JavaDoc getMyOpenedIcon(int type) {
459             Image JavaDoc original = super.getOpenedIcon(type);
460             return broken || brokenServerAction.isEnabled()
461             ? Utilities.mergeImages(original, Utilities.loadImage(BROKEN_PROJECT_BADGE), 8, 0)
462             : original;
463         }
464         
465         public HelpCtx getHelpCtx() {
466             return new HelpCtx(ArchiveLogicalViewRootNode.class);
467         }
468         
469         // Private methods -------------------------------------------------
470

471         private Action JavaDoc[] getAdditionalActions() {
472             
473             ResourceBundle JavaDoc bundle = NbBundle.getBundle(J2eeArchiveLogicalViewProvider.class);
474             
475             J2eeModuleProvider provider = (J2eeModuleProvider) project.getLookup().lookup(J2eeModuleProvider.class);
476             List JavaDoc<Action JavaDoc> actions = new ArrayList JavaDoc<Action JavaDoc>();
477             actions.addAll(specialActions);
478             actions.addAll(Arrays.asList(new Action JavaDoc[] {
479                 null,
480                 ProjectSensitiveActions.projectCommandAction( ActionProvider.COMMAND_BUILD, bundle.getString( "LBL_BuildAction_Name" ), null ), // NOI18N
481
ProjectSensitiveActions.projectCommandAction( ActionProvider.COMMAND_REBUILD, bundle.getString( "LBL_RebuildAction_Name" ), null ), // NOI18N
482
ProjectSensitiveActions.projectCommandAction( ActionProvider.COMMAND_CLEAN, bundle.getString( "LBL_CleanAction_Name" ), null ), // NOI18N
483
}));
484             if (provider != null && provider.hasVerifierSupport()) {
485                 actions.add(ProjectSensitiveActions.projectCommandAction( "verify", bundle.getString( "LBL_VerifyAction_Name" ), null )); // NOI18N
486
}
487             actions.addAll(Arrays.asList(new Action JavaDoc[] {
488                 null,
489                 ProjectSensitiveActions.projectCommandAction( ActionProvider.COMMAND_RUN, bundle.getString( "LBL_RunAction_Name" ), null ), // NOI18N
490
ProjectSensitiveActions.projectCommandAction( ActionProvider.COMMAND_DEBUG, bundle.getString( "LBL_DebugAction_Name" ), null ), // NOI18N
491
ProjectSensitiveActions.projectCommandAction( EjbProjectConstants.COMMAND_REDEPLOY, bundle.getString( "LBL_DeployAction_Name" ), null ), // NOI18N
492
null,
493                 CommonProjectActions.setAsMainProjectAction(),
494                 CommonProjectActions.openSubprojectsAction(),
495                 CommonProjectActions.closeProjectAction(),
496                 null,
497                 CommonProjectActions.renameProjectAction(),
498                 CommonProjectActions.moveProjectAction(),
499                 CommonProjectActions.copyProjectAction(),
500                 CommonProjectActions.deleteProjectAction(),
501                 null,
502                 SystemAction.get( FindAction.class ),
503                 null,
504             }));
505             
506            try {
507                 Repository repository = Repository.getDefault();
508                 FileSystem sfs = repository.getDefaultFileSystem();
509                 FileObject fo = sfs.findResource("Projects/Actions"); // NOI18N
510
if (fo != null) {
511                     DataObject dobj = DataObject.find(fo);
512                     FolderLookup actionRegistry = new FolderLookup((DataFolder)dobj);
513                     Lookup.Template query = new Lookup.Template(Object JavaDoc.class);
514                     Lookup lookup = actionRegistry.getLookup();
515                     Iterator JavaDoc it = lookup.lookup(query).allInstances().iterator();
516                     if (it.hasNext()) {
517                         actions.add(null);
518                     }
519                     while (it.hasNext()) {
520                         Object JavaDoc next = it.next();
521                         if (next instanceof Action JavaDoc) {
522                             actions.add((Action JavaDoc) next);
523                         } else if (next instanceof JSeparator JavaDoc) {
524                             actions.add(null);
525                         }
526                     }
527                 }
528             } catch (DataObjectNotFoundException ex) {
529                 // data folder for existing fileobject expected
530
ErrorManager.getDefault().notify(ex);
531             }
532             
533             actions.add(null);
534             actions.add(SystemAction.get(ToolsAction.class));
535             actions.add(null);
536             
537             if (broken) {
538                 actions.add(getBrokenLinksAction());
539             }
540             if (brokenServerAction.isEnabled()) {
541                 actions.add(brokenServerAction);
542             }
543             actions.add(CommonProjectActions.customizeProjectAction());
544             return (Action JavaDoc[])actions.toArray(new Action JavaDoc[actions.size()]);
545         }
546         
547         /** This action is created only when project has broken references.
548          * Once these are resolved the action is disabled.
549          */

550         private class BrokenLinksAction extends AbstractAction JavaDoc implements PropertyChangeListener JavaDoc, Runnable JavaDoc {
551             
552             private RequestProcessor.Task task = null;
553             private final PropertyChangeListener JavaDoc weakPCL;
554             
555             public BrokenLinksAction() {
556                 evaluator.addPropertyChangeListener(WeakListeners.propertyChange(this, evaluator));
557                 putValue(Action.NAME, NbBundle.getMessage(J2eeArchiveLogicalViewProvider.class, "LBL_Fix_Broken_Links_Action"));
558                 weakPCL = WeakListeners.propertyChange( this, JavaPlatformManager.getDefault() );
559                 JavaPlatformManager.getDefault().addPropertyChangeListener( weakPCL );
560             }
561             
562             public void actionPerformed(ActionEvent JavaDoc e) {
563                 BrokenReferencesSupport.showCustomizer(helper.getAntProjectHelper(), resolver, BREAKABLE_PROPERTIES, new String JavaDoc[]{ EarProjectProperties.JAVA_PLATFORM});
564                 brokenProjectSupport.adjustReferences();
565                 checkProjectValidity();
566             }
567             
568             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
569                 // check project state whenever there was a property change
570
// or change in list of platforms.
571
// Coalesce changes since they can come quickly:
572
if (task == null) {
573                     task = RequestProcessor.getDefault().create(this);
574                 }
575                 task.schedule(100);
576             }
577             
578             public void run() {
579                 checkProjectValidity();
580             }
581             
582         }
583         
584         private class BrokenServerAction extends AbstractAction JavaDoc implements
585                 InstanceListener, PropertyChangeListener JavaDoc {
586             
587             private boolean brokenServer;
588             
589             public BrokenServerAction() {
590                 putValue(Action.NAME, NbBundle.getMessage(J2eeArchiveLogicalViewProvider.class, "LBL_Fix_Missing_Server_Action")); // NOI18N
591
evaluator.addPropertyChangeListener(WeakListeners.propertyChange(this, evaluator));
592                 checkMissingServer();
593             }
594             
595             public boolean isEnabled() {
596                 return brokenServer;
597             }
598             
599             public void actionPerformed(ActionEvent JavaDoc e) {
600                 EarProjectProperties app = new EarProjectProperties(project, resolver, abpt);
601                 String JavaDoc j2eeSpec = (String JavaDoc) app.get(EarProjectProperties.J2EE_PLATFORM);
602                 String JavaDoc instance = BrokenServerSupport.selectServer(j2eeSpec, J2eeModule.EAR);
603                 if (instance != null) {
604                     app.put(EarProjectProperties.J2EE_SERVER_INSTANCE, instance);
605                     app.store();
606                 }
607                 checkMissingServer();
608             }
609             
610             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
611                 if (EarProjectProperties.J2EE_SERVER_INSTANCE.equals(evt.getPropertyName())) {
612                     checkMissingServer();
613                 }
614             }
615             
616             public void changeDefaultInstance(String JavaDoc oldServerInstanceID, String JavaDoc newServerInstanceID) {
617             }
618             
619             public void instanceAdded(String JavaDoc serverInstanceID) {
620                 checkMissingServer();
621             }
622             
623             public void instanceRemoved(String JavaDoc serverInstanceID) {
624                 checkMissingServer();
625             }
626             
627             private void checkMissingServer() {
628                 boolean old = brokenServer;
629                 String JavaDoc serverInstanceID = helper.getAntProjectHelper().getStandardPropertyEvaluator().getProperty(EarProjectProperties.J2EE_SERVER_INSTANCE);
630                 brokenServer = BrokenServerSupport.isBroken(serverInstanceID);
631                 if (old != brokenServer) {
632                     fireIconChange();
633                     fireOpenedIconChange();
634                     fireDisplayNameChange(null, null);
635                 }
636             }
637         }
638         
639     }
640     
641     /** Factory for project actions.<BR>
642      * XXX This class is a candidate for move to org.netbeans.spi.project.ui.support
643      */

644     public static class Actions {
645         
646         private Actions() {} // This is a factory
647

648         public static Action JavaDoc createAction( String JavaDoc key, String JavaDoc name, boolean global ) {
649             return new ActionImpl( key, name, global ? Utilities.actionsGlobalContext() : null );
650         }
651         
652         private static class ActionImpl extends AbstractAction JavaDoc implements ContextAwareAction {
653             
654             Lookup context;
655             String JavaDoc name;
656             String JavaDoc command;
657             
658             public ActionImpl( String JavaDoc command, String JavaDoc name, Lookup context ) {
659                 super( name );
660                 this.context = context;
661                 this.command = command;
662                 this.name = name;
663             }
664             
665             public void actionPerformed( ActionEvent JavaDoc e ) {
666                 
667                 Project project = (Project)context.lookup( Project.class );
668                 ActionProvider ap = (ActionProvider)project.getLookup().lookup( ActionProvider.class);
669                 
670                 ap.invokeAction( command, context );
671                 
672             }
673             
674             public Action JavaDoc createContextAwareInstance( Lookup lookup ) {
675                 return new ActionImpl( command, name, lookup );
676             }
677         }
678         
679     }
680     
681 }
682
Popular Tags