KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > navigation > GoToRule


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
33 package org.antlr.works.navigation;
34
35 import org.antlr.works.components.grammar.CEditorGrammar;
36 import org.antlr.works.syntax.element.ElementRule;
37 import org.antlr.works.utils.OverlayObject;
38 import org.antlr.xjlib.appkit.frame.XJFrameInterface;
39
40 import javax.swing.*;
41 import javax.swing.border.BevelBorder JavaDoc;
42 import javax.swing.event.DocumentEvent JavaDoc;
43 import javax.swing.event.DocumentListener JavaDoc;
44 import java.awt.*;
45 import java.awt.event.KeyAdapter JavaDoc;
46 import java.awt.event.KeyEvent JavaDoc;
47 import java.awt.event.MouseAdapter JavaDoc;
48 import java.awt.event.MouseEvent JavaDoc;
49 import java.util.List JavaDoc;
50
51 public class GoToRule extends OverlayObject {
52
53     public JTextField ruleNameField;
54     public JList matchingRuleList;
55     public DefaultListModel matchingRuleListModel;
56     public JScrollPane matchingRuleScrollPane;
57     public CEditorGrammar editor;
58
59     public static final int VISIBLE_MATCHING_RULES = 15;
60
61     public GoToRule(CEditorGrammar editor, XJFrameInterface parentFrame, JComponent parentComponent) {
62         super(parentFrame, parentComponent);
63         this.editor = editor;
64     }
65
66     public JComponent overlayCreateInterface() {
67         JPanel panel = new JPanel(new BorderLayout());
68         panel.add(new JLabel("Go To Rule:"), BorderLayout.NORTH);
69
70         ruleNameField = new JTextField();
71         ruleNameField.addKeyListener(new TextFieldKeyAdapter());
72         ruleNameField.getDocument().addDocumentListener(new TextFieldDocumentListener());
73
74         matchingRuleListModel = new DefaultListModel();
75
76         matchingRuleList = new JList(matchingRuleListModel);
77         matchingRuleList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
78         matchingRuleList.setBackground(new Color(235, 244, 254));
79         matchingRuleList.setPrototypeCellValue("This is a rule name g");
80         matchingRuleList.addKeyListener(new ListKeyAdapter());
81
82         // FIX AW-85
83
matchingRuleList.addMouseListener(new MouseAdapter JavaDoc() {
84             @Override JavaDoc
85             public void mouseClicked(MouseEvent JavaDoc mouseEvent) {
86                 if(mouseEvent.getClickCount() == 2) {
87                     goToRule();
88                     hide();
89                 }
90             }
91         });
92
93         matchingRuleScrollPane = new JScrollPane(matchingRuleList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
94         matchingRuleScrollPane.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
95         matchingRuleScrollPane.setVisible(false);
96         parentFrame.getLayeredPane().add(matchingRuleScrollPane, JLayeredPane.MODAL_LAYER);
97
98         panel.add(ruleNameField, BorderLayout.SOUTH);
99
100         return panel;
101     }
102
103     public boolean overlayWillDisplay() {
104         ruleNameField.setText("");
105         // Invoke focus later because otherwise it fails on Windows
106
SwingUtilities.invokeLater(new Runnable JavaDoc() {
107             public void run() {
108                 ruleNameField.requestFocusInWindow();
109             }
110         });
111         updateAutoCompletionList();
112         return true;
113     }
114
115     public void hide() {
116         super.hide();
117         matchingRuleScrollPane.setVisible(false);
118     }
119
120     public void resize() {
121         super.resize();
122         resizeMatchingRules();
123     }
124
125     public void resizeMatchingRules() {
126         Rectangle r = content.getBounds();
127         int height = matchingRuleList.getFixedCellHeight();
128         int size = matchingRuleListModel.size();
129         if(size > 0) {
130             height = height*Math.min(VISIBLE_MATCHING_RULES, size)+5;
131             matchingRuleScrollPane.setBounds(r.x, r.y+r.height, r.width, height);
132         }
133     }
134
135     public void updateAutoCompletionList() {
136         matchingRuleListModel.removeAllElements();
137
138         List JavaDoc<String JavaDoc> rules = editor.rules.getRulesStartingWith(ruleNameField.getText().toLowerCase());
139         if(rules.isEmpty()) {
140             matchingRuleScrollPane.setVisible(false);
141             ruleNameField.setForeground(Color.red);
142             return;
143         } else {
144             ruleNameField.setForeground(Color.black);
145         }
146
147         for (String JavaDoc rule : rules) {
148             matchingRuleListModel.addElement(rule);
149         }
150         matchingRuleList.setSelectedIndex(0);
151
152         resizeMatchingRules();
153         matchingRuleScrollPane.setVisible(true);
154     }
155
156     public void selectNextListElement(int direction) {
157         int index = matchingRuleList.getSelectedIndex();
158         index += direction;
159         index = Math.min(Math.max(0, index), matchingRuleListModel.size()-1);
160
161         matchingRuleList.setSelectedIndex(index);
162         matchingRuleList.scrollRectToVisible(matchingRuleList.getCellBounds(index, index));
163     }
164
165     public void goToRule() {
166         if(matchingRuleListModel.isEmpty())
167             return;
168
169         int index = matchingRuleList.getSelectedIndex();
170         if(index >= 0) {
171             ElementRule rule = editor.rules.selectRuleNameInTree((String JavaDoc)matchingRuleListModel.get(index));
172             if(rule != null) {
173                 editor.goToHistoryRememberCurrentPosition();
174                 editor.rules.goToRule(rule);
175             }
176         }
177     }
178
179     public class ListKeyAdapter extends KeyAdapter JavaDoc {
180
181         public void keyPressed(KeyEvent JavaDoc e) {
182             if(e.isConsumed())
183                 return;
184
185             if(!content.isVisible())
186                 return;
187
188             switch(e.getKeyCode()) {
189                 case KeyEvent.VK_ESCAPE:
190                     hide();
191                     e.consume();
192                     break;
193
194                 case KeyEvent.VK_ENTER:
195                     goToRule();
196                     hide();
197                     e.consume();
198                     break;
199
200                 case KeyEvent.VK_UP:
201                     selectNextListElement(-1);
202                     e.consume();
203                     break;
204
205                 case KeyEvent.VK_DOWN:
206                     selectNextListElement(1);
207                     e.consume();
208                     break;
209             }
210         }
211     }
212
213     public class TextFieldKeyAdapter extends KeyAdapter JavaDoc {
214
215         public void keyPressed(KeyEvent JavaDoc e) {
216             if(e.isConsumed())
217                 return;
218
219             if(!content.isVisible())
220                 return;
221
222             switch(e.getKeyCode()) {
223                 case KeyEvent.VK_ESCAPE:
224                     hide();
225                     e.consume();
226                     break;
227
228                 case KeyEvent.VK_ENTER:
229                     goToRule();
230                     hide();
231                     e.consume();
232                     break;
233
234                 case KeyEvent.VK_UP:
235                     selectNextListElement(-1);
236                     e.consume();
237                     break;
238
239                 case KeyEvent.VK_DOWN:
240                     selectNextListElement(1);
241                     e.consume();
242                     break;
243             }
244         }
245     }
246
247     public class TextFieldDocumentListener implements DocumentListener JavaDoc {
248
249         public void insertUpdate(DocumentEvent JavaDoc event) {
250             updateAutoCompletionList();
251         }
252
253         public void removeUpdate(DocumentEvent JavaDoc event) {
254             updateAutoCompletionList();
255         }
256
257         public void changedUpdate(DocumentEvent JavaDoc event) {
258             updateAutoCompletionList();
259         }
260     }
261
262 }
263
Popular Tags