KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > repository > oql > test > DjOqlParserTest


1 package com.genimen.djeneric.repository.oql.test;
2
3 import java.awt.AWTEvent JavaDoc;
4 import java.awt.BorderLayout JavaDoc;
5 import java.awt.Dimension JavaDoc;
6 import java.awt.FlowLayout JavaDoc;
7 import java.awt.Toolkit JavaDoc;
8 import java.awt.event.ActionEvent JavaDoc;
9 import java.awt.event.KeyEvent JavaDoc;
10 import java.awt.event.MouseEvent JavaDoc;
11 import java.awt.event.WindowEvent JavaDoc;
12 import java.io.BufferedReader JavaDoc;
13 import java.io.InputStream JavaDoc;
14 import java.io.InputStreamReader JavaDoc;
15 import java.net.URL JavaDoc;
16
17 import javax.swing.ImageIcon JavaDoc;
18 import javax.swing.JButton JavaDoc;
19 import javax.swing.JCheckBox JavaDoc;
20 import javax.swing.JLabel JavaDoc;
21 import javax.swing.JOptionPane JavaDoc;
22 import javax.swing.JPanel JavaDoc;
23 import javax.swing.JScrollPane JavaDoc;
24 import javax.swing.JSplitPane JavaDoc;
25 import javax.swing.JTextArea JavaDoc;
26 import javax.swing.JTree JavaDoc;
27 import javax.swing.UIManager JavaDoc;
28 import javax.swing.event.TreeSelectionEvent JavaDoc;
29 import javax.swing.tree.DefaultTreeModel JavaDoc;
30 import javax.swing.tree.TreePath JavaDoc;
31
32 import com.genimen.djeneric.repository.DjPersistenceManager;
33 import com.genimen.djeneric.repository.DjPersistenceManagerFactory;
34 import com.genimen.djeneric.repository.oql.DjOqlWhereTranslator;
35 import com.genimen.djeneric.repository.oql.core.DjOqlParserEngine;
36 import com.genimen.djeneric.repository.oql.core.Node;
37 import com.genimen.djeneric.repository.oql.core.ParseException;
38 import com.genimen.djeneric.repository.oql.core.SimpleNode;
39 import com.genimen.djeneric.tools.common.DjenericTool;
40 import com.genimen.djeneric.util.DjLogger;
41 import com.genimen.djeneric.util.DjString;
42 import com.genimen.djeneric.util.DjStringReplacer;
43
44 public class DjOqlParserTest extends DjenericTool
45 {
46   private static final long serialVersionUID = 3257005441048131889L;
47   String JavaDoc indent;
48   BorderLayout JavaDoc borderLayout1 = new BorderLayout JavaDoc();
49   JPanel JavaDoc jPanel1 = new JPanel JavaDoc();
50   BorderLayout JavaDoc borderLayout2 = new BorderLayout JavaDoc();
51   JPanel JavaDoc jPanel2 = new JPanel JavaDoc();
52   JButton JavaDoc _butParse = new JButton JavaDoc();
53   JPanel JavaDoc jPanel3 = new JPanel JavaDoc();
54   FlowLayout JavaDoc flowLayout1 = new FlowLayout JavaDoc();
55   JButton JavaDoc jButton2 = new JButton JavaDoc();
56   int curIdx = 1;
57   JSplitPane JavaDoc jSplitPane2 = new JSplitPane JavaDoc();
58   JTextArea JavaDoc _sampleCode = new JTextArea JavaDoc();
59   JScrollPane JavaDoc _scrCode = new JScrollPane JavaDoc();
60   JTextArea JavaDoc _value = new JTextArea JavaDoc();
61   JScrollPane JavaDoc _scrValue = new JScrollPane JavaDoc();
62   JSplitPane JavaDoc jSplitPane1 = new JSplitPane JavaDoc();
63   JScrollPane JavaDoc _scrTree = new JScrollPane JavaDoc();
64   JTree JavaDoc _tree = new JTree JavaDoc();
65   JLabel JavaDoc _lblStatus = new JLabel JavaDoc();
66   String JavaDoc NEWLINE = System.getProperty("line.separator", "\n");
67   SimpleNode _parseTreeRoot = null;
68   ParseTreeNode _treeRoot = null;
69   DefaultTreeModel JavaDoc _treeModel = null;
70   JCheckBox JavaDoc _chkNormalize = new JCheckBox JavaDoc();
71
72   public DjOqlParserTest()
73   {
74     enableEvents(AWTEvent.WINDOW_EVENT_MASK);
75
76     try
77     {
78       jbInit();
79
80       nextSample();
81
82       setTitle("Oql Parser v1.0");
83       setSize(800, 600);
84
85     }
86     catch (Exception JavaDoc e)
87     {
88       JOptionPane.showMessageDialog(this, e.getMessage());
89       DjLogger.log(e);
90       System.exit(-1);
91     }
92   }
93
94   public static ImageIcon JavaDoc getImageIcon(String JavaDoc fileName)
95   {
96     URL JavaDoc url = DjOqlParserTest.class.getResource(fileName);
97     if (url != null) return new ImageIcon JavaDoc(url);
98     else return new ImageIcon JavaDoc("");
99     // Default to empty image
100
}
101
102   private void jbInit() throws Exception JavaDoc
103   {
104     this.getContentPane().setLayout(borderLayout1);
105     jPanel1.setLayout(borderLayout2);
106     _butParse.setText("(Re)parse");
107     _butParse.addActionListener(new java.awt.event.ActionListener JavaDoc()
108     {
109       public void actionPerformed(ActionEvent JavaDoc e)
110       {
111         _butParse_actionPerformed(e);
112       }
113     });
114     jPanel3.setLayout(flowLayout1);
115     jButton2.setToolTipText("Next example");
116     jButton2.setText(">>");
117     jButton2.addActionListener(new java.awt.event.ActionListener JavaDoc()
118     {
119       public void actionPerformed(ActionEvent JavaDoc e)
120       {
121         jButton2_actionPerformed(e);
122       }
123     });
124     _sampleCode.setFont(new java.awt.Font JavaDoc("Monospaced", 0, 12));
125     _sampleCode.addKeyListener(new java.awt.event.KeyAdapter JavaDoc()
126     {
127       public void keyPressed(KeyEvent JavaDoc e)
128       {
129         _sampleCode_keyPressed(e);
130       }
131     });
132     _value.setFont(new java.awt.Font JavaDoc("Monospaced", 0, 12));
133     _tree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener JavaDoc()
134     {
135       public void valueChanged(TreeSelectionEvent JavaDoc e)
136       {
137         _tree_valueChanged(e);
138       }
139     });
140     _tree.addMouseListener(new java.awt.event.MouseAdapter JavaDoc()
141     {
142       public void mouseClicked(MouseEvent JavaDoc e)
143       {
144         _tree_mouseClicked(e);
145       }
146     });
147     _lblStatus.setText("Ok");
148     jSplitPane1.setOrientation(JSplitPane.VERTICAL_SPLIT);
149     flowLayout1.setAlignment(FlowLayout.LEFT);
150     _chkNormalize.setSelected(true);
151     _chkNormalize.setText("Normalize");
152     _scrValue.setAutoscrolls(true);
153     this.getContentPane().add(jPanel1, BorderLayout.SOUTH);
154     jPanel1.add(jPanel2, BorderLayout.EAST);
155     jPanel2.add(_butParse, null);
156     jPanel1.add(jPanel3, BorderLayout.CENTER);
157     jPanel3.add(jButton2, null);
158     jPanel3.add(_chkNormalize, null);
159     jPanel1.add(_lblStatus, BorderLayout.NORTH);
160     this.getContentPane().add(jSplitPane2, BorderLayout.CENTER);
161
162     jSplitPane2.add(_scrCode, JSplitPane.BOTTOM);
163     jSplitPane2.add(jSplitPane1, JSplitPane.TOP);
164
165     jSplitPane1.add(_scrTree, JSplitPane.TOP);
166     jSplitPane1.add(_scrValue, JSplitPane.BOTTOM);
167     _scrValue.getViewport().add(_value, null);
168     _scrTree.getViewport().add(_tree, null);
169     _scrCode.getViewport().add(_sampleCode, null);
170     jSplitPane1.setDividerLocation(320);
171     jSplitPane2.setDividerLocation(300);
172     _sampleCode.setAutoscrolls(true);
173     _tree.setRootVisible(true);
174     _tree.setShowsRootHandles(true);
175     _tree.setAutoscrolls(true);
176     _tree.setToggleClickCount(0);
177     _tree.setDoubleBuffered(true);
178     _tree.setCellRenderer(new ParseTreeCellRenderer());
179     _tree.putClientProperty("JTree.lineStyle", "Angled");
180
181   }
182
183   public Node getTree(String JavaDoc tpl) throws Exception JavaDoc
184   {
185     return DjOqlParserEngine.parseExpression(tpl);
186   }
187
188   protected void processWindowEvent(WindowEvent JavaDoc e)
189   {
190     if (e.getID() == WindowEvent.WINDOW_CLOSING)
191     {
192       System.exit(0);
193     }
194     super.processWindowEvent(e);
195
196   }
197
198   public static void main(String JavaDoc[] args)
199   {
200     try
201     {
202       UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
203       DjOqlParserTest frame = new DjOqlParserTest();
204       frame.validate();
205       Dimension JavaDoc screenSize = Toolkit.getDefaultToolkit().getScreenSize();
206       Dimension JavaDoc frameSize = frame.getSize();
207       if (frameSize.height > screenSize.height) frameSize.height = screenSize.height;
208
209       if (frameSize.width > screenSize.width) frameSize.width = screenSize.width;
210
211       frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
212       frame.setVisible(true);
213
214     }
215     catch (Exception JavaDoc e)
216     {
217       DjLogger.log(e);
218     }
219   }
220
221   String JavaDoc getSample(int idx) throws Exception JavaDoc
222   {
223     InputStream JavaDoc is = this.getClass().getResourceAsStream(idx + ".scr");
224     if (is == null)
225     {
226       return null;
227     }
228     BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is));
229     String JavaDoc line;
230     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
231     while ((line = br.readLine()) != null)
232     {
233       sb.append(line + NEWLINE);
234     }
235     is.close();
236     return sb.toString();
237   }
238
239   void rebuildTree(SimpleNode root)
240   {
241     _treeRoot = new ParseTreeNode(_tree, root);
242     _treeModel = new DefaultTreeModel JavaDoc(_treeRoot);
243     buildTree(_tree, _treeRoot);
244
245     _tree.setModel(_treeModel);
246
247     _treeModel.nodeStructureChanged(_treeRoot);
248     expandAll(_treeRoot);
249   }
250
251   void buildTree(JTree JavaDoc tree, ParseTreeNode treeNode)
252   {
253
254     SimpleNode parseNode = (SimpleNode) treeNode.getNode();
255     for (int i = 0; i < parseNode.getChildCount(); i++)
256     {
257       SimpleNode child = parseNode.getChild(i);
258       ParseTreeNode ptn = new ParseTreeNode(tree, child);
259       treeNode.insertAsChild(ptn);
260       buildTree(tree, ptn);
261     }
262   }
263
264   void nextSample()
265   {
266     try
267     {
268       String JavaDoc code = getSample(curIdx++);
269       if (code == null)
270       {
271         curIdx = 1;
272         code = getSample(curIdx++);
273       }
274       if (code == null)
275       {
276         _lblStatus.setText("No more samples");
277       }
278       _sampleCode.setText(code);
279       reparse();
280       _lblStatus.setText(_lblStatus.getText() + " (Sample " + (curIdx - 1) + ")");
281     }
282     catch (ParseException px)
283     {
284       handleParseException(px);
285     }
286     catch (Exception JavaDoc x)
287     {
288       curIdx = 1;
289       DjLogger.log(x);
290     }
291
292   }
293
294   public void expandAll(ParseTreeNode node)
295   {
296     _tree.expandPath(new TreePath JavaDoc(node.getPath()));
297     int cc = node.getChildCount();
298     for (int i = 0; i < cc; i++)
299     {
300       ParseTreeNode child = (ParseTreeNode) node.getChildAt(i);
301       expandAll(child);
302     }
303   }
304
305   void handleParseException(ParseException px)
306   {
307     DjLogger.log(px);
308     _value.setText(px.getMessage());
309     _lblStatus.setText("Parse error: " + px.getMessage());
310
311     try
312     {
313       int offset = _sampleCode.getLineStartOffset(px.getLine() - 1) + px.getColumn() - 1;
314
315       _sampleCode.setCaretPosition(offset);
316       _sampleCode.requestFocus();
317     }
318     catch (Exception JavaDoc x2)
319     {
320       DjLogger.log(x2);
321     }
322   }
323
324   void reparse()
325   {
326     try
327     {
328       DjStringReplacer sr = new DjStringReplacer(_sampleCode.getText());
329       String JavaDoc code = sr.replace("\r", "");
330       _parseTreeRoot = DjOqlParserEngine.parseExpression(code);
331
332       rebuildTree(_parseTreeRoot);
333       _lblStatus.setText("Parsed ok");
334
335       DjPersistenceManagerFactory factory = new DjPersistenceManagerFactory(this, "/djeneric/repositories.xml");
336       DjPersistenceManager mgr = factory.createInstance("Local on Hypersonic");
337
338       DjOqlWhereTranslator xltr = new DjOqlWhereTranslator();
339
340       _value.setText(xltr.translateWhere(mgr.getExtent("PropertyUsage"), "src0", code));
341
342     }
343     catch (ParseException px)
344     {
345       handleParseException(px);
346     }
347     catch (Throwable JavaDoc x)
348     {
349       DjLogger.log(x);
350       _value.setText(x.getMessage());
351       _lblStatus.setText("Parse error: " + x.getMessage());
352     }
353
354   }
355
356   void jButton2_actionPerformed(ActionEvent JavaDoc e)
357   {
358     nextSample();
359   }
360
361   void _butParse_actionPerformed(ActionEvent JavaDoc e)
362   {
363     reparse();
364
365   }
366
367   public ParseTreeNode getSelectedNode()
368   {
369     if (_tree.getSelectionPath() == null) return null;
370
371     TreePath JavaDoc tp = _tree.getSelectionPath();
372
373     return (ParseTreeNode) (tp.getLastPathComponent());
374   }
375
376   void _tree_valueChanged(TreeSelectionEvent JavaDoc e)
377   {
378     ParseTreeNode tn = getSelectedNode();
379     if (tn == null) return;
380     SimpleNode node = (SimpleNode) tn.getNode();
381     _value.setText(DjString.stripPackagePrefixFromClassName(node.getClass().getName()) + "\n" + node.toString());
382     showLocation();
383   }
384
385   void showLocation()
386   {
387     try
388     {
389       ParseTreeNode tn = getSelectedNode();
390       if (tn == null) return;
391       SimpleNode sn = (SimpleNode) tn.getNode();
392       if (sn.beginLine == 0)
393       {
394         _lblStatus.setText("Location unspecified");
395       }
396       else
397       {
398         int offset = _sampleCode.getLineStartOffset(sn.beginLine - 1) + sn.beginColumn - 1;
399
400         _sampleCode.setCaretPosition(offset);
401         _lblStatus.setText("Located at " + sn.beginLine + ":" + sn.beginColumn + " (offset " + offset + ")");
402       }
403     }
404     catch (Exception JavaDoc x)
405     {
406       DjLogger.log(x);
407     }
408   }
409
410   void _tree_mouseClicked(MouseEvent JavaDoc e)
411   {
412     if (e.getClickCount() > 1) _sampleCode.requestFocus();
413   }
414
415   public void update()
416   {
417   }
418
419   void _sampleCode_keyPressed(KeyEvent JavaDoc e)
420   {
421     if (e.getKeyCode() == KeyEvent.VK_Z && e.getModifiers() == KeyEvent.CTRL_MASK)
422     {
423       //undoManager.undo();
424
}
425     if (e.getKeyCode() == KeyEvent.VK_Z && e.getModifiers() == KeyEvent.CTRL_MASK + KeyEvent.SHIFT_MASK)
426     {
427       //undoManager.redo();
428

429     }
430   }
431
432 }
Popular Tags