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