KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * GotoFile.java
3  *
4  * Copyright (C) 1998-2002 Peter Graves
5  * $Id: GotoFile.java,v 1.1.1.1 2002/09/24 16:09:17 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 import gnu.regexp.RE;
25 import gnu.regexp.REMatch;
26 import gnu.regexp.UncheckedRE;
27 import java.awt.AWTEvent JavaDoc;
28 import java.awt.event.MouseEvent JavaDoc;
29
30 public final class GotoFile implements Constants
31 {
32     public static void gotoFile()
33     {
34         final Editor editor = Editor.currentEditor();
35
36         // If this method is invoked via a mouse event mapping, move dot to
37
// location of mouse click first.
38
AWTEvent JavaDoc event = editor.getDispatcher().getLastEvent();
39         if (event instanceof MouseEvent JavaDoc)
40             editor.mouseMoveDotToPoint((MouseEvent JavaDoc)event);
41
42         String JavaDoc filename = gotoFileGetFileName(editor);
43         if (filename == null)
44             return;
45         int lineNumber = -1;
46
47         // See if there's a line number at the end of the filename.
48
int index = filename.lastIndexOf(" line ");
49         if (index >= 0) {
50             // "test.pl line 3"
51
try {
52                 lineNumber = Integer.parseInt(filename.substring(index+6)) - 1;
53                 // Shorten filename to exclude line number.
54
filename = filename.substring(0, index);
55             }
56             catch (NumberFormatException JavaDoc e) {
57                 // Not a valid number.
58
}
59         } else {
60             index = filename.lastIndexOf(':');
61             if (index >= 0) {
62                 // "Position.java:140"
63
try {
64                     lineNumber =
65                         Integer.parseInt(filename.substring(index+1)) - 1;
66                     // Shorten filename to exclude line number.
67
filename = filename.substring(0, index);
68                 }
69                 catch (NumberFormatException JavaDoc e) {
70                     // Not a valid number.
71
}
72             }
73         }
74
75         boolean tryCurrentDirectory = true;
76
77         if (filename.length() >= 2 &&
78             filename.charAt(0) == '<' &&
79             filename.charAt(filename.length()-1) == '>') {
80             // We'll only get the angle brackets if we're in C or C++ mode.
81
// Strip the angle brackets and don't look for the file in the
82
// current directory.
83
filename = filename.substring(1, filename.length()-1);
84             tryCurrentDirectory = false;
85         }
86
87         File file = null;
88
89         if (Utilities.isFilenameAbsolute(filename)) {
90             file = File.getInstance(editor.getCurrentDirectory(), filename);
91         } else {
92             // The filename is not absolute.
93
if (tryCurrentDirectory)
94                 // Try current directory first.
95
file = File.getInstance(editor.getCurrentDirectory(),
96                     filename);
97
98             // Try source and include paths if applicable.
99
if (file == null || (file.isLocal() && !file.exists()))
100                 file = Utilities.findFile(editor, filename);
101         }
102
103         if (file != null) {
104             Buffer buf = editor.getBuffer(file);
105             if (buf != null) {
106                 final Frame frame = editor.getFrame();
107                 editor.makeNext(buf);
108                 editor.switchToBuffer(buf);
109                 // Switching buffers might have closed the original editor.
110
Editor ed =
111                     frame.contains(editor) ? editor : frame.getCurrentEditor();
112                 if (ed.getBuffer() == buf) {
113                     if (lineNumber >= 0) {
114                         if (ed.getDot() != null) {
115                             if (ed.getDotLineNumber() != lineNumber) {
116                                 ed.addUndo(SimpleEdit.MOVE);
117                                 ed.unmark();
118                                 ed.gotoline(lineNumber);
119                                 ed.moveCaretToDotCol();
120                             }
121                         }
122                     }
123                     ed.updateDisplay();
124                 }
125             }
126         }
127     }
128
129     private static String JavaDoc gotoFileGetFileName(Editor editor)
130     {
131         if (editor.getDot() == null)
132             return null;
133         final Line dotLine = editor.getDotLine();
134         final int dotOffset = editor.getDotOffset();
135         if (editor.getMark() != null && editor.getMarkLine() == dotLine) {
136             // Use selection.
137
return new Region(editor).toString();
138         }
139         final int modeId = editor.getModeId();
140         if (modeId == HTML_MODE) {
141             RE re = new UncheckedRE("(href|src)=\"([^\"]+)\"", RE.REG_ICASE);
142             REMatch match = null;
143             final String JavaDoc text = dotLine.getText();
144             int index = 0;
145             REMatch m;
146             while ((m = re.getMatch(text, index)) != null) {
147                 match = m;
148                 if (match.getEndIndex() > dotOffset)
149                     break; // All subsequent matches will be further away.
150
index = match.getEndIndex();
151             }
152             if (match != null)
153                 return match.toString(2);
154         } else if (modeId == JAVA_MODE) {
155             String JavaDoc fileName =
156                 getFileNameFromImport(editor.getBuffer(), dotLine.getText());
157             if (fileName != null)
158                 return fileName;
159         } else if ((modeId == C_MODE || modeId == CPP_MODE)) {
160             String JavaDoc fileName = getFileNameFromInclude(dotLine.getText());
161             if (fileName != null)
162                 return fileName;
163         } else if (editor.getBuffer().getType() == Buffer.TYPE_SHELL) {
164             String JavaDoc s = dotLine.getText().trim();
165             REMatch match = Directory.getNativeMoveToFilenameRegExp().getMatch(s);
166             if (match != null)
167                 return s.substring(match.getEndIndex());
168         }
169         return editor.getFilenameAtDot();
170     }
171
172     private static String JavaDoc getFileNameFromImport(Buffer buffer, String JavaDoc s)
173     {
174         if (s.indexOf('*') >= 0)
175             return null;
176         s = s.trim();
177         if (!s.startsWith("import"))
178             return null;
179         s = s.substring(6);
180         if (s.length() == 0)
181             return null;
182         if (s.charAt(0) != ' ' && s.charAt(0) != '\t')
183             return null;
184         s = s.trim();
185         FastStringBuffer sb = new FastStringBuffer();
186         for (int i = 0; i < s.length(); i++) {
187             char c = s.charAt(i);
188             if (c == ' ' || c == '\t' || c == ';')
189                 break;
190             sb.append(c);
191         }
192         File file = JavaSource.findSource(buffer, sb.toString(), true);
193         return file != null ? file.canonicalPath() : null;
194     }
195
196     private static final RE includeRE =
197         new UncheckedRE("[ \t]*#[ \t]*include[ \t]");
198
199     private static final String JavaDoc getFileNameFromInclude(String JavaDoc s)
200     {
201         REMatch match = includeRE.getMatch(s);
202         if (match == null)
203             return null;
204         s = s.substring(match.getEndIndex()).trim();
205         // Need at least one char plus quotes or angle brackets.
206
if (s.length() < 3)
207             return null;
208         char c = s.charAt(0);
209         if (c == '"') {
210             int index = s.indexOf('"', 1);
211             if (index >= 0)
212                 return s.substring(1, index);
213             return null;
214         }
215         if (c == '<') {
216             int index = s.indexOf('>', 1);
217             if (index >= 0)
218                 return s.substring(0, index+1); // Include angle brackets.
219
}
220         return null;
221     }
222 }
223
Popular Tags