1 package com.genimen.djeneric.repository.oql.test; 2 3 import java.awt.AWTEvent ; 4 import java.awt.BorderLayout ; 5 import java.awt.Dimension ; 6 import java.awt.FlowLayout ; 7 import java.awt.Toolkit ; 8 import java.awt.event.ActionEvent ; 9 import java.awt.event.KeyEvent ; 10 import java.awt.event.MouseEvent ; 11 import java.awt.event.WindowEvent ; 12 import java.io.BufferedReader ; 13 import java.io.InputStream ; 14 import java.io.InputStreamReader ; 15 import java.net.URL ; 16 17 import javax.swing.ImageIcon ; 18 import javax.swing.JButton ; 19 import javax.swing.JCheckBox ; 20 import javax.swing.JLabel ; 21 import javax.swing.JOptionPane ; 22 import javax.swing.JPanel ; 23 import javax.swing.JScrollPane ; 24 import javax.swing.JSplitPane ; 25 import javax.swing.JTextArea ; 26 import javax.swing.JTree ; 27 import javax.swing.UIManager ; 28 import javax.swing.event.TreeSelectionEvent ; 29 import javax.swing.tree.DefaultTreeModel ; 30 import javax.swing.tree.TreePath ; 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 indent; 48 BorderLayout borderLayout1 = new BorderLayout (); 49 JPanel jPanel1 = new JPanel (); 50 BorderLayout borderLayout2 = new BorderLayout (); 51 JPanel jPanel2 = new JPanel (); 52 JButton _butParse = new JButton (); 53 JPanel jPanel3 = new JPanel (); 54 FlowLayout flowLayout1 = new FlowLayout (); 55 JButton jButton2 = new JButton (); 56 int curIdx = 1; 57 JSplitPane jSplitPane2 = new JSplitPane (); 58 JTextArea _sampleCode = new JTextArea (); 59 JScrollPane _scrCode = new JScrollPane (); 60 JTextArea _value = new JTextArea (); 61 JScrollPane _scrValue = new JScrollPane (); 62 JSplitPane jSplitPane1 = new JSplitPane (); 63 JScrollPane _scrTree = new JScrollPane (); 64 JTree _tree = new JTree (); 65 JLabel _lblStatus = new JLabel (); 66 String NEWLINE = System.getProperty("line.separator", "\n"); 67 SimpleNode _parseTreeRoot = null; 68 ParseTreeNode _treeRoot = null; 69 DefaultTreeModel _treeModel = null; 70 JCheckBox _chkNormalize = new JCheckBox (); 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 e) 87 { 88 JOptionPane.showMessageDialog(this, e.getMessage()); 89 DjLogger.log(e); 90 System.exit(-1); 91 } 92 } 93 94 public static ImageIcon getImageIcon(String fileName) 95 { 96 URL url = DjOqlParserTest.class.getResource(fileName); 97 if (url != null) return new ImageIcon (url); 98 else return new ImageIcon (""); 99 } 101 102 private void jbInit() throws Exception 103 { 104 this.getContentPane().setLayout(borderLayout1); 105 jPanel1.setLayout(borderLayout2); 106 _butParse.setText("(Re)parse"); 107 _butParse.addActionListener(new java.awt.event.ActionListener () 108 { 109 public void actionPerformed(ActionEvent 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 () 118 { 119 public void actionPerformed(ActionEvent e) 120 { 121 jButton2_actionPerformed(e); 122 } 123 }); 124 _sampleCode.setFont(new java.awt.Font ("Monospaced", 0, 12)); 125 _sampleCode.addKeyListener(new java.awt.event.KeyAdapter () 126 { 127 public void keyPressed(KeyEvent e) 128 { 129 _sampleCode_keyPressed(e); 130 } 131 }); 132 _value.setFont(new java.awt.Font ("Monospaced", 0, 12)); 133 _tree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener () 134 { 135 public void valueChanged(TreeSelectionEvent e) 136 { 137 _tree_valueChanged(e); 138 } 139 }); 140 _tree.addMouseListener(new java.awt.event.MouseAdapter () 141 { 142 public void mouseClicked(MouseEvent 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 tpl) throws Exception 184 { 185 return DjOqlParserEngine.parseExpression(tpl); 186 } 187 188 protected void processWindowEvent(WindowEvent 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 [] args) 199 { 200 try 201 { 202 UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); 203 DjOqlParserTest frame = new DjOqlParserTest(); 204 frame.validate(); 205 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 206 Dimension 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 e) 216 { 217 DjLogger.log(e); 218 } 219 } 220 221 String getSample(int idx) throws Exception 222 { 223 InputStream is = this.getClass().getResourceAsStream(idx + ".scr"); 224 if (is == null) 225 { 226 return null; 227 } 228 BufferedReader br = new BufferedReader (new InputStreamReader (is)); 229 String line; 230 StringBuffer sb = new StringBuffer (); 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 (_treeRoot); 243 buildTree(_tree, _treeRoot); 244 245 _tree.setModel(_treeModel); 246 247 _treeModel.nodeStructureChanged(_treeRoot); 248 expandAll(_treeRoot); 249 } 250 251 void buildTree(JTree 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 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 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 (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 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 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 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 e) 357 { 358 nextSample(); 359 } 360 361 void _butParse_actionPerformed(ActionEvent e) 362 { 363 reparse(); 364 365 } 366 367 public ParseTreeNode getSelectedNode() 368 { 369 if (_tree.getSelectionPath() == null) return null; 370 371 TreePath tp = _tree.getSelectionPath(); 372 373 return (ParseTreeNode) (tp.getLastPathComponent()); 374 } 375 376 void _tree_valueChanged(TreeSelectionEvent 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 x) 405 { 406 DjLogger.log(x); 407 } 408 } 409 410 void _tree_mouseClicked(MouseEvent e) 411 { 412 if (e.getClickCount() > 1) _sampleCode.requestFocus(); 413 } 414 415 public void update() 416 { 417 } 418 419 void _sampleCode_keyPressed(KeyEvent e) 420 { 421 if (e.getKeyCode() == KeyEvent.VK_Z && e.getModifiers() == KeyEvent.CTRL_MASK) 422 { 423 } 425 if (e.getKeyCode() == KeyEvent.VK_Z && e.getModifiers() == KeyEvent.CTRL_MASK + KeyEvent.SHIFT_MASK) 426 { 427 429 } 430 } 431 432 } | Popular Tags |