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.HashMap;
045: import java.util.List;
046: import java.util.Map;
047: import java.util.WeakHashMap;
048: import javax.swing.event.CaretEvent;
049: import javax.swing.event.CaretListener;
050: import javax.swing.event.ChangeEvent;
051: import javax.swing.event.ChangeListener;
052: import javax.swing.text.JTextComponent;
053: import org.netbeans.napi.gsfret.source.Phase;
054: import org.netbeans.napi.gsfret.source.Source.Priority;
055: import org.netbeans.napi.gsfret.source.SourceTaskFactory;
056: import org.openide.filesystems.FileObject;
057: import org.openide.util.RequestProcessor;
058:
059: /**
060: * This file is originally from Retouche, the Java Support
061: * infrastructure in NetBeans. I have modified the file as little
062: * as possible to make merging Retouche fixes back as simple as
063: * possible.
064: *
065: * A {@link SourceTaskFactorySupport} that registers tasks to all files that are
066: * opened in the editor and are visible. This factory also listens on the caret on
067: * opened and visible JTextComponents and reschedules the tasks as necessary.
068: *
069: * The tasks may access current caret position using {@link #getLastPosition} method.
070: *
071: * @author Jan Lahoda
072: */
073: public abstract class CaretAwareSourceTaskFactory extends
074: SourceTaskFactory {
075:
076: private static final int DEFAULT_RESCHEDULE_TIMEOUT = 300;
077: private static final RequestProcessor WORKER = new RequestProcessor(
078: "CaretAwareSourceTaskFactory worker");
079:
080: private int timeout;
081:
082: /**Construct the CaretAwareSourceTaskFactory with given {@link Phase} and {@link Priority}.
083: *
084: * @param phase phase to use for tasks created by {@link #createTask}
085: * @param priority priority to use for tasks created by {@link #createTask}
086: */
087: public CaretAwareSourceTaskFactory(Phase phase, Priority priority) {
088: super (phase, priority);
089: //XXX: weak, or something like this:
090: OpenedEditors.getDefault().addChangeListener(
091: new ChangeListenerImpl());
092: this .timeout = DEFAULT_RESCHEDULE_TIMEOUT;
093: }
094:
095: /**@inheritDoc*/
096: public List<FileObject> getFileObjects() {
097: List<FileObject> files = new ArrayList<FileObject>(
098: OpenedEditors.getDefault().getVisibleEditorsFiles());
099:
100: return files;
101: }
102:
103: private Map<JTextComponent, ComponentListener> component2Listener = new HashMap();
104: private static Map<FileObject, Integer> file2LastPosition = new WeakHashMap();
105:
106: /**Returns current caret position in current {@link JTextComponent} for a given file.
107: *
108: * @param file file from which the position should be found
109: * @return caret position in the current {@link JTextComponent} for a given file.
110: */
111: public synchronized static int getLastPosition(FileObject file) {
112: if (file == null) {
113: throw new NullPointerException("Cannot pass null file!");
114: }
115:
116: Integer position = file2LastPosition.get(file);
117:
118: if (position == null) {
119: //no position set yet:
120: // XXX Shouldn't that be -1?
121: return 0;
122: }
123:
124: return position;
125: }
126:
127: synchronized static void setLastPosition(FileObject file,
128: int position) {
129: file2LastPosition.put(file, position);
130: }
131:
132: private class ChangeListenerImpl implements ChangeListener {
133:
134: public void stateChanged(ChangeEvent e) {
135: List<JTextComponent> added = new ArrayList<JTextComponent>(
136: OpenedEditors.getDefault().getVisibleEditors());
137: List<JTextComponent> removed = new ArrayList<JTextComponent>(
138: component2Listener.keySet());
139:
140: added.removeAll(component2Listener.keySet());
141: removed.removeAll(OpenedEditors.getDefault()
142: .getVisibleEditors());
143:
144: for (JTextComponent c : removed) {
145: c.removeCaretListener(component2Listener.remove(c));
146: }
147:
148: for (JTextComponent c : added) {
149: ComponentListener l = new ComponentListener(c);
150:
151: c.addCaretListener(l);
152: component2Listener.put(c, l);
153:
154: //TODO: are we in AWT Thread?:
155: setLastPosition(OpenedEditors.getFileObject(c), c
156: .getCaretPosition());
157: }
158:
159: fileObjectsChanged();
160: }
161:
162: }
163:
164: private class ComponentListener implements CaretListener {
165:
166: private JTextComponent component;
167: private final RequestProcessor.Task rescheduleTask;
168:
169: public ComponentListener(JTextComponent component) {
170: this .component = component;
171: rescheduleTask = WORKER.create(new Runnable() {
172: public void run() {
173: FileObject file = OpenedEditors
174: .getFileObject(ComponentListener.this .component);
175:
176: if (file != null) {
177: setLastPosition(file,
178: ComponentListener.this .component
179: .getCaretPosition());
180: reschedule(file);
181: }
182: }
183: });
184: }
185:
186: public void caretUpdate(CaretEvent e) {
187: FileObject file = OpenedEditors.getFileObject(component);
188:
189: if (file != null) {
190: setLastPosition(file, component.getCaretPosition());
191: rescheduleTask.schedule(timeout);
192: }
193: }
194:
195: }
196: }
|