KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > menu > MenuGoTo


1 /*
2
3 [The "BSD licence"]
4 Copyright (c) 2005 Jean Bovet
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16 3. The name of the author may not be used to endorse or promote products
17 derived from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 */

31
32 package org.antlr.works.menu;
33
34 import org.antlr.works.ate.syntax.misc.ATELine;
35 import org.antlr.works.ate.syntax.misc.ATEToken;
36 import org.antlr.works.components.grammar.CEditorGrammar;
37 import org.antlr.works.stats.StatisticsAW;
38 import org.antlr.works.syntax.element.ElementReference;
39
40 import javax.swing.*;
41 import java.util.Set JavaDoc;
42
43 public class MenuGoTo extends MenuAbstract {
44
45     public MenuGoTo(CEditorGrammar editor) {
46         super(editor);
47     }
48
49     public void goToRule() {
50         StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_GOTO_RULE);
51         editor.goToRule.display();
52     }
53
54     public void goToDeclaration() {
55         StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_GOTO_DECLARATION);
56
57         ElementReference ref = editor.getCurrentReference();
58         if(ref == null) return;
59
60         for(ATEToken decl : editor.parserEngine.getDecls()) {
61             if(decl.getAttribute().equals(ref.token.getAttribute())) {
62                 editor.goToHistoryRememberCurrentPosition();
63                 setCaretPosition(decl.start);
64                 break;
65             }
66         }
67     }
68
69     public void goToBreakpoint(int direction) {
70         if(direction == -1)
71             StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_GOTO_PREV_BRKPT);
72         else
73             StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_GOTO_NEXT_BRKPT);
74
75         Set JavaDoc<Integer JavaDoc> breakpoints = editor.breakpointManager.getBreakpoints();
76         int line = editor.getTextEditor().getLineIndexAtTextPosition(getCaretPosition());
77         if(line == -1) return;
78
79         while(true) {
80             line += direction;
81             if(line < 0 || line > editor.parserEngine.getMaxLines()-1)
82                 break;
83
84             if(breakpoints.contains(Integer.valueOf(line))) {
85                 moveCursorToLine(line);
86                 break;
87             }
88         }
89     }
90
91     public void goToLine() {
92         StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_GOTO_LINE);
93         String JavaDoc s = (String JavaDoc)JOptionPane.showInputDialog(editor.getJavaContainer(), "Line number:", "Go To Line",
94                 JOptionPane.QUESTION_MESSAGE, null, null, null);
95         if(s != null) {
96             moveCursorToLine(Integer.parseInt(s)-1);
97         }
98     }
99
100     public void goToCharacter() {
101         StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_GOTO_CHAR);
102         String JavaDoc s = (String JavaDoc)JOptionPane.showInputDialog(editor.getJavaContainer(), "Character number:", "Go To Character",
103                 JOptionPane.QUESTION_MESSAGE, null, null, null);
104         if(s != null) {
105             int character = Integer.parseInt(s);
106             if(character < 0 || character > getTextPane().getDocument().getLength()-1)
107                 return;
108
109             editor.goToHistoryRememberCurrentPosition();
110
111             setCaretPosition(character);
112         }
113     }
114
115     public void goToBackward() {
116         StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_GOTO_BACK);
117
118         if(editor.goToHistory.canGoBack()) {
119             setCaretPosition(editor.goToHistory.getBackPosition(getCaretPosition()));
120             editor.refreshMainMenuBar();
121         }
122     }
123
124     public void goToForward() {
125         StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_GOTO_FORWARD);
126
127         if(editor.goToHistory.canGoForward()) {
128             setCaretPosition(editor.goToHistory.getForwardPosition());
129             editor.refreshMainMenuBar();
130         }
131     }
132
133     public void moveCursorToLine(int lineIndex) {
134         if(lineIndex < 0 || lineIndex > editor.getLines().size()-1)
135             return;
136
137         ATELine line = editor.getLines().get(lineIndex);
138         editor.goToHistoryRememberCurrentPosition();
139         setCaretPosition(line.position);
140     }
141
142 }
143
Popular Tags