1 30 package com.genimen.djeneric.tools.administrator.helpers; 31 32 import java.awt.BorderLayout ; 33 import java.awt.Dimension ; 34 import java.awt.Image ; 35 import java.awt.Toolkit ; 36 import java.awt.event.ActionEvent ; 37 import java.awt.event.MouseEvent ; 38 import java.io.BufferedReader ; 39 import java.io.BufferedWriter ; 40 import java.io.File ; 41 import java.io.FileNotFoundException ; 42 import java.io.FileReader ; 43 import java.io.FileWriter ; 44 import java.io.IOException ; 45 import java.net.URL ; 46 import java.sql.Connection ; 47 import java.sql.SQLException ; 48 import java.util.ArrayList ; 49 import java.util.StringTokenizer ; 50 51 import javax.swing.ImageIcon ; 52 import javax.swing.JButton ; 53 import javax.swing.JFileChooser ; 54 import javax.swing.JOptionPane ; 55 import javax.swing.JPanel ; 56 import javax.swing.JScrollPane ; 57 import javax.swing.JSplitPane ; 58 import javax.swing.event.TreeExpansionEvent ; 59 import javax.swing.event.TreeSelectionEvent ; 60 import javax.swing.tree.DefaultMutableTreeNode ; 61 import javax.swing.tree.DefaultTreeModel ; 62 import javax.swing.tree.ExpandVetoException ; 63 import javax.swing.tree.TreePath ; 64 65 import com.genimen.djeneric.language.Messages; 66 import com.genimen.djeneric.repository.DjExtent; 67 import com.genimen.djeneric.repository.DjModelView; 68 import com.genimen.djeneric.repository.DjPersistenceManager; 69 import com.genimen.djeneric.repository.DjPersistenceManagerFactory; 70 import com.genimen.djeneric.repository.DjRepositoryDescriptor; 71 import com.genimen.djeneric.repository.DjSession; 72 import com.genimen.djeneric.repository.exceptions.DjenericException; 73 import com.genimen.djeneric.repository.exceptions.LogonException; 74 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException; 75 import com.genimen.djeneric.repository.rdbms.RdbmsPersistenceManager; 76 import com.genimen.djeneric.repository.rdbms.SqlStatement; 77 import com.genimen.djeneric.tools.administrator.Administrator; 78 import com.genimen.djeneric.tools.administrator.editors.AdminEditor; 79 import com.genimen.djeneric.tools.administrator.nodes.AdministratorTree; 80 import com.genimen.djeneric.tools.administrator.nodes.AdministratorTreeCellRenderer; 81 import com.genimen.djeneric.tools.administrator.nodes.AdministratorTreeNode; 82 import com.genimen.djeneric.tools.administrator.nodes.ContextFolder; 83 import com.genimen.djeneric.tools.administrator.nodes.ContextNode; 84 import com.genimen.djeneric.tools.administrator.nodes.ModelViewFolder; 85 import com.genimen.djeneric.tools.administrator.nodes.ModelViewNode; 86 import com.genimen.djeneric.tools.administrator.nodes.RootNode; 87 import com.genimen.djeneric.tools.common.ExportSelectionDialog; 88 import com.genimen.djeneric.tools.export.Exporter; 89 import com.genimen.djeneric.tools.export.Importer; 90 import com.genimen.djeneric.tools.modeler.ModelEditor; 91 import com.genimen.djeneric.ui.DjCloseable; 92 import com.genimen.djeneric.ui.DjTabbedPane; 93 import com.genimen.djeneric.ui.DjVerticalFlowLayout; 94 import com.genimen.djeneric.ui.Util; 95 import com.genimen.djeneric.util.DjDefaultProgressDisplayer; 96 import com.genimen.djeneric.util.DjFileFilter; 97 import com.genimen.djeneric.util.DjFileUtil; 98 import com.genimen.djeneric.util.DjLogger; 99 import com.genimen.djeneric.util.DjProgressDisplayer; 100 import com.genimen.djeneric.util.DjStringReplacer; 101 102 public class AdminPanel extends JPanel implements DjCloseable, DjProgressDisplayer 103 { 104 private static final long serialVersionUID = 1L; 105 public static final String EXPORTLOCATION = "exportlocation"; 106 public static final String DUMPLOCATION = "dumplocation"; 107 108 private static final String SQLSERVER_DB = "sqlserver"; 109 private static final String HYPERSONIC_DB = "hypersonic"; 110 private static final String ORACLE_DB = "oracle"; 111 private static final String DERBY_DB = "derby"; 112 private static final String DB2_DB = "db2"; 113 private static final String POINTBASE_DB = "pointbase"; 114 private static final String UNKNOWN_DB = "unknown"; 115 public final static String OK_MSG = Messages.getString("global.Ok"); 116 117 BorderLayout borderLayout1 = new BorderLayout (); 118 JButton _butEdit = new JButton (); 119 JButton _butDelete = new JButton (); 120 JScrollPane jScrollPane1 = new JScrollPane (); 121 DjTabbedPane _tabs = new DjTabbedPane(); 122 JButton _butReload = new JButton (); 123 BorderLayout borderLayout3 = new BorderLayout (); 124 JSplitPane _pnlUsers = new JSplitPane (); 125 DjVerticalFlowLayout verticalFlowLayout1 = new DjVerticalFlowLayout(); 126 JButton _butCreate = new JButton (); 127 JPanel jPanel3 = new JPanel (); 128 JPanel jPanel2 = new JPanel (); 129 DjRepositoryDescriptor _repositoryDescriptor; 130 AdministratorTree _tree = new AdministratorTree(); 131 RootNode _root = new RootNode(); 132 DefaultTreeModel _treeModel = new DefaultTreeModel (_root); 133 AdministratorTreeCellRenderer _treeCellRendrer = new AdministratorTreeCellRenderer(); 134 135 DjPersistenceManager _mgr; 136 Administrator _admin; 137 JButton _butClearContext = new JButton (); 138 JButton _butExport = new JButton (); 139 JButton _butImport = new JButton (); 140 141 public AdminPanel(Administrator admin, DjRepositoryDescriptor repositoryDescriptor) throws DjenericException, 142 ClassNotFoundException , SQLException 143 { 144 _admin = admin; 145 _repositoryDescriptor = repositoryDescriptor; 146 147 DjPersistenceManagerFactory fact; 148 try 149 { 150 fact = new DjPersistenceManagerFactory(admin.getConfigFileName()); 151 fact.setMessenger(admin); 152 } 153 catch (FileNotFoundException e) 154 { 155 throw new DjenericException(e); 156 } 157 158 164 try 165 { 166 _mgr = fact.createInstance(repositoryDescriptor); 167 } 168 catch (Exception x) 169 { 170 Throwable rootException = x; 171 if (rootException instanceof DjenericException) 172 { 173 boolean lastXReached = false; 174 while (!lastXReached && !(rootException instanceof SQLException ) 175 && (rootException instanceof DjenericException)) 176 { 177 Throwable newRootException = ((DjenericException) rootException).getRootException(); 178 179 lastXReached = (newRootException == x) || newRootException == rootException; 180 rootException = newRootException; 181 } 182 } 183 184 boolean rethrow = true; 185 boolean retry = false; 186 if (x.getMessage().toLowerCase().indexOf("table not found") != -1) retry = true; 187 if (x.getMessage().toLowerCase().indexOf("table or view does not exist") != -1) retry = true; 188 else if (rootException instanceof SQLException ) 189 { 190 SQLException se = (SQLException ) rootException; 191 192 String sqlState = se.getSQLState(); 193 if (sqlState == null) sqlState = ""; 194 195 int vendorCode = se.getErrorCode(); 196 if (sqlState.startsWith("42") || "S0002".equals(sqlState) || "ZD004".equals(sqlState) || vendorCode == 15006 198 || vendorCode == 942) 199 { 200 retry = true; 201 } 202 } 203 if (retry) 204 { 205 if (createRepositoryTables(repositoryDescriptor)) 206 { 207 _mgr = fact.createInstance(repositoryDescriptor); 209 rethrow = false; 210 } 211 } 212 if (rethrow) throw new DjenericException(x); 213 } 214 215 jbInit(); 216 217 DjDefaultProgressDisplayer pd = new DjDefaultProgressDisplayer(true); 218 if (_mgr.bootstrapRepositoryIfNeeded(pd)) 219 { 220 setStatusMessage(pd.getLog(), true); 221 } 222 223 boolean importExportSupported = _mgr instanceof RdbmsPersistenceManager; 224 225 _butExport.setVisible(importExportSupported); 226 _butImport.setVisible(importExportSupported); 227 228 _butReload.setIcon(getImageIcon("reload.gif")); 229 _butEdit.setIcon(getImageIcon("edit.gif")); 230 _butCreate.setIcon(getImageIcon("new.gif")); 231 _butDelete.setIcon(getImageIcon("delete.gif")); 232 _butImport.setIcon(getImageIcon("import.gif")); 233 _butExport.setIcon(getImageIcon("export.gif")); 234 _butClearContext.setIcon(getImageIcon("clear.gif")); 235 _tree_valueChanged(null); 236 _tree.putClientProperty("JTree.lineStyle", "Angled"); 237 238 } 239 240 protected boolean createRepositoryTables(DjRepositoryDescriptor repositoryDescriptor) throws DjenericException, 241 ClassNotFoundException , SQLException 242 { 243 try 244 { 245 String dbType = UNKNOWN_DB; 246 boolean created = false; 247 248 if (repositoryDescriptor.getDriver().toLowerCase().indexOf("oracle") != -1) dbType = ORACLE_DB; 249 else if (repositoryDescriptor.getDriver().toLowerCase().indexOf("hsqldb") != -1) dbType = HYPERSONIC_DB; 250 else if (repositoryDescriptor.getDriver().toLowerCase().indexOf("sqlserver") != -1) dbType = SQLSERVER_DB; 251 else if (repositoryDescriptor.getDriver().toLowerCase().indexOf("pointbase") != -1) dbType = POINTBASE_DB; 252 else if (repositoryDescriptor.getDriver().toLowerCase().indexOf("derby") != -1) dbType = DERBY_DB; 253 else if (repositoryDescriptor.getUrl().toLowerCase().indexOf("derby") != -1) dbType = DERBY_DB; 254 else if (repositoryDescriptor.getDriver().toLowerCase().indexOf("db2") != -1) dbType = DB2_DB; 255 else if (repositoryDescriptor.getDriver().toLowerCase().indexOf("tds") != -1) dbType = SQLSERVER_DB; 256 257 if (dbType.equals(UNKNOWN_DB)) 258 { 259 JFileChooser fc = new JFileChooser (); 260 261 DjFileFilter filter = new DjFileFilter(new String []{"sql"}, "SQL Create script"); 262 fc.setFileFilter(filter); 263 264 fc.setDialogTitle(Messages.getString("AdminPanel.SelectCreateScript")); 265 String path = System.getProperties().getProperty("user.dir"); 266 fc.setCurrentDirectory(new File (path)); 267 fc.setSelectedFile(new File (path)); 268 fc.setFileSelectionMode(JFileChooser.FILES_ONLY); 269 270 if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) 271 { 272 String fileName = fc.getSelectedFile().getAbsolutePath(); 273 274 doRunCreateScript(repositoryDescriptor, DjFileUtil.readFile(fileName)); 275 276 created = true; 277 } 278 else 279 { 280 JOptionPane.showMessageDialog(_admin, Messages.getString("AdminPanel.EmptyDb"), Messages 281 .getString("AdminPanel.NoTables"), JOptionPane.WARNING_MESSAGE); 282 } 283 } 284 else 285 { 286 int result = JOptionPane.showOptionDialog(_admin, Messages.getString("AdminPanel.EmptyDbKnown"), Messages 287 .getString("AdminPanel.CreateTables"), JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, 288 new String []{Messages.getString("global.Create"), 289 Messages.getString("global.Cancel")}, null); 290 if (result == 0) 291 { 292 runCreateScript(repositoryDescriptor, dbType); 293 created = true; 294 } 295 } 296 return created; 297 } 298 catch (IOException iox) 299 { 300 throw new DjenericException(iox); 301 } 302 } 303 304 private void runCreateScript(DjRepositoryDescriptor repositoryDescriptor, String dbType) throws IOException , 305 DjenericException, ClassNotFoundException , SQLException 306 { 307 String script = null; 308 309 if (dbType.equals(ORACLE_DB)) script = DjFileUtil.readResource(Administrator.class, "schemas/createoracle.sql"); 310 else if (dbType.equals(HYPERSONIC_DB)) script = DjFileUtil.readResource(Administrator.class, 311 "schemas/createhypersonic.sql"); 312 else if (dbType.equals(SQLSERVER_DB)) script = DjFileUtil.readResource(Administrator.class, 313 "schemas/createsqlserver.sql"); 314 else if (dbType.equals(POINTBASE_DB)) script = DjFileUtil.readResource(Administrator.class, 315 "schemas/createpointbase.sql"); 316 else if (dbType.equals(DB2_DB)) script = DjFileUtil.readResource(Administrator.class, "schemas/createdb2.sql"); 317 else if (dbType.equals(DERBY_DB)) script = DjFileUtil.readResource(Administrator.class, "schemas/createderby.sql"); 318 else throw new DjenericException(Messages.getString("AdminPanel.UnknownDb", dbType)); 319 320 doRunCreateScript(repositoryDescriptor, script); 321 } 322 323 private void doRunCreateScript(DjRepositoryDescriptor repositoryDescriptor, String script) throws LogonException, 324 DjenericException, ClassNotFoundException , SQLException 325 { 326 setStatusMessage("Create " + repositoryDescriptor, true); 327 DjStringReplacer sr = new DjStringReplacer(script); 328 sr.replace("\nGO", ";"); 329 script = sr.toString(); 330 Connection conn = getConnection(repositoryDescriptor); 331 try 332 { 333 StringTokenizer stok = new StringTokenizer (script, ";"); 334 while (stok.hasMoreElements()) 335 { 336 String sql = stok.nextToken().trim(); 337 if (sql.length() > 0) 338 { 339 SqlStatement stmt = new SqlStatement(conn, sql); 340 stmt.executeUpdate(); 341 } 342 } 343 344 conn.commit(); 346 } 347 finally 348 { 349 conn.close(); 350 } 351 JOptionPane.showMessageDialog(_admin, Messages.getString("AdminPanel.RepositoryCreated"), Messages 352 .getString("AdminPanel.RepositoryCreatedTitle"), JOptionPane.INFORMATION_MESSAGE); 353 } 354 355 public Connection getConnection(DjRepositoryDescriptor repositoryDescriptor) throws LogonException, 356 DjenericException, ClassNotFoundException , SQLException 357 { 358 return _admin.getConnection(repositoryDescriptor); 359 } 360 361 public boolean canClose() 362 { 363 if (_tabs.getComponentCount() == 0) return true; 364 365 int result = JOptionPane.showOptionDialog(_admin, Messages.getString("AdminPanel.CloseDiscard"), Messages 366 .getString("AdminPanel.CloseDiscardTitle"), JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, 367 new String []{Messages.getString("AdminPanel.Close"), 368 Messages.getString("global.Cancel")}, null); 369 return result == 0; 370 } 371 372 public boolean close() 373 { 374 _mgr.close(); 375 _admin.removeTab(this); 376 return true; 377 } 378 379 void jbInit() 380 { 381 _butDelete.setPreferredSize(new Dimension (24, 24)); 382 _butDelete.setToolTipText(Messages.getString("global.DeleteCtrl")); 383 _butDelete.addActionListener(new java.awt.event.ActionListener () 384 { 385 public void actionPerformed(ActionEvent e) 386 { 387 _butDelete_actionPerformed(e); 388 } 389 }); 390 _butEdit.setPreferredSize(new Dimension (24, 24)); 391 _butEdit.setToolTipText(Messages.getString("global.Edit")); 392 _butEdit.addActionListener(new java.awt.event.ActionListener () 393 { 394 public void actionPerformed(ActionEvent e) 395 { 396 _butEdit_actionPerformed(e); 397 } 398 }); 399 this.setLayout(borderLayout1); 400 _butReload.setPreferredSize(new Dimension (24, 24)); 401 _butReload.setToolTipText(Messages.getString("AdminPanel.ReloadSelectedNode")); 402 _butReload.addActionListener(new java.awt.event.ActionListener () 403 { 404 public void actionPerformed(ActionEvent e) 405 { 406 _butReload_actionPerformed(e); 407 } 408 }); 409 _butCreate.setPreferredSize(new Dimension (24, 24)); 410 _butCreate.setToolTipText(Messages.getString("AdminPanel.CreateNew")); 411 _butCreate.addActionListener(new java.awt.event.ActionListener () 412 { 413 public void actionPerformed(ActionEvent e) 414 { 415 _butCreate_actionPerformed(e); 416 } 417 }); 418 jPanel3.setLayout(verticalFlowLayout1); 419 jPanel2.setLayout(borderLayout3); 420 _pnlUsers.setDividerLocation(220); 421 _tree.addTreeWillExpandListener(new javax.swing.event.TreeWillExpandListener () 422 { 423 public void treeWillExpand(TreeExpansionEvent e) throws ExpandVetoException 424 { 425 _tree_treeWillExpand(e); 426 } 427 428 public void treeWillCollapse(TreeExpansionEvent e) 429 { 430 } 431 }); 432 _tree.addMouseListener(new java.awt.event.MouseAdapter () 433 { 434 public void mouseClicked(MouseEvent e) 435 { 436 _tree_mouseClicked(e); 437 } 438 }); 439 _tree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener () 440 { 441 public void valueChanged(TreeSelectionEvent e) 442 { 443 _tree_valueChanged(e); 444 } 445 }); 446 _butExport.addActionListener(new java.awt.event.ActionListener () 447 { 448 public void actionPerformed(ActionEvent e) 449 { 450 _butExport_actionPerformed(e); 451 } 452 }); 453 _butExport.setToolTipText(Messages.getString("AdminPanel.ExportContext")); 454 _butExport.setPreferredSize(new Dimension (24, 24)); 455 456 _butClearContext.setToolTipText(Messages.getString("AdminPanel.ClearContext")); 457 _butClearContext.setPreferredSize(new Dimension (24, 24)); 458 _butClearContext.addActionListener(new java.awt.event.ActionListener () 459 { 460 public void actionPerformed(ActionEvent e) 461 { 462 _butClearContext_actionPerformed(e); 463 } 464 }); 465 466 _butImport.setPreferredSize(new Dimension (24, 24)); 467 _butImport.setToolTipText(Messages.getString("global.ImportContext")); 468 _butImport.addActionListener(new java.awt.event.ActionListener () 469 { 470 public void actionPerformed(ActionEvent e) 471 { 472 _butImport_actionPerformed(e); 473 } 474 }); 475 _pnlUsers.setOrientation(JSplitPane.VERTICAL_SPLIT); 476 _pnlUsers.add(_tabs, JSplitPane.RIGHT); 477 _pnlUsers.add(jPanel2, JSplitPane.LEFT); 478 jPanel2.add(jScrollPane1, BorderLayout.CENTER); 479 jScrollPane1.getViewport().add(_tree, null); 480 jPanel2.add(jPanel3, BorderLayout.WEST); 481 jPanel3.add(_butReload, null); 482 jPanel3.add(_butEdit, null); 483 jPanel3.add(_butCreate, null); 484 jPanel3.add(_butClearContext, null); 485 jPanel3.add(_butDelete, null); 486 add(_pnlUsers, BorderLayout.CENTER); 487 jPanel3.add(_butExport, null); 488 jPanel3.add(_butImport, null); 489 490 _tree.setRootVisible(false); 491 _tree.setCellRenderer(_treeCellRendrer); 492 _tree.setModel(_treeModel); 493 _tree.setShowsRootHandles(true); 494 _tree.setAutoscrolls(true); 495 _tree.setDoubleBuffered(true); 496 _tree.setToggleClickCount(0); 497 498 _tree.setManager(_mgr); 499 500 _root.setTree(_tree); 501 _root.insertAsChild(new RootNode()); 502 _tree.expandPath(new TreePath (_root.getPath())); 503 504 } 505 506 509 protected void _butClearContext_actionPerformed(ActionEvent e) 510 { 511 if (!(_mgr instanceof RdbmsPersistenceManager)) 512 { 513 setStatusMessage(Messages.getString("AdminPanel.OnlyRdbms"), false); 514 } 515 AdministratorTreeNode[] sels = getSelectedNodes(); 516 if (sels[0] instanceof ContextNode) 517 { 518 ContextNode cn = (ContextNode) sels[0]; 519 try 520 { 521 int result = JOptionPane.showOptionDialog(_admin, Messages.getString("AdminPanel.ClearContextAssert", cn 522 .toString()), Messages.getString("AdminPanel.ClearContextAssertTitle", cn.toString()), 523 JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, 524 new String []{Messages.getString("AdminPanel.Clear"), 525 Messages.getString("global.Cancel")}, null); 526 if (result == 0) 527 { 528 cn.clearContext(); 529 setStatusMessage(Messages.getString("AdminPanel.Cleared", cn.toString()), true); 530 } 531 532 } 533 catch (Exception x) 534 { 535 setStatusMessage(x); 536 } 537 } 538 539 } 540 541 public static ImageIcon getImageIcon(String fileName) 542 { 543 URL url = Administrator.class.getResource("images/" + fileName); 544 if (url != null) return new ImageIcon (url); 545 else return new ImageIcon (""); 546 } 548 549 public static Image getImage(String fileName) 550 { 551 URL url = Administrator.class.getResource("images/" + fileName); 552 if (url != null) return Toolkit.getDefaultToolkit().getImage(url); 553 554 else return Toolkit.getDefaultToolkit().getImage(""); 555 } 556 557 void _tree_treeWillExpand(TreeExpansionEvent e) throws ExpandVetoException 558 { 559 try 560 { 561 if (e.getPath().getLastPathComponent() instanceof AdministratorTreeNode) 562 { 563 AdministratorTreeNode node = (AdministratorTreeNode) e.getPath().getLastPathComponent(); 564 node.expandNode(); 565 } 566 _admin.setStatusMessage(OK_MSG); 567 } 568 catch (Exception x) 569 { 570 DjLogger.log(x); 571 throw new ExpandVetoException (new TreeExpansionEvent (this, e.getPath()), x.getMessage()); 572 } 573 } 574 575 public AdministratorTreeNode[] getSelectedNodes() 576 { 577 if (_tree.getSelectionPath() == null) return new AdministratorTreeNode[0]; 578 ArrayList result = new ArrayList (10); 579 580 TreePath [] tp = _tree.getSelectionPaths(); 581 582 for (int qq = 0; qq < tp.length; qq++) 583 { 584 DefaultMutableTreeNode node = (DefaultMutableTreeNode ) (tp[qq].getLastPathComponent()); 585 if (node instanceof AdministratorTreeNode) result.add(node); 586 } 587 return (AdministratorTreeNode[]) result.toArray(new AdministratorTreeNode[0]); 588 } 589 590 void _butEdit_actionPerformed(ActionEvent e) 591 { 592 AdministratorTreeNode[] sels = getSelectedNodes(); 593 594 for (int i = 0; i < sels.length; i++) 595 { 596 AdminEditor editor = sels[i].getEditor(this); 597 if (editor != null) addToTabs(editor, sels[i].toString(), sels[i].getImageIcon()); 598 } 599 } 600 601 public void closeEditor(JPanel editor) 602 { 603 for (int i = 0; i < _tabs.getComponentCount(); i++) 604 { 605 if (_tabs.getComponentAt(i) == editor) 606 { 607 _tabs.remove(editor); 608 return; 609 } 610 } 611 setStatusMessage(Messages.getString("global.Ok"), true); 612 } 613 614 public void addToTabs(AdminEditor editor, String title, ImageIcon icon) 615 { 616 String id = editor.getID(); 617 618 if (id != null) 620 { 621 for (int i = 0; i < _tabs.getComponentCount(); i++) 622 { 623 if (_tabs.getComponent(i) instanceof AdminEditor) 624 { 625 AdminEditor e = (AdminEditor) _tabs.getComponent(i); 626 if (e.getID() != null) 627 { 628 if (e.getID().equals(id)) 629 { 630 _tabs.setSelectedComponent(e); 631 return; 632 } 633 } 634 } 635 } 636 } 637 638 _tabs.addTab(title, icon, editor); 639 _tabs.setSelectedComponent(editor); 640 editor.requestFocus(); 641 } 642 643 public void setStatusMessage(String msg, boolean isInformative) 644 { 645 _admin.setStatusMessageNow(msg, isInformative); 646 } 647 648 public void setStatusMessage(Throwable t) 649 { 650 _admin.setStatusMessage(t); 651 } 652 653 public DjPersistenceManager getManager() 654 { 655 return _mgr; 656 } 657 658 void _butCreate_actionPerformed(ActionEvent e) 659 { 660 try 661 { 662 AdministratorTreeNode[] sels = getSelectedNodes(); 663 664 for (int i = 0; i < sels.length; i++) 665 { 666 AdminEditor editor = sels[i].getCreateEditor(this); 667 if (editor != null) addToTabs(editor, Messages.getString("AdminPanel.New") + " " + sels[i].getNodeType(), 668 sels[i].getObjectImageIcon()); 669 } 670 } 671 catch (Exception x) 672 { 673 setStatusMessage(x.getMessage(), false); 674 } 675 } 676 677 void _butDelete_actionPerformed(ActionEvent e) 678 { 679 AdministratorTreeNode[] sels = getSelectedNodes(); 680 try 681 { 682 boolean dontask = false; 683 for (int i = 0; i < sels.length; i++) 684 { 685 if (!dontask) 686 { 687 int result = JOptionPane.showOptionDialog(_admin, Messages.getString("AdminPanel.SureToDelete", sels[i] 688 .toString()), Messages.getString("AdminPanel.SureToDeleteTitle", sels[i].toString()), 689 JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, 690 new String []{Messages.getString("global.Yes"), 691 Messages.getString("global.Yes2All"), 692 Messages.getString("global.Cancel")}, null); 693 if ((result != 0) && (result != 1)) return; 694 if (result == 1) dontask = true; 696 } 697 sels[i].delete(); 698 } 699 setStatusMessage(OK_MSG, true); 700 } 701 catch (Exception x) 702 { 703 setStatusMessage(x); 704 } 705 } 706 707 void _butReload_actionPerformed(ActionEvent e) 708 { 709 AdministratorTreeNode[] sels = getSelectedNodes(); 710 711 try 712 { 713 for (int i = 0; i < sels.length; i++) 714 { 715 sels[i].reload(); 716 } 717 } 718 catch (Exception x) 719 { 720 setStatusMessage(x); 721 } 722 } 723 724 void _tree_mouseClicked(MouseEvent e) 725 { 726 if (e.getClickCount() > 1) _butEdit_actionPerformed(null); 727 } 728 729 public DjPersistenceManager getPersistenceManager() 730 { 731 return _mgr; 732 } 733 734 void _tree_valueChanged(TreeSelectionEvent e) 735 { 736 boolean canEdit = false; 737 boolean canDelete = false; 738 boolean canCreate = false; 739 boolean canImport = false; 740 boolean canExport = false; 741 boolean canClear = false; 742 743 AdministratorTreeNode[] sels = getSelectedNodes(); 744 745 for (int i = 0; i < sels.length; i++) 746 { 747 canEdit |= sels[i].canEdit(); 748 canDelete |= sels[i].canDelete(); 749 canCreate |= sels[i].canCreate(); 750 canImport = sels[0].canImport(); 751 canExport = sels[0].canExport(); 752 canClear = sels[0].canClear(); 753 } 754 _butImport.setEnabled(canImport); 755 _butExport.setEnabled(canExport); 756 _butCreate.setEnabled(canCreate); 757 _butDelete.setEnabled(canDelete); 758 _butEdit.setEnabled(canEdit); 759 _butClearContext.setEnabled(canClear); 760 } 761 762 private void exportContext(AdministratorTreeNode[] sels) 763 { 764 ContextNode cn = (ContextNode) sels[0]; 765 JFileChooser fc = new JFileChooser (); 766 767 DjFileFilter filter = new DjFileFilter(new String []{ModelEditor.DUMP_EXTENSION}, "Dump"); 768 fc.setFileFilter(filter); 769 770 fc.setDialogTitle(Messages.getString("AdminPanel.Export2File")); 771 String path = _admin.getProperty(DUMPLOCATION, System.getProperties().getProperty("user.dir")) + "/" 772 + cn.getContext().getCode() + ".dmp"; 773 fc.setCurrentDirectory(new File (path)); 774 fc.setSelectedFile(new File (path)); 775 fc.setFileSelectionMode(JFileChooser.FILES_ONLY); 776 777 if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) 778 { 779 try 780 { 781 String fileName = fc.getSelectedFile().getAbsolutePath(); 782 783 if (!fileName.endsWith(ModelEditor.DUMP_EXTENSION)) fileName += "." + ModelEditor.DUMP_EXTENSION; 784 785 _admin.setProperty(DUMPLOCATION, fc.getSelectedFile().getParent()); 786 787 File file = new File (fileName); 788 boolean skip = false; 789 790 if (file.exists()) 791 { 792 skip = !Util.isOkToOverwrite(this, file); 793 } 794 795 if (!skip) 796 { 797 ExportSelectionDialog esd = new ExportSelectionDialog(Util.findFrame(this), _mgr.getExtents(), false, true); 798 if (!esd.wasCanceled() && esd.getSelectedExtents().length > 0) 799 { 800 DjExtent[] unsels = esd.getUnSelectedExtents(); 801 Exporter exporter = new Exporter(this, (RdbmsPersistenceManager) _mgr, cn.getContext().getCode()); 802 for (int i = 0; i < unsels.length; i++) 803 exporter.ignoreExtent(unsels[i].getName()); 804 long counter = exporter.exportDump(file); 805 setStatusMessage(Messages.getString("AdminPanel.Exported", String.valueOf(counter)), true); 806 } 807 else 808 { 809 setStatusMessage(Messages.getString("AdminPanel.ExportCancelled"), true); 810 } 811 } 812 } 813 catch (Exception x) 814 { 815 setStatusMessage(x); 816 } 817 } 818 } 819 820 boolean isOkToMerge(String contextName) 821 { 822 int result = JOptionPane.showOptionDialog(_admin, Messages.getString("AdminPanel.IsMergeOk", contextName), Messages 823 .getString("AdminPanel.IsMergeOkTitle"), JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, 824 new String []{Messages.getString("AdminPanel.Merge"), 825 Messages.getString("global.Cancel")}, null); 826 return result == 0; 827 } 828 829 void importContext(ContextNode cn) 830 { 831 ImportContextDialog icd; 832 try 833 { 834 if (!isOkToMerge(cn.getContext().getCode())) return; 835 836 icd = new ImportContextDialog(_admin, _mgr, cn.getContext().getCode()); 837 if (!icd.wasCancelled()) 838 { 839 841 Importer importer = new Importer(this, (RdbmsPersistenceManager) _mgr); 842 importer.setContext(cn.getContext().getCode()); 843 long count = importer.importDump(new File (icd.getFileName())); 844 setStatusMessage(Messages.getString("AdminPanel.Imported", String.valueOf(count)), true); 845 } 846 } 847 catch (Exception x) 848 { 849 setStatusMessage(x); 850 } 851 } 852 853 void importContext(ContextFolder cf) 854 { 855 try 856 { 857 ImportContextDialog icd = new ImportContextDialog(_admin, _mgr, null); 858 if (!icd.wasCancelled()) 859 { 860 DjDefaultProgressDisplayer pd = new DjDefaultProgressDisplayer(); 861 862 Importer importer = new Importer(pd, (RdbmsPersistenceManager) _mgr); 863 try 864 { 865 importer.setContext(icd.getContextName()); 866 if (!isOkToMerge(icd.getContextName())) return; 869 } 870 catch (ObjectNotDefinedException onde) 871 { 872 importer.createAndSetContext(pd, icd.getContextName()); 873 } 874 long count = importer.importDump(new File (icd.getFileName())); 875 cf.reload(); 876 setStatusMessage(Messages.getString("AdminPanel.Imported", String.valueOf(count)), true); 877 } 878 } 879 catch (Exception x) 880 { 881 setStatusMessage(x); 882 } 883 } 884 885 void _butImport_actionPerformed(ActionEvent e) 886 { 887 if (!(_mgr instanceof RdbmsPersistenceManager)) 888 { 889 setStatusMessage(Messages.getString("AdminPanel.OnlyRdbms"), false); 890 } 891 AdministratorTreeNode[] sels = getSelectedNodes(); 892 if (sels[0] instanceof ContextNode) 893 { 894 importContext((ContextNode) sels[0]); 895 896 } 897 else if (sels[0] instanceof ContextFolder) 898 { 899 importContext((ContextFolder) sels[0]); 900 } 901 else if (sels[0] instanceof ModelViewFolder) 902 { 903 importView((ModelViewFolder) sels[0]); 904 } 905 else if (sels[0] instanceof ModelViewNode) 906 { 907 importView((ModelViewFolder) sels[0].getParentTreeNode()); 908 } 909 } 910 911 private void importView(ModelViewFolder folder) 912 { 913 try 914 { 915 JFileChooser fc = new JFileChooser (); 916 917 String path = _admin.getProperty(EXPORTLOCATION, System.getProperties().getProperty("user.dir")); 918 fc.setCurrentDirectory(new File (path)); 919 920 DjFileFilter filter = new DjFileFilter(new String []{ModelEditor.VIEW_EXTENSION}, Messages 921 .getString("ModelEditor.DjenericView")); 922 fc.setFileFilter(filter); 923 fc.setDialogTitle(Messages.getString("ModelEditor.ImportView")); 924 fc.setFileSelectionMode(JFileChooser.FILES_ONLY); 925 fc.setMultiSelectionEnabled(true); 926 927 if (fc.showOpenDialog(this) != JFileChooser.CANCEL_OPTION) 928 { 929 File [] allFiles = fc.getSelectedFiles(); 930 931 for (int i = 0; i < allFiles.length; i++) 932 { 933 if (i == 0) _admin.setProperty(EXPORTLOCATION, allFiles[0].getAbsolutePath()); 934 935 String fileName = allFiles[i].getAbsolutePath(); 936 File inFile = new File (fileName); 937 BufferedReader br = new BufferedReader (new FileReader (inFile)); 938 StringBuffer src = new StringBuffer (10000); 939 String ln; 940 while ((ln = br.readLine()) != null) 941 { 942 src.append(ln); 943 src.append("\n"); 944 } 945 br.close(); 946 947 DjModelView newView = _mgr.createModelView(); 948 String viewName = allFiles[i].getName(); 949 if (viewName.indexOf(".") > 0) viewName = viewName.substring(0, viewName.indexOf(".")); 950 951 newView.setCode(viewName); 952 newView.setDefinition(src.toString()); 953 DjSession session = _mgr.createSession(); 954 try 955 { 956 newView.persist(session); 957 session.commit(); 958 } 959 finally 960 { 961 session.close(); 962 } 963 } 964 folder.reload(); 965 } 966 } 967 catch (Exception x) 968 { 969 setStatusMessage(x); 970 } 971 972 } 973 974 void _butExport_actionPerformed(ActionEvent e) 975 { 976 AdministratorTreeNode[] sels = getSelectedNodes(); 977 if (sels[0] instanceof ContextNode) 978 { 979 if (!(_mgr instanceof RdbmsPersistenceManager)) 980 { 981 setStatusMessage(Messages.getString("AdminPanel.OnlyRdbms"), false); 982 } 983 exportContext(sels); 984 } 985 if (sels[0] instanceof ModelViewFolder) 986 { 987 exportViews((ModelViewFolder) sels[0]); 988 } 989 if (sels[0] instanceof ModelViewNode) 990 { 991 exportView((ModelViewNode) sels[0]); 992 } 993 } 994 995 private void exportViews(ModelViewFolder folder) 996 { 997 998 try 999 { 1000 folder.reload(); 1001 1002 JFileChooser fc = new JFileChooser (); 1003 fc.setDialogTitle(Messages.getString("AdminPanel.Export2File")); 1004 DjFileFilter filter = new DjFileFilter(new String []{ModelEditor.VIEW_EXTENSION}, Messages 1005 .getString("ModelEditor.DjenericView")); 1006 fc.setFileFilter(filter); 1007 1008 String path = _admin.getProperty(EXPORTLOCATION, System.getProperties().getProperty("user.dir")); 1009 1010 fc.setCurrentDirectory(new File (path)); 1011 fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 1012 1013 if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) 1014 { 1015 _admin.setProperty(EXPORTLOCATION, fc.getSelectedFile().getParent()); 1016 1017 int count = 0; 1018 for (int i = 0; i < folder.getChildCount(); i++) 1019 { 1020 if (!(folder.getChildAt(i) instanceof ModelViewNode)) continue; 1021 1022 ModelViewNode node = (ModelViewNode) folder.getChildAt(i); 1023 1024 String fileName = fc.getSelectedFile().getAbsolutePath() + "/" + node.getModelView().getCode() + "." 1025 + ModelEditor.VIEW_EXTENSION; 1026 1027 count += exportViewNode(fileName, node); 1028 } 1029 setStatusMessage(Messages.getString("AdminPanel.MultipleViewsExported", String.valueOf(count), fc 1030 .getSelectedFile().getAbsolutePath()), true); 1031 } 1032 } 1033 catch (Exception x) 1034 { 1035 setStatusMessage(x); 1036 } 1037 } 1038 1039 private void exportView(ModelViewNode node) 1040 { 1041 JFileChooser fc = new JFileChooser (); 1042 fc.setDialogTitle(Messages.getString("AdminPanel.Export2File")); 1043 DjFileFilter filter = new DjFileFilter(new String []{ModelEditor.VIEW_EXTENSION}, Messages 1044 .getString("ModelEditor.DjenericView")); 1045 fc.setFileFilter(filter); 1046 1047 String path = _admin.getProperty(EXPORTLOCATION, System.getProperties().getProperty("user.dir")); 1048 String filename = path + "/" + node.getModelView().getCode() + "." + ModelEditor.VIEW_EXTENSION; 1049 1050 fc.setCurrentDirectory(new File (path)); 1051 fc.setSelectedFile(new File (filename)); 1052 fc.setFileSelectionMode(JFileChooser.FILES_ONLY); 1053 1054 if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) 1055 { 1056 try 1057 { 1058 String fileName = fc.getSelectedFile().getAbsolutePath(); 1059 if (!fileName.endsWith(ModelEditor.VIEW_EXTENSION)) fileName += "." + ModelEditor.VIEW_EXTENSION; 1060 _admin.setProperty(EXPORTLOCATION, fc.getSelectedFile().getParent()); 1061 1062 exportViewNode(fileName, node); 1063 } 1064 catch (Exception x) 1065 { 1066 setStatusMessage(x); 1067 } 1068 } 1069 } 1070 1071 private int exportViewNode(String fileName, ModelViewNode node) throws IOException 1072 { 1073 File file = new File (fileName); 1074 boolean skip = false; 1075 int result = 0; 1076 1077 if (file.exists()) 1078 { 1079 skip = !Util.isOkToOverwrite(this, file); 1080 } 1081 1082 if (!skip) 1083 { 1084 DjModelView vw = node.getModelView(); 1085 BufferedWriter w = new BufferedWriter (new FileWriter (file)); 1086 w.write(vw.getDefinition()); 1087 w.close(); 1088 result = 1; 1089 setStatusMessage(Messages.getString("AdminPanel.ViewsExported", fileName), true); 1090 } 1091 return result; 1092 } 1093 1094 public void displayProgress(String msg) 1095 { 1096 setStatusMessage(msg, true); 1097 } 1098} | Popular Tags |