Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2010 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* The Original Software is NetBeans. The Initial Developer of the Original |
41 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2008 Sun |
42 |
* Microsystems, Inc. All Rights Reserved. |
43 |
* |
44 |
* If you wish your version of this file to be governed by only the CDDL |
45 |
* or only the GPL Version 2, indicate your decision by adding |
46 |
* "[Contributor] elects to include this software in this distribution |
47 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
48 |
* single choice of license, a recipient has the option to distribute |
49 |
* your version of this file under either the CDDL, the GPL Version 2 or |
50 |
* to extend the choice of license to its licensees as provided above. |
51 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
52 |
* Version 2 license, then the option applies only if the new code is |
53 |
* made subject to such option by the copyright holder. |
54 |
*/ |
55 |
|
56 |
package org.netbeans.modules.csl.api; |
57 |
|
58 |
import java.awt.event.ActionEvent; |
59 |
import javax.swing.text.Document; |
60 |
import javax.swing.text.JTextComponent; |
61 |
import org.netbeans.editor.BaseAction; |
62 |
import org.netbeans.modules.csl.editor.semantic.MarkOccurrencesHighlighter; |
63 |
import org.netbeans.spi.editor.highlighting.HighlightsSequence; |
64 |
import org.netbeans.spi.editor.highlighting.support.OffsetsBag; |
65 |
import org.openide.awt.StatusDisplayer; |
66 |
import org.openide.util.NbBundle; |
67 |
|
68 |
/** |
69 |
* This file is originally from Retouche, the Java Support |
70 |
* infrastructure in NetBeans. I have modified the file as little |
71 |
* as possible to make merging Retouche fixes back as simple as |
72 |
* possible. |
73 |
* |
74 |
* @todo The Java implementation changed to jumping to the |
75 |
* END of identifiers in this integration: |
76 |
* http://hg.netbeans.org/main/rev/8f417bdb256d |
77 |
* to handle bug 136665 - should we do the same to be |
78 |
* consistent? |
79 |
* |
80 |
* @author Vladimir Voskresensky |
81 |
*/ |
82 |
public final class GoToMarkOccurrencesAction extends BaseAction { |
83 |
|
84 |
private static final String prevActionName = "csl-prev-marked-occurrence"; // NOI18N |
85 |
private static final String nextActionName = "csl-next-marked-occurrence"; // NOI18N |
86 |
|
87 |
private final boolean next; |
88 |
|
89 |
public GoToMarkOccurrencesAction(boolean nextOccurrence) { |
90 |
super(getNameString(nextOccurrence)); |
91 |
this.next = nextOccurrence; |
92 |
putValue(SHORT_DESCRIPTION, getDefaultShortDescription()); |
93 |
} |
94 |
|
95 |
public void actionPerformed(ActionEvent evt, JTextComponent txt) { |
96 |
navigateToOccurence(next, txt); |
97 |
} |
98 |
|
99 |
@Override |
100 |
protected Object getDefaultShortDescription() { |
101 |
return NbBundle.getMessage(GoToMarkOccurrencesAction.class, getNameString(next)); |
102 |
} |
103 |
|
104 |
private static String getNameString(boolean nextOccurrence) { |
105 |
return nextOccurrence ? nextActionName : prevActionName; |
106 |
} |
107 |
|
108 |
@SuppressWarnings("empty-statement") |
109 |
private static int findOccurrencePosition(boolean directionForward, Document doc, int curPos) { |
110 |
OffsetsBag bag = MarkOccurrencesHighlighter.getHighlightsBag(doc); |
111 |
HighlightsSequence hs = bag.getHighlights(0, doc.getLength()); |
112 |
|
113 |
if (hs.moveNext()) { |
114 |
if (directionForward) { |
115 |
int firstStart = hs.getStartOffset(), firstEnd = hs.getEndOffset(); |
116 |
|
117 |
while (hs.getStartOffset() <= curPos && hs.moveNext()); |
118 |
|
119 |
if (hs.getStartOffset() > curPos) { |
120 |
// we found next occurrence |
121 |
return hs.getStartOffset(); |
122 |
} else if (!(firstEnd >= curPos && firstStart <= curPos)) { |
123 |
// cyclic jump to first occurrence unless we already there |
124 |
return firstStart; |
125 |
} |
126 |
} else { |
127 |
int current = hs.getStartOffset(), last; |
128 |
boolean stuck = false; |
129 |
do { |
130 |
last = current; |
131 |
current = hs.getStartOffset(); |
132 |
} while (hs.getEndOffset() < curPos && (stuck = hs.moveNext())); |
133 |
|
134 |
if (last == current) { |
135 |
// we got no options to jump, cyclic jump to last in file unless we already there |
136 |
while (hs.moveNext()); |
137 |
if (!(hs.getEndOffset() >= curPos && hs.getStartOffset() <= curPos)) { |
138 |
return hs.getStartOffset(); |
139 |
} |
140 |
} else if (stuck) { |
141 |
// just move to previous occurence |
142 |
return last; |
143 |
} else { |
144 |
// it was last occurence in the file |
145 |
return current; |
146 |
} |
147 |
} |
148 |
} |
149 |
return -1; |
150 |
} |
151 |
|
152 |
private static void navigateToOccurence(boolean next, JTextComponent txt) { |
153 |
if (txt != null && txt.getDocument() != null) { |
154 |
Document doc = txt.getDocument(); |
155 |
int position = txt.getCaretPosition(); |
156 |
int goTo = findOccurrencePosition(next, doc, position); |
157 |
if (goTo > 0) { |
158 |
txt.setCaretPosition(goTo); |
159 |
} else { |
160 |
StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(GoToMarkOccurrencesAction.class, "csl-no-marked-occurrence")); |
161 |
} |
162 |
} |
163 |
|
164 |
} |
165 |
} |