KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > find > Usages


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

31
32 package org.antlr.works.find;
33
34 import org.antlr.works.ate.syntax.misc.ATEToken;
35 import org.antlr.works.components.grammar.CEditorGrammar;
36 import org.antlr.works.editor.EditorTab;
37 import org.antlr.works.syntax.element.ElementRule;
38
39 import javax.swing.*;
40 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
41 import javax.swing.tree.DefaultTreeCellRenderer JavaDoc;
42 import javax.swing.tree.DefaultTreeModel JavaDoc;
43 import javax.swing.tree.TreePath JavaDoc;
44 import java.awt.*;
45 import java.awt.event.MouseAdapter JavaDoc;
46 import java.awt.event.MouseEvent JavaDoc;
47
48 public class Usages extends EditorTab {
49
50     protected JPanel panel;
51     protected JScrollPane treeScrollPane;
52     protected JTree tree;
53
54     protected DefaultTreeModel JavaDoc model;
55     protected DefaultMutableTreeNode JavaDoc root;
56     protected DefaultMutableTreeNode JavaDoc node;
57     protected String JavaDoc lastRule;
58
59     protected CEditorGrammar editor;
60     protected ATEToken token;
61
62     public Usages(CEditorGrammar editor, ATEToken token) {
63         this.editor = editor;
64         this.token = token;
65
66         panel = new JPanel(new BorderLayout());
67
68         tree = new JTree();
69         tree.setRootVisible(false);
70         tree.setShowsRootHandles(true);
71
72         DefaultTreeCellRenderer JavaDoc treeRenderer = new DefaultTreeCellRenderer JavaDoc();
73         treeRenderer.setClosedIcon(null);
74         treeRenderer.setLeafIcon(null);
75         treeRenderer.setOpenIcon(null);
76
77         tree.setCellRenderer(treeRenderer);
78
79         tree.addMouseListener(new MouseAdapter JavaDoc() {
80             public void mousePressed(MouseEvent JavaDoc e) {
81                 int selRow = tree.getRowForLocation(e.getX(), e.getY());
82                 TreePath JavaDoc selPath = tree.getPathForLocation(e.getX(), e.getY());
83                 if(selRow != -1) {
84                     if(e.getClickCount() == 1) {
85                     }
86                     else if(e.getClickCount() == 2) {
87                         DefaultMutableTreeNode JavaDoc node = (DefaultMutableTreeNode JavaDoc)selPath.getLastPathComponent();
88                         if(node.getUserObject() instanceof UsageMatch)
89                             selectMatch((UsageMatch)node.getUserObject());
90                     }
91                 }
92             }
93         });
94
95         treeScrollPane = new JScrollPane(tree);
96         treeScrollPane.setWheelScrollingEnabled(true);
97
98         panel.add(treeScrollPane, BorderLayout.CENTER);
99
100         root = new DefaultMutableTreeNode JavaDoc();
101         model = new DefaultTreeModel JavaDoc(root);
102         lastRule = null;
103
104         tree.setModel(model);
105     }
106
107     public Container getContainer() {
108         return panel;
109     }
110
111     public void addMatch(ElementRule rule, ATEToken token) {
112         if(lastRule == null || !lastRule.equals(rule.name)) {
113             node = new DefaultMutableTreeNode JavaDoc();
114             node.setUserObject(rule.name);
115             root.add(node);
116
117             lastRule = rule.name;
118         }
119
120         DefaultMutableTreeNode JavaDoc matchNode = new DefaultMutableTreeNode JavaDoc();
121         matchNode.setUserObject(new UsageMatch(rule, token));
122         node.add(matchNode);
123
124         model.reload();
125     }
126
127     public void selectMatch(UsageMatch match) {
128         editor.selectTextRange(match.token.getStartIndex(), match.token.getEndIndex());
129     }
130
131     public String JavaDoc getTabName() {
132         return "Usages of \""+token.getAttribute()+"\"";
133     }
134
135     public Component getTabComponent() {
136         return getContainer();
137     }
138
139     public static class UsageMatch {
140         public ElementRule rule;
141         public ATEToken token;
142         public String JavaDoc contextualText;
143
144         public UsageMatch(ElementRule rule, ATEToken token) {
145             this.rule = rule;
146             this.token = token;
147             createContextString();
148         }
149
150         public void createContextString() {
151             String JavaDoc text = token.getText();
152             int s = token.getStartIndex();
153             while(s > 0 && text.charAt(s) != '\n' && text.charAt(s) != '\r') {
154                 s--;
155             }
156
157             int e = token.getEndIndex();
158             while(s < text.length() && text.charAt(e) != '\n' && text.charAt(e) != '\r') {
159                 e++;
160             }
161             contextualText = text.substring(s, e);
162         }
163
164         public String JavaDoc toString() {
165             // @todo it seems that I have to add white space in order for the string not to be truncated in the tree view
166
return token.getAttribute()+" @ ("+token.startLineNumber +", "+(token.getStartIndex()-token.getStartLineIndex())+") "+contextualText+" ";
167         }
168     }
169 }
170
Popular Tags