1 30 31 package com.genimen.djeneric.tools.templateeditor; 32 33 import java.awt.AWTEvent ; 34 import java.awt.BorderLayout ; 35 import java.awt.Color ; 36 import java.awt.Component ; 37 import java.awt.Dimension ; 38 import java.awt.Image ; 39 import java.awt.SystemColor ; 40 import java.awt.Toolkit ; 41 import java.awt.event.ActionEvent ; 42 import java.awt.event.InputEvent ; 43 import java.awt.event.KeyEvent ; 44 import java.awt.event.KeyListener ; 45 import java.awt.event.WindowEvent ; 46 import java.io.File ; 47 import java.io.FileNotFoundException ; 48 import java.net.URL ; 49 50 import javax.swing.ImageIcon ; 51 import javax.swing.JFileChooser ; 52 import javax.swing.JLabel ; 53 import javax.swing.JMenu ; 54 import javax.swing.JMenuBar ; 55 import javax.swing.JMenuItem ; 56 import javax.swing.JPanel ; 57 import javax.swing.JScrollPane ; 58 import javax.swing.JSplitPane ; 59 import javax.swing.JTabbedPane ; 60 import javax.swing.JTextArea ; 61 import javax.swing.JTree ; 62 63 import com.genimen.djeneric.repository.DjModelView; 64 import com.genimen.djeneric.repository.DjPersistenceManager; 65 import com.genimen.djeneric.repository.DjPersistenceManagerFactory; 66 import com.genimen.djeneric.repository.DjSession; 67 import com.genimen.djeneric.repository.DjUserContextAssociation; 68 import com.genimen.djeneric.repository.exceptions.DjenericException; 69 import com.genimen.djeneric.structure.ExtentUsage; 70 import com.genimen.djeneric.tools.common.AboutDialog; 71 import com.genimen.djeneric.tools.common.DjenericTool; 72 import com.genimen.djeneric.tools.common.ModelViewDialog; 73 import com.genimen.djeneric.tools.generator.util.TemplateSet; 74 import com.genimen.djeneric.tools.generator.util.TemplateSetManager; 75 import com.genimen.djeneric.tools.specifier.tree.DjenericFolder; 76 import com.genimen.djeneric.tools.templateeditor.dialogs.OptionsDialog; 77 import com.genimen.djeneric.tools.templateeditor.ui.TemplatePanel; 78 import com.genimen.djeneric.util.DjEnvironment; 79 import com.genimen.djeneric.util.DjFileFilter; 80 import com.genimen.djeneric.util.DjLogPrintStream; 81 import com.genimen.djeneric.util.DjLogger; 82 import com.genimen.djeneric.util.DjStatusDisplayer; 83 import com.genimen.djeneric.util.DjVersion; 84 85 public class TemplateEditor extends DjenericTool implements KeyListener , DjStatusDisplayer 86 { 87 private static final long serialVersionUID = 1L; 88 protected final String PROPERTIES_FILE_NAME = DjEnvironment.getAbsoluteFileName("templateeditor.properties"); 89 protected final static String LOG_FILE_NAME = DjEnvironment.getAbsoluteFileName("templateeditor.log"); 90 91 public final static String OK_MSG = "Ok"; 92 public final static String KEY_TEMPLATEPATH = "path.template"; 93 public final static String KEY_INCLUDEPATH = "path.include"; 94 95 JSplitPane _splitpane = new JSplitPane (); 96 JMenuBar _menu = new JMenuBar (); 97 JMenu _mnuFile = new JMenu (); 98 JMenuItem _mnuExit = new JMenuItem (); 99 JMenu _mnuHelp = new JMenu (); 100 JMenuItem _mnuAbout = new JMenuItem (); 101 DjPersistenceManager _mgr; 102 DjSession _session; 103 DjenericFolder _root = new DjenericFolder(); 104 105 JLabel _lblStatus = new JLabel (); 106 Color _normalColor = new Color (102, 102, 153); 107 108 JPanel _desktopPanel = new JPanel (); 109 BorderLayout borderLayout2 = new BorderLayout (); 110 JTabbedPane _tabs = new JTabbedPane (); 111 JMenuItem _mnuNewSet = new JMenuItem (); 112 JMenuItem _mnuOpenSet = new JMenuItem (); 113 JMenuItem _mnuSetProperties = new JMenuItem (); 114 JMenuItem _mnuOptions = new JMenuItem (); 115 JMenu _mnuTools = new JMenu (); 116 TemplateSetManager _templateSetManager = new TemplateSetManager(); 117 ExtentUsage[] _navigatorStructure = null; 118 119 JMenu _mnuTabs = new JMenu (); 120 JMenuItem _mnuNextTab = new JMenuItem (); 121 JMenuItem _mnuPrevTab = new JMenuItem (); 122 JMenuItem _mnuCloseTab = new JMenuItem (); 123 JMenuItem _mnuCloseAll = new JMenuItem (); 124 JSplitPane _structureSplitter = new JSplitPane (); 125 JTabbedPane _templateSetTabs = new JTabbedPane (); 126 JScrollPane _scrStructure = new JScrollPane (); 127 JTree _structureTree = new JTree (); 128 129 135 public TemplateEditor(String repositories) 136 { 137 enableEvents(AWTEvent.WINDOW_EVENT_MASK); 138 setIconImage(getImage("templateeditor.gif")); 139 140 try 141 { 142 setStatusLabel(_lblStatus); 143 invalidate(); 144 loadProps(PROPERTIES_FILE_NAME); 145 setupWindow(); 146 147 setupPersistenceManager(repositories); 148 setupSession(); 149 setupContext(); 150 151 jbInit(); 152 getContentPane().setBackground(SystemColor.desktop); 153 154 rescanTemplateSets(); 155 updateTitle(); 156 validate(); 157 _splitpane.setVisible(false); 158 } 159 catch (Exception e) 160 { 161 handleException(e); 162 System.exit(-1); 163 } 164 } 165 166 public TemplateSetManager getTemplateSetManager() 167 { 168 return _templateSetManager; 169 } 170 171 protected void setupPersistenceManager(String repositories) throws DjenericException, FileNotFoundException 172 { 173 DjPersistenceManagerFactory fact = new DjPersistenceManagerFactory(this, repositories); 174 _mgr = fact.createInstance(); 175 } 176 177 protected void setupSession() throws DjenericException 178 { 179 _session = _mgr.createSession(); 180 _session.setValidateObjectsAfterLoading(false); 181 } 182 183 protected void setupContext() throws DjenericException 184 { 185 DjUserContextAssociation[] ctxt = _mgr.getCurrentUser().getContextAssociations(); 186 if (ctxt.length == 1) _mgr.getContextManager().setCurrentContext(ctxt[0].getContext()); 187 188 else 189 _mgr.determineContext(); 191 } 192 193 public ExtentUsage[] getNavigatorStructure() throws DjenericException 194 { 195 if (_navigatorStructure != null) return _navigatorStructure; 196 197 ModelViewDialog vd = new ModelViewDialog(getContentPane(), _mgr.getCurrentUser()); 198 DjModelView selectedView = vd.getView(); 199 200 _navigatorStructure = selectedView.getExtentUsages(); 201 return _navigatorStructure; 202 } 203 204 protected void setupWindow() 205 { 206 int divloc = Integer.parseInt(getProperty("window.dividerloc", "225")); 207 _splitpane.setDividerLocation(divloc); 208 209 divloc = Integer.parseInt(getProperty("window.structuredividerloc", "225")); 210 _structureSplitter.setDividerLocation(divloc); 211 212 int width = Integer.parseInt(getProperty("window.width", "400")); 213 int height = Integer.parseInt(getProperty("window.height", "400")); 214 setSize(new Dimension (width, height)); 215 addKeyListener(this); 216 } 217 218 protected void updateTitle() 219 { 220 try 221 { 222 setTitle("Djeneric TemplateEditor v" + DjVersion.getVersion() + " - " + getManager().getConnectionTitle()); 223 } 224 catch (Exception x) 225 { 226 DjLogger.log(x); 227 } 228 } 229 230 236 protected void jbInit() throws Exception 237 { 238 _structureTree.putClientProperty("JTree.lineStyle", "Angled"); 239 _mnuFile.setMnemonic('F'); 240 _mnuFile.setText("File"); 241 _mnuExit.setText("Exit"); 242 _mnuExit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(115, java.awt.event.KeyEvent.ALT_MASK, false)); 243 _mnuExit.addActionListener(new java.awt.event.ActionListener () 244 { 245 public void actionPerformed(ActionEvent e) 246 { 247 _mnuExit_actionPerformed(e); 248 } 249 }); 250 _mnuHelp.setMnemonic('H'); 251 _mnuHelp.setText("Help"); 252 _mnuAbout.setText("About"); 253 _mnuAbout.addActionListener(new java.awt.event.ActionListener () 254 { 255 public void actionPerformed(ActionEvent e) 256 { 257 _mnuAbout_actionPerformed(e); 258 } 259 }); 260 this.setJMenuBar(_menu); 261 this.setVisible(true); 262 _lblStatus.setText("OK"); 263 _lblStatus.setOpaque(true); 264 _desktopPanel.setLayout(borderLayout2); 265 _desktopPanel.setBackground(SystemColor.desktop); 266 _mnuNewSet.setMnemonic('P'); 267 _mnuNewSet.setText("New Template Set..."); 268 _mnuNewSet.setAccelerator(javax.swing.KeyStroke.getKeyStroke(78, java.awt.event.KeyEvent.CTRL_MASK, false)); 269 _mnuOpenSet.setMnemonic('O'); 270 _mnuOpenSet.setText("Open Template Set..."); 271 _mnuOpenSet.setAccelerator(javax.swing.KeyStroke.getKeyStroke(79, java.awt.event.KeyEvent.CTRL_MASK, false)); 272 _mnuOpenSet.addActionListener(new java.awt.event.ActionListener () 273 { 274 public void actionPerformed(ActionEvent e) 275 { 276 _mnuOpenSet_actionPerformed(e); 277 } 278 }); 279 _mnuSetProperties.setText("Template Set Properties..."); 280 _mnuOptions.setText("Options..."); 281 _mnuOptions.addActionListener(new java.awt.event.ActionListener () 282 { 283 public void actionPerformed(ActionEvent e) 284 { 285 _mnuOptions_actionPerformed(e); 286 } 287 }); 288 _mnuTools.setMnemonic('T'); 289 _mnuTools.setText("Tools"); 290 _mnuTabs.setMnemonic('A'); 291 _mnuTabs.setText("Tabs"); 292 _mnuNextTab.setText("Next"); 293 _mnuNextTab.setAccelerator(javax.swing.KeyStroke.getKeyStroke(117, java.awt.event.KeyEvent.CTRL_MASK, false)); 294 _mnuNextTab.addActionListener(new java.awt.event.ActionListener () 295 { 296 public void actionPerformed(ActionEvent e) 297 { 298 _mnuNextTab_actionPerformed(e); 299 } 300 }); 301 _mnuPrevTab.setText("Previous"); 302 _mnuPrevTab.setAccelerator(javax.swing.KeyStroke.getKeyStroke(117, java.awt.event.KeyEvent.CTRL_MASK, false)); 303 _mnuPrevTab.addActionListener(new java.awt.event.ActionListener () 304 { 305 public void actionPerformed(ActionEvent e) 306 { 307 _mnuPrevTab_actionPerformed(e); 308 } 309 }); 310 _mnuCloseTab.setText("Close"); 311 _mnuCloseTab.setAccelerator(javax.swing.KeyStroke.getKeyStroke(115, java.awt.event.KeyEvent.CTRL_MASK, false)); 312 _mnuCloseTab.addActionListener(new java.awt.event.ActionListener () 313 { 314 public void actionPerformed(ActionEvent e) 315 { 316 _mnuCloseTab_actionPerformed(e); 317 } 318 }); 319 _mnuCloseAll.setText("Close All"); 320 _mnuCloseAll.setAccelerator(javax.swing.KeyStroke.getKeyStroke(115, java.awt.event.KeyEvent.CTRL_MASK 321 | java.awt.event.KeyEvent.SHIFT_MASK, false)); 322 _mnuCloseAll.addActionListener(new java.awt.event.ActionListener () 323 { 324 public void actionPerformed(ActionEvent e) 325 { 326 _mnuCloseAll_actionPerformed(e); 327 } 328 }); 329 _splitpane.setOneTouchExpandable(true); 330 _structureSplitter.setOrientation(JSplitPane.VERTICAL_SPLIT); 331 _templateSetTabs.setOpaque(true); 332 this.getContentPane().add(_splitpane, BorderLayout.CENTER); 333 _splitpane.add(_desktopPanel, JSplitPane.RIGHT); 334 _desktopPanel.add(_tabs, BorderLayout.CENTER); 335 this.getContentPane().add(_lblStatus, BorderLayout.SOUTH); 336 _menu.add(_mnuFile); 337 _menu.add(_mnuTools); 338 _menu.add(_mnuTabs); 339 _menu.add(_mnuHelp); 340 _mnuFile.add(_mnuNewSet); 341 _mnuFile.add(_mnuOpenSet); 342 _mnuFile.addSeparator(); 343 _mnuFile.add(_mnuSetProperties); 344 _mnuFile.addSeparator(); 345 _mnuFile.add(_mnuExit); 346 _mnuHelp.add(_mnuAbout); 347 _mnuTools.add(_mnuOptions); 348 _mnuTabs.add(_mnuNextTab); 349 _mnuTabs.add(_mnuPrevTab); 350 _mnuTabs.add(_mnuCloseTab); 351 _mnuTabs.add(_mnuCloseAll); 352 353 setTabsVisible(false); 354 _structureSplitter.add(_scrStructure, JSplitPane.BOTTOM); 355 _scrStructure.getViewport().add(_structureTree, null); 356 _structureSplitter.add(_templateSetTabs, JSplitPane.TOP); 357 _splitpane.add(_structureSplitter, JSplitPane.LEFT); 358 359 } 360 361 367 protected void processWindowEvent(WindowEvent e) 368 { 369 if (e.getID() == WindowEvent.WINDOW_CLOSING) 370 { 371 try 372 { 373 if (canExit()) exitProgram(); 374 } 375 catch (Exception iox) 376 { 377 setStatusMessage(iox); 378 } 379 } 380 else super.processWindowEvent(e); 381 382 } 383 384 protected boolean canExit() throws Exception 385 { 386 return true; 387 } 388 389 protected void updateProperties() 390 { 391 setProperty("window.width", "" + getWidth()); 392 setProperty("window.height", "" + getHeight()); 393 setProperty("window.dividerloc", "" + _splitpane.getDividerLocation()); 394 setProperty("window.structuredividerloc", "" + _structureSplitter.getDividerLocation()); 395 } 396 397 public static ImageIcon getImageIcon(String fileName) 398 { 399 URL url = TemplateEditor.class.getResource("images/" + fileName); 400 if (url != null) return new ImageIcon (url); 401 else return new ImageIcon (""); 402 } 404 405 public static Image getImage(String fileName) 406 { 407 URL url = TemplateEditor.class.getResource("images/" + fileName); 408 if (url != null) return Toolkit.getDefaultToolkit().getImage(url); 409 410 else return Toolkit.getDefaultToolkit().getImage(""); 411 } 412 413 public DjPersistenceManager getManager() 414 { 415 return _mgr; 416 } 417 418 public DjSession getSession() 419 { 420 return _session; 421 } 422 423 void _mnuExit_actionPerformed(ActionEvent e) 424 { 425 try 426 { 427 if (canExit()) exitProgram(); 428 } 429 catch (Exception x) 430 { 431 setStatusMessage(x); 432 } 433 } 434 435 public void closeTab(Component comp) 436 { 437 _tabs.remove(comp); 438 if (_tabs.getComponentCount() == 0) setTabsVisible(false); 439 } 440 441 public void keyTyped(KeyEvent e) 442 { 443 } 444 445 public void keyPressed(KeyEvent e) 446 { 447 if (_tabs.getComponentCount() == 0) return; 448 449 if ((e.getKeyCode() == KeyEvent.VK_F6) && ((e.getModifiers() & InputEvent.SHIFT_MASK) != 0) 450 && ((e.getModifiers() & InputEvent.CTRL_MASK) != 0)) 451 { 452 prevTab(); 453 } 454 else if ((e.getKeyCode() == KeyEvent.VK_F6) && ((e.getModifiers() & InputEvent.CTRL_MASK) != 0)) 455 { 456 nextTab(); 457 } 458 else if ((e.getKeyCode() == KeyEvent.VK_F4) && ((e.getModifiers() & InputEvent.CTRL_MASK) != 0)) 459 { 460 closeTab(_tabs.getSelectedComponent()); 461 } 462 } 463 464 protected void prevTab() 465 { 466 int idx = _tabs.getSelectedIndex(); 467 if (idx == -1) return; 468 if (idx > 0) 469 { 470 idx--; 471 } 472 else 473 { 474 idx = _tabs.getComponentCount() - 1; 475 } 476 _tabs.setSelectedIndex(idx); 477 } 478 479 protected void nextTab() 480 { 481 int idx = _tabs.getSelectedIndex(); 482 if (idx == -1) return; 483 if (idx < _tabs.getComponentCount() - 1) 484 { 485 idx++; 486 } 487 else 488 { 489 idx = 0; 490 } 491 _tabs.setSelectedIndex(idx); 492 } 493 494 public void keyReleased(KeyEvent e) 495 { 496 } 497 498 public void setTabsVisible(boolean visible) 499 { 500 _tabs.setVisible(visible); 501 if (visible) 502 { 503 _desktopPanel.setBackground(new Color (212, 208, 200)); 504 } 505 else 506 { 507 _desktopPanel.setBackground(SystemColor.desktop); 508 } 509 510 } 511 512 void _mnuAbout_actionPerformed(ActionEvent e) 513 { 514 new AboutDialog(this); 515 } 516 517 public void addTab(Component comp, String title, boolean setSelected) 518 { 519 _tabs.add(comp, title); 520 if (setSelected) _tabs.setSelectedComponent(comp); 521 setTabsVisible(true); 522 } 523 524 void _mnuOptions_actionPerformed(ActionEvent e) 525 { 526 OptionsDialog od = new OptionsDialog(this, getProperties()); 527 if (!od.wasCancelled()) rescanTemplateSets(); 528 } 529 530 void _mnuSaveProject_actionPerformed(ActionEvent e) 531 { 532 try 533 { 534 535 } 536 catch (Exception x) 537 { 538 setStatusMessage(x); 539 } 540 } 541 542 void _mnuSaveAs_actionPerformed(ActionEvent e) 543 { 544 try 545 { 546 547 } 548 catch (Exception x) 549 { 550 setStatusMessage(x); 551 } 552 553 } 554 555 protected void rescanTemplateSets() 556 { 557 _templateSetManager.rescanTemplateSets(getProperty(KEY_TEMPLATEPATH, ".")); 558 } 559 560 public JTextArea createTextTab(String title, boolean setSelected) 561 { 562 JScrollPane scr = new JScrollPane (); 563 JTextArea ta = new JTextArea (); 564 ta.setFont(new java.awt.Font ("Monospaced", 0, 11)); 565 ta.setEditable(false); 566 567 scr.getViewport().add(ta); 568 addTab(scr, title, setSelected); 569 return ta; 570 } 571 572 void _mnuNextTab_actionPerformed(ActionEvent e) 573 { 574 nextTab(); 575 } 576 577 void _mnuCloseTab_actionPerformed(ActionEvent e) 578 { 579 closeTab(_tabs.getSelectedComponent()); 580 } 581 582 void _mnuPrevTab_actionPerformed(ActionEvent e) 583 { 584 prevTab(); 585 } 586 587 void _mnuCloseAll_actionPerformed(ActionEvent e) 588 { 589 while (_tabs.getSelectedComponent() != null) 590 closeTab(_tabs.getSelectedComponent()); 591 } 592 593 void _mnuOpenSet_actionPerformed(ActionEvent e) 594 { 595 openTemplateSet(); 596 } 597 598 protected void initFileChooser(JFileChooser fc) 599 { 600 fc.setCurrentDirectory(new File (getProperty("filelocation", System.getProperties().getProperty("user.dir")))); 601 } 602 603 protected void recordFileChooserSettings(JFileChooser fc) 604 { 605 setProperty("filelocation", fc.getCurrentDirectory().getAbsolutePath()); 606 } 607 608 public void openTemplateSet() 609 { 610 try 611 { 612 JFileChooser fc = new JFileChooser (); 613 fc.setDialogTitle("Open Template Set"); 614 DjFileFilter filter = new DjFileFilter(new String []{TemplatePanel.TEMPLATESET_EXTENSION}, "Template Set"); 615 fc.setFileFilter(filter); 616 initFileChooser(fc); 617 618 fc.setFileSelectionMode(JFileChooser.FILES_ONLY); 619 620 if (fc.showDialog(this, "Open Template Set") == JFileChooser.APPROVE_OPTION) 621 { 622 recordFileChooserSettings(fc); 623 TemplateSet ts = new TemplateSet(fc.getSelectedFile().getAbsolutePath()); 624 TemplatePanel tp = new TemplatePanel(this, ts); 625 626 _templateSetTabs.add(tp, ts.getName()); 627 _templateSetTabs.setSelectedComponent(tp); 628 setTemplatesetVisible(true); 629 } 630 } 631 catch (Exception x) 632 { 633 setStatusMessage(x); 634 } 635 } 636 637 protected void setTemplatesetVisible(boolean b) 638 { 639 _splitpane.setVisible(b); 640 invalidate(); 641 validate(); 642 } 643 644 public static void main(String [] args) 645 { 646 try 647 { 648 DjenericTool.setLookAndFeel(); 649 650 if (args.length < 1) 651 { 652 System.out.println("Usage: java " + TemplateEditor.class.getName() + " <repositories.xml>"); 653 return; 654 } 655 String banner = TemplateEditor.class.getName() + " version " + DjVersion.getVersion(); 656 DjLogPrintStream.logAll(banner, LOG_FILE_NAME); 657 658 System.out.println("Reading " + args[0]); 659 new TemplateEditor(args[0]).startApp(); 660 } 661 catch (Exception e) 662 { 663 DjLogger.log(e); 664 } 665 } 666 667 } | Popular Tags |