~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to java/source/src/org/netbeans/api/java/source/support/CaretAwareJavaSourceTaskFactory.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
package org.netbeans.api.java.source.support;
 
42
 
 
43
import java.util.ArrayList;
 
44
import java.util.Arrays;
 
45
import java.util.HashMap;
 
46
import java.util.List;
 
47
import java.util.Map;
 
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;
 
60
 
 
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.
 
64
 *
 
65
 * The tasks may access current caret position using {@link #getLastPosition} method.
 
66
 * 
 
67
 * @author Jan Lahoda
 
68
 */
 
69
public abstract class CaretAwareJavaSourceTaskFactory extends JavaSourceTaskFactory {
 
70
    
 
71
    private static final int DEFAULT_RESCHEDULE_TIMEOUT = 300;
 
72
    private static final RequestProcessor WORKER = new RequestProcessor("CaretAwareJavaSourceTaskFactory worker");
 
73
    
 
74
    private int timeout;
 
75
    private String[] supportedMimeTypes;
 
76
    
 
77
    /**Construct the CaretAwareJavaSourceTaskFactory with given {@link Phase} and {@link Priority}.
 
78
     *
 
79
     * @param phase phase to use for tasks created by {@link #createTask}
 
80
     * @param priority priority to use for tasks created by {@link #createTask}
 
81
     */
 
82
    public CaretAwareJavaSourceTaskFactory(Phase phase, Priority priority) {
 
83
        this(phase, priority, (String[]) null);
 
84
    }
 
85
    
 
86
    /**Construct the CaretAwareJavaSourceTaskFactory with given {@link Phase} and {@link Priority}.
 
87
     *
 
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
 
91
     * @since 0.21
 
92
     */
 
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;
 
99
    }
 
100
    
 
101
    /**@inheritDoc*/
 
102
    public List<FileObject> getFileObjects() {
 
103
        List<FileObject> files = OpenedEditors.filterSupportedMIMETypes(OpenedEditors.getDefault().getVisibleEditorsFiles(), supportedMimeTypes);
 
104
 
 
105
        return files;
 
106
    }
 
107
 
 
108
    private Map<JTextComponent, ComponentListener> component2Listener = new HashMap<JTextComponent, ComponentListener>();
 
109
    private static Map<FileObject, Integer> file2LastPosition = new WeakHashMap<FileObject, Integer>();
 
110
    
 
111
    /**Returns current caret position in current {@link JTextComponent} for a given file.
 
112
     *
 
113
     * @param file file from which the position should be found
 
114
     * @return caret position in the current {@link JTextComponent} for a given file.
 
115
     */
 
116
    public synchronized static int getLastPosition(FileObject file) {
 
117
        if (file == null) {
 
118
            throw new NullPointerException("Cannot pass null file!");
 
119
        }
 
120
        
 
121
        Integer position = file2LastPosition.get(file);
 
122
        
 
123
        if (position == null) {
 
124
            //no position set yet:
 
125
            return 0;
 
126
        }
 
127
        
 
128
        return position;    
 
129
    }
 
130
    
 
131
    synchronized static void setLastPosition(FileObject file, int position) {
 
132
       file2LastPosition.put(file, position);
 
133
    }
 
134
    
 
135
    private class ChangeListenerImpl implements ChangeListener {
 
136
        
 
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());
 
140
            
 
141
            added.removeAll(component2Listener.keySet());
 
142
            removed.removeAll(OpenedEditors.getDefault().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.getCaretPosition());
 
156
            }
 
157
            
 
158
            fileObjectsChanged();
 
159
        }
 
160
        
 
161
    }
 
162
    
 
163
    private class ComponentListener implements CaretListener {
 
164
        
 
165
        private JTextComponent component;
 
166
        private final RequestProcessor.Task rescheduleTask;
 
167
        
 
168
        public ComponentListener(JTextComponent component) {
 
169
            this.component = component;
 
170
            rescheduleTask = WORKER.create(new Runnable() {
 
171
                public void run() {
 
172
                    FileObject file = OpenedEditors.getFileObject(ComponentListener.this.component);
 
173
                    
 
174
                    if (file != null) {
 
175
                        setLastPosition(file, ComponentListener.this.component.getCaretPosition());
 
176
                        reschedule(file);
 
177
                    }
 
178
                }
 
179
            });
 
180
        }
 
181
        
 
182
        public void caretUpdate(CaretEvent e) {
 
183
            FileObject file = OpenedEditors.getFileObject(component);
 
184
            
 
185
            if (file != null) {
 
186
                setLastPosition(file, component.getCaretPosition());
 
187
                rescheduleTask.schedule(timeout);
 
188
            }
 
189
        }
 
190
        
 
191
    }
 
192
}