KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > retouche > source > support > LookupBasedSourceTaskFactory


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.retouche.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.retouche.source.Phase;
28 import org.netbeans.api.retouche.source.Source.Priority;
29 import org.netbeans.api.retouche.source.SourceTaskFactory;
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 /**
39  * This file is originally from Retouche, the Java Support
40  * infrastructure in NetBeans. I have modified the file as little
41  * as possible to make merging Retouche fixes back as simple as
42  * possible.
43  *
44  * A {@link SourceTaskFactorySupport} that registers tasks to all files that are
45  * found in the given {@link Lookup}.
46  *
47  * This factory searches for {@link FileObject}, {@link DataObject} and {@link Node}
48  * in the lookup. If {@link Node}(s) are found, its/their lookup is searched for
49  * {@link FileObject} and {@link DataObject}.
50  *
51  * @author Jan Lahoda
52  */

53 public abstract class LookupBasedSourceTaskFactory extends SourceTaskFactory {
54
55     private Result<FileObject> fileObjectResult;
56     private Result<DataObject> dataObjectResult;
57     private Result<Node> nodeResult;
58     
59     private List JavaDoc<FileObject> currentFiles;
60     private LookupListener listener;
61
62     /**Construct the LookupBasedSourceTaskFactory with given {@link Phase} and {@link Priority}.
63      *
64      * @param phase phase to use for tasks created by {@link #createTask}
65      * @param priority priority to use for tasks created by {@link #createTask}
66      */

67     public LookupBasedSourceTaskFactory(Phase phase, Priority priority) {
68         super(phase, priority);
69         currentFiles = Collections.emptyList();
70         listener = new LookupListenerImpl();
71     }
72
73     /**Sets a new {@link Lookup} to search.
74      *
75      * @param lookup new {@link Lookup}
76      */

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

129     protected void lookupContentChanged() {
130     }
131
132     private class LookupListenerImpl implements LookupListener {
133         public void resultChanged(LookupEvent ev) {
134             updateCurrentFiles();
135             fileObjectsChanged();
136         }
137     }
138
139 }
140
Popular Tags