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-2007 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.visualweb.project.jsf.ui;
043:
044: import org.netbeans.modules.visualweb.complib.api.ComplibService;
045: import org.netbeans.modules.visualweb.project.jsf.api.JsfProjectUtils;
046: import org.netbeans.modules.visualweb.project.jsf.services.ThemeNodeService;
047: import org.netbeans.modules.visualweb.project.jsf.services.DataSourceService;
048:
049: import java.beans.PropertyChangeEvent;
050: import java.beans.PropertyChangeListener;
051: import java.util.ArrayList;
052: import java.util.Iterator;
053: import java.util.List;
054: import javax.swing.SwingUtilities;
055: import javax.swing.event.ChangeEvent;
056: import javax.swing.event.ChangeListener;
057:
058: import org.netbeans.api.project.Project;
059: import org.netbeans.spi.project.ui.support.NodeFactory;
060: import org.netbeans.spi.project.ui.support.NodeList;
061: import org.openide.nodes.Node;
062: import org.openide.util.Lookup;
063: import org.openide.util.NbBundle;
064:
065: /** Visual Web framework extra nodes factory.
066: *
067: * @author Po-Ting Wu
068: */
069: public class JSFNodeFactory implements NodeFactory {
070: /** Creates a new instance of JSFNodeFactory */
071: public JSFNodeFactory() {
072: }
073:
074: public NodeList createNodes(Project p) {
075: return new JSFNodeList(p);
076: }
077:
078: private static class JSFNodeList implements NodeList<String>,
079: PropertyChangeListener {
080: private static final String THEMES_FOLDER = "themesFolder"; //NOI18N
081: private static final String COMPONENT_LIBS = "compLib"; //NOI18N
082: private static final String DATASOURCE_REFS = "dataSource"; //NOI18N
083:
084: private final Project project;
085: private final ArrayList<ChangeListener> listeners = new ArrayList<ChangeListener>();
086:
087: JSFNodeList(Project proj) {
088: project = proj;
089: }
090:
091: public List<String> keys() {
092: List<String> result = new ArrayList<String>();
093: if (JsfProjectUtils.isJsfProject(project)) {
094: result.add(THEMES_FOLDER);
095: result.add(COMPONENT_LIBS);
096: result.add(DATASOURCE_REFS);
097: }
098: return result;
099: }
100:
101: public synchronized void addChangeListener(ChangeListener l) {
102: listeners.add(l);
103: }
104:
105: public synchronized void removeChangeListener(ChangeListener l) {
106: listeners.remove(l);
107: }
108:
109: private void fireChange() {
110: for (ChangeListener elem : listeners) {
111: elem.stateChanged(new ChangeEvent(this ));
112: }
113: }
114:
115: public Node node(String key) {
116: if (key == THEMES_FOLDER) {
117: Lookup.Result<ThemeNodeService> themeNodeServices = Lookup
118: .getDefault().lookup(
119: new Lookup.Template<ThemeNodeService>(
120: ThemeNodeService.class));
121: for (ThemeNodeService service : themeNodeServices
122: .allInstances()) {
123: // We can return only one node depending on the project
124: Node themeNode = service.getThemeNode(project);
125: if (themeNode != null) {
126: return themeNode;
127: }
128: }
129:
130: return null;
131: } else if (key == COMPONENT_LIBS) {
132: ComplibService complibService = Lookup.getDefault()
133: .lookup(ComplibService.class);
134: if (complibService != null) {
135: return complibService.getComplibsRootNode(project);
136: } else {
137: return null;
138: }
139: } else if (key == DATASOURCE_REFS) {
140: DataSourceService dss = (DataSourceService) Lookup
141: .getDefault().lookup(DataSourceService.class);
142: if (dss != null) {
143: return dss.getDataSourceReferenceNode(project);
144: } else {
145: return null;
146: }
147: }
148: assert false : "No node for key: " + key;
149: return null;
150: }
151:
152: public void addNotify() {
153: JsfProjectUtils
154: .addJsfFrameworkChangeListener(project, this );
155: }
156:
157: public void removeNotify() {
158: JsfProjectUtils.removeJsfFrameworkChangeListener(project,
159: this );
160: }
161:
162: public void propertyChange(PropertyChangeEvent evt) {
163: // The caller holds ProjectManager.mutex() read lock
164: SwingUtilities.invokeLater(new Runnable() {
165: public void run() {
166: fireChange();
167: }
168: });
169: }
170:
171: }
172: }
|