KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > JumpCommands


1 /*
2  * JumpCommands.java
3  *
4  * Copyright (C) 1998-2003 Peter Graves
5  * $Id: JumpCommands.java,v 1.2 2003/07/01 12:44:43 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 public final class JumpCommands implements Constants
25 {
26     public static void jumpToLine()
27     {
28         final Editor editor = Editor.currentEditor();
29         String JavaDoc response = InputDialog.showInputDialog(editor, "Line number:",
30             "Jump To Line");
31         if (response == null || response.length() == 0)
32             return;
33         try {
34             final int here = editor.getDotLineNumber() + 1;
35             int lineNumber = parseNumericInput(response, here) - 1;
36             editor.jumpToLine(lineNumber);
37         }
38         catch (NumberFormatException JavaDoc e) {
39             MessageDialog.showMessageDialog(editor, "Invalid line number", "Error");
40         }
41     }
42
43     public static void jumpToColumn()
44     {
45         final Editor editor = Editor.currentEditor();
46         String JavaDoc response = InputDialog.showInputDialog(editor, "Column number:",
47             "Jump To Column");
48         if (response == null || response.length() == 0)
49             return;
50         try {
51             final int here = editor.getDotCol() + 1;
52             int col = parseNumericInput(response, here) - 1;
53             final Display display = editor.getDisplay();
54             if (col >= 0 && col != display.getAbsoluteCaretCol()) {
55                 editor.addUndo(SimpleEdit.MOVE);
56                 editor.unmark();
57                 display.setCaretCol(col - display.getShift());
58                 editor.moveDotToCaretCol();
59                 display.setUpdateFlag(REFRAME);
60             }
61         }
62         catch (NumberFormatException JavaDoc e) {
63             MessageDialog.showMessageDialog(editor, "Invalid column number", "Error");
64         }
65     }
66
67     public static void jumpToOffset()
68     {
69         final Editor editor = Editor.currentEditor();
70         String JavaDoc response = InputDialog.showInputDialog(editor, "Offset:",
71             "Jump To Offset");
72         if (response == null || response.length() == 0)
73             return;
74         try {
75             final Buffer buffer = editor.getBuffer();
76             // Zero-based.
77
int here = buffer.getAbsoluteOffset(editor.getDot());
78             int offset = parseNumericInput(response, here);
79             Position pos = buffer.getPosition(offset);
80             if (pos != null) {
81                 editor.moveDotTo(pos);
82                 return;
83             }
84         }
85         catch (NumberFormatException JavaDoc e) {
86             MessageDialog.showMessageDialog(editor, "Invalid offset", "Error");
87         }
88     }
89
90     private static int parseNumericInput(String JavaDoc s, int here) throws NumberFormatException JavaDoc
91     {
92         s = s.trim();
93         if (s.length() == 0)
94             throw new NumberFormatException JavaDoc();
95         char c = s.charAt(0);
96         if (c == '+' || c == '-') {
97             // It's an offset from the current location.
98
int offset = Integer.parseInt(s.substring(1).trim());
99             return c == '+' ? here + offset : here - offset;
100         }
101         return Integer.parseInt(s);
102     }
103 }
104
Popular Tags