KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > editor > EditorTips


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

44
45 public class EditorTips implements TipsProvider {
46
47     public TipsManager tipsManager;
48     public CEditorGrammar editor;
49
50     public EditorTips(CEditorGrammar editor) {
51         this.editor = editor;
52     }
53
54     public void awake() {
55         tipsManager = new TipsManager();
56         tipsManager.setOverlay(new TipsOverlay(editor.getXJFrame(), editor.getTextPane()));
57         tipsManager.addProvider(this);
58     }
59
60     public void toggleEnabled() {
61         tipsManager.setEnabled(!tipsManager.enabled());
62     }
63
64     public void hide() {
65         tipsManager.hide();
66     }
67
68     public void display(Point relativePoint, Point absolutePoint) {
69         if(editor.getTokens() == null)
70             return;
71
72         int position = editor.getTextPane().viewToModel(relativePoint);
73
74         Point p = null;
75         try {
76             ATEToken token = editor.getTokenAtPosition(position);
77             if(token != null) {
78                 // Make sure the mouse is over the token because
79
// Swing will return a valid position even if the mouse
80
// is on the remaining blank part of the line
81
Rectangle r1 = editor.getTextPane().modelToView(token.getStartIndex());
82                 Rectangle r2 = editor.getTextPane().modelToView(token.getEndIndex());
83                 if(r1.union(r2).contains(relativePoint)) {
84                     p = SwingUtilities.convertPoint(editor.getTextPane(), new Point(relativePoint.x+2, r2.y-5), editor.getXJFrame().getJavaContainer());
85                 }
86             }
87         } catch (BadLocationException JavaDoc e) {
88             // Ignore
89
}
90         tipsManager.displayAnyTipsAvailable(position, p);
91     }
92
93     public List JavaDoc<String JavaDoc> tipsProviderGetTips(int position) {
94         List JavaDoc<String JavaDoc> tips = new ArrayList JavaDoc<String JavaDoc>();
95
96         List JavaDoc<EditorInspectorItem> items = editor.editorInspector.getAllItemsAtIndex(position);
97         for(int index=0; index<items.size(); index++) {
98             EditorInspectorItem item = items.get(index);
99             tips.add(item.description);
100         }
101
102         return tips;
103     }
104
105 }
106
Popular Tags