2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
41
package org.netbeans.api.java.source.support;
43
import java.util.ArrayList;
44
import java.util.Arrays;
45
import java.util.HashMap;
46
import java.util.List;
48
import java.util.WeakHashMap;
49
import javax.swing.event.CaretEvent;
50
import javax.swing.event.CaretListener;
51
import javax.swing.event.ChangeEvent;
52
import javax.swing.event.ChangeListener;
53
import javax.swing.text.JTextComponent;
54
import org.netbeans.api.java.source.JavaSource.Phase;
55
import org.netbeans.api.java.source.JavaSource.Priority;
56
import org.netbeans.api.java.source.JavaSourceTaskFactory;
57
import org.netbeans.api.java.source.SourceUtils;
58
import org.openide.filesystems.FileObject;
59
import org.openide.util.RequestProcessor;
61
/**A {@link JavaSourceTaskFactorySupport} that registers tasks to all files that are
62
* opened in the editor and are visible. This factory also listens on the caret on
63
* opened and visible JTextComponents and reschedules the tasks as necessary.
65
* The tasks may access current caret position using {@link #getLastPosition} method.
69
public abstract class CaretAwareJavaSourceTaskFactory extends JavaSourceTaskFactory {
71
private static final int DEFAULT_RESCHEDULE_TIMEOUT = 300;
72
private static final RequestProcessor WORKER = new RequestProcessor("CaretAwareJavaSourceTaskFactory worker");
75
private String[] supportedMimeTypes;
77
/**Construct the CaretAwareJavaSourceTaskFactory with given {@link Phase} and {@link Priority}.
79
* @param phase phase to use for tasks created by {@link #createTask}
80
* @param priority priority to use for tasks created by {@link #createTask}
82
public CaretAwareJavaSourceTaskFactory(Phase phase, Priority priority) {
83
this(phase, priority, (String[]) null);
86
/**Construct the CaretAwareJavaSourceTaskFactory with given {@link Phase} and {@link Priority}.
88
* @param phase phase to use for tasks created by {@link #createTask}
89
* @param priority priority to use for tasks created by {@link #createTask}
90
* @param supportedMimeTypes a list of mime types on which the tasks created by this factory should be run
93
public CaretAwareJavaSourceTaskFactory(Phase phase, Priority priority, String... supportedMimeTypes) {
94
super(phase, priority);
95
//XXX: weak, or something like this:
96
OpenedEditors.getDefault().addChangeListener(new ChangeListenerImpl());
97
this.timeout = DEFAULT_RESCHEDULE_TIMEOUT;
98
this.supportedMimeTypes = supportedMimeTypes != null ? supportedMimeTypes.clone() : null;
102
public List<FileObject> getFileObjects() {
103
List<FileObject> files = OpenedEditors.filterSupportedMIMETypes(OpenedEditors.getDefault().getVisibleEditorsFiles(), supportedMimeTypes);
108
private Map<JTextComponent, ComponentListener> component2Listener = new HashMap<JTextComponent, ComponentListener>();
109
private static Map<FileObject, Integer> file2LastPosition = new WeakHashMap<FileObject, Integer>();
111
/**Returns current caret position in current {@link JTextComponent} for a given file.
113
* @param file file from which the position should be found
114
* @return caret position in the current {@link JTextComponent} for a given file.
116
public synchronized static int getLastPosition(FileObject file) {
118
throw new NullPointerException("Cannot pass null file!");
121
Integer position = file2LastPosition.get(file);
123
if (position == null) {
124
//no position set yet:
131
synchronized static void setLastPosition(FileObject file, int position) {
132
file2LastPosition.put(file, position);
135
private class ChangeListenerImpl implements ChangeListener {
137
public void stateChanged(ChangeEvent e) {
138
List<JTextComponent> added = new ArrayList<JTextComponent>(OpenedEditors.getDefault().getVisibleEditors());
139
List<JTextComponent> removed = new ArrayList<JTextComponent>(component2Listener.keySet());
141
added.removeAll(component2Listener.keySet());
142
removed.removeAll(OpenedEditors.getDefault().getVisibleEditors());
144
for (JTextComponent c : removed) {
145
c.removeCaretListener(component2Listener.remove(c));
148
for (JTextComponent c : added) {
149
ComponentListener l = new ComponentListener(c);
151
c.addCaretListener(l);
152
component2Listener.put(c, l);
154
//TODO: are we in AWT Thread?:
155
setLastPosition(OpenedEditors.getFileObject(c), c.getCaretPosition());
158
fileObjectsChanged();
163
private class ComponentListener implements CaretListener {
165
private JTextComponent component;
166
private final RequestProcessor.Task rescheduleTask;
168
public ComponentListener(JTextComponent component) {
169
this.component = component;
170
rescheduleTask = WORKER.create(new Runnable() {
172
FileObject file = OpenedEditors.getFileObject(ComponentListener.this.component);
175
setLastPosition(file, ComponentListener.this.component.getCaretPosition());
182
public void caretUpdate(CaretEvent e) {
183
FileObject file = OpenedEditors.getFileObject(component);
186
setLastPosition(file, component.getCaretPosition());
187
rescheduleTask.schedule(timeout);