1 30 31 package com.genimen.djeneric.tools.strongtyper; 32 33 import java.awt.AWTEvent ; 34 import java.awt.BorderLayout ; 35 import java.awt.Color ; 36 import java.awt.Dimension ; 37 import java.awt.FlowLayout ; 38 import java.awt.Image ; 39 import java.awt.Toolkit ; 40 import java.awt.event.ActionEvent ; 41 import java.awt.event.KeyEvent ; 42 import java.awt.event.WindowEvent ; 43 import java.io.BufferedWriter ; 44 import java.io.File ; 45 import java.io.FileWriter ; 46 import java.net.URL ; 47 import java.util.ArrayList ; 48 import java.util.Arrays ; 49 import java.util.Collections ; 50 import java.util.StringTokenizer ; 51 52 import javax.swing.DefaultComboBoxModel ; 53 import javax.swing.ImageIcon ; 54 import javax.swing.JButton ; 55 import javax.swing.JCheckBox ; 56 import javax.swing.JComboBox ; 57 import javax.swing.JFileChooser ; 58 import javax.swing.JLabel ; 59 import javax.swing.JList ; 60 import javax.swing.JOptionPane ; 61 import javax.swing.JPanel ; 62 import javax.swing.JScrollPane ; 63 import javax.swing.JTabbedPane ; 64 import javax.swing.JTextArea ; 65 import javax.swing.JTextField ; 66 67 import com.genimen.djeneric.language.Messages; 68 import com.genimen.djeneric.repository.DjExtent; 69 import com.genimen.djeneric.repository.DjPackage; 70 import com.genimen.djeneric.repository.DjPersistenceManager; 71 import com.genimen.djeneric.repository.DjPersistenceManagerFactory; 72 import com.genimen.djeneric.repository.DjSession; 73 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException; 74 import com.genimen.djeneric.tools.common.DjenericTool; 75 import com.genimen.djeneric.ui.DjVerticalFlowLayout; 76 import com.genimen.djeneric.ui.Util; 77 import com.genimen.djeneric.util.DjEnvironment; 78 import com.genimen.djeneric.util.DjFileFilter; 79 import com.genimen.djeneric.util.DjFileUtil; 80 import com.genimen.djeneric.util.DjLogPrintStream; 81 import com.genimen.djeneric.util.DjLogger; 82 import com.genimen.djeneric.util.DjStringComparator; 83 import com.genimen.djeneric.util.DjStringReplacer; 84 import com.genimen.djeneric.util.DjVersion; 85 86 public class StrongTyper extends DjenericTool 87 { 88 private static final long serialVersionUID = 1L; 89 private final String PROPERTIES_FILE_NAME = DjEnvironment.getAbsoluteFileName("strongtyper.properties"); 90 private final static String LOG_FILE_NAME = DjEnvironment.getAbsoluteFileName("strongtyper.log"); 91 92 private final String SST_EXTENSION = "sts"; 93 94 public final static String OK_MSG = Messages.getString("global.Ok"); 95 96 private final static String CODE_START_TAG = "//<DjenericManualCode%>"; 97 private final static String CODE_END_TAG = "//</DjenericManualCode>"; 98 private final static String GENERATE_SUPER = "//<DjenericGenerateSuper/>"; 99 private final static String REGENERATION_TAGS = "\n" + "// Place your custom code BETWEEN the following tags\n" 100 + StrongTyper.CODE_START_TAG + "\n" + StrongTyper.CODE_END_TAG 101 + "\n"; 102 103 DjPersistenceManager _mgr; 104 DjSession _session; 105 Color _normalColor = new Color (102, 102, 153); 106 JTabbedPane _tabs = new JTabbedPane (); 107 JPanel _pnlSelection = new JPanel (); 108 JScrollPane _scrSelection = new JScrollPane (); 109 BorderLayout borderLayout1 = new BorderLayout (); 110 JLabel _lblStatus = new JLabel (); 111 JPanel _pnlOptions = new JPanel (); 112 BorderLayout borderLayout2 = new BorderLayout (); 113 JPanel jPanel1 = new JPanel (); 114 JPanel jPanel2 = new JPanel (); 115 DjVerticalFlowLayout verticalFlowLayout1 = new DjVerticalFlowLayout(); 116 DjVerticalFlowLayout verticalFlowLayout2 = new DjVerticalFlowLayout(); 117 JLabel jLabel1 = new JLabel (); 118 JLabel jLabel2 = new JLabel (); 119 JLabel jLabel3 = new JLabel (); 120 JLabel jLabel4 = new JLabel (); 121 JTextField _edtPackage = new JTextField (); 122 JTextField _edtPrefix = new JTextField (); 123 JTextField _edtSuffix = new JTextField (); 124 JPanel jPanel3 = new JPanel (); 125 JTextField _edtDirectory = new JTextField (); 126 BorderLayout borderLayout3 = new BorderLayout (); 127 JButton _butBrowse = new JButton (); 128 JPanel jPanel4 = new JPanel (); 129 BorderLayout borderLayout4 = new BorderLayout (); 130 JPanel jPanel5 = new JPanel (); 131 JButton _butSave = new JButton (); 132 JButton _butLoad = new JButton (); 133 JPanel jPanel6 = new JPanel (); 134 BorderLayout borderLayout5 = new BorderLayout (); 135 JPanel jPanel7 = new JPanel (); 136 JButton _butGenerate = new JButton (); 137 DefaultComboBoxModel _model; 138 JList _lstExtents = new JList (); 139 JLabel jLabel5 = new JLabel (); 140 JTextField _edtSystemPrefix = new JTextField (); 141 JPanel jPanel8 = new JPanel (); 142 JCheckBox _chkGenerateAll = new JCheckBox (); 143 FlowLayout flowLayout1 = new FlowLayout (); 144 JLabel jLabel6 = new JLabel (); 145 JTextField _edtImplPackage = new JTextField (); 146 JPanel _pnlLog = new JPanel (); 147 JScrollPane jScrollPane1 = new JScrollPane (); 148 BorderLayout borderLayout6 = new BorderLayout (); 149 JTextArea _txtLog = new JTextArea (); 150 JPanel jPanel9 = new JPanel (); 151 BorderLayout borderLayout7 = new BorderLayout (); 152 JLabel jLabel7 = new JLabel (); 153 DjPackage _defaultPackage = new DjPackage(Messages 154 .getString("ExtentSelectorDialog.DefaultPackage")); 155 DjPackage _allPackages = new DjPackage(Messages 156 .getString("ExtentSelectorDialog.AllPackages")); 157 JPanel jPanel10 = new JPanel (); 158 DjVerticalFlowLayout verticalFlowLayout3 = new DjVerticalFlowLayout(); 159 JPanel jPanel11 = new JPanel (); 160 DjVerticalFlowLayout verticalFlowLayout4 = new DjVerticalFlowLayout(); 161 JLabel jLabel8 = new JLabel (); 162 JTextField _edtSelection = new JTextField (); 163 JPanel jPanel12 = new JPanel (); 164 BorderLayout borderLayout8 = new BorderLayout (); 165 JComboBox _cbbPackage = new JComboBox (); 166 JButton _butAddPackage = new JButton (); 167 168 174 public StrongTyper(String repositories) 175 { 176 enableEvents(AWTEvent.WINDOW_EVENT_MASK); 177 setIconImage(getImage("strongtyper.gif")); 178 179 try 180 { 181 setStatusLabel(_lblStatus); 182 DjPersistenceManagerFactory fact = new DjPersistenceManagerFactory(this, repositories); 183 _mgr = fact.createInstance(); 184 185 _session = _mgr.createSession(); 186 187 DjExtent[] extents = _mgr.getExtents(); 188 Arrays.sort(extents, new DjStringComparator(false)); 189 _model = new DefaultComboBoxModel (extents); 190 191 ArrayList packs = new ArrayList (); 192 packs.add(_allPackages); 193 packs.add(_defaultPackage); 194 packs.addAll(Arrays.asList(_mgr.getPackages())); 195 _cbbPackage.setModel(new DefaultComboBoxModel (packs.toArray())); 196 197 jbInit(); 198 199 _butAddPackage.setIcon(getImageIcon("add.gif")); 200 201 Util.sizeLabels(jPanel9); 202 Util.sizeButtons(jPanel9); 203 204 loadProps(PROPERTIES_FILE_NAME); 205 206 copyPropertiesToFields(); 207 208 updateSelection(); 209 210 int width = Integer.parseInt(getProperty("window.width", "400")); 211 int height = Integer.parseInt(getProperty("window.height", "400")); 212 setSize(new Dimension (width, height)); 213 214 setTitle("Djeneric Strong Typer v" + DjVersion.getVersion() + " logged on to " + _mgr.getCurrentRepository()); 215 } 216 catch (Exception e) 217 { 218 handleException(e); 219 System.exit(-1); 220 } 221 } 222 223 229 private void jbInit() throws Exception 230 { 231 _scrSelection.setPreferredSize(new Dimension (260, 131)); 232 _pnlSelection.setLayout(borderLayout1); 233 _lblStatus.setText(Messages.getString("global.Ok")); 234 _pnlOptions.setLayout(borderLayout2); 235 jPanel1.setLayout(verticalFlowLayout1); 236 jPanel2.setLayout(verticalFlowLayout2); 237 jLabel1.setText(Messages.getString("StrongTyper.ImplPackage")); 238 jLabel2.setText(Messages.getString("StrongTyper.ClassNamePrefix")); 239 jLabel3.setText(Messages.getString("StrongTyper.ClassNameSuffix")); 240 jLabel4.setText(Messages.getString("StrongTyper.OutputRoot")); 241 jPanel3.setLayout(borderLayout3); 242 _butBrowse.setPreferredSize(new Dimension (21, 21)); 243 _butBrowse.setText("..."); 244 _butBrowse.addActionListener(new java.awt.event.ActionListener () 245 { 246 public void actionPerformed(ActionEvent e) 247 { 248 _butBrowse_actionPerformed(e); 249 } 250 }); 251 jPanel4.setLayout(borderLayout4); 252 _butSave.setText(Messages.getString("global.Save")); 253 _butSave.addActionListener(new java.awt.event.ActionListener () 254 { 255 public void actionPerformed(ActionEvent e) 256 { 257 _butSave_actionPerformed(e); 258 } 259 }); 260 _butLoad.setText(Messages.getString("StrongTyper.Load")); 261 _butLoad.addActionListener(new java.awt.event.ActionListener () 262 { 263 public void actionPerformed(ActionEvent e) 264 { 265 _butLoad_actionPerformed(e); 266 } 267 }); 268 jPanel6.setLayout(borderLayout5); 269 _butGenerate.setText(Messages.getString("global.Generate")); 270 _butGenerate.addActionListener(new java.awt.event.ActionListener () 271 { 272 public void actionPerformed(ActionEvent e) 273 { 274 _butGenerate_actionPerformed(e); 275 } 276 }); 277 _lstExtents.setModel(_model); 278 jLabel5.setText(Messages.getString("StrongTyper.Systemprefix")); 279 _edtPackage.setPreferredSize(new Dimension (4, 21)); 280 _edtPackage.setToolTipText(Messages.getString("StrongTyper.Package1")); 281 _edtPrefix.setPreferredSize(new Dimension (4, 21)); 282 _edtPrefix.setToolTipText(Messages.getString("StrongTyper.ClassNamePrefixDescr")); 283 _edtSuffix.setPreferredSize(new Dimension (4, 21)); 284 _edtSuffix.setToolTipText(Messages.getString("StrongTyper.ClassNameSuffixDescr")); 285 _edtSystemPrefix.setPreferredSize(new Dimension (4, 21)); 286 _edtSystemPrefix.setToolTipText(Messages.getString("StrongTyper.PmSessionPrefix")); 287 _edtDirectory.setPreferredSize(new Dimension (4, 21)); 288 _edtDirectory.setToolTipText(Messages.getString("StrongTyper.DestDir")); 289 _chkGenerateAll.setText(Messages.getString("StrongTyper.GenerateManagerSession")); 290 jPanel8.setLayout(flowLayout1); 291 flowLayout1.setAlignment(FlowLayout.LEFT); 292 jLabel6.setText(Messages.getString("StrongTyper.Package")); 293 _edtImplPackage.setPreferredSize(new Dimension (4, 21)); 294 _edtImplPackage.setToolTipText(Messages.getString("StrongTyper.Package1")); 295 _pnlLog.setLayout(borderLayout6); 296 _txtLog.setFont(new java.awt.Font ("Monospaced", 0, 11)); 297 _txtLog.setEditable(false); 298 jPanel9.setLayout(borderLayout7); 299 jLabel7.setText("Package"); 300 jPanel10.setLayout(verticalFlowLayout3); 301 jPanel11.setLayout(verticalFlowLayout4); 302 jLabel8.setText(Messages.getString("StrongTyper.Selection")); 303 _edtSelection.setToolTipText(Messages.getString("StrongTyper.SelectionHowTo")); 304 _edtSelection.addKeyListener(new java.awt.event.KeyAdapter () 305 { 306 public void keyReleased(KeyEvent e) 307 { 308 _edtSelection_keyReleased(e); 309 } 310 }); 311 jPanel12.setLayout(borderLayout8); 312 _cbbPackage.setMinimumSize(new Dimension (26, 21)); 313 _cbbPackage.setPreferredSize(new Dimension (21, 21)); 314 _butAddPackage.setPreferredSize(new Dimension (21, 21)); 315 _butAddPackage.setSelected(false); 316 _butAddPackage.addActionListener(new java.awt.event.ActionListener () 317 { 318 public void actionPerformed(ActionEvent e) 319 { 320 butAdd_actionPerformed(e); 321 } 322 }); 323 jPanel5.add(_butLoad, null); 324 this.getContentPane().add(_tabs, BorderLayout.CENTER); 325 this.getContentPane().add(_lblStatus, BorderLayout.SOUTH); 326 _tabs.add(_pnlSelection, Messages.getString("StrongTyper.Selection")); 327 _pnlSelection.add(_scrSelection, BorderLayout.CENTER); 328 _scrSelection.getViewport().add(_lstExtents, null); 329 _tabs.add(_pnlOptions, Messages.getString("global.Options")); 330 _pnlOptions.add(jPanel1, BorderLayout.WEST); 331 jPanel1.add(jLabel6, null); 332 jPanel1.add(jLabel1, null); 333 jPanel1.add(jLabel5, null); 334 jPanel1.add(jLabel2, null); 335 jPanel1.add(jLabel3, null); 336 jPanel1.add(jLabel4, null); 337 _pnlOptions.add(jPanel2, BorderLayout.CENTER); 338 jPanel2.add(_edtPackage, null); 339 jPanel2.add(_edtImplPackage, null); 340 jPanel2.add(_edtSystemPrefix, null); 341 jPanel2.add(_edtPrefix, null); 342 jPanel2.add(_edtSuffix, null); 343 jPanel2.add(jPanel3, null); 344 jPanel3.add(_edtDirectory, BorderLayout.CENTER); 345 jPanel3.add(_butBrowse, BorderLayout.EAST); 346 _pnlOptions.add(jPanel4, BorderLayout.SOUTH); 347 jPanel4.add(jPanel5, BorderLayout.EAST); 348 jPanel5.add(_butSave, null); 349 _tabs.add(_pnlLog, Messages.getString("StrongTyper.Log")); 350 _pnlSelection.add(jPanel6, BorderLayout.SOUTH); 351 jPanel6.add(jPanel7, BorderLayout.EAST); 352 jPanel7.add(_butGenerate, null); 353 jPanel6.add(jPanel8, BorderLayout.CENTER); 354 jPanel8.add(_chkGenerateAll, null); 355 _pnlSelection.add(jPanel9, BorderLayout.NORTH); 356 jPanel9.add(jPanel10, BorderLayout.WEST); 357 jPanel10.add(jLabel7, null); 358 jPanel10.add(jLabel8, null); 359 jPanel9.add(jPanel11, BorderLayout.CENTER); 360 jPanel11.add(jPanel12, null); 361 jPanel12.add(_cbbPackage, BorderLayout.CENTER); 362 jPanel12.add(_butAddPackage, BorderLayout.EAST); 363 jPanel11.add(_edtSelection, null); 364 _pnlLog.add(jScrollPane1, BorderLayout.CENTER); 365 jScrollPane1.getViewport().add(_txtLog, null); 366 Util.sizeLabels(jPanel1); 367 Util.sizeButtons(jPanel4); 368 } 369 370 376 protected void processWindowEvent(WindowEvent e) 377 { 378 if (e.getID() == WindowEvent.WINDOW_CLOSING) 379 { 380 exitProgram(); 381 } 382 super.processWindowEvent(e); 383 384 } 385 386 protected void updateProperties() 387 { 388 setProperty("window.width", "" + getWidth()); 389 setProperty("window.height", "" + getHeight()); 390 391 setProperty("options.directory", _edtDirectory.getText()); 392 setProperty("options.package", _edtPackage.getText()); 393 setProperty("options.implpackage", _edtImplPackage.getText()); 394 setProperty("options.prefix", _edtPrefix.getText()); 395 setProperty("options.systemprefix", _edtSystemPrefix.getText()); 396 setProperty("options.suffix", _edtSuffix.getText()); 397 setProperty("options.generatetoplevel", "" + _chkGenerateAll.isSelected()); 398 setProperty("options.selection", _edtSelection.getText()); 399 } 400 401 protected void copyPropertiesToFields() 402 { 403 _edtDirectory.setText(getProperty("options.directory", "")); 404 _edtPackage.setText(getProperty("options.package", "")); 405 _edtImplPackage.setText(getProperty("options.implpackage", "")); 406 _edtPrefix.setText(getProperty("options.prefix", "")); 407 _edtSystemPrefix.setText(getProperty("options.systemprefix", "")); 408 _edtSuffix.setText(getProperty("options.suffix", "")); 409 _chkGenerateAll.setSelected(getProperty("options.generatetoplevel", "true").equalsIgnoreCase("true")); 410 _edtSelection.setText(getProperty("options.selection", "")); 411 } 412 413 private static String replaceTagId(String src, int id) 414 { 415 DjStringReplacer sr = new DjStringReplacer(src); 416 String to = ""; 417 if (id != 0) to = " id=\"" + String.valueOf(id) + "\""; 418 return sr.replace("%", to); 419 } 420 421 public static String getRegenerationTags(int id) 422 { 423 return replaceTagId(REGENERATION_TAGS, id); 424 } 425 426 private String getRegenerationStartTag(int id) 427 { 428 return replaceTagId(CODE_START_TAG, id); 429 } 430 431 public static ImageIcon getImageIcon(String fileName) 432 { 433 URL url = StrongTyper.class.getResource("images/" + fileName); 434 if (url != null) return new ImageIcon (url); 435 else return new ImageIcon (""); 436 } 438 439 public static Image getImage(String fileName) 440 { 441 URL url = StrongTyper.class.getResource("images/" + fileName); 442 if (url != null) return Toolkit.getDefaultToolkit().getImage(url); 443 444 else return Toolkit.getDefaultToolkit().getImage(""); 445 } 446 447 public DjPersistenceManager getManager() 448 { 449 return _mgr; 450 } 451 452 public DjSession getSession() 453 { 454 return _session; 455 } 456 457 void _butBrowse_actionPerformed(ActionEvent e) 458 { 459 JFileChooser fc = new JFileChooser (); 460 fc.setDialogTitle(Messages.getString("global.GeneratePath")); 461 String path = _edtDirectory.getText(); 462 if (path.trim().length() == 0) path = System.getProperties().getProperty("user.dir"); 463 fc.setCurrentDirectory(new File (path)); 464 fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 465 466 if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) 467 { 468 _edtDirectory.setText(fc.getSelectedFile().getAbsolutePath()); 469 } 470 } 471 472 void _butLoad_actionPerformed(ActionEvent e) 473 { 474 JFileChooser fc = new JFileChooser (); 475 initFileChooser(fc); 476 DjFileFilter filter = new DjFileFilter(new String []{SST_EXTENSION}, Messages 477 .getString("StrongTyper.StrongTyperSettings")); 478 fc.setFileFilter(filter); 479 fc.setDialogTitle(Messages.getString("StrongTyper.LoadSettings")); 480 fc.setFileSelectionMode(JFileChooser.FILES_ONLY); 481 482 if (fc.showOpenDialog(this) != JFileChooser.CANCEL_OPTION) 483 { 484 recordFileChooserSettings(fc); 485 getPropsFrom(fc.getSelectedFile().getAbsolutePath()); 486 copyPropertiesToFields(); 487 } 488 489 } 490 491 protected void initFileChooser(JFileChooser fc) 492 { 493 fc.setCurrentDirectory(new File (getProperty("filelocation", System.getProperties().getProperty("user.dir")))); 494 } 495 496 protected void recordFileChooserSettings(JFileChooser fc) 497 { 498 setProperty("filelocation", fc.getCurrentDirectory().getAbsolutePath()); 499 } 500 501 boolean isOkToOverwrite(File file) 502 { 503 int result = JOptionPane.showOptionDialog(this, 504 Messages.getString("global.Sure2Overwrite", file.getAbsolutePath()), 505 Messages.getString("global.FileExists"), JOptionPane.DEFAULT_OPTION, 506 JOptionPane.INFORMATION_MESSAGE, null 507 , new String []{Messages.getString("global.Overwrite"), 509 Messages.getString("global.Cancel")}, null); 510 return result == 0; 511 } 512 513 void _butSave_actionPerformed(ActionEvent e) 514 { 515 JFileChooser fc = new JFileChooser (); 516 DjFileFilter filter = new DjFileFilter(new String []{SST_EXTENSION}, Messages 517 .getString("StrongTyper.StrongTyperSettings")); 518 fc.setFileFilter(filter); 519 520 fc.setDialogTitle(Messages.getString("StrongTyper.SaveSettings")); 521 initFileChooser(fc); 522 fc.setFileSelectionMode(JFileChooser.FILES_ONLY); 523 524 if (fc.showSaveDialog(this) != JFileChooser.CANCEL_OPTION) 525 { 526 recordFileChooserSettings(fc); 527 String fileName = fc.getSelectedFile().getAbsolutePath(); 528 if (!fileName.toLowerCase().endsWith("." + SST_EXTENSION)) 529 { 530 fileName += "." + SST_EXTENSION; 531 } 532 File chk = new File (fileName); 533 if (chk.exists()) 534 { 535 if (!isOkToOverwrite(chk)) 536 { 537 return; 538 } 539 } 540 saveProps(fileName); 541 } 542 543 } 544 545 private void initGenerator(Generator gen) throws ObjectNotDefinedException 546 { 547 gen.setItfPackageName(_edtPackage.getText()); 548 gen.setImplPackageName(_edtImplPackage.getText()); 549 gen.setPrefix(_edtPrefix.getText()); 550 gen.setSystemPrefix(_edtSystemPrefix.getText()); 551 gen.setSuffix(_edtSuffix.getText()); 552 gen.setAbstract(false); 553 gen.setExtentSet(Arrays.asList(getExtents())); 554 } 555 556 private String getRootDirectory() 557 { 558 String dirName = _edtDirectory.getText(); 559 if (!dirName.endsWith("/") && !dirName.endsWith("\\")) dirName += "/"; 560 561 return dirName; 562 } 563 564 private void writeFile(String fileName, String code) throws Exception 565 { 566 BufferedWriter w = new BufferedWriter (new FileWriter (fileName)); 567 w.write(code); 568 w.close(); 569 } 570 571 private DjExtent[] getExtents() throws ObjectNotDefinedException 577 { 578 ArrayList all = new ArrayList (); 579 Object [] e = _lstExtents.getSelectedValues(); 580 for (int i = 0; i < e.length; i++) 581 { 582 all.add(_mgr.getExtent(((DjExtent) e[i]).getName())); 583 } 584 return (DjExtent[]) all.toArray(new DjExtent[0]); 585 } 586 587 public void createDirectory(String dir) 588 { 589 try 590 { 591 File fd; 592 if (dir.endsWith("/") || dir.endsWith("\\")) 593 { 594 fd = new File (dir.substring(0, dir.length() - 1)); 595 fd.mkdirs(); 596 } 597 else 598 { 599 fd = new File (dir); 600 fd.mkdirs(); 601 } 602 } 603 catch (Exception iox) 604 { 605 DjLogger.log("createDirectory " + dir + ": " + iox.getMessage()); 606 } 607 } 608 609 private String getOutputDir(Generator gen) 610 { 611 String pack = gen.getPackageName(); 612 DjStringReplacer sr = new DjStringReplacer(pack); 613 614 String result = getRootDirectory() + sr.replace(".", "/"); 615 createDirectory(result); 616 if (!result.endsWith("/") && !result.endsWith("\\")) result += File.separator; 617 return result; 618 } 619 620 public void logMessage(String msg) 621 { 622 _txtLog.append(msg); 623 } 624 625 private void process(Generator gen) throws Exception 626 { 627 process(gen, true); 628 } 629 630 public String fetchOrgCode(String code, int tagId) throws Exception 631 { 632 String startTag = getRegenerationStartTag(tagId); 633 634 int sidx = code.indexOf(startTag); 635 if (sidx == -1) return null; 636 637 int eidx = code.indexOf(CODE_END_TAG, sidx); 638 639 if ((sidx != -1 && eidx == -1) || (sidx == -1 && eidx != -1)) throw new Exception (Messages 640 .getString("StrongTyper.UnmatchedTags", String.valueOf(tagId))); 641 String result = code.substring(sidx + startTag.length(), eidx); 642 if (result.trim().length() == 0) return null; 643 return result; 644 } 645 646 private void process(Generator gen, boolean overwrite) throws Exception 647 { 648 process(gen, overwrite, true); 649 } 650 651 private void process(Generator gen, boolean overwrite, boolean callInit) throws Exception 652 { 653 if (callInit) initGenerator(gen); 654 655 String fileName = getOutputDir(gen) + gen.getClassName() + ".java"; 656 String genCode = gen.getCode(); 657 658 File ex = new File (fileName); 659 if (ex.exists()) 660 { 663 String oldCode = DjFileUtil.readFile(fileName); 664 665 if (!gen.isAbstract() && oldCode.toUpperCase().indexOf(GENERATE_SUPER.toUpperCase()) != -1) 666 { 667 gen.setAbstract(true); 668 669 process(gen, overwrite, false); 672 return; 673 } 674 675 if (!overwrite) 676 { 677 logMessage("\n> " 678 + Messages.getString("StrongTyper.NOTOverwritten", gen.getPackageName() + "." + gen.getClassName()) 679 + "\n\n"); 680 return; 681 } 682 683 for (int i = 0; i < 10; i++) 684 { 685 String snippet = fetchOrgCode(oldCode, i); 686 687 if (snippet != null) 688 { 689 int idx = genCode.indexOf(getRegenerationStartTag(i)); 690 if (idx == -1) throw new Exception (Messages.getString("StrongTyper.StartCodeTags", String.valueOf(i), 691 fileName)); 692 int eidx = genCode.indexOf(CODE_END_TAG, idx); 693 if (eidx == -1) throw new Exception (Messages.getString("StrongTyper.EndTagsNotFound", fileName)); 694 genCode = genCode.substring(0, idx) + getRegenerationStartTag(i) + snippet + genCode.substring(eidx); 695 logMessage(Messages.getString("StrongTyper.ManualCode", String.valueOf(i))); 696 } 697 } 698 } 699 700 writeFile(fileName, genCode); 701 logMessage(Messages.getString("StrongTyper.Generated", gen.getPackageName() + "." + gen.getClassName()) + "\n"); 702 } 703 704 void _butGenerate_actionPerformed(ActionEvent e) 705 { 706 try 707 { 708 _mgr.loadModel(); 712 713 _txtLog.setText(""); 714 if (_edtSystemPrefix.getText().trim().length() == 0) throw new Exception (Messages 715 .getString("StrongTyper.SystemPrefixReqd")); 716 logMessage(Messages.getString("StrongTyper.Writing2", _edtDirectory.getText()) + "\n"); 717 logMessage(Messages.getString("StrongTyper.UsingItfPackage", _edtPackage.getText()) + "\n"); 718 logMessage(Messages.getString("StrongTyper.UsingImplPakage", _edtImplPackage.getText()) + "\n"); 719 logMessage("------------------------------------------------------------\n"); 720 ObjectGenerator gen = new ObjectGenerator(_mgr); 721 InterfaceGenerator intfGen = new InterfaceGenerator(_mgr); 722 QbeGenerator qgen = new QbeGenerator(_mgr); 723 QbeInterfaceGenerator qintfGen = new QbeInterfaceGenerator(_mgr); 724 CursorGenerator cgen = new CursorGenerator(_mgr); 725 CursorInterfaceGenerator cintfGen = new CursorInterfaceGenerator(_mgr); 726 727 DjExtent[] extents = getExtents(); 728 729 for (int i = 0; i < extents.length; i++) 730 { 731 try 732 { 733 gen.setExtent(extents[i]); 734 process(gen); 735 736 intfGen.setExtent(extents[i]); 737 process(intfGen); 738 739 qgen.setExtent(extents[i]); 740 process(qgen); 741 742 qintfGen.setExtent(extents[i]); 743 process(qintfGen); 744 745 cgen.setExtent(extents[i]); 746 process(cgen); 747 748 cintfGen.setExtent(extents[i]); 749 process(cintfGen); 750 } 751 catch (Exception x) 752 { 753 logMessage("EXCEPTION: " + x.getMessage() + "\n"); 754 setStatusMessage(x); 755 } 756 } 757 758 if (_chkGenerateAll.isSelected()) 759 { 760 process(new OqlGenerator()); 761 process(new OqlInterfaceGenerator()); 762 process(new ManagerGenerator()); 763 process(new ExceptionGenerator()); 764 process(new NotFoundExceptionGenerator()); 765 process(new MultipleFoundExceptionGenerator()); 766 process(new ExceptionGenerator()); 767 process(new ParentObjectGenerator()); 768 process(new ParentInterfaceGenerator()); 769 process(new AbstractSessionGenerator(extents)); 770 process(new SessionInterfaceGenerator(extents, getManager().getDomains())); 771 process(new SessionGenerator(extents), false); 772 } 773 logMessage("\n" + Messages.getString("StrongTyper.Done")); 774 setStatusMessage(Messages.getString("StrongTyper.AllFilesGenerated")); 775 } 776 catch (Exception x) 777 { 778 logMessage("EXCEPTION: " + x.getMessage() + "\n"); 779 setStatusMessage(x); 780 } 781 _tabs.setSelectedComponent(_pnlLog); 782 } 783 784 public static void main(String [] args) 785 { 786 try 787 { 788 DjenericTool.setLookAndFeel(); 789 790 if (args.length < 1) 791 { 792 System.out 793 .println(Messages.getString("StrongTyper.Usage", StrongTyper.class.getName()) + " <repositories.xml>"); 794 return; 795 } 796 String banner = Messages.getString("global.Version", StrongTyper.class.getName(), DjVersion.getVersion()); 797 DjLogPrintStream.logAll(banner, LOG_FILE_NAME); 798 799 System.out.println(Messages.getString("global.Reading", args[0])); 800 new StrongTyper(args[0]).startApp(); 801 } 802 catch (Exception e) 803 { 804 DjLogger.log(e); 805 } 806 } 807 808 void updateSelection() 809 { 810 DjExtent[] extents = _mgr.getExtents(); 811 812 ArrayList selection = new ArrayList (); 813 814 StringTokenizer stok = new StringTokenizer (_edtSelection.getText(), ";, "); 815 while (stok.hasMoreTokens()) 816 { 817 String currentPackage = stok.nextToken(); 818 819 for (int i = 0; i < extents.length; i++) 820 { 821 DjExtent ext = extents[i]; 822 DjPackage curPack = ext.getPackage(); 823 if ((curPack == null && currentPackage.equals(_defaultPackage.getName())) 824 || (curPack != null && curPack.getName().equals(currentPackage)) || currentPackage.equals("*")) if (!selection 825 .contains(ext)) selection.add(ext); 826 } 827 } 828 829 Collections.sort(selection, new DjStringComparator(false)); 830 _model = new DefaultComboBoxModel (selection.toArray()); 831 _lstExtents.setModel(_model); 832 833 _lstExtents.getSelectionModel().setSelectionInterval(0, _model.getSize() - 1); 834 835 } 836 837 void butAdd_actionPerformed(ActionEvent e) 838 { 839 String packName = _cbbPackage.getSelectedItem().toString(); 840 String cur = _edtSelection.getText(); 841 842 if (packName.equals(Messages.getString("ExtentSelectorDialog.AllPackages"))) packName = "*"; 843 844 if (cur.trim().length() != 0) cur += "," + packName; 845 else cur = packName; 846 847 _edtSelection.setText(cur); 848 updateSelection(); 849 } 850 851 void _edtSelection_keyReleased(KeyEvent e) 852 { 853 updateSelection(); 854 } 855 856 } | Popular Tags |