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.impl;
043:
044: import java.awt.Image;
045: import java.beans.PropertyChangeEvent;
046: import java.beans.PropertyChangeListener;
047: import java.util.ArrayList;
048: import java.util.Collection;
049: import java.util.Iterator;
050: import javax.swing.SwingUtilities;
051: import org.netbeans.spi.tasklist.TaskScanningScope;
052: import org.openide.filesystems.FileObject;
053: import org.openide.loaders.DataObject;
054: import org.openide.util.Lookup;
055: import org.openide.util.NbBundle;
056: import org.openide.util.Utilities;
057: import org.openide.util.lookup.AbstractLookup;
058: import org.openide.util.lookup.InstanceContent;
059: import org.openide.windows.TopComponent;
060: import org.openide.windows.WindowManager;
061:
062: /**
063: * Default implementtion of TaskScanningScope. The scope is the currently edited file.
064: *
065: * @author S. Aubrecht
066: */
067: public class CurrentEditorScanningScope extends TaskScanningScope
068: implements PropertyChangeListener, Runnable {
069:
070: private FileObject currentFile = null;
071: private Callback callback;
072: private InstanceContent lookupContent = new InstanceContent();
073: private Lookup lookup;
074:
075: /** Creates a new instance of CurrentEditorScope */
076: public CurrentEditorScanningScope(String displayName,
077: String description, Image icon) {
078: super (displayName, description, icon);
079: }
080:
081: public static CurrentEditorScanningScope create() {
082: return new CurrentEditorScanningScope(
083: NbBundle.getBundle(CurrentEditorScanningScope.class)
084: .getString("LBL_CurrentEditorScope"), //NOI18N)
085: NbBundle.getBundle(CurrentEditorScanningScope.class)
086: .getString("HINT_CurrentEditorScope"), //NOI18N
087: Utilities
088: .loadImage("org/netbeans/modules/tasklist/ui/resources/cur_editor_scope.png") //NOI18N
089: );
090: }
091:
092: public Iterator<FileObject> iterator() {
093: ArrayList<FileObject> list = new ArrayList<FileObject>(1);
094: if (null != currentFile)
095: list.add(currentFile);
096: return list.iterator();
097: }
098:
099: @Override
100: public boolean isInScope(FileObject resource) {
101: if (null == resource)
102: return false;
103: return null != currentFile && currentFile.equals(resource);
104: }
105:
106: public Lookup getLookup() {
107: if (null == lookup) {
108: lookup = new AbstractLookup(lookupContent);
109: }
110: return lookup;
111: }
112:
113: public void attach(Callback newCallback) {
114: if (null != newCallback && null == callback) {
115: WindowManager.getDefault().getRegistry()
116: .addPropertyChangeListener(this );
117: } else if (null == newCallback && null != callback) {
118: WindowManager.getDefault().getRegistry()
119: .removePropertyChangeListener(this );
120: if (null != currentFile) {
121: lookupContent.remove(currentFile);
122: }
123: currentFile = null;
124: }
125: if (null != newCallback && newCallback != this .callback) {
126: this .callback = newCallback;
127: if (SwingUtilities.isEventDispatchThread()) {
128: run();
129: } else {
130: SwingUtilities.invokeLater(this );
131: }
132: }
133: this .callback = newCallback;
134: }
135:
136: public void propertyChange(PropertyChangeEvent e) {
137: if (TopComponent.Registry.PROP_ACTIVATED_NODES.equals(e
138: .getPropertyName())
139: || TopComponent.Registry.PROP_OPENED.equals(e
140: .getPropertyName())
141: || TopComponent.Registry.PROP_ACTIVATED.equals(e
142: .getPropertyName())) {
143:
144: run();
145: }
146: }
147:
148: public void run() {
149: FileObject newActiveFile = getCurrentFile();
150: if ((null == currentFile && null != newActiveFile)
151: || (null != currentFile && null == newActiveFile)
152: || (null != currentFile && null != newActiveFile && !currentFile
153: .equals(newActiveFile))) {
154:
155: if (null != currentFile)
156: lookupContent.remove(currentFile);
157: if (null != newActiveFile)
158: lookupContent.add(newActiveFile);
159: currentFile = newActiveFile;
160: //notify the TaskManager that user activated other file
161: if (null != callback)
162: callback.refresh();
163: } else {
164: currentFile = newActiveFile;
165: }
166: }
167:
168: private FileObject getCurrentFile() {
169: TopComponent.Registry registry = TopComponent.getRegistry();
170:
171: TopComponent activeTc = registry.getActivated();
172: FileObject newFile = getFileFromTopComponent(activeTc);
173:
174: ArrayList<FileObject> availableFiles = new ArrayList<FileObject>(
175: 3);
176: if (null == newFile) {
177: Collection<TopComponent> openedTcs = new ArrayList<TopComponent>(
178: registry.getOpened());
179: for (Iterator i = openedTcs.iterator(); i.hasNext();) {
180: TopComponent tc = (TopComponent) i.next();
181:
182: FileObject file = getFileFromTopComponent(tc);
183: if (null != file) {
184: availableFiles.add(file);
185: }
186: }
187: if (null != currentFile
188: && (availableFiles.contains(currentFile)))
189: newFile = currentFile;
190: else if (availableFiles.size() > 0)
191: newFile = availableFiles.get(0);
192: }
193: return newFile;
194: }
195:
196: private FileObject getFileFromTopComponent(final TopComponent tc) {
197: if (null == tc || !tc.isShowing())
198: return null;
199: if (WindowManager.getDefault().isOpenedEditorTopComponent(tc)) {
200: DataObject dob = tc.getLookup().lookup(DataObject.class);
201: if (null != dob) {
202: return dob.getPrimaryFile();
203: }
204: }
205: return null;
206: }
207: }
|