KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbjarproject > ui > EjbJarLogicalViewProvider


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;
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
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.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
46 import org.netbeans.modules.j2ee.ejbjarproject.SourceRoots;
47 import org.netbeans.modules.j2ee.ejbjarproject.ui.logicalview.LogicalViewChildren;
48 import org.openide.actions.ToolsAction;
49
50 import org.openide.nodes.*;
51 import org.openide.util.*;
52 import org.openide.util.actions.SystemAction;
53 import org.openide.xml.XMLUtil;
54
55 import org.netbeans.api.project.FileOwnerQuery;
56 import org.netbeans.api.project.Project;
57 import org.netbeans.api.project.ProjectUtils;
58 import org.netbeans.spi.java.project.support.ui.PackageView;
59 import org.netbeans.spi.project.ui.support.CommonProjectActions;
60 import org.netbeans.spi.project.ActionProvider;
61 import org.netbeans.spi.project.SubprojectProvider;
62 import org.netbeans.spi.project.support.ant.AntProjectHelper;
63 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
64 import org.netbeans.spi.project.support.ant.ReferenceHelper;
65 import org.netbeans.spi.project.ui.LogicalViewProvider;
66 import org.netbeans.spi.project.ui.support.ProjectSensitiveActions;
67 import org.netbeans.spi.java.project.support.ui.BrokenReferencesSupport;
68 import org.netbeans.api.java.project.JavaProjectConstants;
69 import org.netbeans.api.project.SourceGroup;
70 import org.netbeans.api.project.Sources;
71 import org.netbeans.modules.j2ee.ejbjarproject.ui.customizer.EjbJarProjectProperties;
72 import org.netbeans.modules.j2ee.ejbjarproject.UpdateHelper;
73 import org.openide.filesystems.FileObject;
74 import org.openide.loaders.DataFolder;
75 import org.openide.util.lookup.Lookups;
76
77
78 import org.netbeans.modules.j2ee.api.ejbjar.EjbProjectConstants;
79 import org.netbeans.modules.j2ee.common.ui.BrokenServerSupport;
80 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
81 import org.netbeans.modules.j2ee.deployment.devmodules.spi.InstanceListener;
82 import org.netbeans.modules.j2ee.ejbjarproject.EjbJarProject;
83 import org.netbeans.modules.j2ee.spi.ejbjar.support.J2eeProjectView;
84 import org.netbeans.spi.project.ui.support.DefaultProjectOperations;
85 import org.openide.ErrorManager;
86 import org.openide.filesystems.FileStateInvalidException;
87 import org.openide.filesystems.FileStatusEvent;
88 import org.openide.filesystems.FileStatusListener;
89 import org.openide.filesystems.FileSystem;
90 import org.openide.filesystems.FileUtil;
91 import org.openide.filesystems.Repository;
92 import org.openide.loaders.DataObject;
93 import org.openide.loaders.DataObjectNotFoundException;
94 import org.openide.loaders.FolderLookup;
95
96 /**
97  * Support for creating logical views.
98  * @author Petr Hrebejk
99  */

100 public class EjbJarLogicalViewProvider implements LogicalViewProvider {
101     private static final RequestProcessor BROKEN_LINKS_RP = new RequestProcessor("EjbJarLogicalViewProvider.BROKEN_LINKS_RP"); // NOI18N
102

103     private final EjbJarProject project;
104     private final AntProjectHelper helper;
105     private final UpdateHelper updateHelper;
106     private final PropertyEvaluator evaluator;
107     private final SubprojectProvider spp;
108     private final ReferenceHelper resolver;
109     private List JavaDoc changeListeners;
110     
111     public EjbJarLogicalViewProvider(EjbJarProject project, UpdateHelper updateHelper, PropertyEvaluator evaluator, SubprojectProvider spp, ReferenceHelper resolver) {
112         this.project = project;
113         assert project != null;
114         this.updateHelper = updateHelper;
115         assert updateHelper != null;
116         this.helper = updateHelper.getAntProjectHelper();
117         this.evaluator = evaluator;
118         assert evaluator != null;
119         this.spp = spp;
120         assert spp != null;
121         this.resolver = resolver;
122     }
123         
124     public Node createLogicalView() {
125         return new WebLogicalViewRootNode();
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         if ( target instanceof FileObject ) {
135             FileObject fo = (FileObject)target;
136             Project owner = FileOwnerQuery.getOwner( fo );
137             if ( !project.equals( owner ) ) {
138                 return null; // Don't waste time if project does not own the fo
139
}
140             
141             Node[] nodes = root.getChildren().getNodes( true );
142             for ( int i = 0; i < nodes.length; i++ ) {
143                 Node result = PackageView.findPath( nodes[i], target );
144                 if ( result != null ) {
145                     return result;
146                 }
147             }
148             return findNodeInConfigFiles(root, fo);
149         }
150
151         return null;
152     }
153     
154     private Node findNodeInConfigFiles(Node root, FileObject fo) {
155         // XXX ugly, some node names contain the extension and other don't
156
// so retrieving the node name from the corresp. DataObject
157
String JavaDoc nodeName;
158         try {
159             DataObject dobj = DataObject.find(fo);
160             nodeName = dobj.getName();
161         } catch (DataObjectNotFoundException e) {
162             nodeName = fo.getName();
163         }
164         Node configFiles = root.getChildren().findChild(J2eeProjectView.CONFIG_FILES_VIEW_NAME); // NOI18N
165
if (configFiles == null) {
166             return null;
167         }
168         return NodeOp.findChild(configFiles, nodeName);
169     }
170     
171     public synchronized void addChangeListener (ChangeListener JavaDoc l) {
172         if (this.changeListeners == null) {
173             this.changeListeners = new ArrayList JavaDoc ();
174         }
175         this.changeListeners.add (l);
176     }
177     
178     public synchronized void removeChangeListener (ChangeListener JavaDoc l) {
179         if (this.changeListeners == null) {
180             return;
181         }
182         this.changeListeners.remove (l);
183     }
184     
185     /**
186      * Used by EjbJarProjectCustomizer to mark the project as broken when it warns user
187      * about project's broken references and advices him to use BrokenLinksAction to correct it.
188      *
189      */

190     public void testBroken () {
191         ChangeListener JavaDoc[] _listeners;
192         synchronized (this) {
193             if (this.changeListeners == null) {
194                 return;
195             }
196             _listeners = (ChangeListener JavaDoc[]) this.changeListeners.toArray(
197                     new ChangeListener JavaDoc[this.changeListeners.size()]);
198         }
199         ChangeEvent JavaDoc event = new ChangeEvent JavaDoc (this);
200         for (int i=0; i<_listeners.length; i++) {
201             _listeners[i].stateChanged(event);
202         }
203     }
204             
205     private static Lookup createLookup( Project project ) {
206         DataFolder rootFolder = DataFolder.findFolder( project.getProjectDirectory() );
207         // XXX Remove root folder after FindAction rewrite
208
return Lookups.fixed( new Object JavaDoc[] { project, rootFolder } );
209     }
210
211     // Private innerclasses ----------------------------------------------------
212

213     private static final String JavaDoc[] BREAKABLE_PROPERTIES = new String JavaDoc[] {
214         EjbJarProjectProperties.JAVAC_CLASSPATH,
215         EjbJarProjectProperties.DEBUG_CLASSPATH,
216         EjbJarProjectProperties.RUN_TEST_CLASSPATH,
217         EjbJarProjectProperties.DEBUG_TEST_CLASSPATH,
218         EjbJarProjectProperties.JAVAC_TEST_CLASSPATH,
219     };
220
221     public boolean hasBrokenLinks() {
222         return BrokenReferencesSupport.isBroken(helper, resolver, getBreakableProperties(),
223             new String JavaDoc[] {EjbJarProjectProperties.JAVA_PLATFORM});
224     }
225     
226     private String JavaDoc[] getBreakableProperties() {
227         SourceRoots roots = this.project.getSourceRoots();
228         String JavaDoc[] srcRootProps = roots.getRootProperties();
229         roots = this.project.getTestSourceRoots();
230         String JavaDoc[] testRootProps = roots.getRootProperties();
231         String JavaDoc[] result = new String JavaDoc [BREAKABLE_PROPERTIES.length + srcRootProps.length + testRootProps.length];
232         System.arraycopy(BREAKABLE_PROPERTIES, 0, result, 0, BREAKABLE_PROPERTIES.length);
233         System.arraycopy(srcRootProps, 0, result, BREAKABLE_PROPERTIES.length, srcRootProps.length);
234         System.arraycopy(testRootProps, 0, result, BREAKABLE_PROPERTIES.length + srcRootProps.length, testRootProps.length);
235         return result;
236     }
237
238     /** Filter node containin additional features for the J2SE physical
239      */

240     private final class WebLogicalViewRootNode extends AbstractNode implements Runnable JavaDoc, FileStatusListener, ChangeListener JavaDoc, PropertyChangeListener JavaDoc {
241
242         private Action JavaDoc brokenLinksAction;
243         private BrokenServerAction brokenServerAction;
244         private boolean broken;
245         private static final String JavaDoc BROKEN_PROJECT_BADGE = "org/netbeans/modules/j2ee/ejbjarproject/ui/resources/brokenProjectBadge.gif"; // NOI18N
246

247         // icon badging >>>
248
private Set JavaDoc files;
249         private Map JavaDoc fileSystemListeners;
250         private RequestProcessor.Task task;
251         private final Object JavaDoc privateLock = new Object JavaDoc();
252         private boolean iconChange;
253         private boolean nameChange;
254         private ChangeListener JavaDoc sourcesListener;
255         private Map JavaDoc groupsListeners;
256         // icon badging <<<
257

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

477         private Action JavaDoc[] getAdditionalActions() {
478
479             ResourceBundle JavaDoc bundle = NbBundle.getBundle(EjbJarLogicalViewProvider.class);
480             
481             J2eeModuleProvider provider = (J2eeModuleProvider) project.getLookup().lookup(J2eeModuleProvider.class);
482             List JavaDoc actions = new ArrayList JavaDoc(30);
483             actions.add(CommonProjectActions.newFileAction());
484             actions.add(null);
485             actions.add(ProjectSensitiveActions.projectCommandAction( ActionProvider.COMMAND_BUILD, bundle.getString( "LBL_BuildAction_Name" ), null )); // NOI18N
486
actions.add(ProjectSensitiveActions.projectCommandAction( ActionProvider.COMMAND_REBUILD, bundle.getString( "LBL_RebuildAction_Name" ), null )); // NOI18N
487
actions.add(ProjectSensitiveActions.projectCommandAction( ActionProvider.COMMAND_CLEAN, bundle.getString( "LBL_CleanAction_Name" ), null )); // NOI18N
488
if (provider != null && provider.hasVerifierSupport()) {
489                 actions.add(ProjectSensitiveActions.projectCommandAction( "verify", bundle.getString( "LBL_VerifyAction_Name" ), null )); // NOI18N
490
}
491             actions.add(ProjectSensitiveActions.projectCommandAction( JavaProjectConstants.COMMAND_JAVADOC, bundle.getString( "LBL_JavadocAction_Name" ), null )); // NOI18N
492
actions.add(null);
493             actions.add(ProjectSensitiveActions.projectCommandAction( ActionProvider.COMMAND_RUN, bundle.getString( "LBL_RunAction_Name" ), null )); // NOI18N
494
actions.add(ProjectSensitiveActions.projectCommandAction( ActionProvider.COMMAND_DEBUG, bundle.getString( "LBL_DebugAction_Name" ), null )); // NOI18N
495
actions.add(ProjectSensitiveActions.projectCommandAction( EjbProjectConstants.COMMAND_REDEPLOY, bundle.getString( "LBL_RedeployAction_Name" ), null )); // NOI18N
496
actions.add(null);
497             actions.add(CommonProjectActions.setAsMainProjectAction());
498             actions.add(CommonProjectActions.openSubprojectsAction());
499             actions.add(CommonProjectActions.closeProjectAction());
500             actions.add(null);
501             actions.add(CommonProjectActions.renameProjectAction());
502             actions.add(CommonProjectActions.moveProjectAction());
503             actions.add(CommonProjectActions.copyProjectAction());
504             actions.add(CommonProjectActions.deleteProjectAction());
505             actions.add(null);
506             actions.add(SystemAction.get( org.openide.actions.FindAction.class ));
507             
508             // honor 57874 contract
509

510             try {
511                 Repository repository = Repository.getDefault();
512                 FileSystem sfs = repository.getDefaultFileSystem();
513                 FileObject fo = sfs.findResource("Projects/Actions"); // NOI18N
514
if (fo != null) {
515                     DataObject dobj = DataObject.find(fo);
516                     FolderLookup actionRegistry = new FolderLookup((DataFolder)dobj);
517                     Lookup.Template query = new Lookup.Template(Object JavaDoc.class);
518                     Lookup lookup = actionRegistry.getLookup();
519                     Iterator JavaDoc it = lookup.lookup(query).allInstances().iterator();
520                     if (it.hasNext()) {
521                         actions.add(null);
522                     }
523                     while (it.hasNext()) {
524                         Object JavaDoc next = it.next();
525                         if (next instanceof Action JavaDoc) {
526                             actions.add(next);
527                         } else if (next instanceof JSeparator JavaDoc) {
528                             actions.add(null);
529                         }
530                     }
531                 }
532             } catch (DataObjectNotFoundException ex) {
533                 // data folder for existing fileobject expected
534
ErrorManager.getDefault().notify(ex);
535             }
536             
537             actions.add(null);
538             actions.add(SystemAction.get(ToolsAction.class));
539             actions.add(null);
540             
541             if (brokenLinksAction != null && brokenLinksAction.isEnabled()) {
542                 actions.add(brokenLinksAction);
543             }
544             if (brokenServerAction.isEnabled()) {
545                 actions.add(brokenServerAction);
546             }
547             actions.add(CommonProjectActions.customizeProjectAction());
548
549             return (Action JavaDoc[])actions.toArray(new Action JavaDoc[actions.size()]);
550         }
551
552         /** This action is created only when project has broken references.
553          * Once these are resolved the action is disabled.
554          */

555         private class BrokenLinksAction extends AbstractAction JavaDoc implements PropertyChangeListener JavaDoc, ChangeListener JavaDoc, Runnable JavaDoc {
556
557             private RequestProcessor.Task task = null;
558
559             private PropertyChangeListener JavaDoc weakPCL;
560
561             public BrokenLinksAction() {
562                 putValue(Action.NAME, NbBundle.getMessage(EjbJarLogicalViewProvider.class, "LBL_Fix_Broken_Links_Action"));
563                 setEnabled(broken);
564                 evaluator.addPropertyChangeListener(this);
565                 
566                 // When evaluator fires changes that platform properties were
567
// removed the platform still exists in JavaPlatformManager.
568
// That's why I have to listen here also on JPM:
569
weakPCL = WeakListeners.propertyChange( this, JavaPlatformManager.getDefault() );
570                 JavaPlatformManager.getDefault().addPropertyChangeListener( weakPCL );
571                 EjbJarLogicalViewProvider.this.addChangeListener ((ChangeListener JavaDoc)WeakListeners.change(this, EjbJarLogicalViewProvider.this));
572             }
573
574             public void actionPerformed(ActionEvent JavaDoc e) {
575                 BrokenReferencesSupport.showCustomizer(helper, resolver, getBreakableProperties(), new String JavaDoc[]{EjbJarProjectProperties.JAVA_PLATFORM});
576                 run();
577             }
578
579             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
580                 refsMayChanged();
581             }
582             
583             public void stateChanged (ChangeEvent JavaDoc evt) {
584                 refsMayChanged ();
585             }
586             
587             public synchronized void run() {
588                 boolean old = broken;
589                 broken = hasBrokenLinks();
590                 if (old != broken) {
591                     setEnabled(broken);
592                     fireIconChange();
593                     fireOpenedIconChange();
594                     fireDisplayNameChange(null, null);
595                     //to refresh library references in private.properties
596
new EjbJarProjectProperties (project, updateHelper, evaluator, resolver).save();
597                 }
598             }
599             
600             public void refsMayChanged() {
601                 // check project state whenever there was a property change
602
// or change in list of platforms.
603
// Coalesce changes since they can come quickly:
604
if (task == null) {
605                     task = BROKEN_LINKS_RP.create(this);
606                 }
607                 task.schedule(100);
608             }
609
610         }
611         
612         private class BrokenServerAction extends AbstractAction JavaDoc implements
613                     InstanceListener, PropertyChangeListener JavaDoc {
614
615             private RequestProcessor.Task task = null;
616             private boolean brokenServer;
617             
618             public BrokenServerAction() {
619                 putValue(Action.NAME, NbBundle.getMessage(EjbJarLogicalViewProvider.class, "LBL_Fix_Missing_Server_Action")); // NOI18N
620
evaluator.addPropertyChangeListener(this);
621                 checkMissingServer();
622             }
623             
624             public boolean isEnabled() {
625                 return brokenServer;
626             }
627
628             public void actionPerformed(ActionEvent JavaDoc e) {
629                 String JavaDoc j2eeSpec = EjbJarProjectProperties.getProperty(
630                         EjbJarProjectProperties.J2EE_PLATFORM,
631                         helper,
632                         AntProjectHelper.PROJECT_PROPERTIES_PATH);
633                 String JavaDoc instance = BrokenServerSupport.selectServer(j2eeSpec, J2eeModule.EJB);
634                 if (instance != null) {
635                     EjbJarProjectProperties.setServerInstance(
636                             project, helper, instance);
637                 }
638                 checkMissingServer();
639             }
640
641             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
642                 if (EjbJarProjectProperties.J2EE_SERVER_INSTANCE.equals(evt.getPropertyName())) {
643                     checkMissingServer();
644                 }
645             }
646                 
647             public void changeDefaultInstance(String JavaDoc oldServerInstanceID, String JavaDoc newServerInstanceID) {
648             }
649
650             public void instanceAdded(String JavaDoc serverInstanceID) {
651                 checkMissingServer();
652             }
653
654             public void instanceRemoved(String JavaDoc serverInstanceID) {
655                 checkMissingServer();
656             }
657
658             private void checkMissingServer() {
659                 boolean old = brokenServer;
660                 String JavaDoc servInstID = EjbJarProjectProperties.getProperty(EjbJarProjectProperties.J2EE_SERVER_INSTANCE, helper, AntProjectHelper.PRIVATE_PROPERTIES_PATH);
661                 brokenServer = BrokenServerSupport.isBroken(servInstID);
662                 if (old != brokenServer) {
663                     fireIconChange();
664                     fireOpenedIconChange();
665                     fireDisplayNameChange(null, null);
666                 }
667             }
668         }
669
670     }
671
672     /** Factory for project actions.<BR>
673      * XXX This class is a candidate for move to org.netbeans.spi.project.ui.support
674      */

675     public static class Actions {
676         
677         private Actions() {} // This is a factory
678

679         public static Action JavaDoc createAction( String JavaDoc key, String JavaDoc name, boolean global ) {
680             return new ActionImpl( key, name, global ? Utilities.actionsGlobalContext() : null );
681         }
682                 
683         private static class ActionImpl extends AbstractAction JavaDoc implements ContextAwareAction {
684             
685             Lookup context;
686             String JavaDoc name;
687             String JavaDoc command;
688             
689             public ActionImpl( String JavaDoc command, String JavaDoc name, Lookup context ) {
690                 super( name );
691                 this.context = context;
692                 this.command = command;
693                 this.name = name;
694             }
695             
696             public void actionPerformed( ActionEvent JavaDoc e ) {
697                 
698                 Project project = (Project)context.lookup( Project.class );
699                 ActionProvider ap = (ActionProvider)project.getLookup().lookup( ActionProvider.class);
700                 
701                 ap.invokeAction( command, context );
702                                 
703             }
704             
705             public Action JavaDoc createContextAwareInstance( Lookup lookup ) {
706                 return new ActionImpl( command, name, lookup );
707             }
708         }
709         
710     }
711
712 }
713
Popular Tags