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.dataconnectivity.datasource;
043:
044: import com.sun.rave.designtime.DesignBean;
045: import com.sun.rave.designtime.DesignContext;
046: import java.beans.PropertyChangeEvent;
047: import java.beans.PropertyChangeListener;
048: import java.util.Arrays;
049: import java.util.LinkedHashSet;
050: import java.util.List;
051: import java.util.Set;
052: import org.netbeans.api.project.FileOwnerQuery;
053: import org.netbeans.api.project.Project;
054: import org.netbeans.api.project.ui.OpenProjects;
055: import org.netbeans.modules.visualweb.insync.live.LiveUnit;
056: import org.netbeans.modules.visualweb.insync.models.FacesModel;
057: import org.openide.filesystems.FileObject;
058: import org.openide.loaders.DataObject;
059: import org.openide.util.Lookup;
060: import org.openide.util.Utilities;
061: import org.openide.util.WeakListeners;
062: import org.openide.windows.TopComponent;
063:
064: /**
065: *
066: * @author JohnBaker
067: */
068: public class CurrentProject {
069: private static CurrentProject _instance = null;
070: private Project project = null;
071: protected ProjectsChangedListener changedProjectsListener = new ProjectsChangedListener();
072:
073: /** Creates a new instance of CurrentProject */
074: private CurrentProject() {
075: DataObject obj = Utilities.actionsGlobalContext().lookup(
076: DataObject.class);
077: OpenProjects.getDefault().addPropertyChangeListener(
078: WeakListeners.create(PropertyChangeListener.class,
079: changedProjectsListener, OpenProjects
080: .getDefault()));
081:
082: if (obj != null) {
083: FileObject fileObject = obj.getPrimaryFile();
084: project = FileOwnerQuery.getOwner(fileObject);
085: }
086: }
087:
088: public static CurrentProject getInstance() {
089: if (_instance == null) {
090: _instance = new CurrentProject();
091: }
092:
093: return _instance;
094: }
095:
096: public void setProject(Project project) {
097: this .project = project;
098: }
099:
100: /**
101: * getOpenedProject returns the project that is being opened
102: */
103: public Project getOpenedProject() {
104: if (TopComponent.getRegistry().getActivated() != null) {
105: Lookup lookup = TopComponent.getRegistry().getActivated()
106: .getLookup();
107: DataObject obj = lookup.lookup(DataObject.class);
108:
109: if (obj != null) {
110: FileObject fileObject = obj.getPrimaryFile();
111: project = FileOwnerQuery.getOwner(fileObject);
112: }
113: }
114:
115: // When a project is opened, get the main project if Projects window is not in focus
116: if (project == null) {
117: project = OpenProjects.getDefault().getMainProject();
118: }
119:
120: return project;
121: }
122:
123: // Return project associated with the current page
124: public Project getCurrentProject(DesignBean[] designBeans) {
125: DesignContext context = designBeans[0].getDesignContext();
126: FacesModel model = ((LiveUnit) context).getModel();
127: project = model.getProject();
128: return project;
129: }
130:
131: public class ProjectsChangedListener implements
132: PropertyChangeListener {
133: public void propertyChange(PropertyChangeEvent event) {
134:
135: // The list of open projects has changed; clean up any old projects we may be holding on to.
136: if (OpenProjects.PROPERTY_OPEN_PROJECTS.equals(event
137: .getPropertyName())) {
138: List<Project> oldOpenProjectsList = Arrays
139: .asList((Project[]) event.getOldValue());
140: List<Project> newOpenProjectsList = Arrays
141: .asList((Project[]) event.getNewValue());
142: Set<Project> closedProjectsSet = new LinkedHashSet<Project>(
143: oldOpenProjectsList);
144: closedProjectsSet.removeAll(newOpenProjectsList);
145: for (Project project : closedProjectsSet) {
146: // Project has been closed; null the project
147: if (_instance.project == project) {
148: _instance.project = null;
149: OpenProjects.getDefault()
150: .removePropertyChangeListener(this);
151: }
152: }
153: }
154: }
155: }
156:
157: }
|