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: package org.netbeans.napi.gsfret.source.support;
042:
043: import java.util.ArrayList;
044: import java.util.Collections;
045: import java.util.HashSet;
046: import java.util.List;
047: import java.util.Set;
048: import javax.swing.event.ChangeEvent;
049: import org.netbeans.napi.gsfret.source.Phase;
050: import org.netbeans.napi.gsfret.source.Source.Priority;
051: import org.netbeans.napi.gsfret.source.SourceTaskFactory;
052: import org.openide.filesystems.FileObject;
053: import org.openide.loaders.DataObject;
054: import org.openide.nodes.Node;
055: import org.openide.util.Lookup;
056: import org.openide.util.Lookup.Result;
057: import org.openide.util.LookupEvent;
058: import org.openide.util.LookupListener;
059:
060: /**
061: * This file is originally from Retouche, the Java Support
062: * infrastructure in NetBeans. I have modified the file as little
063: * as possible to make merging Retouche fixes back as simple as
064: * possible.
065: *
066: * A {@link SourceTaskFactorySupport} that registers tasks to all files that are
067: * found in the given {@link Lookup}.
068: *
069: * This factory searches for {@link FileObject}, {@link DataObject} and {@link Node}
070: * in the lookup. If {@link Node}(s) are found, its/their lookup is searched for
071: * {@link FileObject} and {@link DataObject}.
072: *
073: * @author Jan Lahoda
074: */
075: public abstract class LookupBasedSourceTaskFactory extends
076: SourceTaskFactory {
077:
078: private Result<FileObject> fileObjectResult;
079: private Result<DataObject> dataObjectResult;
080: private Result<Node> nodeResult;
081:
082: private List<FileObject> currentFiles;
083: private LookupListener listener;
084:
085: /**Construct the LookupBasedSourceTaskFactory with given {@link Phase} and {@link Priority}.
086: *
087: * @param phase phase to use for tasks created by {@link #createTask}
088: * @param priority priority to use for tasks created by {@link #createTask}
089: */
090: public LookupBasedSourceTaskFactory(Phase phase, Priority priority) {
091: super (phase, priority);
092: currentFiles = Collections.emptyList();
093: listener = new LookupListenerImpl();
094: }
095:
096: /**Sets a new {@link Lookup} to search.
097: *
098: * @param lookup new {@link Lookup}
099: */
100: protected synchronized final void setLookup(Lookup lookup) {
101: if (fileObjectResult != null) {
102: fileObjectResult.removeLookupListener(listener);
103: }
104: if (dataObjectResult != null) {
105: dataObjectResult.removeLookupListener(listener);
106: }
107: if (nodeResult != null) {
108: nodeResult.removeLookupListener(listener);
109: }
110: fileObjectResult = lookup.lookupResult(FileObject.class);
111: dataObjectResult = lookup.lookupResult(DataObject.class);
112: nodeResult = lookup.lookupResult(Node.class);
113:
114: fileObjectResult.addLookupListener(listener);
115: dataObjectResult.addLookupListener(listener);
116: nodeResult.addLookupListener(listener);
117:
118: updateCurrentFiles();
119: fileObjectsChanged();
120: }
121:
122: private synchronized void updateCurrentFiles() {
123: Set<FileObject> newCurrentFiles = new HashSet();
124:
125: newCurrentFiles.addAll(fileObjectResult.allInstances());
126:
127: for (DataObject d : dataObjectResult.allInstances()) {
128: newCurrentFiles.add(d.getPrimaryFile());
129: }
130:
131: for (Node n : nodeResult.allInstances()) {
132: newCurrentFiles.addAll(n.getLookup().lookupAll(
133: FileObject.class));
134:
135: for (DataObject d : n.getLookup().lookupAll(
136: DataObject.class)) {
137: newCurrentFiles.add(d.getPrimaryFile());
138: }
139: }
140:
141: currentFiles = new ArrayList<FileObject>(newCurrentFiles);
142:
143: lookupContentChanged();
144: }
145:
146: /**@inheritDoc*/
147: public synchronized List<FileObject> getFileObjects() {
148: return currentFiles;
149: }
150:
151: /**This method is called when the provided Lookup's content changed.
152: * Subclasses may override this method in order to be notified about such change.
153: */
154: protected void lookupContentChanged() {
155: }
156:
157: private class LookupListenerImpl implements LookupListener {
158: public void resultChanged(LookupEvent ev) {
159: updateCurrentFiles();
160: fileObjectsChanged();
161: }
162: }
163:
164: }
|