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.tasklist.projectint;
043:
044: import java.awt.Image;
045: import java.beans.PropertyChangeEvent;
046: import java.beans.PropertyChangeListener;
047: import java.util.Collection;
048: import java.util.Iterator;
049: import javax.swing.SwingUtilities;
050: import org.netbeans.api.project.FileOwnerQuery;
051: import org.netbeans.api.project.Project;
052: import org.netbeans.api.project.ui.OpenProjects;
053: import org.netbeans.spi.project.SubprojectProvider;
054: import org.netbeans.spi.tasklist.TaskScanningScope;
055: import org.openide.filesystems.FileObject;
056: import org.openide.util.Lookup;
057: import org.openide.util.NbBundle;
058: import org.openide.util.Utilities;
059: import org.openide.util.lookup.AbstractLookup;
060: import org.openide.util.lookup.InstanceContent;
061: import org.openide.windows.TopComponent;
062:
063: /**
064: * Task scanning scope for the main project and all opened projects that depend on it.
065: *
066: * @author S. Aubrecht
067: */
068: public class MainProjectScanningScope extends TaskScanningScope
069: implements PropertyChangeListener, Runnable {
070:
071: private Callback callback;
072: private InstanceContent lookupContent = new InstanceContent();
073: private Lookup lookup;
074: private Project currentProject;
075: private Collection<FileObject> editedFiles;
076:
077: private MainProjectScanningScope(String displayName,
078: String description, Image icon) {
079: super (displayName, description, icon, true);
080: }
081:
082: /**
083: * @return New instance of MainProjectScanningScope
084: */
085: public static MainProjectScanningScope create() {
086: return new MainProjectScanningScope(
087: NbBundle.getBundle(MainProjectScanningScope.class)
088: .getString("LBL_MainProjectScope"), //NOI18N
089: NbBundle.getBundle(MainProjectScanningScope.class)
090: .getString("HINT_MainProjectScope"), //NOI18N
091: Utilities
092: .loadImage("org/netbeans/modules/tasklist/projectint/main_project_scope.png") //NOI18N
093: );
094: }
095:
096: public Iterator<FileObject> iterator() {
097: return new MainProjectIterator(editedFiles);
098: }
099:
100: @Override
101: public boolean isInScope(FileObject resource) {
102: if (null == resource || null == currentProject)
103: return false;
104:
105: Project owner = FileOwnerQuery.getOwner(resource);
106: if (null == owner)
107: return false;
108:
109: if (owner.equals(currentProject))
110: return true;
111:
112: Project[] projects = OpenProjects.getDefault()
113: .getOpenProjects();
114: for (int i = 0; i < projects.length; i++) {
115: if (projects[i].equals(currentProject))
116: continue;
117:
118: SubprojectProvider subProjectProvider = projects[i]
119: .getLookup().lookup(SubprojectProvider.class);
120: if (null != subProjectProvider
121: && subProjectProvider.getSubprojects().contains(
122: currentProject)
123: && projects[i].equals(owner)) {
124: return true;
125: }
126: }
127:
128: return false;
129: }
130:
131: public Lookup getLookup() {
132: if (null == lookup) {
133: lookup = new AbstractLookup(lookupContent);
134: }
135: return lookup;
136: }
137:
138: public void attach(Callback newCallback) {
139: if (null != newCallback && null == callback) {
140: OpenProjects.getDefault().addPropertyChangeListener(this );
141: TopComponent.getRegistry().addPropertyChangeListener(this );
142: setLookupContent(OpenProjects.getDefault().getMainProject());
143: if (SwingUtilities.isEventDispatchThread()) {
144: run();
145: } else {
146: SwingUtilities.invokeLater(this );
147: }
148: } else if (null == newCallback && null != callback) {
149: OpenProjects.getDefault()
150: .removePropertyChangeListener(this );
151: TopComponent.getRegistry().removePropertyChangeListener(
152: this );
153: editedFiles = null;
154: setLookupContent(null);
155: }
156: this .callback = newCallback;
157: }
158:
159: public void propertyChange(PropertyChangeEvent e) {
160: if (OpenProjects.PROPERTY_MAIN_PROJECT.equals(e
161: .getPropertyName())) {
162: if (null != callback) {
163: setLookupContent(OpenProjects.getDefault()
164: .getMainProject());
165: callback.refresh();
166: }
167: } else if (TopComponent.Registry.PROP_OPENED.equals(e
168: .getPropertyName())) {
169: //remember which files are opened so that they can be scanned first
170: run();
171: }
172: }
173:
174: public void run() {
175: editedFiles = Utils.collectEditedFiles();
176: }
177:
178: private void setLookupContent(Project newProject) {
179: if (null != currentProject) {
180: lookupContent.remove(currentProject);
181: }
182: if (null != newProject) {
183: lookupContent.add(newProject);
184: }
185: currentProject = newProject;
186: }
187: }
|