KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > menu > MenuGrammar


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.menu;
33
34 import org.antlr.tool.Grammar;
35 import org.antlr.works.ate.syntax.misc.ATEToken;
36 import org.antlr.works.components.grammar.CEditorGrammar;
37 import org.antlr.works.grammar.CheckGrammar;
38 import org.antlr.works.grammar.CheckGrammarDelegate;
39 import org.antlr.works.grammar.RulesDependency;
40 import org.antlr.works.grammar.TokensDFA;
41 import org.antlr.works.grammar.decisiondfa.DecisionDFA;
42 import org.antlr.works.stats.StatisticsAW;
43 import org.antlr.works.syntax.GrammarSyntaxParser;
44 import org.antlr.works.syntax.element.ElementGroup;
45 import org.antlr.works.syntax.element.ElementRule;
46 import org.antlr.xjlib.appkit.utils.XJAlert;
47 import org.antlr.xjlib.appkit.utils.XJDialogProgressDelegate;
48
49 import javax.swing.*;
50 import java.util.List JavaDoc;
51
52 public class MenuGrammar extends MenuAbstract implements CheckGrammarDelegate, XJDialogProgressDelegate {
53
54     protected CheckGrammar checkGrammar;
55     protected boolean checkingGrammar;
56
57     public MenuGrammar(CEditorGrammar editor) {
58         super(editor);
59         checkGrammar = new CheckGrammar(editor, this);
60     }
61
62     public void showTokensSD() {
63         StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_SHOW_TOKENS_SD);
64         editor.visual.setRule(new ElementRule(Grammar.ARTIFICIAL_TOKENS_RULENAME), true);
65         editor.makeBottomComponentVisible();
66     }
67
68     public void showTokensDFA() {
69         StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_SHOW_TOKENS_DFA);
70         TokensDFA decision = new TokensDFA(editor);
71         decision.launch();
72     }
73
74     public void showDecisionDFA() {
75         DecisionDFA decision = new DecisionDFA(editor);
76         decision.launch();
77     }
78
79     public void highlightDecisionDFA() {
80         StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_SHOW_DECISION_DFA);
81         try {
82             if(editor.decisionDFAEngine.getDecisionDFACount() == 0) {
83                 editor.decisionDFAEngine.discoverAllDecisions();
84             } else {
85                 editor.decisionDFAEngine.reset();
86             }
87             editor.decisionDFAEngine.refresh();
88             editor.decisionDFAEngine.refreshMenu();
89         } catch (Exception JavaDoc e) {
90             e.printStackTrace();
91             XJAlert.display(editor.getWindowContainer(), "Error", "Cannot show the DFA:\n"+e.toString());
92         }
93     }
94
95     public void showDependency() {
96         StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_SHOW_RULE_DEPENDENCY);
97         RulesDependency dependency = new RulesDependency(editor);
98         dependency.launch();
99     }
100
101     public void insertRuleFromTemplate() {
102         StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_INSERT_RULE_TEMPLATE);
103         editor.ruleTemplates.display();
104     }
105
106     public void group() {
107         StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_RULE_GROUP);
108
109         String JavaDoc s = (String JavaDoc)JOptionPane.showInputDialog(editor.getWindowContainer(), "Group Name:", "Group",
110                 JOptionPane.QUESTION_MESSAGE, null, null, "Group");
111         if(s != null && s.length() > 0) {
112             List JavaDoc<ElementRule> rules = editor.rules.getSelectedRules();
113             if(!rules.isEmpty()) {
114                 editor.beginGroupChange("Group");
115
116                 ElementRule firstRule = rules.get(0);
117                 ElementRule lastRule = rules.get(rules.size()-1);
118
119                 int end = lastRule.getEndIndex();
120                 editor.textEditor.insertText(end+1, "\n"+GrammarSyntaxParser.END_GROUP+"\n");
121
122                 int start = firstRule.getStartIndex();
123                 editor.textEditor.insertText(start-1, "\n"+ GrammarSyntaxParser.BEGIN_GROUP+s+"\n");
124
125                 editor.endGroupChange();
126             }
127         }
128     }
129
130     public void ungroup() {
131         StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_RULE_UNGROUP);
132
133         ElementGroup openGroup = editor.rules.getSelectedGroup();
134         if(openGroup == null) {
135             // No open group selected in the tree. Try to find the closest open group
136
// by moving backward
137
openGroup = editor.rules.findOpenGroupClosestToLocation(editor.getTextPane().getSelectionStart());
138             if(openGroup == null) {
139                 // Still no open group ? Give up
140
XJAlert.display(editor.getWindowContainer(), "Ungroup", "Cannot ungroup because no enclosing group has been found.");
141                 return;
142             }
143         }
144
145         ElementGroup closingGroup = editor.rules.findClosingGroupForGroup(openGroup);
146
147         editor.beginGroupChange("Ungroup");
148
149         if(closingGroup != null) {
150             // End of file is considered as a closing group but no group really exists
151
// for that purpose
152
ATEToken t = closingGroup.token;
153             editor.replaceText(t.getStartIndex()-1, t.getEndIndex(), "");
154         }
155
156         ATEToken t = openGroup.token;
157         editor.replaceText(t.getStartIndex()-1, t.getEndIndex(), "");
158
159         editor.endGroupChange();
160     }
161
162     public void ignore() {
163         editor.rules.ignoreSelectedRules(true);
164     }
165
166     public void consider() {
167         editor.rules.ignoreSelectedRules(false);
168     }
169
170     public void checkGrammar() {
171         editor.showProgress("Checking Grammar...", this);
172
173         editor.console.makeCurrent();
174         editor.console.println("Checking Grammar...");
175         checkGrammar.check();
176
177         StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_CHECK_GRAMMAR);
178     }
179
180     public void checkGrammarDidBegin() {
181         checkingGrammar = true;
182     }
183
184     public void checkGrammarDidEnd(String JavaDoc errorMsg) {
185         checkingGrammar = false;
186         editor.hideProgress();
187         if(errorMsg != null) {
188             XJAlert.display(editor.getWindowContainer(), "Failure", "Check Grammar failed:\n"+errorMsg+"\nConsult the console for more information.");
189         } else {
190             XJAlert.display(editor.getWindowContainer(), "Success", "Check Grammar succeeded.");
191         }
192     }
193
194     public void dialogDidCancel() {
195         if(checkingGrammar)
196             checkGrammar.cancel();
197     }
198 }
199
Popular Tags