001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.j2ee.common.project.ui;
043:
044: import java.awt.Image;
045: import java.beans.PropertyChangeEvent;
046: import java.beans.PropertyChangeListener;
047: import java.io.File;
048: import java.util.ArrayList;
049: import java.util.Collections;
050: import java.util.List;
051: import javax.swing.Action;
052: import javax.swing.Icon;
053: import javax.swing.ImageIcon;
054: import org.netbeans.api.project.Project;
055: import org.openide.nodes.Children;
056: import org.openide.nodes.AbstractNode;
057: import org.openide.nodes.Node;
058: import org.openide.util.WeakListeners;
059: import org.netbeans.api.project.SourceGroup;
060: import org.netbeans.modules.j2ee.common.project.classpath.ClassPathSupport;
061: import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
062: import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform;
063: import org.netbeans.modules.j2ee.deployment.devmodules.spi.InstanceListener;
064: import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
065: import org.netbeans.spi.project.support.ant.PropertyEvaluator;
066: import org.netbeans.spi.java.project.support.ui.PackageView;
067: import org.openide.filesystems.FileObject;
068: import org.openide.filesystems.FileUtil;
069: import org.openide.util.NbBundle;
070: import org.openide.util.Utilities;
071: import org.openide.util.actions.SystemAction;
072:
073: /**
074: * J2eePlatformNode represents the J2EE platform in the logical view.
075: * Listens on the {@link PropertyEvaluator} for change of
076: * the ant property holding the platform name.
077: * @see J2eePlatform
078: * @author Andrei Badea
079: */
080: class J2eePlatformNode extends AbstractNode implements
081: PropertyChangeListener, InstanceListener {
082:
083: private static final String ARCHIVE_ICON = "org/netbeans/modules/j2ee/common/project/ui/resources/jar.gif"; //NOI18N
084: private static final String DEFAULT_ICON = "org/netbeans/modules/j2ee/common/project/ui/resources/j2eeServer.gif"; //NOI18N
085: private static final String BROKEN_PROJECT_BADGE = "org/netbeans/modules/j2ee/common/project/ui/resources/brokenProjectBadge.gif"; //NOI18N
086:
087: private static final Icon icon = new ImageIcon(Utilities
088: .loadImage(ARCHIVE_ICON));
089:
090: private static final Image brokenIcon = Utilities.mergeImages(
091: Utilities.loadImage(DEFAULT_ICON), Utilities
092: .loadImage(BROKEN_PROJECT_BADGE), 8, 0);
093:
094: private final PropertyEvaluator evaluator;
095: private final String platformPropName;
096: private J2eePlatform platformCache;
097:
098: private final PropertyChangeListener platformListener = new PropertyChangeListener() {
099: public void propertyChange(PropertyChangeEvent evt) {
100: if (J2eePlatform.PROP_DISPLAY_NAME.equals(evt
101: .getPropertyName())) {
102: fireNameChange((String) evt.getOldValue(), (String) evt
103: .getNewValue());
104: fireDisplayNameChange((String) evt.getOldValue(),
105: (String) evt.getNewValue());
106: }
107: if (J2eePlatform.PROP_CLASSPATH.equals(evt
108: .getPropertyName())) {
109: postAddNotify();
110: }
111: }
112: };
113: private PropertyChangeListener weakPlatformListener;
114:
115: private J2eePlatformNode(Project project,
116: PropertyEvaluator evaluator, String platformPropName,
117: ClassPathSupport cs) {
118: super (new PlatformContentChildren(cs));
119: this .evaluator = evaluator;
120: this .platformPropName = platformPropName;
121: evaluator.addPropertyChangeListener(WeakListeners
122: .propertyChange(this , evaluator));
123:
124: J2eeModuleProvider moduleProvider = (J2eeModuleProvider) project
125: .getLookup().lookup(J2eeModuleProvider.class);
126: moduleProvider
127: .addInstanceListener((InstanceListener) WeakListeners
128: .create(InstanceListener.class, this ,
129: moduleProvider));
130: }
131:
132: public static J2eePlatformNode create(Project project,
133: PropertyEvaluator evaluator, String platformPropName,
134: ClassPathSupport cs) {
135: return new J2eePlatformNode(project, evaluator,
136: platformPropName, cs);
137: }
138:
139: public String getName() {
140: return this .getDisplayName();
141: }
142:
143: public String getDisplayName() {
144: return "";
145: }
146:
147: public String getHtmlDisplayName() {
148: if (getPlatform() != null)
149: return getPlatform().getDisplayName();
150: else
151: return NbBundle.getMessage(J2eePlatformNode.class,
152: "LBL_J2eeServerMissing");
153: }
154:
155: public Image getIcon(int type) {
156: Image result = null;
157: if (getPlatform() != null) {
158: result = getPlatform().getIcon();
159: }
160: return result != null ? result : brokenIcon;
161: }
162:
163: public Image getOpenedIcon(int type) {
164: return getIcon(type);
165: }
166:
167: public boolean canCopy() {
168: return false;
169: }
170:
171: public Action[] getActions(boolean context) {
172: return new SystemAction[0];
173: }
174:
175: public void propertyChange(PropertyChangeEvent evt) {
176: //The caller holds ProjectManager.mutex() read lock
177:
178: if (platformPropName.equals(evt.getPropertyName())) {
179: refresh();
180: }
181: }
182:
183: private void refresh() {
184: if (platformCache != null)
185: platformCache
186: .removePropertyChangeListener(weakPlatformListener);
187:
188: platformCache = null;
189:
190: this .fireNameChange(null, null);
191: this .fireDisplayNameChange(null, null);
192: this .fireIconChange();
193:
194: // The caller may hold ProjectManager.mutex() read lock (i.e., the propertyChange() method)
195: postAddNotify();
196: }
197:
198: public void instanceAdded(String serverInstanceID) {
199: refresh();
200: }
201:
202: public void instanceRemoved(String serverInstanceID) {
203: refresh();
204: }
205:
206: public void changeDefaultInstance(String oldServerInstanceID,
207: String newServerInstanceID) {
208: }
209:
210: private void postAddNotify() {
211: LibrariesNode.rp.post(new Runnable() {
212: public void run() {
213: ((PlatformContentChildren) getChildren()).addNotify();
214: }
215: });
216: }
217:
218: private J2eePlatform getPlatform() {
219: if (platformCache == null) {
220: String j2eePlatformInstanceId = this .evaluator
221: .getProperty(this .platformPropName);
222: if (j2eePlatformInstanceId != null) {
223: platformCache = Deployment.getDefault()
224: .getJ2eePlatform(j2eePlatformInstanceId);
225: }
226: if (platformCache != null) {
227: weakPlatformListener = WeakListeners.propertyChange(
228: platformListener, platformCache);
229: platformCache
230: .addPropertyChangeListener(weakPlatformListener);
231: // the platform has likely changed, so force the node to display the new platform's icon
232: this .fireIconChange();
233: }
234: }
235: return platformCache;
236: }
237:
238: private static class PlatformContentChildren extends Children.Keys {
239:
240: private ClassPathSupport cs;
241:
242: PlatformContentChildren(ClassPathSupport cs) {
243: this .cs = cs;
244: }
245:
246: protected void addNotify() {
247: this .setKeys(this .getKeys());
248: }
249:
250: protected void removeNotify() {
251: this .setKeys(Collections.EMPTY_SET);
252: }
253:
254: protected Node[] createNodes(Object key) {
255: SourceGroup sg = (SourceGroup) key;
256: return new Node[] { ActionFilterNode.create(PackageView
257: .createPackageView(sg), null, null, null, null,
258: null, null) };
259: }
260:
261: private List getKeys() {
262: List result;
263:
264: J2eePlatform j2eePlatform = ((J2eePlatformNode) this
265: .getNode()).getPlatform();
266: if (j2eePlatform != null) {
267: File[] classpathEntries = j2eePlatform
268: .getClasspathEntries();
269: result = new ArrayList(classpathEntries.length);
270: for (int i = 0; i < classpathEntries.length; i++) {
271: FileObject file = FileUtil
272: .toFileObject(classpathEntries[i]);
273: if (file != null) {
274: FileObject archiveFile = FileUtil
275: .getArchiveRoot(file);
276: if (archiveFile != null) {
277: result.add(new LibrariesSourceGroup(
278: archiveFile, file.getNameExt(),
279: icon, icon));
280: }
281: }
282: }
283: } else {
284: result = Collections.EMPTY_LIST;
285: }
286:
287: return result;
288: }
289: }
290: }
|