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-2007 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.
42
package org.netbeans.modules.editor.java;
44
import com.sun.source.tree.Tree;
45
import com.sun.source.util.SourcePositions;
46
import com.sun.source.util.TreePath;
47
import java.awt.event.ActionEvent;
48
import java.io.IOException;
49
import java.util.ArrayList;
50
import java.util.List;
51
import java.util.MissingResourceException;
52
import javax.swing.Action;
53
import javax.swing.event.CaretEvent;
54
import javax.swing.event.CaretListener;
55
import javax.swing.text.Caret;
56
import javax.swing.text.JTextComponent;
57
import org.netbeans.api.java.source.Task;
58
import org.netbeans.api.java.source.CompilationController;
59
import org.netbeans.api.java.source.JavaSource;
60
import org.netbeans.api.java.source.JavaSource.Phase;
61
import org.netbeans.editor.BaseAction;
62
import org.openide.ErrorManager;
63
import org.openide.util.NbBundle;
66
* Code selection according to syntax tree.
68
* TODO: javadoc selection
70
* @author Miloslav Metelka, Jan Pokorsky
72
final class SelectCodeElementAction extends BaseAction {
74
private boolean selectNext;
77
* Construct new action that selects next/previous code elements
78
* according to the language model.
81
* @param name name of the action (should be one of
83
* <code>JavaKit.selectNextElementAction</code>
84
* <code>JavaKit.selectPreviousElementAction</code>
85
* @param selectNext <code>true</code> if the next element should be selected.
86
* <code>False</code> if the previous element should be selected.
88
public SelectCodeElementAction(String name, boolean selectNext) {
90
this.selectNext = selectNext;
91
String desc = getShortDescription();
93
putValue(SHORT_DESCRIPTION, desc);
97
public String getShortDescription(){
98
String name = (String)getValue(Action.NAME);
99
if (name == null) return null;
102
shortDesc = NbBundle.getBundle(JavaKit.class).getString(name); // NOI18N
103
}catch (MissingResourceException mre){
109
public void actionPerformed(ActionEvent evt, JTextComponent target) {
110
if (target != null) {
111
int selectionStartOffset = target.getSelectionStart();
112
int selectionEndOffset = target.getSelectionEnd();
113
if (selectionEndOffset > selectionStartOffset || selectNext) {
114
SelectionHandler handler = (SelectionHandler)target.getClientProperty(SelectionHandler.class);
115
if (handler == null) {
116
handler = new SelectionHandler(target);
117
target.addCaretListener(handler);
118
// No need to remove the listener above as the handler
119
// is stored is the client-property of the component itself
120
target.putClientProperty(SelectionHandler.class, handler);
123
if (selectNext) { // select next element
124
handler.selectNext();
125
} else { // select previous
126
handler.selectPrevious();
132
private static final class SelectionHandler implements CaretListener, Task<CompilationController>, Runnable {
134
private JTextComponent target;
135
private SelectionInfo[] selectionInfos;
136
private int selIndex = -1;
137
private boolean ignoreNextCaretUpdate;
139
SelectionHandler(JTextComponent target) {
140
this.target = target;
143
public void selectNext() {
144
if (selectionInfos == null) {
145
JavaSource js = JavaSource.forDocument(target.getDocument());
147
js.runUserActionTask(this, true);
148
} catch (IOException ex) {
149
ErrorManager.getDefault().notify(ex);
156
public synchronized void selectPrevious() {
158
select(selectionInfos[--selIndex]);
162
private void select(SelectionInfo selectionInfo) {
163
Caret caret = target.getCaret();
164
markIgnoreNextCaretUpdate();
165
caret.setDot(selectionInfo.getStartOffset());
166
markIgnoreNextCaretUpdate();
167
caret.moveDot(selectionInfo.getEndOffset());
170
private void markIgnoreNextCaretUpdate() {
171
ignoreNextCaretUpdate = true;
174
public void caretUpdate(CaretEvent e) {
175
if (!ignoreNextCaretUpdate) {
176
synchronized (this) {
177
selectionInfos = null;
181
ignoreNextCaretUpdate = false;
185
public void run(CompilationController cc) {
187
cc.toPhase(Phase.RESOLVED);
188
selectionInfos = initSelectionPath(target, cc);
189
} catch (IOException ex) {
190
ErrorManager.getDefault().notify(ex);
194
private SelectionInfo[] initSelectionPath(JTextComponent target, CompilationController ci) {
195
List<SelectionInfo> positions = new ArrayList<SelectionInfo>();
196
SourcePositions sp = ci.getTrees().getSourcePositions();
197
TreePath tp = ci.getTreeUtilities().pathFor(target.getCaretPosition());
198
for (Tree tree: tp) {
199
int startPos = (int)sp.getStartPosition(tp.getCompilationUnit(), tree);
200
int endPos = (int)sp.getEndPosition(tp.getCompilationUnit(), tree);
201
positions.add(new SelectionInfo(startPos, endPos));
203
return positions.toArray(new SelectionInfo[positions.size()]);
207
if (selIndex < selectionInfos.length - 1) {
208
select(selectionInfos[++selIndex]);
214
private static final class SelectionInfo {
216
private int startOffset;
217
private int endOffset;
219
SelectionInfo(int startOffset, int endOffset) {
220
this.startOffset = startOffset;
221
this.endOffset = endOffset;
224
public int getStartOffset() {
228
public int getEndOffset() {