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.modules.cnd.model.tasks;
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.openide.filesystems.FileObject;
054: import org.openide.util.RequestProcessor;
055:
056: public abstract class CaretAwareCsmFileTaskFactory extends
057: CsmFileTaskFactory {
058:
059: private static final int DEFAULT_RESCHEDULE_TIMEOUT = 300;
060: private static final RequestProcessor WORKER = new RequestProcessor(
061: "CaretAwareCsmFileTaskFactory worker"); //NOI18N
062: private int timeout;
063:
064: public CaretAwareCsmFileTaskFactory() {
065: super ();
066: OpenedEditors.getDefault().addChangeListener(
067: new ChangeListenerImpl());
068: this .timeout = DEFAULT_RESCHEDULE_TIMEOUT;
069: }
070:
071: public List<FileObject> getFileObjects() {
072: List<FileObject> files = OpenedEditors
073: .filterSupportedFiles(OpenedEditors.getDefault()
074: .getVisibleEditorsFiles());
075:
076: return files;
077: }
078:
079: private Map<JTextComponent, ComponentListener> component2Listener = new HashMap<JTextComponent, ComponentListener>();
080: private static Map<FileObject, Integer> file2LastPosition = new WeakHashMap<FileObject, Integer>();
081:
082: public synchronized static int getLastPosition(FileObject file) {
083: if (file == null) {
084: throw new NullPointerException("Cannot pass null file!");
085: }
086:
087: Integer position = file2LastPosition.get(file);
088:
089: if (position == null) {
090: //no position set yet:
091: return 0;
092: }
093:
094: return position;
095: }
096:
097: synchronized static void setLastPosition(FileObject file,
098: int position) {
099: file2LastPosition.put(file, position);
100: }
101:
102: private class ChangeListenerImpl implements ChangeListener {
103:
104: public void stateChanged(ChangeEvent e) {
105: List<JTextComponent> added = new ArrayList<JTextComponent>(
106: OpenedEditors.getDefault().getVisibleEditors());
107: List<JTextComponent> removed = new ArrayList<JTextComponent>(
108: component2Listener.keySet());
109:
110: added.removeAll(component2Listener.keySet());
111: removed.removeAll(OpenedEditors.getDefault()
112: .getVisibleEditors());
113:
114: for (JTextComponent c : removed) {
115: c.removeCaretListener(component2Listener.remove(c));
116: }
117:
118: for (JTextComponent c : added) {
119: ComponentListener l = new ComponentListener(c);
120:
121: c.addCaretListener(l);
122: component2Listener.put(c, l);
123:
124: setLastPosition(OpenedEditors.getFileObject(c), c
125: .getCaretPosition());
126: }
127:
128: fileObjectsChanged();
129: }
130: }
131:
132: private class ComponentListener implements CaretListener {
133:
134: private JTextComponent component;
135: private final RequestProcessor.Task rescheduleTask;
136:
137: public ComponentListener(JTextComponent component) {
138: this .component = component;
139: rescheduleTask = WORKER.create(new Runnable() {
140:
141: public void run() {
142: FileObject file = OpenedEditors
143: .getFileObject(ComponentListener.this .component);
144:
145: if (file != null) {
146: setLastPosition(file,
147: ComponentListener.this .component
148: .getCaretPosition());
149: reschedule(file);
150: }
151: }
152: });
153: }
154:
155: public void caretUpdate(CaretEvent e) {
156: FileObject file = OpenedEditors.getFileObject(component);
157:
158: if (file != null) {
159: setLastPosition(file, component.getCaretPosition());
160: rescheduleTask.schedule(timeout);
161: }
162: }
163: }
164: }
|