| 1 17 18 package com.finalist.jaggenerator; 19 20 import com.finalist.jag.JApplicationGen; 21 import com.finalist.jag.uml.Jag2UMLGenerator; 22 import com.finalist.jag.uml.UML2JagGenerator; 23 import com.finalist.jag.util.TemplateString; 24 import com.finalist.jaggenerator.modules.*; 25 import com.finalist.jaggenerator.template.Template; 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 import org.apache.log4j.lf5.LogLevel; 29 import org.w3c.dom.Document ; 30 import org.w3c.dom.Element ; 31 32 import javax.swing.*; 33 import javax.swing.event.TreeSelectionEvent ; 34 import javax.swing.tree.*; 35 import javax.xml.parsers.DocumentBuilder ; 36 import javax.xml.parsers.DocumentBuilderFactory ; 37 import javax.xml.parsers.ParserConfigurationException ; 38 import javax.xml.transform.OutputKeys ; 39 import javax.xml.transform.Transformer ; 40 import javax.xml.transform.TransformerFactory ; 41 import javax.xml.transform.dom.DOMSource ; 42 import javax.xml.transform.stream.StreamResult ; 43 import java.awt.*; 44 import java.awt.event.KeyEvent ; 45 import java.io.File ; 46 import java.io.FileWriter ; 47 import java.io.IOException ; 48 import java.io.StringWriter ; 49 import java.net.URL ; 50 import java.util.*; 51 import java.util.List ; 52 53 59 public class JagGenerator extends JFrame { 60 61 static Log log = LogFactory.getLog(JagGenerator.class); 62 private ConsoleLogger logger; 63 private boolean offlineMode = false; 64 65 private DefaultTreeModel treeModel = null; 66 public Root root = null; 67 private File file = null; 68 private File outputDir = null; 69 private static File applicationFileDir = new File ("."); 70 public static JagGenerator jagGenerator; 71 private static GenericJdbcManager conManager; 72 private static final int SPLIT_PANE_WIDTH = 400; 73 private static boolean relationsEnabled = true; 74 private static Thread runningThread; 75 private static final Icon CANCEL_ICON = new ImageIcon("../images/cancel.png"); 76 private static final Icon RUN_ICON = new ImageIcon("../images/execute.png"); 77 private static final String RUN_ACTION = "run"; 78 private static final String STOP_ACTION = "stop"; 79 private static final HashMap entities = new HashMap(); 80 private static final HashMap entitiesByTableName = new HashMap(); 81 private static final String XMI_SUFFIX = ".xmi"; 82 83 84 private static final HashMap FILECHOOSER_START_DIR = new HashMap(); 85 private static final String FILECHOOSER_UMLEXPORT = "UML export"; 86 private static final String FILECHOOSER_UMLIMPORT = "UML import"; 87 public static final String FILECHOOSER_APPFILE_OPEN = "AppFile open"; 88 private static final String FILECHOOSER_APPFILE_SAVE = "AppFile save"; 89 90 public final static String TEMPLATE_USE_RELATIONS = "useRelations"; 91 public final static String TEMPLATE_USE_MOCK = "useMock"; 92 public final static String TEMPLATE_USE_JAVA5 = "useJava5"; 93 public final static String TEMPLATE_USE_WEB_SERVICE = "useWebService"; 94 public final static String TEMPLATE_USE_SECURITY = "useSecurity"; 95 96 public final static String TEMPLATE_WEB_TIER = "webTier"; 97 public final static String TEMPLATE_WEB_TIER_STRUTS1_2 = "Struts 1.2"; 98 public final static String TEMPLATE_WEB_TIER_TAPESTRY4 = "Tapestry 4"; 99 public final static String TEMPLATE_WEB_TIER_SWING = "Swing"; 100 101 public final static String TEMPLATE_BUSINESS_TIER = "businessTier"; 102 public final static String TEMPLATE_BUSINESS_TIER_EJB2 = "EJB 2.0"; 103 public final static String TEMPLATE_BUSINESS_TIER_EJB3 = "EJB 3.0"; 104 public final static String TEMPLATE_BUSINESS_TIER_HIBERNATE2 = "Hibernate 2"; 105 public final static String TEMPLATE_BUSINESS_TIER_HIBERNATE3 = "Hibernate 3"; 106 public final static String TEMPLATE_BUSINESS_TIER_MOCK = "Mock"; 107 108 public final static String TEMPLATE_SERVICE_TIER = "serviceTier"; 109 public final static String TEMPLATE_SERVICE_TIER_SERVICE_LOCATOR = "ServiceLocator"; 110 public final static String TEMPLATE_SERVICE_TIER_SPRING = "Spring"; 111 112 public final static String TEMPLATE_APPLICATION_SERVER = "appserver"; 113 public final static String TEMPLATE_APPLICATION_SERVER_JBOSS_4_X = "JBoss 4.x"; 114 public final static String TEMPLATE_APPLICATION_SERVER_JBOSS_3_2_2_7 = "JBoss 3.2.2-7"; 115 public final static String TEMPLATE_APPLICATION_SERVER_JBOSS_3_2_0_1 = "JBoss 3.2.0-1"; 116 public final static String TEMPLATE_APPLICATION_SERVER_JBOSS_3_0 = "JBoss 3.0"; 117 public final static String TEMPLATE_APPLICATION_SERVER_TOMCAT_5 = "Tomcat 5"; 118 public final static String TEMPLATE_APPLICATION_SERVER_SUN_ONE_7 = "Sun ONE Application Server 7"; 119 public final static String TEMPLATE_APPLICATION_SERVER_WEBLOGIC_8_1 = "BEA WebLogic 8.1"; 120 public final static String TEMPLATE_APPLICATION_SERVER_WEBLOGIC_EJBGEN_8_1 = "BEA WebLogic 8.1 (Workshop EJBGen)"; 121 public final static String TEMPLATE_APPLICATION_SERVER_IBM_WEBSPERE = "IBM WebSphere"; 122 public final static String TEMPLATE_APPLICATION_SERVER_ORACLE = "Oracle Application Server"; 123 124 125 130 public static void main(String args[]) { 131 log.info("Starting jag..."); 133 jagGenerator = new JagGenerator(); 134 jagGenerator.setVisible(true); 135 } 136 137 138 142 public ConsoleLogger getLogger() { 143 return logger; 144 } 145 146 147 151 public void setLogger(ConsoleLogger logger) { 152 this.logger = logger; 153 } 154 157 public JagGenerator() { 158 Settings.read(); 160 161 try { 163 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 164 } catch (Exception e) { 165 } 166 initComponents(); 168 initCustomComponents(); 170 logger = new ConsoleLogger(console); 171 consoleScrollPane.setViewportView(console); 172 173 logger.log("Java Application Generator - console output:\n"); 174 initDesktop(); 175 fileNameLabel.setToolTipText("No application file selected."); 176 root = new Root(); 177 treeModel = new DefaultTreeModel(root); 178 treeModel.addTreeModelListener(new JagTreeModelListener()); 179 tree.setCellRenderer(new JagTreeCellRenderer()); 180 tree.setModel(treeModel); 181 tree.setSelectionPath(new TreePath(((DefaultMutableTreeNode) root.getFirstChild()).getPath())); 182 183 splitPane.setDividerLocation(Settings.getVerticalDividerPosition()); 185 desktopConsoleSplitPane.setDividerLocation(Settings.getHorizontalDividerPosition()); 186 setBounds(Settings.getUserWindowBounds(this)); 188 if (Settings.isMaximized()) { 190 setExtendedState(MAXIMIZED_BOTH); 191 } 192 } 193 194 195 202 public static GenericJdbcManager getConManager() { 203 if (conManager == null) { 204 JDialog dialog = new ConnectDialog(jagGenerator); 205 dialog.setVisible(true); } 207 if (conManager != null) { 208 jagGenerator.disconnectMenuItem.setEnabled(true); 209 } 210 return conManager; 211 } 212 213 218 public static boolean isDatabaseConnected() { 219 return conManager != null; 220 } 221 222 225 public boolean isOfflineMode() { 226 return offlineMode; 227 } 228 229 232 public void setOfflineMode(boolean offlineMode) { 233 this.offlineMode = offlineMode; 234 } 235 236 242 public static void setForeignKeyInField(String tableName, String fieldName) { 243 TreeModel model = jagGenerator.tree.getModel(); 244 for (int i = 0; i < model.getChildCount(model.getRoot()); i++) { 245 Object kid = model.getChild(model.getRoot(), i); 246 if (kid instanceof Entity) { 247 Entity entity = (Entity) kid; 248 if (entity.getLocalTableName().equals(tableName)) { 249 for (int j = 0; j < entity.getChildCount(); j++) { 250 Object kid2 = entity.getChildAt(j); 251 if (kid2 instanceof Field) { 252 Field field = (Field) kid2; 253 if (field.getName().toString().equals(fieldName)) { 254 field.setForeignKey(true); 255 } 256 } 257 } 258 } 259 } 260 } 261 } 262 263 public static boolean isRelationsEnabled() { 264 return relationsEnabled; 265 } 266 267 public static void logToConsole(Object o) { 268 logToConsole(o, LogLevel.INFO); 269 } 270 271 public static void stateChanged(boolean updateTree) { 272 setFileNeedsSavingIndicator(true); 273 if (updateTree) { 274 jagGenerator.tree.updateUI(); 275 } 276 } 277 278 public static void finishedGeneration() { 279 jagGenerator.executeButton.setIcon(RUN_ICON); 280 jagGenerator.executeButton.setActionCommand(RUN_ACTION); 281 } 282 283 public static Template getTemplate() { 284 Enumeration children = jagGenerator.root.children(); 285 while (children.hasMoreElements()) { 286 DefaultMutableTreeNode child = (DefaultMutableTreeNode) children.nextElement(); 287 if (child instanceof Config) { 288 return ((Config) child).getTemplate(); 289 } 290 } 291 return null; 292 } 293 294 public static List getObjectsFromTree(Class clazz) { 295 ArrayList list = new ArrayList(); 296 Enumeration children = jagGenerator.root.children(); 297 while (children.hasMoreElements()) { 298 DefaultMutableTreeNode child = (DefaultMutableTreeNode) children.nextElement(); 299 if (child != null && child.getClass().equals(clazz)) { 300 list.add(child); 301 } 302 } 303 return list; 304 } 305 306 public static void addEntity(String refName, Entity entity) { 307 entities.put(refName, entity); 308 entitiesByTableName.put(entity.getTableName(), entity); 309 } 310 311 public static Entity getEntityByRefName(String refName) { 312 return (Entity) entities.get(refName); 313 } 314 315 public static Entity getEntityByTableName(String tableName) { 316 return (Entity) entitiesByTableName.get(tableName); 317 } 318 319 326 public static void entityHasupdatedTableName(String entityName, String newTableName) { 327 synchronized (entitiesByTableName) { 328 Iterator i = entitiesByTableName.entrySet().iterator(); 329 while (i.hasNext()) { 330 Map.Entry entry = (Map.Entry) i.next(); 331 Entity entity = (Entity) entry.getValue(); 332 if (entity.getName().equals(entityName)) { 333 entitiesByTableName.remove(entry.getKey()); 334 entitiesByTableName.put(newTableName, entity); 335 } 336 } 337 } 338 } 339 340 343 public static void normaliseSQLTypesWithChosenDatabase() { 344 346 } 356 357 363 public static File getFileChooserStartDir(String filechooserKey) { 364 File dir = (File ) FILECHOOSER_START_DIR.get(filechooserKey); 365 return dir == null ? applicationFileDir : dir; 366 } 367 368 374 public static void setFileChooserStartDir(String filechooserKey, File dir) { 375 FILECHOOSER_START_DIR.put(filechooserKey, dir); 376 } 377 378 private static void saveGuiSettings() { 379 Settings.setVerticalDividerPosition(jagGenerator.splitPane.getDividerLocation()); 381 Settings.setHorizontalDividerPosition(jagGenerator.desktopConsoleSplitPane.getDividerLocation()); 382 int extendedState = jagGenerator.getExtendedState(); 384 boolean isMaximized = ((extendedState & MAXIMIZED_BOTH) == MAXIMIZED_BOTH); 385 if (!isMaximized) { 386 Settings.setUserWindowBounds(jagGenerator.getBounds()); 388 } 389 Settings.setMaximized(isMaximized); 390 } 391 392 397 public static void kickTheBucket(String error) { 398 if (error == null) { 399 saveGuiSettings(); 401 Settings.write(); 402 ConfigManager.getInstance().save(); 404 System.exit(0); 407 } else { 408 JOptionPane.showMessageDialog(jagGenerator, 410 error, "JAG - Fatal error!", JOptionPane.ERROR_MESSAGE); 411 System.exit(1); 412 } 413 } 414 415 420 public void setConManager(GenericJdbcManager conManager) { 421 JagGenerator.conManager = conManager; 422 } 423 424 425 private void initDesktop() { 426 desktopPane.setLayout(new GridBagLayout()); 427 this.setTitle("Java Application Generator - Finalist IT Group"); 428 GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints (); 429 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.RELATIVE; 430 gridBagConstraints.gridheight = java.awt.GridBagConstraints.RELATIVE; 431 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 432 gridBagConstraints.ipadx = 1; 433 gridBagConstraints.ipady = 1; 434 gridBagConstraints.weightx = 1.0; 435 gridBagConstraints.weighty = 1.0; 436 desktopPane.add(splitPane, gridBagConstraints); 437 pack(); 438 } 439 440 private void newRelationMenuItemActionPerformed() { 441 DefaultMutableTreeNode selected = (DefaultMutableTreeNode) 442 tree.getLastSelectedPathComponent(); 443 if (!(selected instanceof Entity)) { 444 TreePath selectedPath = tree.getSelectionPath(); 446 selected = (DefaultMutableTreeNode) selectedPath.getParentPath().getLastPathComponent(); 447 if (!(selected instanceof Entity)) { 448 JOptionPane.showMessageDialog(this, 449 "A relation can only be added to an entity bean. Please first select an entity in the application tree.", "Can't add relation!", JOptionPane.ERROR_MESSAGE); 450 return; 451 } 452 } 453 Entity selectedEntity = (Entity) selected; 454 455 DefaultMutableTreeNode newNode = new Relation(selectedEntity); 456 selectedEntity.addRelation((Relation) newNode); 457 tree.setSelectionPath(new TreePath(newNode.getPath())); 458 tree.updateUI(); 459 } 460 461 462 467 private void updateLocalSideRelations() { 468 Iterator entities = root.getEntityEjbs().iterator(); 469 while (entities.hasNext()) { 470 Entity entity = (Entity) entities.next(); 471 for (int i = 0; i < entity.getChildCount(); i++) { 473 TreeNode child = entity.getChildAt(i); 474 if (child instanceof Relation) { 476 Relation relation = (Relation) child; 477 String fkFieldName = relation.getFieldName().toString(); 478 for (int j = 0; j < entity.getChildCount(); j++) { 479 TreeNode child2 = entity.getChildAt(j); 480 if (child2 instanceof Field) { 482 Field field = (Field) child2; 483 if (field.getName().equals(fkFieldName)) { 485 field.setRelation(relation); 486 field.setForeignKey(true); 487 relation.setFieldName(field.getName().toString()); 488 relation.setFkField(field); 489 if (relation.getLocalColumn() == null) { 490 relation.setLocalColumn(relation.getFkField().getColumnName()); 491 } 492 logToConsole("relation " + relation + ": local-side fk field is " + entity.getName() + ":" + field); 493 } 494 } 495 } 496 } 497 } 498 } 499 } 500 501 506 private void updateForeignSideRelations() { 507 Iterator entities = root.getEntityEjbs().iterator(); 508 while (entities.hasNext()) { 509 Entity entity = (Entity) entities.next(); 510 for (int i = 0; i < entity.getChildCount(); i++) { 511 TreeNode child = entity.getChildAt(i); 512 if (child instanceof Relation) { 513 Relation relation = (Relation) child; 514 Entity relatedEntity = relation.getRelatedEntity(); 515 String column = relation.getForeignColumn(); 516 for (int j = 0; j < relatedEntity.getChildCount(); j++) { 517 TreeNode child2 = relatedEntity.getChildAt(j); 518 if (child2 instanceof Field) { 519 Field field = (Field) child2; 520 if (field.getColumnName().equals(column)) { 521 relation.setForeignPkField(field); 522 logToConsole("relation " + relation + ": foreign-side pk is " + relatedEntity + ":" + field); 523 } 524 } 525 } 526 } 527 } 528 } 529 } 530 531 532 538 private boolean addRelatedEntitiesToSessionBeans() { 539 boolean somethingAdded = false; 540 HashMap relatedTablesPerEB = new HashMap(); 544 HashMap entityPerTable = new HashMap(); 545 HashMap tablePerEntity = new HashMap(); 546 Iterator entities = root.getEntityEjbs().iterator(); 547 while (entities.hasNext()) { 548 Entity entity = (Entity) entities.next(); 549 entityPerTable.put(entity.getLocalTableName().toString(), entity.getRefName()); 550 tablePerEntity.put(entity.getRefName(), entity.getLocalTableName().toString()); 551 for (int i = 0; i < entity.getChildCount(); i++) { 552 TreeNode child = entity.getChildAt(i); 553 if (child instanceof Relation) { 554 Relation relation = (Relation) child; 555 String relatedTableName = relation.getForeignTable(); 556 Set existing = (Set) relatedTablesPerEB.get(entity.getRefName()); 557 if (existing == null) { 558 existing = new HashSet(); 559 } 560 existing.add(relatedTableName); 561 relatedTablesPerEB.put(entity.getRefName(), existing); 562 563 } 564 } 565 } 566 567 Iterator sessions = root.getSessionEjbs().iterator(); 570 while (sessions.hasNext()) { 571 Session session = (Session) sessions.next(); 572 Iterator entitiesWithinSession = session.getEntityRefs().iterator(); 573 while (entitiesWithinSession.hasNext()) { 574 String localTable = (String ) tablePerEntity.get(entitiesWithinSession.next()); 575 Iterator relatedEntitySets = relatedTablesPerEB.values().iterator(); 576 while (relatedEntitySets.hasNext()) { 577 Set set = (Set) relatedEntitySets.next(); 578 set.remove(localTable); 579 } 580 } 581 } 582 583 HashSet addedTables = new HashSet(); 585 sessions = root.getSessionEjbs().iterator(); 586 while (sessions.hasNext()) { 587 Session session = (Session) sessions.next(); 588 Iterator entitiesWithinSession = session.getEntityRefs().iterator(); 589 while (entitiesWithinSession.hasNext()) { 590 String entityName = (String ) entitiesWithinSession.next(); 591 Set tablesToBeAdded = (Set) relatedTablesPerEB.get(entityName); 592 if (tablesToBeAdded != null) { 593 Iterator i = tablesToBeAdded.iterator(); 594 while (i.hasNext()) { 595 String table = (String ) i.next(); 596 if (!addedTables.contains(table)) { 597 String entity = (String ) entityPerTable.get(table); 598 if (entity == null) { 599 JOptionPane.showMessageDialog(this, 600 "Entity '" + entityName + "' contains a relation to a table '" + table + "'\n" + 601 "for which no entity bean exists in the current application.\n" + 602 "Please either create a new entity bean for this table, or delete the relation.", 603 "Invalid Container-managed relation!", 604 JOptionPane.ERROR_MESSAGE); 605 return false; 606 } else { 607 session.addRelationRef(entity); 608 addedTables.add(table); 609 somethingAdded = true; 610 JOptionPane.showMessageDialog(this, 611 "Entity '" + entityName + "' added to the service bean '" + session.getRefName() + 612 "' contains a relation to the entity '" + entity + "', which doesn't appear in any service beans.\n" + 613 "The relation requires accessor methods to '" + entity + "', so these were automatically added to the service bean.", 614 "Service bean modified", 615 JOptionPane.INFORMATION_MESSAGE); 616 } 617 } 618 } 619 } 620 } 621 } 622 623 if (somethingAdded) { 626 addRelatedEntitiesToSessionBeans(); 627 } 628 return true; 629 } 630 631 632 private File selectJagOutDirectory(String startDir) { 633 File directory = null; 634 int fileChooserStatus; 635 JFileChooser fileChooser = new JFileChooser(); 636 fileChooser.setDialogTitle("Select an ouput directory for the generated application.."); 637 fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 638 fileChooser.setCurrentDirectory(new File (startDir)); 639 fileChooserStatus = fileChooser.showOpenDialog(this); 640 if (fileChooserStatus == JFileChooser.APPROVE_OPTION) { 641 directory = fileChooser.getSelectedFile(); 642 String projectName = root.app.nameText.getText(); 643 directory = new File (directory.getAbsoluteFile(), projectName); 644 } 645 return directory; 646 647 } 648 649 654 private boolean generateRelationsFromDB(Entity entity) { 655 if (getConManager() == null) { 656 logger.log("Can't regenerate relations - no database!"); 657 return false; 658 } 659 log.debug("Get the foreign keys for table: " + entity.getTableName()); 660 List fkeys = DatabaseUtils.getForeignKeys(entity.getTableName()); 661 HashMap foreignTablesTally = new HashMap(); 662 Set foreignKeyFieldNames = new HashSet(fkeys.size()); 663 Iterator i = fkeys.iterator(); 664 while (i.hasNext()) { 665 ForeignKey fk = (ForeignKey) i.next(); 666 foreignKeyFieldNames.add(Utils.format(fk.getFkColumnName())); 667 Relation relation = new Relation(entity, fk); 668 String foreignTable = fk.getPkTableName(); 669 670 relation.setBidirectional(false); 672 relation.setTargetMultiple(true); 674 683 684 if (foreignTablesTally.keySet().contains(foreignTable)) { 685 int tally = ((Integer ) foreignTablesTally.get(foreignTable)).intValue() + 1; 686 relation.setName(relation.getName() + tally); 687 foreignTablesTally.put(foreignTable, new Integer (tally)); 688 } else { 689 foreignTablesTally.put(foreignTable, new Integer (1)); 690 } 691 addObject(entity, relation, false, true); 692 } 693 return true; 694 } 695 696 private ArrayList sortColumns(ArrayList columns, ArrayList pKeys, Entity entity, String pKey) { 697 ArrayList sortedColumns = new ArrayList(); 698 ArrayList primaryKeyColumns = new ArrayList(); 700 Column primaryKeyColumn = null; 701 for (Iterator colIt = columns.iterator(); colIt.hasNext();) { 702 Column column = (Column) colIt.next(); 703 if (pKeys.contains(column.getName())) { 704 primaryKeyColumn = column; 706 primaryKeyColumn.setPrimaryKey(true); 707 primaryKeyColumns.add(primaryKeyColumn); 708 } else { 709 column.setPrimaryKey(false); 710 sortedColumns.add(column); 711 } 712 } 713 if (pKeys.size() > 1) { 714 entity.isCompositeCombo.setSelectedItem("true"); 716 String compositePK = entity.rootPackageText.getText() + "." + entity.nameText.getText() + "PK"; 717 entity.pKeyTypeText.setText(compositePK); 718 entity.pKeyText.setText(""); } else { 720 entity.isCompositeCombo.setSelectedItem("false"); 721 if (pKeys.size() == 1) { 722 entity.pKeyText.setText(Utils.format(pKey)); 724 } 725 } 726 columns = new ArrayList(); 728 if (primaryKeyColumn != null) 729 columns.addAll(primaryKeyColumns); 730 columns.addAll(sortedColumns); 731 return columns; 732 } 733 734 private void addObject(DefaultMutableTreeNode parent, DefaultMutableTreeNode child, boolean forceUpdate, boolean topOfQueue) { 735 if (parent == null) { 736 parent = root; 737 } 738 treeModel.insertNodeInto(child, parent, topOfQueue ? 0 : parent.getChildCount()); 739 if (forceUpdate) { 740 tree.setSelectionPath(new TreePath(child.getPath())); 741 } 742 } 743 744 public boolean save() { 745 updateLocalSideRelations(); 747 updateForeignSideRelations(); 748 749 if (!addRelatedEntitiesToSessionBeans()) { 750 return false; 751 } 752 753 try { 754 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 755 DocumentBuilder builder = dbf.newDocumentBuilder(); 756 Document doc = builder.newDocument(); 757 Element skelet = doc.createElement("skelet"); 758 doc.appendChild(skelet); 759 root.getXML(skelet); 760 String XMLDoc = outXML(doc); 761 String fileName = file.getName(); 762 if (fileName != null) { 763 if (fileName.indexOf(".xml") == -1) { 764 file = new File (file.getAbsolutePath() + ".xml"); 765 } 766 } 767 FileWriter fw = new FileWriter (file); 768 fw.write(XMLDoc); 769 fw.close(); 770 fileNameLabel.setText("Application file: " + file.getName()); 771 fileNameLabel.setToolTipText(file.getAbsolutePath()); 772 773 } catch (ParserConfigurationException e) { 774 e.printStackTrace(); 775 } catch (IOException e) { 776 e.printStackTrace(); 777 } 778 779 setFileNeedsSavingIndicator(false); 780 return true; 781 } 782 783 784 public static String outXML(Document doc) { 785 try { 786 DOMSource domSource = new DOMSource (doc); 787 StringWriter sw = new StringWriter (); 788 StreamResult streamResult = new StreamResult (sw); 789 TransformerFactory tf = TransformerFactory.newInstance(); 790 Transformer serializer = tf.newTransformer(); 791 serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); 792 serializer.setOutputProperty(OutputKeys.INDENT, "yes"); 793 serializer.transform(domSource, streamResult); 794 return sw.toString(); 795 } catch (Exception e) { 796 e.printStackTrace(); 797 } 798 return null; 799 } 800 801 private static void setFileNeedsSavingIndicator(boolean sterretje) { 802 if (jagGenerator != null && jagGenerator.file != null) { 803 String filename = jagGenerator.fileNameLabel.getText(); 804 if (sterretje && filename.charAt(filename.length() - 1) != '*') { 805 jagGenerator.fileNameLabel.setText(filename + '*'); 806 } else if (!sterretje && filename.charAt(filename.length() - 1) == '*') { 807 jagGenerator.fileNameLabel.setText(filename.substring(0, filename.length() - 1)); 808 } 809 } 810 } 811 812 private void initCustomComponents() { 813 recentMenu = new com.finalist.jaggenerator.menu.RecentMenu(); 816 recentMenu.setMnemonic(KeyEvent.VK_R); 817 recentMenu.setText("Recent..."); 818 recentMenu.setMainApp(this); 819 fileMenu.insert(recentMenu, 2); 820 } 821 822 823 824 830 831 837 private void initComponents() { 839 splitPane = new javax.swing.JSplitPane (); 840 treeScrollPane = new javax.swing.JScrollPane (); 841 tree = new javax.swing.JTree (); 842 toolBar = new javax.swing.JToolBar (); 843 newButton = new javax.swing.JButton (); 844 openButton = new javax.swing.JButton (); 845 saveButton = new javax.swing.JButton (); 846 entityButton = new javax.swing.JButton (); 847 sessionButton = new javax.swing.JButton (); 848 relationButton = new javax.swing.JButton (); 849 businessMethodButton = new javax.swing.JButton (); 850 fieldButton = new javax.swing.JButton (); 851 deleteButton = new javax.swing.JButton (); 852 executeButton = new javax.swing.JButton (); 853 helpButton = new javax.swing.JButton (); 854 spacer = new javax.swing.JPanel (); 855 applicationFileInfoPanel = new javax.swing.JPanel (); 856 fileNameLabel = new javax.swing.JLabel (); 857 databaseConnectionInfoPanel = new javax.swing.JPanel (); 858 databaseConnectionLabel = new javax.swing.JLabel (); 859 desktopConsoleSplitPane = new javax.swing.JSplitPane (); 860 desktopPane = new javax.swing.JDesktopPane (); 861 consoleScrollPane = new javax.swing.JScrollPane (); 862 console = new javax.swing.JTextPane (); 863 textConsole = new javax.swing.JTextArea (); 864 menuBar = new javax.swing.JMenuBar (); 865 fileMenu = new javax.swing.JMenu (); 866 newMenuItem = new javax.swing.JMenuItem (); 867 openMenuItem = new javax.swing.JMenuItem (); 868 importMenuItem = new javax.swing.JMenuItem (); 869 jSeparator1 = new javax.swing.JSeparator (); 870 saveMenuItem = new javax.swing.JMenuItem (); 871 saveAsMenuItem = new javax.swing.JMenuItem (); 872 exportMenuItem = new javax.swing.JMenuItem (); 873 jSeparator2 = new javax.swing.JSeparator (); 874 generateJavaApplicationAsMenuItem = new javax.swing.JMenuItem (); 875 jSeparator3 = new javax.swing.JSeparator (); 876 exitMenuItem = new javax.swing.JMenuItem (); 877 editMenu = new javax.swing.JMenu (); 878 addSubMenu = new javax.swing.JMenu (); 879 addEntityMenuItem = new javax.swing.JMenuItem (); 880 addSessionMenuItem = new javax.swing.JMenuItem (); 881 addRelationMenuItem = new javax.swing.JMenuItem (); 882 addBusinessMenuItem = new javax.swing.JMenuItem (); 883 addFieldMenuItem = new javax.swing.JMenuItem (); 884 deleteMenuItem = new javax.swing.JMenuItem (); 885 connectionMenu = new javax.swing.JMenu (); 886 driverManagerMenuItem = new javax.swing.JMenuItem (); 887 jSeparator4 = new javax.swing.JSeparator (); 888 connectMenuItem = new javax.swing.JMenuItem (); 889 disconnectMenuItem = new javax.swing.JMenuItem (); 890 helpMenu = new javax.swing.JMenu (); 891 aboutMenuItem = new javax.swing.JMenuItem (); 892 contentMenuItem = new javax.swing.JMenuItem (); 893 894 splitPane.setBorder(null); 895 splitPane.setDividerLocation(400); 896 splitPane.setDividerSize(3); 897 splitPane.setContinuousLayout(true); 898 splitPane.setOpaque(false); 899 splitPane.setVerifyInputWhenFocusTarget(false); 900 tree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener () { 901 public void valueChanged(javax.swing.event.TreeSelectionEvent evt) { 902 treeValueChanged(evt); 903 } 904 }); 905 906 treeScrollPane.setViewportView(tree); 907 908 splitPane.setLeftComponent(treeScrollPane); 909 910 addWindowListener(new java.awt.event.WindowAdapter () { 911 public void windowClosing(java.awt.event.WindowEvent evt) { 912 exitForm(evt); 913 } 914 }); 915 916 toolBar.setBorder(javax.swing.BorderFactory.createEtchedBorder()); 917 toolBar.setFloatable(false); 918 toolBar.setName("toolBar"); 919 newButton.setIcon(new javax.swing.ImageIcon ("../images/new.png")); 920 newButton.setText(" "); 921 newButton.setToolTipText("New JAG application"); 922 newButton.setBorder(null); 923 newButton.addActionListener(new java.awt.event.ActionListener () { 924 public void actionPerformed(java.awt.event.ActionEvent evt) { 925 newMenuItemActionPerformed(evt); 926 } 927 }); 928 929 toolBar.add(newButton); 930 931 openButton.setIcon(new javax.swing.ImageIcon ("../images/open.png")); 932 openButton.setText(" "); 933 openButton.setToolTipText("Open a JAG application file"); 934 openButton.setBorder(null); 935 openButton.addActionListener(new java.awt.event.ActionListener () { 936 public void actionPerformed(java.awt.event.ActionEvent evt) { 937 openMenuItemActionPerformed(evt); 938 } 939 }); 940 941 toolBar.add(openButton); 942 943 saveButton.setIcon(new javax.swing.ImageIcon ("../images/save.png")); 944 saveButton.setText(" "); 945 saveButton.setToolTipText("Save"); 946 saveButton.setBorder(null); 947 saveButton.addActionListener(new java.awt.event.ActionListener () { 948 public void actionPerformed(java.awt.event.ActionEvent evt) { 949 saveButtonActionPerformed(evt); 950 } 951 }); 952 953 toolBar.add(saveButton); 954 955 toolBar.addSeparator(); 956 entityButton.setIcon(new javax.swing.ImageIcon ("../images/entity.png")); 957 entityButton.setText(" "); 958 entityButton.setToolTipText("New entity bean"); 959 entityButton.setBorder(null); 960 entityButton.addActionListener(new java.awt.event.ActionListener () { 961 public void actionPerformed(java.awt.event.ActionEvent evt) { 962 addEntityMenuItemActionPerformed(evt); 963 } 964 }); 965 966 toolBar.add(entityButton); 967 968 sessionButton.setIcon(new javax.swing.ImageIcon ("../images/session.png")); 969 sessionButton.setText(" "); 970 sessionButton.setToolTipText("New service bean"); 971 sessionButton.setBorder(null); 972 sessionButton.addActionListener(new java.awt.event.ActionListener () { 973 public void actionPerformed(java.awt.event.ActionEvent evt) { 974 addSessionMenuItemActionPerformed(evt); 975 } 976 }); 977 978 toolBar.add(sessionButton); 979 980 relationButton.setIcon(new javax.swing.ImageIcon ("../images/relation.png")); 981 relationButton.setText(" "); 982 relationButton.setToolTipText("New relation"); 983 relationButton.setBorder(null); 984 relationButton.addActionListener(new java.awt.event.ActionListener () { 985 public void actionPerformed(java.awt.event.ActionEvent evt) { 986 addRelationPopupMenuItemActionPerformed(evt); 987 } 988 }); 989 990 toolBar.add(relationButton); 991 992 businessMethodButton.setIcon(new javax.swing.ImageIcon ("../images/business.png")); 993 businessMethodButton.setText(" "); 994 businessMethodButton.setToolTipText("New business method"); 995 businessMethodButton.setBorder(null); 996 businessMethodButton.addActionListener(new java.awt.event.ActionListener () { 997 public void actionPerformed(java.awt.event.ActionEvent evt) { 998 businessMethodButtonActionPerformed(evt); 999 } 1000 }); 1001 1002 toolBar.add(businessMethodButton); 1003 1004 fieldButton.setIcon(new javax.swing.ImageIcon ("../images/field.png")); 1005 fieldButton.setText(" "); 1006 fieldButton.setToolTipText("New entity field"); 1007 fieldButton.setBorder(null); 1008 fieldButton.addActionListener(new java.awt.event.ActionListener () { 1009 public void actionPerformed(java.awt.event.ActionEvent evt) { 1010 fieldButtonActionPerformed(evt); 1011 } 1012 }); 1013 1014 toolBar.add(fieldButton); 1015 1016 deleteButton.setIcon(new javax.swing.ImageIcon ("../images/delete.png")); 1017 deleteButton.setText(" "); 1018 deleteButton.setToolTipText("Delete"); 1019 deleteButton.setBorder(null); 1020 deleteButton.addActionListener(new java.awt.event.ActionListener () { 1021 public void actionPerformed(java.awt.event.ActionEvent evt) { 1022 deleteMenuItemActionPerformed(evt); 1023 } 1024 }); 1025 1026 toolBar.add(deleteButton); 1027 1028 toolBar.addSeparator(); 1029 executeButton.setIcon(new javax.swing.ImageIcon ("../images/execute.png")); 1030 executeButton.setText(" "); 1031 executeButton.setToolTipText("Generate the application"); 1032 executeButton.setBorder(null); 1033 executeButton.addActionListener(new java.awt.event.ActionListener () { 1034 public void actionPerformed(java.awt.event.ActionEvent evt) { 1035 generateJavaApplicationAsMenuItemActionPerformed(evt); 1036 } 1037 }); 1038 1039 toolBar.add(executeButton); 1040 1041 toolBar.addSeparator(); 1042 helpButton.setIcon(new javax.swing.ImageIcon ("../images/help.png")); 1043 helpButton.setText(" "); 1044 helpButton.setToolTipText("Help"); 1045 helpButton.setBorder(null); 1046 helpButton.addActionListener(new java.awt.event.ActionListener () { 1047 public void actionPerformed(java.awt.event.ActionEvent evt) { 1048 contentMenuItemActionPerformed(evt); 1049 } 1050 }); 1051 1052 toolBar.add(helpButton); 1053 1054 spacer.setLayout(null); 1055 1056 toolBar.add(spacer); 1057 1058 applicationFileInfoPanel.setName("fileStatusComponent"); 1059 fileNameLabel.setText("Application file:"); 1060 fileNameLabel.setToolTipText("File name of the XML skelet"); 1061 fileNameLabel.setName("fileNameLabel"); 1062 applicationFileInfoPanel.add(fileNameLabel); 1063 1064 toolBar.add(applicationFileInfoPanel); 1065 1066 databaseConnectionInfoPanel.setName("databaseConnectionComponent"); 1067 databaseConnectionLabel.setText("Database Connection:"); 1068 databaseConnectionLabel.setName("databaseConnectionLabel"); 1069 databaseConnectionInfoPanel.add(databaseConnectionLabel); 1070 1071 toolBar.add(databaseConnectionInfoPanel); 1072 1073 getContentPane().add(toolBar, java.awt.BorderLayout.NORTH); 1074 1075 desktopConsoleSplitPane.setDividerLocation(590); 1076 desktopConsoleSplitPane.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT); 1077 desktopConsoleSplitPane.setTopComponent(desktopPane); 1078 1079 console.setBackground(new java.awt.Color (204, 204, 204)); 1080 console.setEditable(false); 1081 console.setFont(new java.awt.Font ("Lucida Console", 0, 10)); 1082 console.setForeground(new java.awt.Color (0, 0, 1)); 1083 consoleScrollPane.setViewportView(console); 1084 1085 textConsole.setColumns(20); 1086 textConsole.setRows(5); 1087 consoleScrollPane.setViewportView(textConsole); 1088 1089 desktopConsoleSplitPane.setBottomComponent(consoleScrollPane); 1090 1091 getContentPane().add(desktopConsoleSplitPane, java.awt.BorderLayout.CENTER); 1092 1093 fileMenu.setMnemonic(KeyEvent.VK_F); 1094 fileMenu.setText("File"); 1095 newMenuItem.setMnemonic(KeyEvent.VK_N); 1096 newMenuItem.setAccelerator( 1097 KeyStroke.getKeyStroke(KeyEvent.VK_N, KeyEvent.CTRL_MASK)); 1098 newMenuItem.setText("New"); 1099 newMenuItem.addActionListener(new java.awt.event.ActionListener () { 1100 public void actionPerformed(java.awt.event.ActionEvent evt) { 1101 newMenuItemActionPerformed(evt); 1102 } 1103 }); 1104 1105 fileMenu.add(newMenuItem); 1106 1107 openMenuItem.setMnemonic(KeyEvent.VK_O); 1108 openMenuItem.setAccelerator( 1109 KeyStroke.getKeyStroke(KeyEvent.VK_O, KeyEvent.CTRL_MASK)); 1110 openMenuItem.setText("Open"); 1111 openMenuItem.addActionListener(new java.awt.event.ActionListener () { 1112 public void actionPerformed(java.awt.event.ActionEvent evt) { 1113 openMenuItemActionPerformed(evt); 1114 } 1115 }); 1116 1117 fileMenu.add(openMenuItem); 1118 1119 importMenuItem.setMnemonic(KeyEvent.VK_I); 1120 importMenuItem.setAccelerator( 1121 KeyStroke.getKeyStroke(KeyEvent.VK_I, KeyEvent.CTRL_MASK)); 1122 importMenuItem.setText("Import UML model.."); 1123 importMenuItem.addActionListener(new java.awt.event.ActionListener () { 1124 public void actionPerformed(java.awt.event.ActionEvent evt) { 1125 importMenuItemActionPerformed(evt); 1126 } 1127 }); 1128 1129 fileMenu.add(importMenuItem); 1130 1131 fileMenu.add(jSeparator1); 1132 1133 saveMenuItem.setMnemonic(KeyEvent.VK_S); 1134 saveMenuItem.setAccelerator( 1135 KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_MASK)); 1136 saveMenuItem.setText("Save"); 1137 saveMenuItem.addActionListener(new java.awt.event.ActionListener () { 1138 public void actionPerformed(java.awt.event.ActionEvent evt) { 1139 saveMenuItemActionPerformed(evt); 1140 } 1141 }); 1142 1143 fileMenu.add(saveMenuItem); 1144 1145 saveAsMenuItem.setMnemonic(KeyEvent.VK_A); 1146 saveAsMenuItem.setAccelerator( 1147 KeyStroke.getKeyStroke(KeyEvent.VK_F12, 0)); 1148 saveAsMenuItem.setText("Save As ..."); 1149 saveAsMenuItem.addActionListener(new java.awt.event.ActionListener () { 1150 public void actionPerformed(java.awt.event.ActionEvent evt) { 1151 saveAsMenuItemActionPerformed(evt); 1152 } 1153 }); 1154 1155 fileMenu.add(saveAsMenuItem); 1156 1157 exportMenuItem.setMnemonic(KeyEvent.VK_E); 1158 exportMenuItem.setAccelerator( 1159 KeyStroke.getKeyStroke(KeyEvent.VK_E, KeyEvent.CTRL_MASK)); 1160 exportMenuItem.setText("Export UML model.."); 1161 exportMenuItem.addActionListener(new java.awt.event.ActionListener () { 1162 public void actionPerformed(java.awt.event.ActionEvent evt) { 1163 exportMenuItemActionPerformed(evt); 1164 } 1165 }); 1166 1167 fileMenu.add(exportMenuItem); 1168 1169 fileMenu.add(jSeparator2); 1170 1171 generateJavaApplicationAsMenuItem.setMnemonic(KeyEvent.VK_G); 1172 generateJavaApplicationAsMenuItem.setAccelerator( 1173 KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0)); 1174 generateJavaApplicationAsMenuItem.setText("Generate application..."); 1175 generateJavaApplicationAsMenuItem.setToolTipText("Generate a J2EE Applicatin"); 1176 generateJavaApplicationAsMenuItem.addActionListener(new java.awt.event.ActionListener () { 1177 public void actionPerformed(java.awt.event.ActionEvent evt) { 1178 generateJavaApplicationAsMenuItemActionPerformed(evt); 1179 } 1180 }); 1181 1182 fileMenu.add(generateJavaApplicationAsMenuItem); 1183 1184 fileMenu.add(jSeparator3); 1185 1186 exitMenuItem.setMnemonic(KeyEvent.VK_X); 1187 exitMenuItem.setText("Exit"); 1188 exitMenuItem.addActionListener(new java.awt.event.ActionListener () { 1189 public void actionPerformed(java.awt.event.ActionEvent evt) { 1190 exitMenuItemActionPerformed(evt); 1191 } 1192 }); 1193 1194 fileMenu.add(exitMenuItem); 1195 1196 menuBar.add(fileMenu); 1197 1198 editMenu.setMnemonic(KeyEvent.VK_E); 1199 editMenu.setText("Edit"); 1200 editMenu.addActionListener(new java.awt.event.ActionListener () { 1201 public void actionPerformed(java.awt.event.ActionEvent evt) { 1202 editMenuActionPerformed(evt); 1203 } 1204 }); 1205 1206 addSubMenu.setText("Add"); 1207 addEntityMenuItem.setMnemonic(KeyEvent.VK_1); 1208 addEntityMenuItem.setAccelerator( 1209 KeyStroke.getKeyStroke(KeyEvent.VK_1, KeyEvent.CTRL_MASK)); 1210 addEntityMenuItem.setText("entity bean"); 1211 addEntityMenuItem.addActionListener(new java.awt.event.ActionListener () { 1212 public void actionPerformed(java.awt.event.ActionEvent evt) { 1213 addEntityMenuItemActionPerformed(evt); 1214 } 1215 }); 1216 1217 addSubMenu.add(addEntityMenuItem); 1218 1219 addSessionMenuItem.setMnemonic(KeyEvent.VK_2); 1220 addSessionMenuItem.setAccelerator( 1221 KeyStroke.getKeyStroke(KeyEvent.VK_2, KeyEvent.CTRL_MASK)); 1222 addSessionMenuItem.setText("service bean"); 1223 addSessionMenuItem.addActionListener(new java.awt.event.ActionListener () { 1224 public void actionPerformed(java.awt.event.ActionEvent evt) { 1225 addSessionMenuItemActionPerformed(evt); 1226 } 1227 }); 1228 1229 addSubMenu.add(addSessionMenuItem); 1230 1231 addRelationMenuItem.setMnemonic(KeyEvent.VK_3); 1232 addRelationMenuItem.setAccelerator( 1233 KeyStroke.getKeyStroke(KeyEvent.VK_3, KeyEvent.CTRL_MASK)); 1234 addRelationMenuItem.setText("relation"); 1235 addRelationMenuItem.addActionListener(new java.awt.event.ActionListener () { 1236 public void actionPerformed(java.awt.event.ActionEvent evt) { 1237 addRelationPopupMenuItemActionPerformed(evt); 1238 } 1239 }); 1240 1241 addSubMenu.add(addRelationMenuItem); 1242 1243 addBusinessMenuItem.setMnemonic(KeyEvent.VK_4); 1244 addBusinessMenuItem.setAccelerator( 1245 KeyStroke.getKeyStroke(KeyEvent.VK_4, KeyEvent.CTRL_MASK)); 1246 addBusinessMenuItem.setText("business"); 1247 addBusinessMenuItem.addActionListener(new java.awt.event.ActionListener () { 1248 public void actionPerformed(java.awt.event.ActionEvent evt) { 1249 addBusinessMenuItemActionPerformed(evt); 1250 } 1251 }); 1252 1253 addSubMenu.add(addBusinessMenuItem); 1254 1255 addFieldMenuItem.setMnemonic(KeyEvent.VK_5); 1256 addFieldMenuItem.setAccelerator( 1257 KeyStroke.getKeyStroke(KeyEvent.VK_5, KeyEvent.CTRL_MASK)); 1258 addFieldMenuItem.setText("field"); 1259 addFieldMenuItem.addActionListener(new java.awt.event.ActionListener () { 1260 public void actionPerformed(java.awt.event.ActionEvent evt) { 1261 addFieldMenuItemActionPerformed(evt); 1262 } 1263 }); 1264 1265 addSubMenu.add(addFieldMenuItem); 1266 1267 editMenu.add(addSubMenu); 1268 1269 deleteMenuItem.setMnemonic(KeyEvent.VK_D); 1270 deleteMenuItem.setAccelerator( 1271 KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, KeyEvent.CTRL_MASK)); 1272 deleteMenuItem.setText(" Delete"); 1273 deleteMenuItem.addActionListener(new java.awt.event.ActionListener () { 1274 public void actionPerformed(java.awt.event.ActionEvent evt) { 1275 deleteMenuItemActionPerformed(evt); 1276 } 1277 }); 1278 1279 editMenu.add(deleteMenuItem); 1280 1281 menuBar.add(editMenu); 1282 1283 connectionMenu.setMnemonic(KeyEvent.VK_D); 1284 connectionMenu.setText("Database"); 1285 driverManagerMenuItem.setText("Driver Manager.."); 1286 driverManagerMenuItem.addActionListener(new java.awt.event.ActionListener () { 1287 public void actionPerformed(java.awt.event.ActionEvent evt) { 1288 driverManagerMenuItemActionPerformed(evt); 1289 } 1290 }); 1291 1292 connectionMenu.add(driverManagerMenuItem); 1293 1294 connectionMenu.add(jSeparator4); 1295 1296 connectMenuItem.setText("Create connection..."); 1297 connectMenuItem.addActionListener(new java.awt.event.ActionListener () { 1298 public void actionPerformed(java.awt.event.ActionEvent evt) { 1299 connectMenuItemActionPerformed(evt); 1300 } 1301 }); 1302 1303 connectionMenu.add(connectMenuItem); 1304 1305 disconnectMenuItem.setText("Disconnect"); 1306 disconnectMenuItem.setEnabled(false); 1307 disconnectMenuItem.addActionListener(new java.awt.event.ActionListener () { 1308 public void actionPerformed(java.awt.event.ActionEvent evt) { 1309 disconnectMenuItemActionPerformed(evt); 1310 } 1311 }); 1312 1313 connectionMenu.add(disconnectMenuItem); 1314 1315 menuBar.add(connectionMenu); 1316 1317 helpMenu.setMnemonic(KeyEvent.VK_H); 1318 helpMenu.setText("Help"); 1319 aboutMenuItem.setText("About"); 1320 aboutMenuItem.addActionListener(new java.awt.event.ActionListener () { 1321 public void actionPerformed(java.awt.event.ActionEvent evt) { 1322 aboutMenuItemActionPerformed(evt); 1323 } 1324 }); 1325 1326 helpMenu.add(aboutMenuItem); 1327 1328 contentMenuItem.setMnemonic(KeyEvent.VK_C); 1329 contentMenuItem.setAccelerator( 1330 KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0)); 1331 contentMenuItem.setText("Content"); 1332 contentMenuItem.addActionListener(new java.awt.event.ActionListener () { 1333 public void actionPerformed(java.awt.event.ActionEvent evt) { 1334 contentMenuItemActionPerformed(evt); 1335 } 1336 }); 1337 1338 helpMenu.add(contentMenuItem); 1339 1340 menuBar.add(helpMenu); 1341 1342 setJMenuBar(menuBar); 1343 1344 pack(); 1345 } 1347 private void addFieldMenuItemActionPerformed(java.awt.event.ActionEvent evt) { fieldButtonActionPerformed(evt); 1350 } 1352 private void fieldButtonActionPerformed(java.awt.event.ActionEvent evt) { DefaultMutableTreeNode selected = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); 1355 if (!(selected instanceof Entity)) { 1356 JOptionPane.showMessageDialog(this, 1357 "A field can only be added to an entity. Please first select an entity in the application tree.", "Can't add field!", JOptionPane.ERROR_MESSAGE); 1358 return; 1359 } 1360 Entity selectedEntity = (Entity) selected; 1361 Field newField = new Field(selectedEntity, new Column()); 1362 selectedEntity.add(newField); 1363 tree.setSelectionPath(new TreePath(newField.getPath())); 1364 tree.updateUI(); 1365 } 1367 private void addBusinessMenuItemActionPerformed(java.awt.event.ActionEvent evt) { businessMethodButtonActionPerformed(evt); 1370 } 1372 private void editMenuActionPerformed(java.awt.event.ActionEvent evt) { } 1376 private void businessMethodButtonActionPerformed(java.awt.event.ActionEvent evt) { DefaultMutableTreeNode selected = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); 1378 if (!(selected instanceof Session)) { 1379 TreePath selectedPath = tree.getSelectionPath(); 1381 selected = (DefaultMutableTreeNode) selectedPath.getParentPath().getLastPathComponent(); 1382 if (!(selected instanceof Session)) { 1383 JOptionPane.showMessageDialog(this, 1384 "A business method can only be added to a service bean. Please first select a session in the application tree.", "Can't add business method!", JOptionPane.ERROR_MESSAGE); 1385 return; 1386 } 1387 } 1388 Session selectedSession = (Session) selected; 1389 1390 BusinessMethod newBusinessMethod = new BusinessMethod(selectedSession); 1391 selectedSession.add(newBusinessMethod); 1392 tree.setSelectionPath(new TreePath(newBusinessMethod.getPath())); 1393 tree.updateUI(); 1394 } 1396 private void driverManagerMenuItemActionPerformed(java.awt.event.ActionEvent evt) { DatabaseManagerFrame.getInstance().show(); 1398 } 1400 private void exportMenuItemActionPerformed(java.awt.event.ActionEvent evt) { if (file == null) { 1402 logger.log("UML Export: save application file first!"); 1403 String message = "Before exporting the current application to UML, please save it to an application file."; 1404 JOptionPane.showMessageDialog(this, message, "No application file!", JOptionPane.ERROR_MESSAGE); 1405 saveButtonActionPerformed(null); 1406 if (file == null) { 1407 logger.log("Aborted UML Export!"); 1408 return; 1409 } 1410 } else { 1411 saveButtonActionPerformed(null); 1412 } 1413 1414 int fileChooserStatus; 1415 logToConsole("Exporting application to an XMI file. Please wait..."); 1416 final JFileChooser fileChooser = new JFileChooser(getFileChooserStartDir(FILECHOOSER_UMLEXPORT)); 1417 fileChooser.setDialogType(JFileChooser.SAVE_DIALOG); 1418 String [] extenstions = {"xmi", "xml"}; 1419 ExtensionsFileFilter filter = new ExtensionsFileFilter(extenstions); 1420 fileChooser.setFileFilter(filter); 1421 1422 fileChooser.setDialogTitle("UML Export: Choose a destination XMI file.."); 1423 fileChooserStatus = fileChooser.showSaveDialog(this); 1424 if (fileChooserStatus == JFileChooser.APPROVE_OPTION) { 1425 File xmiFile = fileChooser.getSelectedFile(); 1426 if (!xmiFile.getAbsolutePath().endsWith(XMI_SUFFIX)) { 1427 xmiFile = new File (xmiFile.getAbsolutePath() + XMI_SUFFIX); 1428 } 1429 new Jag2UMLGenerator(logger).generateXMI(file.getAbsolutePath(), xmiFile); 1431 logToConsole("...UML export complete."); 1432 setFileChooserStartDir(FILECHOOSER_UMLEXPORT, xmiFile); 1433 1434 } else { 1435 logToConsole("...aborted!"); 1436 } 1437 } 1439 private void importMenuItemActionPerformed(java.awt.event.ActionEvent evt) { int fileChooserStatus; 1441 logToConsole("Importing UML model from XMI file. Please wait..."); 1442 final JFileChooser fileChooser = new JFileChooser(getFileChooserStartDir(FILECHOOSER_UMLIMPORT)); 1443 String [] extenstions = {"xmi", "xml"}; 1444 ExtensionsFileFilter filter = new ExtensionsFileFilter(extenstions); 1445 fileChooser.setDialogTitle("UML Import: Choose an XMI file.."); 1446 fileChooser.setFileFilter(filter); 1447 fileChooserStatus = fileChooser.showOpenDialog(this); 1448 if (fileChooserStatus == JFileChooser.APPROVE_OPTION) { 1449 String xmiFile = fileChooser.getSelectedFile().getAbsolutePath(); 1450 String outputDir = "."; 1451 File xmi = new UML2JagGenerator(logger).generateXML(xmiFile, outputDir); 1453 log.info("Generated the jag project file from the UML Model. Now load the file to JAG."); 1454 loadApplicationFile(xmi); 1455 log.info("JAG project file was loaded."); 1456 xmi.delete(); logToConsole("...UML import complete."); 1458 setFileChooserStartDir(FILECHOOSER_UMLIMPORT, fileChooser.getSelectedFile()); 1459 1460 1461 } else { 1462 logToConsole("...aborted!"); 1463 } 1464 1465 } 1467 private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) { if (file == null) { 1469 saveAsMenuItemActionPerformed(evt); 1470 } else { 1471 save(); 1472 } 1473 } 1475 private void disconnectMenuItemActionPerformed(java.awt.event.ActionEvent evt) { conManager = null; 1477 databaseConnectionLabel.setText("Database Connection: not connected"); 1478 disconnectMenuItem.setEnabled(false); 1479 DatabaseUtils.clearCache(); 1480 } 1482 private void addEntityMenuItemActionPerformed(java.awt.event.ActionEvent evt) { if (!isOfflineMode()) { 1484 getConManager(); 1485 } 1486 if (isOfflineMode()) { 1487 Entity entity = new Entity(root.getRootPackage(), "entity", null); 1488 DefaultMutableTreeNode parent = (DefaultMutableTreeNode) treeModel.getRoot(); 1489 addObject(parent, entity, true, false); 1490 tree.updateUI(); 1491 return; 1492 } 1493 if (conManager == null) { 1494 logger.log("Can't add entity - no database connection!"); 1495 return; 1496 } 1497 new SelectTablesDialog(this).show(); 1498 1499 new Thread (new Runnable () { 1500 public void run() { 1501 DefaultMutableTreeNode parent = (DefaultMutableTreeNode) treeModel.getRoot(); 1502 Object referencingModule = tree.getLastSelectedPathComponent(); 1503 String templateValue = (String ) root.config.getTemplateSettings().get(JagGenerator.TEMPLATE_USE_RELATIONS); 1504 if ("true".equalsIgnoreCase(templateValue)) { 1505 relationsEnabled = true; 1506 } else if ("false".equalsIgnoreCase(templateValue)) { 1507 relationsEnabled = false; 1508 } else { 1509 relationsEnabled = false; 1510 } 1511 ArrayList createdEntities = new ArrayList(); 1512 for (Iterator tabIt = SelectTablesDialog.getTablelist().iterator(); tabIt.hasNext();) { 1513 String table = (String ) tabIt.next(); 1514 logger.log("Creating entity for table '" + table + "'..."); 1515 ArrayList pKeys = DatabaseUtils.getPrimaryKeys(table); 1516 String pKey = ""; 1517 if (pKeys.size() == 1) { 1518 pKey = (String ) pKeys.get(0); 1519 } else if (pKeys.size() > 1) { 1520 String tableClassName = Utils.toClassName(table); 1521 pKey = root.getRootPackage() + ".entity" + tableClassName + "PK"; 1522 } 1523 1524 Entity entity = new Entity(root.getRootPackage(), table, pKey); 1525 entity.setTableName(table); 1526 addObject(parent, entity, true, false); 1527 if (referencingModule instanceof Session) { 1528 Session session = (Session) referencingModule; 1529 session.addRef(entity.getRefName()); 1530 } 1531 1532 ArrayList columns = sortColumns(DatabaseUtils.getColumns(table), pKeys, entity, pKey); 1533 if (relationsEnabled) { 1534 generateRelationsFromDB(entity); 1535 } 1536 1537 for (Iterator colIt = columns.iterator(); colIt.hasNext();) { 1539 Column column = (Column) colIt.next(); 1540 Field field = new Field(entity, column); 1541 addObject(entity, field, false, false); 1542 if (column.getName().equalsIgnoreCase(pKey)) { 1543 entity.setPKeyType(field.getType(column)); 1544 } 1545 } 1546 createdEntities.add(entity); 1547 } 1548 if (relationsEnabled) { 1549 checkForAssociationEntities(createdEntities); 1550 } 1551 for (Iterator iterator = createdEntities.iterator(); iterator.hasNext();) { 1553 Entity entity = (Entity) iterator.next(); 1554 entity.notifyRelationsThatConstructionIsFinished(); 1555 } 1556 logger.log("...finished!"); 1557 tree.updateUI(); 1558 } 1559 1560 }).start(); 1561 } 1563 1564 1570 private void checkForAssociationEntities(ArrayList createdEntities) { 1571 for (Iterator iterator = createdEntities.iterator(); iterator.hasNext();) { 1572 Entity entity = (Entity) iterator.next(); 1573 if (entity.getRelations() != null && entity.getRelations().size() == 2 && entity.getFields().size() == 2) { 1574 if (((Relation) entity.getRelations().get(0)).isTargetMultiple() && 1576 ((Relation) entity.getRelations().get(1)).isTargetMultiple() 1577 ) { 1578 entity.setIsAssociationEntity("true"); 1580 } 1581 } 1582 } 1583 } 1584 1585 private void addRelationPopupMenuItemActionPerformed 1586 (java.awt.event.ActionEvent  1587 evt) { newRelationMenuItemActionPerformed(); 1589 } 1591 private void contentMenuItemActionPerformed 1592 (java.awt.event.ActionEvent  1593 evt) { URL helpURL = null; 1595 String s = null; 1596 try { 1597 s = "file:" 1598 + System.getProperty("user.dir") 1599 + System.getProperty("file.separator") 1600 + "../doc/help/help.html"; 1601 helpURL = new URL (s); 1602 } catch (IOException e) { 1603 JagGenerator.logToConsole("Missing help file: " + s, LogLevel.ERROR); 1604 } 1605 new HtmlContentPopUp(null, "JAG Help", false, helpURL).show(); 1606 } 1608 private void aboutMenuItemActionPerformed 1609 (java.awt.event.ActionEvent  1610 evt) { URL helpURL = null; 1612 String s = null; 1613 try { 1614 s = "file:" 1615 + System.getProperty("user.dir") 1616 + System.getProperty("file.separator") 1617 + "../doc/help/about.html"; 1618 helpURL = new URL (s); 1619 } catch (IOException e) { 1620 JagGenerator.logToConsole("Missing help file: " + s, LogLevel.ERROR); 1621 } 1622 new HtmlContentPopUp(null, "JAG About", false, helpURL).show(); 1623 } 1625 private void generateJavaApplicationAsMenuItemActionPerformed 1626 (java.awt.event.ActionEvent  1627 evt) { if (evt.getActionCommand() == STOP_ACTION) { 1629 runningThread.interrupt(); 1630 return; 1631 } 1632 1633 if (file == null) { 1634 logger.log("No file specified! Save file first."); 1635 String message = "No application file (XML skelet) has been selected.\n" + 1636 "Please save the current application to a file or open an existing application file."; 1637 JOptionPane.showMessageDialog(this, message, "No application file!", JOptionPane.ERROR_MESSAGE); 1638 } else { 1639 SkeletValidator validator = new SkeletValidator(root, tree, entitiesByTableName, logger); 1640 String message = validator.validateSkelet(); 1641 if (message != null) { 1642 logger.log("Not a valid application file!"); 1643 message += "\r\nSelect 'Yes' if you want to generate anyway. This will very probably lead to incorrect code!"; 1644 int rc = JOptionPane.showConfirmDialog(this, message, "Invalid configuration", JOptionPane.YES_NO_OPTION); 1646 if (rc != 0) { 1647 return; 1649 } 1650 logger.log("Warning! Code is generated in spite of an invalid project file!"); 1651 } 1652 if (!save()) { 1654 logger.log("Can't generate application - Invalid relation(s)."); 1655 return; 1656 } 1657 1658 String outDir = Settings.getLastSelectedOutputDir(); 1659 1661 if (outputDir != null) { 1662 outDir = outputDir.getParentFile().getAbsolutePath(); 1663 } 1664 outputDir = selectJagOutDirectory(outDir); 1665 if (outputDir == null) { 1666 return; 1667 } 1668 1669 Settings.setLastSelectedOutputDir(outputDir.getParentFile().getAbsolutePath()); 1670 1671 final String [] args = new String [3]; 1672 args[0] = outputDir.getAbsolutePath(); 1673 args[1] = file.getAbsolutePath(); 1674 runningThread = new Thread () { 1675 public void run() { 1676 1677 logger.log("Running jag in the " + args[0] + " directory for application file: " + args[1]); 1678 JApplicationGen.setLogger(logger); 1679 JApplicationGen.main(args); 1680 } 1681 }; 1682 runningThread.start(); 1683 executeButton.setIcon(CANCEL_ICON); 1684 executeButton.setActionCommand(STOP_ACTION); 1685 } 1686 setFileNeedsSavingIndicator(false); 1687 } 1689 private void deleteMenuItemActionPerformed 1690 (java.awt.event.ActionEvent  1691 evt) { TreePath[] sel = tree.getSelectionPaths(); 1693 for (int i = 0; i < sel.length; i++) { 1694 Object selectedObject = sel[i].getLastPathComponent(); 1695 if (!(selectedObject instanceof Config || 1696 selectedObject instanceof App || 1697 selectedObject instanceof Paths || 1698 selectedObject instanceof Datasource)) { 1699 1700 treeModel.removeNodeFromParent((DefaultMutableTreeNode) selectedObject); 1701 } 1702 if (selectedObject instanceof Entity) { 1703 TemplateString table = ((Entity) selectedObject).getLocalTableName(); 1704 SelectTablesDialog.getAlreadyselected().remove(table); 1705 DatabaseUtils.clearColumnsCacheForTable(table.toString()); 1706 } 1707 } 1708 setFileNeedsSavingIndicator(true); 1709 } 1711 private void newMenuItemActionPerformed 1712 (java.awt.event.ActionEvent  1713 evt) { root = new Root(); 1715 file = null; 1716 fileNameLabel.setText("Application file:"); 1717 fileNameLabel.setToolTipText("No application file selected"); 1718 disconnectMenuItemActionPerformed(null); 1719 1720 treeModel.setRoot(root); 1721 } 1723 private void openMenuItemActionPerformed 1724 (java.awt.event.ActionEvent  1725 evt) { int fileChooserStatus; 1727 JFileChooser fileChooser = new JFileChooser(getFileChooserStartDir(FILECHOOSER_APPFILE_OPEN)); 1728 ExtensionsFileFilter filter = new ExtensionsFileFilter("xml"); 1729 logToConsole("Opening application file.."); 1730 1731 fileChooser.setDialogTitle("Open an existing application file.."); 1732 fileChooser.setFileFilter(filter); 1733 fileChooserStatus = fileChooser.showOpenDialog(this); 1734 if (fileChooserStatus == JFileChooser.APPROVE_OPTION) { 1735 file = fileChooser.getSelectedFile(); 1736 loadApplicationFile(file); 1737 1738 } else { 1739 logToConsole("..aborted application file load!"); 1740 } 1741 1742 if (file != null) { 1743 fileNameLabel.setText("Application file: " + file.getName()); 1744 fileNameLabel.setToolTipText(file.getAbsolutePath()); 1745 setFileChooserStartDir(FILECHOOSER_APPFILE_OPEN, file); 1746 } 1747 } 1749 public void loadApplicationFile 1750 (File  1751 file) { 1752 this.file = file; 1753 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 1754 DocumentBuilder builder = null; 1755 Document doc = null; 1756 try { 1757 builder = dbf.newDocumentBuilder(); 1758 doc = builder.parse(file); 1759 root = new Root(doc); 1760 logToConsole("..application file " + file + " loaded!"); 1761 treeModel.setRoot(root); 1762 tree.setSelectionPath(new TreePath(((DefaultMutableTreeNode) root.getFirstChild()).getPath())); 1763 setFileNeedsSavingIndicator(false); 1764 SelectTablesDialog.clear(); 1765 disconnectMenuItemActionPerformed(null); 1766 getRecentMenu().addToRecentList(file.getAbsolutePath()); 1767 } catch (Exception e) { 1768 e.printStackTrace(); 1769 logToConsole("Failed to load application file! (" + e + ")", LogLevel.ERROR); 1770 getRecentMenu().removeFromRecentList(file.getAbsolutePath()); 1771 } 1772 } 1773 1774 private static void logToConsole(Object o, LogLevel error) { 1775 if (jagGenerator == null) { 1776 System.out.println(o); 1777 } else { 1778 jagGenerator.logger.log(o.toString(), error); 1779 } 1780 } 1781 1782 private void saveMenuItemActionPerformed 1783 (java.awt.event.ActionEvent  1784 evt) { saveButtonActionPerformed(evt); 1786 } 1788 private void saveAsMenuItemActionPerformed 1789 (java.awt.event.ActionEvent  1790 evt) { int fileChooserStatus; 1792 JFileChooser fileChooser = new JFileChooser(getFileChooserStartDir(FILECHOOSER_APPFILE_SAVE)); 1793 fileChooser.setDialogTitle("Save application file.."); 1794 ExtensionsFileFilter filter = new ExtensionsFileFilter("xml"); 1795 fileChooser.setFileFilter(filter); 1796 fileChooserStatus = fileChooser.showSaveDialog(this); 1797 if (fileChooserStatus == JFileChooser.APPROVE_OPTION) { 1798 file = fileChooser.getSelectedFile(); 1799 setFileChooserStartDir(FILECHOOSER_APPFILE_SAVE, file); 1800 save(); 1801 } 1802 } 1804 private void connectMenuItemActionPerformed 1805 (java.awt.event.ActionEvent  1806 evt) { GenericJdbcManager previous = conManager; 1808 conManager = null; 1809 getConManager(); 1810 if (conManager == null) { 1811 conManager = previous; 1812 1813 } else { 1814 DatabaseUtils.clearCache(); 1816 1817 Iterator entities = root.getEntityEjbs().iterator(); 1818 while (entities.hasNext()) { 1819 Entity entity = (Entity) entities.next(); 1820 for (int i = 0; i < entity.getChildCount(); i++) { 1821 TreeNode child = entity.getChildAt(i); 1822 if (child instanceof Relation) { 1823 ((RelationPanel) ((Relation) child).getPanel()).initValues(false); 1824 } 1825 } 1826 } 1827 } 1828 } 1830 private void addSessionMenuItemActionPerformed 1831 (java.awt.event.ActionEvent  1832 evt) { addObject(root, new Session(root.getRootPackage()), true, false); 1834 setFileNeedsSavingIndicator(true); 1835 } 1837 private void treeValueChanged 1838 (TreeSelectionEvent  1839 evt) { TreePath path = evt.getNewLeadSelectionPath(); 1841 JagBean jagBean; 1842 if (path != null) { 1843 jagBean = (JagBean) path.getLastPathComponent(); 1844 } else { 1845 jagBean = (JagBean) treeModel.getRoot(); 1846 } 1847 splitPane.setRightComponent(jagBean.getPanel()); 1848 splitPane.setDividerLocation(SPLIT_PANE_WIDTH); 1849 } 1851 private void exitMenuItemActionPerformed 1852 (java.awt.event.ActionEvent  1853 evt) { exitForm(null); 1855 } 1857 private void exitForm 1858 (java.awt.event.WindowEvent  1859 evt) { kickTheBucket(null); 1861 } 1863 private javax.swing.JMenuItem aboutMenuItem; 1865 private javax.swing.JMenuItem addBusinessMenuItem; 1866 private javax.swing.JMenuItem addEntityMenuItem; 1867 private javax.swing.JMenuItem addFieldMenuItem; 1868 private javax.swing.JMenuItem addRelationMenuItem; 1869 private javax.swing.JMenuItem addSessionMenuItem; 1870 private javax.swing.JMenu addSubMenu; 1871 public javax.swing.JPanel applicationFileInfoPanel; 1872 private javax.swing.JButton businessMethodButton; 1873 private javax.swing.JMenuItem connectMenuItem; 1874 private javax.swing.JMenu connectionMenu; 1875 private javax.swing.JTextPane console; 1876 private javax.swing.JScrollPane consoleScrollPane; 1877 private javax.swing.JMenuItem contentMenuItem; 1878 public javax.swing.JPanel databaseConnectionInfoPanel; 1879 public javax.swing.JLabel databaseConnectionLabel; 1880 private javax.swing.JButton deleteButton; 1881 private javax.swing.JMenuItem deleteMenuItem; 1882 private javax.swing.JSplitPane desktopConsoleSplitPane; 1883 private javax.swing.JDesktopPane desktopPane; 1884 private javax.swing.JMenuItem disconnectMenuItem; 1885 private javax.swing.JMenuItem driverManagerMenuItem; 1886 private javax.swing.JMenu editMenu; 1887 private javax.swing.JButton entityButton; 1888 private javax.swing.JButton executeButton; 1889 private javax.swing.JMenuItem exitMenuItem; 1890 private javax.swing.JMenuItem exportMenuItem; 1891 private javax.swing.JButton fieldButton; 1892 private javax.swing.JMenu fileMenu; 1893 public javax.swing.JLabel fileNameLabel; 1894 private javax.swing.JMenuItem generateJavaApplicationAsMenuItem; 1895 private javax.swing.JButton helpButton; 1896 private javax.swing.JMenu helpMenu; 1897 private javax.swing.JMenuItem importMenuItem; 1898 private javax.swing.JSeparator jSeparator1; 1899 private javax.swing.JSeparator jSeparator2; 1900 private javax.swing.JSeparator jSeparator3; 1901 private javax.swing.JSeparator jSeparator4; 1902 private javax.swing.JMenuBar menuBar; 1903 private javax.swing.JButton newButton; 1904 private javax.swing.JMenuItem newMenuItem; 1905 private javax.swing.JButton openButton; 1906 private javax.swing.JMenuItem openMenuItem; 1907 private javax.swing.JButton relationButton; 1908 private javax.swing.JMenuItem saveAsMenuItem; 1909 private javax.swing.JButton saveButton; 1910 private javax.swing.JMenuItem saveMenuItem; 1911 private javax.swing.JButton sessionButton; 1912 private javax.swing.JPanel spacer; 1913 private javax.swing.JSplitPane splitPane; 1914 public javax.swing.JTextArea textConsole; 1915 public javax.swing.JToolBar toolBar; 1916 private javax.swing.JTree tree; 1917 private javax.swing.JScrollPane treeScrollPane; 1918 private com.finalist.jaggenerator.menu.RecentMenu recentMenu; 1920 1921 public com.finalist.jaggenerator.menu.RecentMenu getRecentMenu() { 1922 return recentMenu; 1923 } 1924 1925} 1926 | Popular Tags |