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: package org.netbeans.modules.visualweb.dataconnectivity.explorer;
042:
043: import java.awt.event.ActionEvent;
044: import java.beans.PropertyChangeListener;
045: import org.netbeans.modules.visualweb.dataconnectivity.datasource.BrokenDataSourceSupport;
046: import javax.swing.Action;
047: import org.openide.util.NbBundle;
048: import org.netbeans.modules.visualweb.dataconnectivity.project.datasource.ProjectDataSourcesListener;
049: import org.netbeans.modules.visualweb.dataconnectivity.project.datasource.ProjectDataSourceTracker;
050: import org.netbeans.modules.visualweb.dataconnectivity.project.datasource.ProjectDataSourcesChangeEvent;
051: import java.awt.Image;
052: import java.io.CharConversionException;
053: import org.netbeans.api.db.explorer.ConnectionListener;
054: import org.netbeans.api.db.explorer.ConnectionManager;
055: import org.netbeans.modules.visualweb.dataconnectivity.datasource.DataSourceResolver;
056: import org.netbeans.modules.visualweb.dataconnectivity.utils.ImportDataSource;
057: import org.netbeans.modules.visualweb.project.jsf.api.JsfProjectUtils;
058: import org.openide.nodes.AbstractNode;
059: import org.openide.nodes.Node;
060: import org.openide.util.Utilities;
061: import org.openide.util.WeakListeners;
062: import org.openide.xml.XMLUtil;
063:
064: /**
065: * Parent node for the data sources in the project navigator
066: * @author Winston Prakash
067: * @author jfbrown - conversion to NB4 projects
068: */
069: public class ProjectDataSourceNode extends AbstractNode implements
070: Node.Cookie, ProjectDataSourcesListener, ConnectionListener {
071:
072: org.netbeans.api.project.Project nbProject = null;
073: private static Image brokenDsReferenceBadge = Utilities
074: .loadImage("org/netbeans/modules/visualweb/dataconnectivity/resources/disconnected.png"); // NOI18N
075: private static Image dSContainerImage = Utilities
076: .loadImage("org/netbeans/modules/visualweb/dataconnectivity/resources/datasource_container.png"); // NOI18N
077:
078: public ProjectDataSourceNode(
079: org.netbeans.api.project.Project project) {
080: super (new ProjectDataSourceNodeChildren(project));
081: nbProject = project;
082:
083: // Create a weak listener so that the connection listener can be GC'd when listener for a project is no longer referenced
084: ConnectionManager.getDefault().addConnectionListener(
085: WeakListeners.create(ConnectionListener.class, this ,
086: ConnectionManager.getDefault()));
087: initPuppy();
088: }
089:
090: protected void addListener() {
091: ProjectDataSourceTracker.addListener(nbProject, this );
092: }
093:
094: protected void removeListener() {
095: ProjectDataSourceTracker.refreshDataSources(nbProject);
096: ProjectDataSourceTracker.removeListener(nbProject, this );
097: }
098:
099: // This allows us to get a reference to ourselves if we're called
100: // from a FilterNode.
101: public Node.Cookie getCookie(Class clazz) {
102: if (clazz == ProjectDataSourceNode.class) {
103: return this ;
104: } else
105: return super .getCookie(clazz);
106: }
107:
108: private void initPuppy() {
109: setIconBaseWithExtension("org/netbeans/modules/visualweb/dataconnectivity/resources/datasource_container.png"); // NOI18N
110: setName(NbBundle.getMessage(ProjectDataSourceNode.class,
111: "PROJECT_DATA_SOURCES"));
112: setDisplayName(NbBundle.getMessage(ProjectDataSourceNode.class,
113: "PROJECT_DATA_SOURCES"));
114: setShortDescription(NbBundle.getMessage(
115: ProjectDataSourceNode.class, "PROJECT_DATA_SOURCES"));
116: setValue("propertiesHelpID",
117: "projrave_ui_elements_project_nav_node_prop_sheets_data_source_node_props"); // NOI18N
118: addListener();
119: }
120:
121: public Action[] getActions(boolean context) {
122: return new Action[] {};
123: }
124:
125: public org.netbeans.api.project.Project getNbProject() {
126: return nbProject;
127: }
128:
129: // For icon badging
130: public Image getIcon(int type) {
131: String dispName = super .getDisplayName();
132: try {
133: dispName = XMLUtil.toElementContent(dispName);
134: } catch (CharConversionException ex) {
135: // ignore
136: }
137:
138: // Manage the migration of legacy projects
139: if (ImportDataSource.isLegacyProject(nbProject)
140: && JsfProjectUtils.getProjectProperty(nbProject,
141: "migrated").equals("")) { //NOI18N
142: DataSourceResolver.getInstance()
143: .modelProjectForDataSources(nbProject);
144: }
145:
146: // Check if Data Source Reference node has any child nodes, if it does, check if any data sources are missing
147: boolean isBroken = false;
148: if (this .getChildren().getNodes().length > 0) {
149: if (BrokenDataSourceSupport.isBroken(nbProject)) {
150: isBroken = true;
151: } else {
152: isBroken = false;
153: }
154: }
155:
156: if (isBroken) {
157: Image brokenBadge = Utilities.mergeImages(dSContainerImage,
158: brokenDsReferenceBadge, 8, 0);
159: if (ImportDataSource.isLegacyProject(nbProject)) {
160: ImportDataSource.showAlert();
161: }
162: return brokenBadge;
163: } else {
164: return dSContainerImage;
165: }
166: }
167:
168: public String getHtmlDisplayName() {
169: String dispName = super .getDisplayName();
170: try {
171: dispName = XMLUtil.toElementContent(dispName);
172: } catch (CharConversionException ex) {
173: // ignore
174: }
175:
176: boolean isBroken = false;
177: // Check if Data Source Reference node has any child nodes, if it does, check if any data sources are missing
178: if (this .getChildren().getNodes().length > 0) {
179: if (BrokenDataSourceSupport.isBroken(nbProject)) {
180: isBroken = true;
181: } else {
182: isBroken = false;
183: }
184: }
185:
186: return isBroken ? "<font color=\"#A40000\">" + dispName
187: + "</font>" : null; //NOI18N;
188: }
189:
190: public void dataSourcesChange(ProjectDataSourcesChangeEvent evt) {
191: fireIconChange();
192: fireDisplayNameChange(null, null);
193: }
194:
195: public void connectionsChanged() {
196: fireIconChange();
197: fireDisplayNameChange(null, null);
198: }
199: }
|