KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * FollowContextTask.java
3  *
4  * Copyright (C) 2002 Peter Graves
5  * $Id: FollowContextTask.java,v 1.1.1.1 2002/09/24 16:07: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 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26 import javax.swing.SwingUtilities JavaDoc;
27
28 public class FollowContextTask extends IdleThreadTask implements Constants
29 {
30     private Expression lastExpression;
31     private Position lastPos;
32
33     public FollowContextTask()
34     {
35         setIdle(500); // 500 ms
36
setRunnable(runnable);
37     }
38
39     private final Runnable JavaDoc runnable = new Runnable JavaDoc() {
40         public void run()
41         {
42             final Editor editor = Editor.currentEditor();
43             if (editor == null)
44                 return;
45             if (editor.getBuffer() == null)
46                 return;
47             if (editor.getMark() != null)
48                 return;
49             if (editor.getDot() == null || editor.getDot().equals(lastPos))
50                 return;
51             lastPos = new Position(editor.getDot());
52             Expression expression =
53                 editor.getMode().getExpressionAtDot(editor, false);
54             if (expression == null)
55                 return;
56             if (!expression.equals(lastExpression)) {
57                 final Tag tag = findMatchingTag(editor, expression);
58                 if (tag != null) {
59                     Runnable JavaDoc r = new Runnable JavaDoc() {
60                         public void run()
61                         {
62                             if (tag instanceof LocalTag)
63                                 TagCommands.gotoLocalTag(editor, (LocalTag)tag, true);
64                             else if (tag instanceof GlobalTag)
65                                 TagCommands.gotoGlobalTag(editor, (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, Expression expression)
77     {
78         List JavaDoc list =
79             TagCommands.findMatchingTags(editor.getBuffer(), 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     {
89         if (!Editor.checkExperimental())
90             return;
91         IdleThread.runFollowContextTask(new FollowContextTask());
92     }
93 }
94
Popular Tags