1 package com.genimen.djeneric.tools.generator.ui; 2 3 import java.awt.BorderLayout ; 4 import java.awt.Component ; 5 import java.awt.Dimension ; 6 import java.awt.Insets ; 7 import java.awt.event.ActionEvent ; 8 import java.awt.event.MouseEvent ; 9 import java.io.BufferedWriter ; 10 import java.io.File ; 11 import java.io.FileWriter ; 12 import java.io.IOException ; 13 import java.util.ArrayList ; 14 import java.util.Hashtable ; 15 16 import javax.swing.BoxLayout ; 17 import javax.swing.JButton ; 18 import javax.swing.JLabel ; 19 import javax.swing.JOptionPane ; 20 import javax.swing.JPanel ; 21 import javax.swing.JScrollPane ; 22 import javax.swing.JSplitPane ; 23 import javax.swing.JTextArea ; 24 import javax.swing.RepaintManager ; 25 import javax.swing.event.TreeExpansionEvent ; 26 import javax.swing.event.TreeSelectionEvent ; 27 import javax.swing.tree.DefaultMutableTreeNode ; 28 import javax.swing.tree.DefaultTreeModel ; 29 import javax.swing.tree.ExpandVetoException ; 30 import javax.swing.tree.TreePath ; 31 32 import com.genimen.djeneric.repository.DjExtent; 33 import com.genimen.djeneric.repository.DjModelView; 34 import com.genimen.djeneric.repository.DjPersistenceManager; 35 import com.genimen.djeneric.repository.DjSession; 36 import com.genimen.djeneric.structure.ExtentUsage; 37 import com.genimen.djeneric.tools.common.ModelViewDialog; 38 import com.genimen.djeneric.tools.generator.Generator; 39 import com.genimen.djeneric.tools.generator.core.ParseException; 40 import com.genimen.djeneric.tools.generator.tree.GeneratorTreeCellRenderer; 41 import com.genimen.djeneric.tools.generator.tree.GlobalTemplateNode; 42 import com.genimen.djeneric.tools.generator.tree.GlobalTemplatesFolder; 43 import com.genimen.djeneric.tools.generator.util.ProjectProperties; 44 import com.genimen.djeneric.tools.generator.util.Template; 45 import com.genimen.djeneric.tools.generator.util.TemplateSetManager; 46 import com.genimen.djeneric.tools.specifier.tree.DjenericFolder; 47 import com.genimen.djeneric.tools.specifier.tree.DjenericNode; 48 import com.genimen.djeneric.tools.specifier.tree.DjenericTree; 49 import com.genimen.djeneric.tools.specifier.tree.DjenericTreeNode; 50 import com.genimen.djeneric.ui.DjVerticalFlowLayout; 51 import com.genimen.djeneric.util.DjLogger; 52 import com.genimen.djeneric.util.DjProperties; 53 54 60 public class ProjectPanel extends JPanel 61 { 62 private static final long serialVersionUID = 1L; 63 66 public final static String OK_MSG = "Ok"; 67 68 Hashtable _allTemplateCheckBoxes = new Hashtable (); 69 DjProperties _templateSettings = new DjProperties(); 70 Hashtable _allTemplates = new Hashtable (); 71 ArrayList _allTemplatesWithErrors = new ArrayList (); 72 73 BorderLayout borderLayout1 = new BorderLayout (); 74 JPanel _treePanel = new JPanel (); 75 JButton _butGenerate = new JButton (); 76 JPanel jPanel2 = new JPanel (); 77 DjenericFolder _root = new DjenericFolder(); 78 DefaultTreeModel _treeModel = new DefaultTreeModel (_root); 79 DjenericTree _tree = new DjenericTree(); 80 JPanel jPanel1 = new JPanel (); 81 GeneratorTreeCellRenderer _treeCellRendrer = new GeneratorTreeCellRenderer(); 82 JPanel _templateListPanel = new JPanel () 83 { 84 private static final long serialVersionUID = 1L; 85 Insets insets = new Insets (0, 4, 0, 86 0); 87 88 public Insets getInsets() 89 { 90 return insets; 91 } 92 }; 93 BoxLayout _boxlayout = new BoxLayout (_templateListPanel, BoxLayout.Y_AXIS); 94 JScrollPane jScrollPane2 = new JScrollPane (); 95 JButton _butFilter = new JButton (); 96 JScrollPane jScrollPane1 = new JScrollPane (); 97 JButton _butReload = new JButton (); 98 JSplitPane _splitPane = new JSplitPane (); 99 JButton _butRescan = new JButton (); 100 JButton _butCollapseAll = new JButton (); 101 JLabel jLabel2 = new JLabel (); 102 JLabel jLabel1 = new JLabel (); 103 BorderLayout borderLayout3 = new BorderLayout (); 104 BorderLayout borderLayout2 = new BorderLayout (); 105 JLabel _lblTemplateTitle = new JLabel (); 106 JButton _butExpandAll = new JButton (); 107 DjVerticalFlowLayout verticalFlowLayout1 = new DjVerticalFlowLayout(); 108 DjPersistenceManager _mgr; 109 DjSession _session; 110 Generator _generator; 111 TemplateSetManager _templateSetManager; 112 ProjectProperties _projectProperties; 113 114 117 public ProjectPanel() 118 { 119 try 120 { 121 jbInit(); 122 } 123 catch (Exception ex) 124 { 125 DjLogger.log(ex); 126 } 127 } 128 129 135 public ProjectPanel(Generator generator, ProjectProperties projectProperties) 136 { 137 try 138 { 139 _projectProperties = projectProperties; 140 _templateSetManager = generator.getTemplateSetManager(); 141 _mgr = generator.getManager(); 142 _session = generator.getSession(); 143 _generator = generator; 144 jbInit(); 145 _splitPane.setDividerLocation(Integer.parseInt(_generator.getProperty("window.treedividerloc", "300"))); 146 setupNavigator(); 147 } 148 catch (Exception ex) 149 { 150 DjLogger.log(ex); 151 } 152 } 153 154 159 void jbInit() throws Exception 160 { 161 _treePanel.setLayout(borderLayout2); 162 this.setLayout(borderLayout1); 163 _butGenerate.setPreferredSize(new Dimension (24, 24)); 164 _butGenerate.setToolTipText("Generate"); 165 _butGenerate.addActionListener(new java.awt.event.ActionListener () 166 { 167 public void actionPerformed(ActionEvent e) 168 { 169 _butGenerate_actionPerformed(e); 170 } 171 }); 172 jPanel2.setLayout(verticalFlowLayout1); 173 _tree.setRootVisible(false); 174 _tree.setCellRenderer(_treeCellRendrer); 175 _tree.setModel(_treeModel); 176 _tree.setShowsRootHandles(true); 177 _tree.setAutoscrolls(true); 178 _tree.setDoubleBuffered(true); 179 _tree.setToggleClickCount(0); 180 _tree.setManager(getManager()); 181 _tree.setSession(getSession()); 182 _tree.addMouseListener(new java.awt.event.MouseAdapter () 183 { 184 public void mouseClicked(MouseEvent e) 185 { 186 _tree_mouseClicked(e); 187 } 188 }); 189 _tree.addTreeWillExpandListener(new javax.swing.event.TreeWillExpandListener () 190 { 191 public void treeWillExpand(TreeExpansionEvent e) throws ExpandVetoException 192 { 193 _tree_treeWillExpand(e); 194 } 195 196 public void treeWillCollapse(TreeExpansionEvent e) 197 { 198 } 199 }); 200 _tree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener () 201 { 202 public void valueChanged(TreeSelectionEvent e) 203 { 204 _tree_valueChanged(e); 205 } 206 }); 207 _tree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener () 208 { 209 public void valueChanged(TreeSelectionEvent e) 210 { 211 _tree_valueChanged(e); 212 } 213 }); 214 _tree.addTreeWillExpandListener(new javax.swing.event.TreeWillExpandListener () 215 { 216 public void treeWillExpand(TreeExpansionEvent e) throws ExpandVetoException 217 { 218 _tree_treeWillExpand(e); 219 } 220 221 public void treeWillCollapse(TreeExpansionEvent e) 222 { 223 } 224 }); 225 jPanel1.setLayout(borderLayout3); 226 _templateListPanel.setLayout(_boxlayout); 227 _butFilter.addActionListener(new java.awt.event.ActionListener () 228 { 229 public void actionPerformed(ActionEvent e) 230 { 231 _butFilter_actionPerformed(e); 232 } 233 }); 234 _butFilter.setToolTipText("Find"); 235 _butFilter.setPreferredSize(new Dimension (24, 24)); 236 _butReload.setPreferredSize(new Dimension (24, 24)); 237 _butReload.setToolTipText("Reload selected object from repository"); 238 _butReload.addActionListener(new java.awt.event.ActionListener () 239 { 240 public void actionPerformed(ActionEvent e) 241 { 242 _butReload_actionPerformed(e); 243 } 244 }); 245 _splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT); 246 _butRescan.addActionListener(new java.awt.event.ActionListener () 247 { 248 public void actionPerformed(ActionEvent e) 249 { 250 _butRescan_actionPerformed(e); 251 } 252 }); 253 _butRescan.setToolTipText("Rescan template set"); 254 _butRescan.setPreferredSize(new Dimension (24, 24)); 255 _butCollapseAll.setPreferredSize(new Dimension (24, 24)); 256 _butCollapseAll.setToolTipText("Collapse all"); 257 _butCollapseAll.addActionListener(new java.awt.event.ActionListener () 258 { 259 public void actionPerformed(ActionEvent e) 260 { 261 _butCollapseAll_actionPerformed(e); 262 } 263 }); 264 jLabel2.setText(" "); 265 jLabel2.setPreferredSize(new Dimension (3, 6)); 266 jLabel1.setPreferredSize(new Dimension (3, 6)); 267 jLabel1.setText(" "); 268 _lblTemplateTitle.setText("Associated templates"); 269 _butExpandAll.setPreferredSize(new Dimension (24, 24)); 270 _butExpandAll.setToolTipText("Expand all"); 271 _butExpandAll.addActionListener(new java.awt.event.ActionListener () 272 { 273 public void actionPerformed(ActionEvent e) 274 { 275 _butExpandAll_actionPerformed(e); 276 } 277 }); 278 _tree.putClientProperty("JTree.lineStyle", "Angled"); 279 _treePanel.add(jScrollPane1, BorderLayout.CENTER); 280 _treePanel.add(jPanel2, BorderLayout.WEST); 281 jScrollPane1.getViewport().add(_tree, null); 282 jPanel2.add(_butRescan, null); 283 jPanel2.add(_butReload, null); 284 jPanel2.add(_butFilter, null); 285 jPanel2.add(jLabel1, null); 286 jPanel2.add(_butGenerate, null); 287 jPanel2.add(jLabel2, null); 288 jPanel2.add(_butExpandAll, null); 289 jPanel2.add(_butCollapseAll, null); 290 _splitPane.add(jPanel1, JSplitPane.BOTTOM); 291 _splitPane.add(_treePanel, JSplitPane.TOP); 292 jPanel1.add(jScrollPane2, BorderLayout.CENTER); 293 jPanel1.add(_lblTemplateTitle, BorderLayout.NORTH); 294 jScrollPane2.getViewport().add(_templateListPanel, null); 295 this.add(_splitPane, BorderLayout.CENTER); 296 297 _butCollapseAll.setIcon(Generator.getImageIcon("collapseall.gif")); 298 _butExpandAll.setIcon(Generator.getImageIcon("expandall.gif")); 299 _butReload.setIcon(Generator.getImageIcon("reload.gif")); 300 _butRescan.setIcon(Generator.getImageIcon("refresh.gif")); 301 _butFilter.setIcon(Generator.getImageIcon("filter.gif")); 302 _butGenerate.setIcon(Generator.getImageIcon("generate.gif")); 303 304 } 305 306 311 void _butGenerate_actionPerformed(ActionEvent e) 312 { 313 performGenerate(); 314 } 315 316 319 void performGenerate() 320 { 321 initGenerateBatch(); 322 JTextArea logPane = _generator.getTextTab(Generator.GENERATOR_CONSOLE_TITLE, true); 323 DjenericTreeNode[] sels = getSelectedNodes(); 324 try 325 { 326 long currentTime = System.currentTimeMillis(); 327 for (int i = 0; i < sels.length; i++) 328 { 329 if (sels[i] instanceof GlobalTemplateNode) 330 { 331 GlobalTemplateNode gtn = (GlobalTemplateNode) sels[i]; 332 generateObjectBased(gtn.getTemplate(), logPane, null); 333 } 334 335 if (sels[i] instanceof GlobalTemplatesFolder) 336 { 337 GlobalTemplatesFolder gtf = (GlobalTemplatesFolder) sels[i]; 338 GlobalTemplateNode[] gtns = gtf.getChildNodes(); 339 for (int g = 0; g < gtns.length; g++) 340 { 341 generateObjectBased(gtns[g].getTemplate(), logPane, null); 342 } 343 } 344 345 if (sels[i] instanceof DjenericNode) 346 { 347 generate((DjenericNode) sels[i], logPane); 348 } 349 350 if (sels[i] instanceof DjenericFolder) 351 { 352 DjenericFolder folder = (DjenericFolder) sels[i]; 353 DjenericNode[] subs = folder.getChildNodes(); 354 for (int x = 0; x < subs.length; x++) 355 { 356 generate(subs[x], logPane); 357 } 358 } 359 } 360 currentTime = (System.currentTimeMillis() - currentTime) / 1000; 361 printMsg(logPane, "\nDone in " + currentTime + " seconds.\n"); 362 setStatusMessage(OK_MSG); 363 } 364 catch (Exception x) 365 { 366 setStatusMessage(x.getMessage(), false); 367 DjLogger.log(x); 368 } 369 } 370 371 377 void _tree_treeWillExpand(TreeExpansionEvent e) throws ExpandVetoException 378 { 379 try 380 { 381 if (e.getPath().getLastPathComponent() instanceof DjenericTreeNode) 382 { 383 DjenericTreeNode node = (DjenericTreeNode) e.getPath().getLastPathComponent(); 384 node.expandNode(); 385 } 386 setStatusMessage(OK_MSG); 387 } 388 catch (Exception x) 389 { 390 setStatusMessage(x); 391 throw new ExpandVetoException (new TreeExpansionEvent (this, e.getPath()), x.getMessage()); 392 } 393 } 394 395 401 void _tree_treeWillCollapse(TreeExpansionEvent e) 402 { 403 } 404 405 410 void _tree_valueChanged(TreeSelectionEvent e) 411 { 412 displayAssociatedTemplates(); 413 } 414 415 420 void _butFilter_actionPerformed(ActionEvent e) 421 { 422 423 DjenericTreeNode[] sels = getSelectedNodes(); 424 try 425 { 426 for (int i = 0; i < sels.length; i++) 427 { 428 if (sels[i].canFilter()) 429 { 430 sels[i].filter(); 431 } 432 } 433 434 } 435 catch (Exception x) 436 { 437 setStatusMessage(x); 438 } 439 440 } 441 442 447 void _butReload_actionPerformed(ActionEvent e) 448 { 449 450 DjenericTreeNode[] sels = getSelectedNodes(); 451 try 452 { 453 for (int i = 0; i < sels.length; i++) 454 { 455 sels[i].reload(); 456 } 457 458 setStatusMessage(OK_MSG); 459 } 460 catch (Exception x) 461 { 462 setStatusMessage(x); 463 } 464 } 465 466 471 void _butRescan_actionPerformed(ActionEvent e) 472 { 473 refreshTemplates(); 474 } 475 476 481 void _butCollapseAll_actionPerformed(ActionEvent e) 482 { 483 DjenericTreeNode[] sels = getSelectedNodes(); 484 try 485 { 486 for (int i = 0; i < sels.length; i++) 487 { 488 sels[i].collapseAll(); 489 } 490 491 setStatusMessage(OK_MSG); 492 } 493 catch (Exception x) 494 { 495 setStatusMessage(x); 496 } 497 } 498 499 504 void _butExpandAll_actionPerformed(ActionEvent e) 505 { 506 DjenericTreeNode[] sels = getSelectedNodes(); 507 try 508 { 509 for (int i = 0; i < sels.length; i++) 510 { 511 sels[i].expandAll(0); 512 } 513 514 setStatusMessage(OK_MSG); 515 } 516 catch (Exception x) 517 { 518 setStatusMessage(x); 519 } 520 } 521 522 527 protected void setupNavigator() throws Exception 528 { 529 _root.setTree(_tree); 530 531 ModelViewDialog vd = new ModelViewDialog(this, _mgr.getCurrentUser()); 532 DjModelView selectedView = vd.getView(); 533 534 ExtentUsage[] navigatorStructure = selectedView.getExtentUsages(); 535 536 _root.insertAsFolder(new GlobalTemplatesFolder(_templateSetManager, _projectProperties)); 537 538 for (int i = 0; i < navigatorStructure.length; i++) 539 { 540 DjenericFolder folder = new DjenericFolder(); 541 folder.setExtentUsage(navigatorStructure[i]); 542 _root.insertAsFolder(folder); 543 } 544 _treeModel.nodeStructureChanged(_root); 545 _tree_valueChanged(null); 546 547 } 548 549 554 public DjPersistenceManager getManager() 555 { 556 return _mgr; 557 } 558 559 564 public DjSession getSession() 565 { 566 return _session; 567 } 568 569 574 public DjenericTreeNode[] getSelectedNodes() 575 { 576 if (_tree.getSelectionPath() == null) 577 { 578 return new DjenericTreeNode[0]; 579 } 580 ArrayList result = new ArrayList (10); 581 582 TreePath [] tp = _tree.getSelectionPaths(); 583 584 for (int qq = 0; qq < tp.length; qq++) 585 { 586 DefaultMutableTreeNode node = (DefaultMutableTreeNode ) (tp[qq].getLastPathComponent()); 587 if (node instanceof DjenericTreeNode) 588 { 589 result.add(node); 590 } 591 } 592 return (DjenericTreeNode[]) result.toArray(new DjenericTreeNode[0]); 593 } 594 595 600 public void setStatusMessage(String msg) 601 { 602 _generator.setStatusMessage(msg, true); 603 } 604 605 611 public void setStatusMessage(String msg, boolean informative) 612 { 613 _generator.setStatusMessage(msg, informative); 614 } 615 616 public void setStatusMessage(Throwable t) 617 { 618 _generator.setStatusMessage(t); 619 } 620 621 627 public void setStatusMessageNow(String msg, boolean informative) 628 { 629 _generator.setStatusMessage(msg, informative); 630 } 631 632 635 void displayAssociatedTemplates() 636 { 637 DjenericTreeNode[] sels = getSelectedNodes(); 638 639 boolean canCollapse = false; 640 boolean canExpand = false; 641 boolean canReload = false; 642 boolean canFilter = false; 643 boolean canGenerate = false; 644 645 for (int i = 0; i < sels.length; i++) 646 { 647 canFilter |= sels[i].canFilter(); 648 canCollapse = true; 649 canExpand |= (sels[i].getChildCount() > 0); 650 canReload |= sels[i] instanceof DjenericNode; 651 canReload |= sels[i] instanceof DjenericFolder; 652 canGenerate |= sels[i] instanceof GlobalTemplateNode; 653 canGenerate |= sels[i] instanceof GlobalTemplatesFolder; 654 } 655 _butCollapseAll.setEnabled(canCollapse); 656 _butExpandAll.setEnabled(canExpand); 657 _butCollapseAll.setEnabled(canExpand); 658 _butReload.setEnabled(canReload); 659 _butFilter.setEnabled(canFilter); 660 661 if (sels.length > 0) 662 { 663 canGenerate |= updateTemplateList(sels[0]); 664 } 665 666 _butGenerate.setEnabled(canGenerate); 667 } 668 669 private boolean specializationsHaveTemplate(DjExtent ext) 670 { 671 DjExtent[] specs = ext.getSpecializations(); 672 673 boolean result = false; 674 for (int i = 0; i < specs.length && !result; i++) 675 { 676 ArrayList templates = (ArrayList ) _allTemplateCheckBoxes.get(specs[i].getObjectType()); 677 if (templates != null) return true; 678 result |= specializationsHaveTemplate(specs[i]); 679 } 680 return result; 681 } 682 683 689 boolean updateTemplateList(DjenericTreeNode node) 690 { 691 boolean result = false; 692 693 _templateListPanel.removeAll(); 694 695 DjExtent ext = null; 696 if (node instanceof DjenericNode) 697 { 698 ext = ((DjenericNode) node).getObject().getExtent(); 699 } 700 else if (node instanceof DjenericFolder) 701 { 702 ext = node.getExtentUsage().getExtent(); 703 704 708 result = specializationsHaveTemplate(ext); 709 } 710 711 while (ext != null) 712 { 713 String objectType = ext.getObjectType(); 714 715 ArrayList templates = (ArrayList ) _allTemplateCheckBoxes.get(objectType); 716 if (templates != null) 717 { 718 for (int i = 0; i < templates.size(); i++) 719 { 720 _templateListPanel.add((Component ) templates.get(i)); 721 } 722 result = true; 723 } 724 ext = ext.getSuper(); 725 } 726 727 if (node instanceof GlobalTemplateNode || node instanceof GlobalTemplatesFolder) 728 { 729 _templateListPanel.add(new JLabel ("Global template")); 730 } 731 else if (!result) 732 { 733 _templateListPanel.add(new JLabel ("No template available")); 734 } 735 736 _templateListPanel.invalidate(); 737 _templateListPanel.validate(); 738 _templateListPanel.repaint(); 739 return result; 740 } 741 742 748 void printMsg(JTextArea ta, String msg) 749 { 750 ta.append(msg); 751 ta.append("\n"); 752 RepaintManager rm = RepaintManager.currentManager(ta); 753 if (rm != null) 754 { 755 rm.paintDirtyRegions(); 756 } 757 } 758 759 766 void generate(DjenericNode node, JTextArea logPane) throws Exception 767 { 768 DjExtent ext = node.getObject().getExtent(); 769 770 while (ext != null) 771 { 772 String objectType = ext.getObjectType(); 773 774 ArrayList templates = (ArrayList ) _allTemplateCheckBoxes.get(objectType); 775 if (templates != null) 776 { 777 for (int i = 0; i < templates.size(); i++) 778 { 779 TemplateCheckBox cb = (TemplateCheckBox) templates.get(i); 780 if (cb.isSelected()) 781 { 782 generateObjectBased(cb.getTemplate(), logPane, node); 783 } 784 } 785 } 786 ext = ext.getSuper(); 787 } 788 } 789 790 796 public void createDirectories(String fileName) 797 { 798 int idx = fileName.lastIndexOf("/"); 799 int idx2 = fileName.lastIndexOf("\\"); 800 if (idx == -1) 801 { 802 idx = idx2; 803 } 804 if (idx < idx2) 805 { 806 idx = idx2; 807 } 808 if (idx != -1) 809 { 810 fileName = fileName.substring(0, idx); 811 } 812 813 File fd = new File (fileName); 814 fd.mkdirs(); 815 } 816 817 820 public void refreshTemplates() 821 { 822 _allTemplateCheckBoxes.clear(); 823 _allTemplates.clear(); 824 _allTemplatesWithErrors.clear(); 825 826 Template[] allTemplates = _templateSetManager.getTemplates(_projectProperties.getTemplateSets()); 827 for (int i = 0; i < allTemplates.length; i++) 828 { 829 String objectTypeBasedOn = "<Root based>"; 830 if (allTemplates[i].isRootBased()) 831 { 832 objectTypeBasedOn = allTemplates[i].getObjectType(); 833 } 834 835 ArrayList templateCbs = (ArrayList ) _allTemplateCheckBoxes.get(objectTypeBasedOn); 836 if (templateCbs == null) 837 { 838 templateCbs = new ArrayList (); 839 _allTemplateCheckBoxes.put(objectTypeBasedOn, templateCbs); 840 } 841 templateCbs.add(new TemplateCheckBox(allTemplates[i], _projectProperties)); 842 843 ArrayList template = (ArrayList ) _allTemplates.get(objectTypeBasedOn); 844 if (template == null) 845 { 846 template = new ArrayList (); 847 _allTemplates.put(objectTypeBasedOn, template); 848 } 849 template.add(allTemplates[i]); 850 } 851 String loadLog = _templateSetManager.getLoadLog(); 852 853 if (loadLog.length() != 0) 854 { 855 JTextArea ta = _generator.getTextTab("Template load log", true); 856 ta.setText(loadLog); 857 } 858 859 try 860 { 861 for (int i = 0; i < _root.getChildCount(); i++) 862 { 863 if (_root.getChildAt(i) instanceof DjenericTreeNode) 864 { 865 DjenericTreeNode dtn = (DjenericTreeNode) _root.getChildAt(i); 866 dtn.reload(); 867 } 868 } 869 } 870 catch (Exception x) 871 { 872 setStatusMessage("Reloaded ok, but: " + x.getMessage()); 873 } 874 displayAssociatedTemplates(); 875 876 setStatusMessage("Templates reloaded"); 877 } 878 879 882 public void close() 883 { 884 _generator.setProperty("window.treedividerloc", "" + _splitPane.getDividerLocation()); 885 } 886 887 896 private void writeCode(Template tpl, String code, JTextArea logPane) throws IOException , ParseException 897 { 898 String outputPath = _projectProperties.getOutputPath(); 899 String fileName = tpl.getFileName(); 900 if (!outputPath.endsWith("/") && !outputPath.endsWith("\\")) 901 { 902 outputPath += File.separator; 903 } 904 outputPath += fileName; 905 906 createDirectories(outputPath); 907 File file = new File (outputPath); 908 if (checkOverwrite(file)) 909 { 910 printMsg(logPane, "Writing to " + outputPath); 911 BufferedWriter w = new BufferedWriter (new FileWriter (file)); 912 w.write(code); 913 w.close(); 914 if (_projectProperties.isOpenAfterGenerate()) 915 { 916 JTextArea ta = _generator.getTextTab(fileName, false); 917 ta.setText(code); 918 } 919 } 920 else 921 { 922 printMsg(logPane, "Skipped; file existed and not overwritten: " + outputPath); 923 } 924 } 925 926 boolean _dontAskAgain = false; 927 928 931 protected void initGenerateBatch() 932 { 933 _dontAskAgain = false; 934 } 935 936 942 private boolean checkOverwrite(File file) 943 { 944 if (_dontAskAgain) 945 { 946 return true; 947 } 948 if (!file.exists()) 949 { 950 return true; 951 } 952 953 if (_projectProperties.getOverwriteAction() == ProjectProperties.OVERWRITE_ALWAYS) 954 { 955 return true; 956 } 957 if (_projectProperties.getOverwriteAction() == ProjectProperties.OVERWRITE_NEVER) 958 { 959 return false; 960 } 961 962 int result = JOptionPane.showOptionDialog(this, "Do you want to overwrite " + file.getAbsolutePath() + "?", 963 "File exists", JOptionPane.DEFAULT_OPTION, 964 JOptionPane.INFORMATION_MESSAGE, null 965 , new String []{"Overwrite", "Overwrite All", "Skip"}, null); 967 _dontAskAgain = result == 1; 968 return (result == 0 || result == 1); 969 } 970 971 978 private void generateObjectBased(Template tpl, JTextArea logPane, DjenericNode node) 979 { 980 try 981 { 982 if (node != null) 983 { 984 printMsg(logPane, "Generating " + tpl.getModuleName() + " based on " + node.getObject().toString()); 985 } 986 else 987 { 988 printMsg(logPane, "Generating " + tpl.getModuleName() + " (global template)"); 989 } 990 991 String code; 992 if (node != null) 993 { 994 code = tpl.generate(getManager(), node.getObject().getObjectId()); 995 } 996 else 997 { 998 code = tpl.generate(getManager()); 999 } 1000 1001 writeCode(tpl, code, logPane); 1002 } 1003 catch (ParseException x) 1004 { 1005 printMsg(logPane, "Exception occurred in template " + tpl + ", line " + x.getLine() + ", column " + x.getColumn() 1006 + "\n" + x.getMessage()); 1007 DjLogger.log(x); 1008 } 1009 catch (Exception x) 1010 { 1011 printMsg(logPane, "Exception occurred in template " + tpl + "\n" + x.getMessage()); 1012 DjLogger.log(x); 1013 } 1014 } 1015 1016 1021 void _tree_mouseClicked(MouseEvent e) 1022 { 1023 if (e.getClickCount() > 1) 1024 { 1025 _butGenerate_actionPerformed(null); 1026 } 1027 } 1028} | Popular Tags |