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