KickJava   Java API By Example, From Geeks To Geeks.

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


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.web.project.ui;
21
22
23 import java.awt.Image JavaDoc;
24 import java.beans.PropertyChangeEvent JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import java.io.File JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.List JavaDoc;
30 import javax.swing.Action JavaDoc;
31 import javax.swing.Icon JavaDoc;
32 import javax.swing.ImageIcon JavaDoc;
33 import org.netbeans.api.project.Project;
34 import org.openide.nodes.Children;
35 import org.openide.nodes.AbstractNode;
36 import org.openide.nodes.Node;
37 import org.openide.util.WeakListeners;
38 import org.netbeans.api.project.SourceGroup;
39 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
40 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform;
41 import org.netbeans.modules.j2ee.deployment.devmodules.spi.InstanceListener;
42 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
43 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
44 import org.netbeans.spi.java.project.support.ui.PackageView;
45 import org.openide.filesystems.FileObject;
46 import org.openide.filesystems.FileUtil;
47 import org.openide.util.NbBundle;
48 import org.openide.util.Utilities;
49 import org.openide.util.actions.SystemAction;
50
51 /**
52  * J2eePlatformNode represents the J2EE platform in the logical view.
53  * Listens on the {@link PropertyEvaluator} for change of
54  * the ant property holding the platform name.
55  * @see J2eePlatform
56  * @author Andrei Badea
57  */

58 class J2eePlatformNode extends AbstractNode implements PropertyChangeListener JavaDoc, InstanceListener {
59
60     private static final String JavaDoc ARCHIVE_ICON = "org/netbeans/modules/web/project/ui/resources/jar.gif"; //NOI18N
61
private static final String JavaDoc DEFAULT_ICON = "org/netbeans/modules/web/project/ui/resources/j2eeServer.gif"; //NOI18N
62
private static final String JavaDoc BROKEN_PROJECT_BADGE = "org/netbeans/modules/web/project/ui/resources/brokenProjectBadge.gif"; //NOI18N
63

64     private static final Icon JavaDoc icon = new ImageIcon JavaDoc(Utilities.loadImage(ARCHIVE_ICON));
65     
66     private static final Image JavaDoc brokenIcon = Utilities.mergeImages(
67             Utilities.loadImage(DEFAULT_ICON),
68             Utilities.loadImage(BROKEN_PROJECT_BADGE),
69             8, 0);
70
71     private final PropertyEvaluator evaluator;
72     private final String JavaDoc platformPropName;
73     private J2eePlatform platformCache;
74     
75     private final PropertyChangeListener JavaDoc platformListener = new PropertyChangeListener JavaDoc() {
76         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
77             if (J2eePlatform.PROP_DISPLAY_NAME.equals(evt.getPropertyName())) {
78                 fireNameChange((String JavaDoc)evt.getOldValue(), (String JavaDoc)evt.getNewValue());
79                 fireDisplayNameChange((String JavaDoc)evt.getOldValue(), (String JavaDoc)evt.getNewValue());
80             }
81             if (J2eePlatform.PROP_CLASSPATH.equals(evt.getPropertyName())) {
82                 postAddNotify();
83             }
84         }
85     };
86     private PropertyChangeListener JavaDoc weakPlatformListener;
87
88     private J2eePlatformNode(Project project, PropertyEvaluator evaluator, String JavaDoc platformPropName) {
89         super(new PlatformContentChildren());
90         this.evaluator = evaluator;
91         this.platformPropName = platformPropName;
92         evaluator.addPropertyChangeListener(WeakListeners.propertyChange(this, evaluator));
93         
94         J2eeModuleProvider moduleProvider = (J2eeModuleProvider)project.getLookup().lookup(J2eeModuleProvider.class);
95         moduleProvider.addInstanceListener(
96                 (InstanceListener)WeakListeners.create(InstanceListener.class, this, moduleProvider));
97     }
98     
99     public static J2eePlatformNode create(Project project, PropertyEvaluator evaluator, String JavaDoc platformPropName) {
100         return new J2eePlatformNode(project, evaluator, platformPropName);
101     }
102
103     public String JavaDoc getName () {
104         return this.getDisplayName();
105     }
106     
107     public String JavaDoc getDisplayName() {
108         return "";
109     }
110     
111     public String JavaDoc getHtmlDisplayName() {
112         if (getPlatform() != null)
113             return getPlatform().getDisplayName();
114         else
115             return NbBundle.getMessage(J2eePlatformNode.class, "LBL_J2eeServerMissing");
116     }
117     
118     public Image JavaDoc getIcon(int type) {
119         Image JavaDoc result = null;
120         if (getPlatform() != null) {
121             result = getPlatform().getIcon();
122         }
123         return result != null ? result : brokenIcon;
124     }
125     
126     public Image JavaDoc getOpenedIcon(int type) {
127         return getIcon(type);
128     }
129
130     public boolean canCopy() {
131         return false;
132     }
133     
134     public Action JavaDoc[] getActions(boolean context) {
135         return new SystemAction[0];
136     }
137
138     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
139         //The caller holds ProjectManager.mutex() read lock
140

141         if (platformPropName.equals(evt.getPropertyName())) {
142             refresh();
143         }
144     }
145     
146     private void refresh() {
147         if (platformCache != null)
148             platformCache.removePropertyChangeListener(weakPlatformListener);
149
150         platformCache = null;
151
152         this.fireNameChange(null, null);
153         this.fireDisplayNameChange(null, null);
154         this.fireIconChange();
155         
156         // The caller may hold ProjectManager.mutex() read lock (i.e., the propertyChange() method)
157
postAddNotify();
158     }
159     
160     public void instanceAdded(String JavaDoc serverInstanceID) {
161         refresh();
162     }
163     
164     public void instanceRemoved(String JavaDoc serverInstanceID) {
165         refresh();
166     }
167     
168     public void changeDefaultInstance(String JavaDoc oldServerInstanceID, String JavaDoc newServerInstanceID) {
169     }
170
171     private void postAddNotify() {
172         LibrariesNode.rp.post (new Runnable JavaDoc () {
173             public void run () {
174                 ((PlatformContentChildren)getChildren()).addNotify ();
175             }
176         });
177     }
178
179     private J2eePlatform getPlatform () {
180         if (platformCache == null) {
181             String JavaDoc j2eePlatformInstanceId = this.evaluator.getProperty(this.platformPropName);
182             if (j2eePlatformInstanceId != null) {
183                 platformCache = Deployment.getDefault().getJ2eePlatform(j2eePlatformInstanceId);
184             }
185             if (platformCache != null) {
186                 weakPlatformListener = WeakListeners.propertyChange(platformListener, platformCache);
187                 platformCache.addPropertyChangeListener(weakPlatformListener);
188                 // the platform has likely changed, so force the node to display the new platform's icon
189
this.fireIconChange();
190             }
191         }
192         return platformCache;
193     }
194
195     private static class PlatformContentChildren extends Children.Keys {
196
197         PlatformContentChildren () {
198         }
199
200         protected void addNotify() {
201             this.setKeys (this.getKeys());
202         }
203
204         protected void removeNotify() {
205             this.setKeys(Collections.EMPTY_SET);
206         }
207
208         protected Node[] createNodes(Object JavaDoc key) {
209             SourceGroup sg = (SourceGroup) key;
210             return new Node[] {ActionFilterNode.create(PackageView.createPackageView(sg), null, null, null, null, null, null)};
211         }
212
213         private List JavaDoc getKeys () {
214             List JavaDoc result;
215             
216             J2eePlatform j2eePlatform = ((J2eePlatformNode)this.getNode()).getPlatform();
217             if (j2eePlatform != null) {
218                 File JavaDoc[] classpathEntries = j2eePlatform.getClasspathEntries();
219                 result = new ArrayList JavaDoc(classpathEntries.length);
220                 for (int i = 0; i < classpathEntries.length; i++) {
221                     FileObject file = FileUtil.toFileObject(classpathEntries[i]);
222                     if (file != null) {
223                         FileObject archiveFile = FileUtil.getArchiveRoot(file);
224                         if (archiveFile != null) {
225                             result.add(new LibrariesSourceGroup(archiveFile, file.getNameExt(), icon, icon));
226                         }
227                     }
228                 }
229             } else {
230                 result = Collections.EMPTY_LIST;
231             }
232             
233             return result;
234         }
235     }
236 }
237
Popular Tags