KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > debugger > tree > DBASTPanel


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

49
50 public class DBASTPanel extends DetachablePanel implements DBASTModelListener, XJTableDelegate, AWTreePanelDelegate {
51
52     public Debugger debugger;
53
54     public XJTableView rulesTableView;
55     public XJTableView rootsTableView;
56     public AWTreePanel treePanel;
57
58     public DBASTModel model;
59     public JSplitPane tablesSplitPane;
60     public JSplitPane tableTreeSplitPane;
61
62     public DBASTPanel(Debugger debugger) {
63         super("AST", debugger);
64
65         this.debugger = debugger;
66
67         model = new DBASTModel(debugger);
68         model.addListener(this);
69
70         rulesTableView = new XJTableView();
71         rulesTableView.setFocusable(true);
72         rulesTableView.getTable().setModel(new RulesTableModel());
73         rulesTableView.getTable().setDelegate(this);
74         rulesTableView.getTable().setAllowEmptySelection(false);
75         rulesTableView.getTable().setRememberSelection(true);
76         rulesTableView.autoresizeColumns();
77
78         rootsTableView = new XJTableView();
79         rootsTableView.getTable().setModel(new RootsTableModel());
80         rootsTableView.setFocusable(true);
81         rootsTableView.getTable().setDelegate(this);
82         rootsTableView.getTable().setAllowEmptySelection(false);
83         rootsTableView.getTable().setRememberSelection(true);
84         rootsTableView.autoresizeColumns();
85
86         treePanel = new AWTreePanel(new DefaultTreeModel JavaDoc(null));
87         treePanel.setRootVisible(true);
88         treePanel.setDelegate(this);
89
90         tablesSplitPane = createSplitPane();
91         tableTreeSplitPane = createSplitPane();
92
93         tablesSplitPane.setLeftComponent(rulesTableView);
94         tablesSplitPane.setRightComponent(rootsTableView);
95
96         tableTreeSplitPane.setLeftComponent(tablesSplitPane);
97         tableTreeSplitPane.setRightComponent(treePanel);
98
99         mainPanel.add(tableTreeSplitPane, BorderLayout.CENTER);
100     }
101
102     public JSplitPane createSplitPane() {
103         JSplitPane sp = new JSplitPane();
104         sp.setBorder(null);
105         sp.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
106         sp.setContinuousLayout(true);
107         sp.setOneTouchExpandable(true);
108         return sp;
109     }
110
111     public void componentShouldLayout(Dimension size) {
112         tableTreeSplitPane.setDividerLocation((int)(size.width*0.5));
113         tablesSplitPane.setDividerLocation((int)(size.width*0.3));
114     }
115
116     public void clear() {
117         model.clear();
118         treePanel.clear();
119     }
120
121     public void updateOnBreakEvent() {
122         model.fireDataChanged();
123
124         SwingUtilities.invokeLater(new Runnable JavaDoc() {
125             public void run() {
126                 selectLastRule();
127                 selectLastRootNode();
128             }
129         });
130     }
131
132     public DBASTModel getModel() {
133         return model;
134     }
135     
136     public void modelChanged(DBASTModel model) {
137         rulesModelChanged();
138         rootsModelChanged();
139     }
140
141     public void rulesModelChanged() {
142         rulesTableView.getTable().reload();
143     }
144
145     public void rootsModelChanged() {
146         rootsTableView.getTable().reload();
147         updateTreePanel();
148     }
149
150     public void updateTreePanel() {
151         int row = rootsTableView.getTable().getSelectedRow();
152         if(row == -1)
153             treePanel.setRoot(null);
154         else
155             treePanel.setRoot(getRootAtIndex(row));
156         treePanel.refresh();
157     }
158
159     public void selectToken(Token token) {
160         /** Look currently only on the selected rule roots */
161         DBASTModel.Rule rule = getSelectedRule();
162         if(rule == null)
163             return;
164
165         Stack JavaDoc<DBASTModel.ASTNode> roots = rule.getRoots();
166         for (int r = 0; r < roots.size(); r++) {
167             DBASTModel.ASTNode node = roots.get(r);
168             DBTreeNode candidate = node.findNodeWithToken(token);
169             if(candidate != null) {
170                 rootsTableView.getTable().setSelectedRow(r);
171                 treePanel.selectNode(candidate);
172                 break;
173             }
174         }
175     }
176
177     public void selectLastRule() {
178         rulesTableView.getTable().selectLastRow();
179     }
180
181     public void selectLastRootNode() {
182         rootsTableView.getTable().selectLastRow();
183     }
184
185     public DBASTModel.Rule getSelectedRule() {
186         int row = rulesTableView.getTable().getSelectedRow();
187         if(row == -1)
188             return null;
189         else
190             return model.getRuleAtIndex(row);
191     }
192
193     public DBASTModel.ASTNode getRootAtIndex(int index) {
194         return getSelectedRule().getRootAtIndex(index);
195     }
196
197     public GView getGraphView() {
198         return treePanel.getGraphView();
199     }
200
201     public void tableSelectionChanged(XJTable table, int selectedRow) {
202         if(table == rulesTableView.getTable()) {
203             rootsModelChanged();
204         } else if(table == rootsTableView.getTable()) {
205             updateTreePanel();
206         }
207     }
208
209     public void awTreeDidSelectTreeNode(TreeNode JavaDoc node, boolean shiftKey) {
210         DBASTModel.ASTNode n = (DBASTModel.ASTNode)node;
211         debugger.selectToken(n.token, n.token.getLine(), n.token.getCharPositionInLine());
212     }
213
214     public JPopupMenu awTreeGetContextualMenu() {
215         return debugger.treeGetContextualMenu();
216     }
217
218     public class RulesTableModel extends DefaultTableModel JavaDoc {
219
220         public int getRowCount() {
221             if(DBASTPanel.this == null)
222                 return 0;
223             
224             if(model == null)
225                 return 0;
226             else
227                 return model.getRuleCount();
228         }
229
230         public int getColumnCount() {
231             return 1;
232         }
233
234         public String JavaDoc getColumnName(int column) {
235             return "Rule";
236         }
237
238         public Object JavaDoc getValueAt(int rowIndex, int columnIndex) {
239             return model.getRuleAtIndex(rowIndex).name;
240         }
241
242         public boolean isCellEditable(int row, int column) {
243             return false;
244         }
245     }
246
247     public class RootsTableModel extends DefaultTableModel JavaDoc {
248
249         public int getRowCount() {
250             if(DBASTPanel.this == null)
251                 return 0;
252
253             DBASTModel.Rule r = getSelectedRule();
254             if(r == null)
255                 return 0;
256             else
257                 return r.roots.size();
258         }
259
260         public int getColumnCount() {
261             return 1;
262         }
263
264         public String JavaDoc getColumnName(int column) {
265             return "Root";
266         }
267
268         public Object JavaDoc getValueAt(int rowIndex, int columnIndex) {
269             return "r"+rowIndex;
270             //return getRootAtIndex(rowIndex);
271
}
272
273         public boolean isCellEditable(int row, int column) {
274             return false;
275         }
276     }
277
278 }
279
Popular Tags