01: /*
02: * FollowContextTask.java
03: *
04: * Copyright (C) 2002 Peter Graves
05: * $Id: FollowContextTask.java,v 1.1.1.1 2002/09/24 16:07:43 piso Exp $
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License
09: * 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 java.util.ArrayList;
25: import java.util.List;
26: import javax.swing.SwingUtilities;
27:
28: public class FollowContextTask extends IdleThreadTask implements
29: Constants {
30: private Expression lastExpression;
31: private Position lastPos;
32:
33: public FollowContextTask() {
34: setIdle(500); // 500 ms
35: setRunnable(runnable);
36: }
37:
38: private final Runnable runnable = new Runnable() {
39: public void run() {
40: final Editor editor = Editor.currentEditor();
41: if (editor == null)
42: return;
43: if (editor.getBuffer() == null)
44: return;
45: if (editor.getMark() != null)
46: return;
47: if (editor.getDot() == null
48: || editor.getDot().equals(lastPos))
49: return;
50: lastPos = new Position(editor.getDot());
51: Expression expression = editor.getMode()
52: .getExpressionAtDot(editor, false);
53: if (expression == null)
54: return;
55: if (!expression.equals(lastExpression)) {
56: final Tag tag = findMatchingTag(editor, expression);
57: if (tag != null) {
58: Runnable r = new Runnable() {
59: public void run() {
60: if (tag instanceof LocalTag)
61: TagCommands.gotoLocalTag(editor,
62: (LocalTag) tag, true);
63: else if (tag instanceof GlobalTag)
64: TagCommands.gotoGlobalTag(editor,
65: (GlobalTag) tag, true);
66: editor.updateDisplay();
67: }
68: };
69: SwingUtilities.invokeLater(r);
70: }
71: lastExpression = expression;
72: }
73: }
74: };
75:
76: private static Tag findMatchingTag(Editor editor,
77: Expression expression) {
78: List list = TagCommands.findMatchingTags(editor.getBuffer(),
79: expression);
80: if (list != null && list.size() == 1) {
81: // Exactly one match.
82: return (Tag) list.get(0);
83: }
84: return null;
85: }
86:
87: public static void followContext() {
88: if (!Editor.checkExperimental())
89: return;
90: IdleThread.runFollowContextTask(new FollowContextTask());
91: }
92: }
|