Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2008 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 |
* If you wish your version of this file to be governed by only the CDDL |
25 |
* or only the GPL Version 2, indicate your decision by adding |
26 |
* "[Contributor] elects to include this software in this distribution |
27 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
28 |
* single choice of license, a recipient has the option to distribute |
29 |
* your version of this file under either the CDDL, the GPL Version 2 or |
30 |
* to extend the choice of license to its licensees as provided above. |
31 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
32 |
* Version 2 license, then the option applies only if the new code is |
33 |
* made subject to such option by the copyright holder. |
34 |
* |
35 |
* Contributor(s): |
36 |
* |
37 |
* The Original Software is NetBeans. The Initial Developer of the Original |
38 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2008 Sun |
39 |
* Microsystems, Inc. All Rights Reserved. |
40 |
* |
41 |
* If you wish your version of this file to be governed by only the CDDL |
42 |
* or only the GPL Version 2, indicate your decision by adding |
43 |
* "[Contributor] elects to include this software in this distribution |
44 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
45 |
* single choice of license, a recipient has the option to distribute |
46 |
* your version of this file under either the CDDL, the GPL Version 2 or |
47 |
* to extend the choice of license to its licensees as provided above. |
48 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
49 |
* Version 2 license, then the option applies only if the new code is |
50 |
* made subject to such option by the copyright holder. |
51 |
*/ |
52 |
|
53 |
package org.netbeans.modules.javafx.editor.semantic; |
54 |
|
55 |
import java.awt.event.ActionEvent; |
56 |
import javax.swing.text.Document; |
57 |
import javax.swing.text.JTextComponent; |
58 |
import org.netbeans.editor.BaseAction; |
59 |
import org.netbeans.spi.editor.highlighting.HighlightsSequence; |
60 |
import org.netbeans.spi.editor.highlighting.support.OffsetsBag; |
61 |
import org.openide.awt.StatusDisplayer; |
62 |
import org.openide.util.NbBundle; |
63 |
|
64 |
/** |
65 |
* (COPIED FROM THE JAVA MODULE!!!) |
66 |
* @author Vladimir Voskresensky |
67 |
*/ |
68 |
public class GoToMarkOccurrencesAction extends BaseAction { |
69 |
|
70 |
private static final String prevActionName = "java-prev-marked-occurrence"; // NOI18N |
71 |
private static final String nextActionName = "java-next-marked-occurrence"; // NOI18N |
72 |
|
73 |
private final boolean next; |
74 |
|
75 |
public GoToMarkOccurrencesAction(boolean nextOccurrence) { |
76 |
super(getNameString(nextOccurrence)); |
77 |
this.next = nextOccurrence; |
78 |
putValue(SHORT_DESCRIPTION, getDefaultShortDescription()); |
79 |
} |
80 |
|
81 |
public void actionPerformed(ActionEvent evt, JTextComponent txt) { |
82 |
navigateToOccurence(next, txt); |
83 |
} |
84 |
|
85 |
@Override |
86 |
protected Object getDefaultShortDescription() { |
87 |
return NbBundle.getMessage(GoToMarkOccurrencesAction.class, getNameString(next)); |
88 |
} |
89 |
|
90 |
private static String getNameString(boolean nextOccurrence) { |
91 |
return nextOccurrence ? nextActionName : prevActionName; |
92 |
} |
93 |
|
94 |
@SuppressWarnings("empty-statement") |
95 |
private static int findOccurrencePosition(boolean directionForward, Document doc, int curPos) { |
96 |
OffsetsBag bag = MarkOccurrencesHighlighter.getHighlightsBag(doc); |
97 |
HighlightsSequence hs = bag.getHighlights(0, doc.getLength()); |
98 |
|
99 |
if (hs.moveNext()) { |
100 |
if (directionForward) { |
101 |
int firstStart = hs.getStartOffset(), firstEnd = hs.getEndOffset(); |
102 |
|
103 |
while (hs.getStartOffset() <= curPos && hs.moveNext()); |
104 |
|
105 |
if (hs.getStartOffset() > curPos) { |
106 |
// we found next occurrence |
107 |
//return hs.getStartOffset(); |
108 |
// Workaround for the fact that cycling through mark occurrences |
109 |
// sometimes "looses" the marked symbol when the caret is right |
110 |
// at the beginning of an identifier. So instead place the caret |
111 |
// in the -middle- of the identifier |
112 |
return (hs.getStartOffset()+hs.getEndOffset())/2; |
113 |
} else if (!(firstEnd >= curPos && firstStart <= curPos)) { |
114 |
// cyclic jump to first occurrence unless we already there |
115 |
//return firstStart; |
116 |
// Middle -- see comment above |
117 |
return (firstStart+firstEnd)/2; |
118 |
} |
119 |
} else { |
120 |
int current = hs.getStartOffset(), last; |
121 |
boolean stuck = false; |
122 |
do { |
123 |
last = current; |
124 |
current = hs.getStartOffset(); |
125 |
} while (hs.getEndOffset() < curPos && (stuck = hs.moveNext())); |
126 |
|
127 |
if (last == current) { |
128 |
// we got no options to jump, cyclic jump to last in file unless we already there |
129 |
while (hs.moveNext()); |
130 |
if (!(hs.getEndOffset() >= curPos && hs.getStartOffset() <= curPos)) { |
131 |
return hs.getStartOffset(); |
132 |
} |
133 |
} else if (stuck) { |
134 |
// just move to previous occurence |
135 |
return last; |
136 |
} else { |
137 |
// it was last occurence in the file |
138 |
return current; |
139 |
} |
140 |
} |
141 |
} |
142 |
return -1; |
143 |
} |
144 |
|
145 |
private static void navigateToOccurence(boolean next, JTextComponent txt) { |
146 |
if (txt != null && txt.getDocument() != null) { |
147 |
Document doc = txt.getDocument(); |
148 |
int position = txt.getCaretPosition(); |
149 |
int goTo = findOccurrencePosition(next, doc, position); |
150 |
if (goTo > 0) { |
151 |
txt.setCaretPosition(goTo); |
152 |
} else { |
153 |
StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(GoToMarkOccurrencesAction.class, "java-no-marked-occurrence")); |
154 |
} |
155 |
} |
156 |
|
157 |
} |
158 |
} |