KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > source > support > LookupBasedJavaSourceTaskFactory


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.api.java.source.support;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Set JavaDoc;
26 import javax.swing.event.ChangeEvent JavaDoc;
27 import org.netbeans.api.java.source.JavaSource.Phase;
28 import org.netbeans.api.java.source.JavaSource.Priority;
29 import org.netbeans.api.java.source.JavaSourceTaskFactory;
30 import org.openide.filesystems.FileObject;
31 import org.openide.loaders.DataObject;
32 import org.openide.nodes.Node;
33 import org.openide.util.Lookup;
34 import org.openide.util.Lookup.Result;
35 import org.openide.util.LookupEvent;
36 import org.openide.util.LookupListener;
37
38 /**A {@link JavaSourceTaskFactorySupport} that registers tasks to all files that are
39  * found in the given {@link Lookup}.
40  *
41  * This factory searches for {@link FileObject}, {@link DataObject} and {@link Node}
42  * in the lookup. If {@link Node}(s) are found, its/their lookup is searched for
43  * {@link FileObject} and {@link DataObject}.
44  *
45  * @author Jan Lahoda
46  */

47 public abstract class LookupBasedJavaSourceTaskFactory extends JavaSourceTaskFactory {
48
49     private Result<FileObject> fileObjectResult;
50     private Result<DataObject> dataObjectResult;
51     private Result<Node> nodeResult;
52     
53     private List JavaDoc<FileObject> currentFiles;
54     private LookupListener listener;
55
56     /**Construct the LookupBasedJavaSourceTaskFactory with given {@link Phase} and {@link Priority}.
57      *
58      * @param phase phase to use for tasks created by {@link #createTask}
59      * @param priority priority to use for tasks created by {@link #createTask}
60      */

61     public LookupBasedJavaSourceTaskFactory(Phase phase, Priority priority) {
62         super(phase, priority);
63         currentFiles = Collections.emptyList();
64         listener = new LookupListenerImpl();
65     }
66
67     /**Sets a new {@link Lookup} to search.
68      *
69      * @param lookup new {@link Lookup}
70      */

71     protected synchronized final void setLookup(Lookup lookup) {
72         if (fileObjectResult != null) {
73             fileObjectResult.removeLookupListener(listener);
74         }
75         if (dataObjectResult != null) {
76             dataObjectResult.removeLookupListener(listener);
77         }
78         if (nodeResult != null) {
79             nodeResult.removeLookupListener(listener);
80         }
81         fileObjectResult = lookup.lookupResult(FileObject.class);
82         dataObjectResult = lookup.lookupResult(DataObject.class);
83         nodeResult = lookup.lookupResult(Node.class);
84
85         fileObjectResult.addLookupListener(listener);
86         dataObjectResult.addLookupListener(listener);
87         nodeResult.addLookupListener(listener);
88
89         updateCurrentFiles();
90         fileObjectsChanged();
91     }
92
93     private synchronized void updateCurrentFiles() {
94         Set JavaDoc<FileObject> newCurrentFiles = new HashSet JavaDoc();
95
96         newCurrentFiles.addAll(fileObjectResult.allInstances());
97
98         for (DataObject d : dataObjectResult.allInstances()) {
99             newCurrentFiles.add(d.getPrimaryFile());
100         }
101
102         for (Node n : nodeResult.allInstances()) {
103             newCurrentFiles.addAll(n.getLookup().lookupAll(FileObject.class));
104
105             for (DataObject d : n.getLookup().lookupAll(DataObject.class)) {
106                 newCurrentFiles.add(d.getPrimaryFile());
107             }
108         }
109
110         currentFiles = new ArrayList JavaDoc<FileObject>(newCurrentFiles);
111         
112         lookupContentChanged();
113     }
114     
115     /**@inheritDoc*/
116     public synchronized List JavaDoc<FileObject> getFileObjects() {
117         return currentFiles;
118     }
119
120     /**This method is called when the provided Lookup's content changed.
121      * Subclasses may override this method in order to be notified about such change.
122      */

123     protected void lookupContentChanged() {
124     }
125
126     private class LookupListenerImpl implements LookupListener {
127         public void resultChanged(LookupEvent ev) {
128             updateCurrentFiles();
129             fileObjectsChanged();
130         }
131     }
132
133 }
134
Popular Tags