1 56 57 package org.objectstyle.cayenne.dataview.dvmodeler; 58 59 import java.awt.CardLayout ; 60 import java.awt.event.ActionEvent ; 61 import java.awt.event.ActionListener ; 62 import java.awt.event.WindowAdapter ; 63 import java.awt.event.WindowEvent ; 64 import java.awt.event.WindowListener ; 65 import java.beans.PropertyChangeEvent ; 66 import java.beans.PropertyChangeListener ; 67 import java.io.File ; 68 import java.io.IOException ; 69 import java.util.ArrayList ; 70 import java.util.Iterator ; 71 import java.util.Map ; 72 73 import javax.swing.AbstractAction ; 74 import javax.swing.Action ; 75 import javax.swing.ImageIcon ; 76 import javax.swing.JFileChooser ; 77 import javax.swing.JOptionPane ; 78 import javax.swing.JPanel ; 79 import javax.swing.JTabbedPane ; 80 import javax.swing.JTable ; 81 import javax.swing.JTree ; 82 import javax.swing.KeyStroke ; 83 import javax.swing.event.ChangeEvent ; 84 import javax.swing.event.ChangeListener ; 85 import javax.swing.event.TreeSelectionEvent ; 86 import javax.swing.event.TreeSelectionListener ; 87 import javax.swing.tree.TreePath ; 88 89 90 91 97 98 99 class DVModelerFrameHelper { 100 private DVModelerFrame dvModelerFrame; 101 private CayenneProject cayenneProject; 102 103 104 private Action saveAction = new SaveAction(); 105 private Action openAction = new OpenAction(); 106 private Action addNewObjEntityViewAction = new AddNewObjEntityViewAction(); 107 private Action addNewFieldAction = new AddNewFieldAction(); 108 private Action addNewDataViewAction = new AddNewDataViewAction(); 109 private Action deleteAction = new DeleteAction(); 110 111 private JFileChooser openProjectFileChooser = new JFileChooser (); 112 113 private PropertyChangeMediator propertyChangeMediator = 114 new PropertyChangeMediator(); 115 116 private ErrorsDialog loadErrorsDialog; 117 private ErrorsDialog saveErrorsDialog; 118 119 public DVModelerFrameHelper(DVModelerFrame dvModelerFrame) { 120 this.dvModelerFrame = dvModelerFrame; 121 configureFileChoosers(); 122 } 123 124 public Action getSaveAction(){ 125 return saveAction; 126 } 127 128 public Action getOpenAction(){ 129 return openAction; 130 } 131 132 public Action getAddNewObjEntityViewAction(){ 133 return addNewObjEntityViewAction; 134 } 135 136 public Action getAddNewFieldAction(){ 137 return addNewFieldAction; 138 } 139 140 public Action getAddNewDataViewAction(){ 141 return addNewDataViewAction; 142 } 143 public Action getDeleteAction(){ 144 return deleteAction; 145 } 146 147 148 public WindowListener getDVModelerFrameWindowListener(){ 149 return new WindowAdapter () { 150 public void windowClosing(WindowEvent e) { 151 exitAction(e); 152 } 153 }; 154 } 155 156 public ActionListener getMenuItemExitActionListener(){ 157 return new ActionListener (){ 158 public void actionPerformed(ActionEvent e) { 159 exitAction(null); 160 } 161 }; 162 } 163 164 private void exitAction(WindowEvent e){ 165 if (cayenneProject != null){ 166 int returnVal = JOptionPane.showConfirmDialog( 167 dvModelerFrame, 168 "Save current project before exit?", 169 "DVModeler :: Exit Confirmation", 170 JOptionPane.YES_NO_CANCEL_OPTION, 171 JOptionPane.QUESTION_MESSAGE); 172 if (returnVal == JOptionPane.YES_OPTION){ 173 int selectedValue = saveCayenneProjectFileAction(null, SaveErrorsDialog.EXIT_DIALOG); 174 if ((selectedValue == -1)||(selectedValue == SaveErrorsDialog.EXIT_WITHOUT_SAVING)){ 175 System.exit(0); 176 } 177 } else if (returnVal == JOptionPane.NO_OPTION){ 178 System.exit(0); 179 } 180 } else { 181 System.exit(0); 182 } 183 } 184 185 public ActionListener getMenuItemCloseActionListener(){ 186 return new ActionListener (){ 187 public void actionPerformed(ActionEvent e){ 188 closeAction(e); 189 } 190 }; 191 } 192 193 private void closeAction(ActionEvent e){ 194 if (cayenneProject != null){ 195 int returnVal = JOptionPane.showConfirmDialog( 196 dvModelerFrame, 197 "Save current project before closing?", 198 "DVModeler :: Close Confirmation", 199 JOptionPane.YES_NO_CANCEL_OPTION, 200 JOptionPane.QUESTION_MESSAGE); 201 if (returnVal == JOptionPane.YES_OPTION){ 202 int selectedValue = saveCayenneProjectFileAction(null, SaveErrorsDialog.CLOSE_DIALOG); 203 if ((selectedValue == -1) 204 || (selectedValue == SaveErrorsDialog.CLOSE_WITHOUT_SAVING)){ 205 closeCayenneProject(); 206 } 207 208 } else if (returnVal == JOptionPane.NO_OPTION){ 209 closeCayenneProject(); 210 } 211 } 212 } 213 214 private void closeCayenneProject(){ 215 JTree dataMapTree = dvModelerFrame.getDataMapTree(); 216 JTree dataViewTree = dvModelerFrame.getDataViewTree(); 217 cayenneProject = null; 218 DataMapTreeModel dataMapTreeModel = 219 (DataMapTreeModel)dataMapTree.getModel(); 220 dataMapTreeModel.setDataMaps(null); 221 222 DataViewTreeModel dataViewTreeModel = 223 (DataViewTreeModel)dataViewTree.getModel(); 224 dataViewTreeModel.setDataViews(null); 225 226 dvModelerFrame.getTopCardPanel().showPanel(null); 227 showBottomEmptyPanel(); 228 229 addNewDataViewAction.setEnabled(false); 230 saveAction.setEnabled(false); 231 dvModelerFrame.getMenuItemClose().setEnabled(false); 232 dvModelerFrame.getMenuItemLoadErrors().setEnabled(false); 233 dvModelerFrame.getMenuItemSaveErrors().setEnabled(false); 234 } 235 236 237 public ChangeListener getDataTabbedPaneChangeListener(){ 238 return new ChangeListener () { 239 public void stateChanged(ChangeEvent e) { 240 browserTabChanged(); 241 } 242 }; 243 } 244 245 public void browserTabChanged() { 246 JTabbedPane dataTabbedPane = dvModelerFrame.getDataTabbedPane(); 247 int selectedIndex = dataTabbedPane.getSelectedIndex(); 248 String title = ""; 249 if (selectedIndex == 0) { 250 title = "View Browser"; 251 dataViewTreeNodeSelected(); 252 } else if (selectedIndex == 1) { 253 title = "Entity Browser"; 254 dataMapTreeNodeSelected(); 255 } 256 dvModelerFrame.getBrowserTile().setCaption(title); 257 enableControls(); 258 } 259 260 private void enableControls() { 261 JTree dataMapTree = dvModelerFrame.getDataMapTree(); 262 JTree dataViewTree = dvModelerFrame.getDataViewTree(); 263 264 JTabbedPane dataTabbedPane = dvModelerFrame.getDataTabbedPane(); 265 boolean dataViewTabSelected = (dataTabbedPane.getSelectedIndex() == 0); 266 boolean openProject = (cayenneProject != null); 267 Object node = (dataViewTabSelected ? 268 dataViewTree.getLastSelectedPathComponent() : 269 dataMapTree.getLastSelectedPathComponent()); 270 addNewDataViewAction.setEnabled( 271 openProject && dataViewTabSelected); 272 addNewObjEntityViewAction.setEnabled( 273 node instanceof DataView || 274 node instanceof ObjEntity); 275 addNewFieldAction.setEnabled( 276 node instanceof ObjEntityView); 277 deleteAction.setEnabled( 278 node instanceof DataView || 279 node instanceof ObjEntityView || 280 node instanceof ObjEntityViewField); 281 } 282 283 284 private class OpenAction extends AbstractAction { 285 private OpenAction() { 286 super( 287 "Open Cayenne Project...", 288 new ImageIcon (SaveAction.class.getResource("images/open.gif"))); 289 setEnabled(true); 290 putValue(Action.ACTION_COMMAND_KEY, "open-project"); 291 putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control O")); 292 putValue(Action.SHORT_DESCRIPTION, "Open Cayenne Project"); 293 } 294 public void actionPerformed(ActionEvent e) { 295 openCayenneProjectFileAction(e); 296 } 297 } 298 299 private void openCayenneProjectFileAction(ActionEvent event){ 300 JTree dataMapTree = dvModelerFrame.getDataMapTree(); 301 JTree dataViewTree = dvModelerFrame.getDataViewTree(); 302 303 int returnVal = openProjectFileChooser.showDialog(dvModelerFrame, "Open Project"); 304 305 if (returnVal == JFileChooser.APPROVE_OPTION) { 306 if (cayenneProject != null) { 307 int result = JOptionPane.showConfirmDialog( 308 dvModelerFrame, 309 "Save current project before its closing?", 310 "DVModeler :: Confirmation", 311 JOptionPane.YES_NO_CANCEL_OPTION, 312 JOptionPane.QUESTION_MESSAGE); 313 if (result == JOptionPane.CANCEL_OPTION) 314 return; 315 else if (result == JOptionPane.YES_OPTION) 316 result = saveCayenneProjectFileAction(null, SaveErrorsDialog.CLOSE_DIALOG); 317 if ((result == -1) || (result == SaveErrorsDialog.CLOSE_WITHOUT_SAVING)){ 318 cayenneProject = null; 319 dvModelerFrame.getMenuItemLoadErrors().setEnabled(false); 320 dvModelerFrame.getMenuItemSaveErrors().setEnabled(false); 321 } else return; 322 } 323 324 File file = openProjectFileChooser.getSelectedFile(); 325 326 try { 327 cayenneProject = new CayenneProject(file); 328 java.util.List loadErrorsList = cayenneProject.getLoadErrors(); 329 330 if (loadErrorsList.size() != 0){ 331 loadErrorsDialog = new ErrorsDialog(dvModelerFrame, loadErrorsList, "Load Errors"); 332 loadErrorsDialog.setVisible(true); 333 dvModelerFrame.getMenuItemLoadErrors().setEnabled(true); 334 } 335 336 DataViewTreeModel dataViewTreeModel = (DataViewTreeModel)dataViewTree.getModel(); 337 dataViewTreeModel.setDataViews(cayenneProject.getDataViews()); 338 DataMapTreeModel dataMapTreeModel = (DataMapTreeModel)dataMapTree.getModel(); 339 dataMapTreeModel.setDataMaps(cayenneProject.getDataMaps()); 340 341 JTable fieldsTable = dvModelerFrame.getFieldsTable(); 342 FieldsTableModel tableModel = (FieldsTableModel)fieldsTable.getModel(); 343 dvModelerFrame.getTopCardPanel().setModels(dataMapTreeModel,dataViewTreeModel,tableModel); 344 for (Iterator i = cayenneProject.getDataViews().iterator(); i.hasNext(); ) { 345 DataView dataView = (DataView)i.next(); 346 dataView.addPropertyChangeListener(propertyChangeMediator); 347 for (Iterator j = dataView.getObjEntityViews().iterator(); j.hasNext(); ) { 348 ObjEntityView view = (ObjEntityView)j.next(); 349 view.addPropertyChangeListener(propertyChangeMediator); 350 for (Iterator k = view.getObjEntityViewFields().iterator(); k.hasNext(); ) { 351 ObjEntityViewField field = (ObjEntityViewField)k.next(); 352 field.addPropertyChangeListener(propertyChangeMediator); 353 } 354 } 355 } 356 } 357 catch (DVModelerException ex) { 358 showExceptionDialog( 359 "Could not open selected Cayenne project", ex); 360 } finally { 361 dvModelerFrame.getTopCardPanel().setProject(cayenneProject); 362 enableControls(); 363 saveAction.setEnabled(true); 364 dvModelerFrame.getMenuItemClose().setEnabled(true); 365 } 366 } 367 } 368 369 private class AddNewDataViewAction extends AbstractAction { 370 private AddNewDataViewAction() { 371 super( 372 "Add Data View", 373 new ImageIcon (DVModelerFrame.class.getResource("images/add-dataview.gif"))); 374 setEnabled(false); 375 putValue(Action.ACTION_COMMAND_KEY, "create-new-data-view"); 376 putValue(Action.SHORT_DESCRIPTION, "Create New Data View"); 377 } 378 public void actionPerformed(ActionEvent e) { 379 addNewDataViewAction(); 380 } 381 } 382 383 private void addNewDataViewAction() { 384 JTree dataViewTree = dvModelerFrame.getDataViewTree(); 385 386 DataView dataView = cayenneProject.createDataView(); 387 DataViewTreeModel dataViewTreeModel = 388 (DataViewTreeModel)dataViewTree.getModel(); 389 TreePath path = dataViewTreeModel.dataViewAdded(dataView); 390 dataViewTree.makeVisible(path); 391 dataView.addPropertyChangeListener(propertyChangeMediator); 392 } 393 394 private class AddNewObjEntityViewAction extends AbstractAction { 395 private AddNewObjEntityViewAction() { 396 super( 397 "Add ObjEntityView", 398 new ImageIcon (AddNewObjEntityViewAction.class.getResource("images/add-objentityview.gif"))); 399 setEnabled(false); 400 putValue(Action.ACTION_COMMAND_KEY, "create-new-view"); 401 putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control W")); 402 putValue(Action.SHORT_DESCRIPTION, "Create New ObjEntityView"); 403 } 404 public void actionPerformed(ActionEvent e) { 405 addNewObjEntityViewAction(e); 406 } 407 } 408 409 private void addNewObjEntityViewAction(ActionEvent event){ 410 JTabbedPane dataTabbedPane = dvModelerFrame.getDataTabbedPane(); 411 JTree dataMapTree = dvModelerFrame.getDataMapTree(); 412 JTree dataViewTree = dvModelerFrame.getDataViewTree(); 413 414 int tabIndex = dataTabbedPane.getSelectedIndex(); 415 ObjEntityView view = null; 416 if (tabIndex == 0) { 417 TreePath path = dataViewTree.getSelectionPath(); 418 DataView dataView = (DataView)path.getLastPathComponent(); 419 ObjEntity selectedObjEntity = (ObjEntity)JOptionPane.showInputDialog( 420 dvModelerFrame, 421 "Select ObjEntity", 422 "DVModeler :: New Entity View", 423 JOptionPane.PLAIN_MESSAGE, 424 null, 425 cayenneProject.getObjEntities(), 426 cayenneProject.getObjEntities()[0]); 427 if (selectedObjEntity != null){ 428 view = new ObjEntityView(dataView); 429 view.setObjEntity(selectedObjEntity); 430 } else return; 431 432 433 } else if (tabIndex == 1) { 434 TreePath path = dataMapTree.getSelectionPath(); 435 436 ObjEntity objEntity = (ObjEntity)path.getLastPathComponent(); 437 438 Object [] dataViews = cayenneProject.getDataViews().toArray(); 439 if (dataViews.length == 0) { 440 JOptionPane.showMessageDialog( 441 dvModelerFrame, 442 "First, create a data view (Tab \"Views\")", 443 "DVModeler :: Warning", 444 JOptionPane.WARNING_MESSAGE); 445 return; 446 } 447 DataView selectedDataView = (DataView)JOptionPane.showInputDialog( 448 dvModelerFrame, 449 "Select Data View", 450 "DVModeler :: New Entity View", 451 JOptionPane.PLAIN_MESSAGE, 452 null, 453 dataViews, 454 dataViews[0]); 455 if (selectedDataView == null) 456 return; 457 java.util.List allObjEntityViews = new ArrayList (); 458 459 for (Iterator j = cayenneProject.getDataViews().iterator(); j.hasNext();){ 460 DataView dataView = (DataView)j.next(); 461 allObjEntityViews.addAll(dataView.getObjEntityViews()); 462 } 463 view = selectedDataView.createObjEntityView(allObjEntityViews); 464 view.setObjEntity(objEntity); 465 } 466 467 DataViewTreeModel dataViewTreeModel = 468 (DataViewTreeModel)dataViewTree.getModel(); 469 TreePath path = dataViewTreeModel.objEntityViewAdded(view); 470 dataViewTree.makeVisible(path); 471 DataMapTreeModel dataMapTreeModel = 472 (DataMapTreeModel)dataMapTree.getModel(); 473 path = dataMapTreeModel.objEntityViewAdded(view); 474 if (path != null) 475 dataMapTree.makeVisible(path); 476 477 view.addPropertyChangeListener(propertyChangeMediator); 478 } 479 480 private class AddNewFieldAction extends AbstractAction { 481 private AddNewFieldAction() { 482 super( 483 "Add Field", 484 new ImageIcon (AddNewFieldAction.class.getResource("images/add-objentityviewfield.gif"))); 485 setEnabled(false); 486 putValue(Action.ACTION_COMMAND_KEY, "create-new-field"); 487 putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control F")); 488 putValue(Action.SHORT_DESCRIPTION, "Create New Field in ObjEntityView"); 489 } 490 public void actionPerformed(ActionEvent e) { 491 addNewFieldAction(e); 492 } 493 } 494 495 private void addNewFieldAction(ActionEvent event){ 496 JTabbedPane dataTabbedPane = dvModelerFrame.getDataTabbedPane(); 497 JTree dataMapTree = dvModelerFrame.getDataMapTree(); 498 JTree dataViewTree = dvModelerFrame.getDataViewTree(); 499 500 int tabIndex = dataTabbedPane.getSelectedIndex(); 501 TreePath selectionPath = null; 502 if (tabIndex == 0) { 503 selectionPath = dataViewTree.getSelectionPath(); 504 } else if (tabIndex == 1) { 505 selectionPath = dataMapTree.getSelectionPath(); 506 } 507 ObjEntityView objEntityView = (ObjEntityView)selectionPath.getLastPathComponent(); 508 ObjEntityViewField newField = objEntityView.createObjEntityViewField(); 509 DataViewTreeModel dataViewTreeModel = 510 (DataViewTreeModel)dataViewTree.getModel(); 511 TreePath path = dataViewTreeModel.fieldAdded(newField); 512 dataViewTree.makeVisible(path); 513 DataMapTreeModel dataMapTreeModel = 514 (DataMapTreeModel)dataMapTree.getModel(); 515 path = dataMapTreeModel.fieldAdded(newField); 516 if (path != null) 517 dataMapTree.makeVisible(path); 518 519 int fieldIndex = objEntityView.getIndexOfObjEntityViewField(newField); 520 521 JTable fieldsTable = dvModelerFrame.getFieldsTable(); 522 FieldsTableModel tableModel = (FieldsTableModel)fieldsTable.getModel(); 523 tableModel.fireTableRowsInserted(fieldIndex,fieldIndex); 524 525 526 newField.addPropertyChangeListener(propertyChangeMediator); 527 } 528 529 530 531 private class SaveAction extends AbstractAction { 532 private SaveAction() { 533 super( 534 "Save Cayenne Project", 535 new ImageIcon (SaveAction.class.getResource("images/save_edit.gif"))); 536 setEnabled(true); 537 putValue(Action.ACTION_COMMAND_KEY, "save-project"); 538 putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control S")); 539 putValue(Action.SHORT_DESCRIPTION, "Save Cayenne Project"); 540 } 541 public void actionPerformed(ActionEvent e) { 542 saveCayenneProjectFileAction(e, SaveErrorsDialog.SAVE_DIALOG); 543 } 544 } 545 548 private int saveCayenneProjectFileAction(ActionEvent event, String dialogType){ 549 if (cayenneProject == null) 550 return -1; 551 try { 552 cayenneProject.buildDataViewsElements(); 553 java.util.List saveErrorsList = cayenneProject.getSaveErrors(); 554 555 if (saveErrorsList.size() != 0){ 556 saveErrorsDialog = new ErrorsDialog(dvModelerFrame, saveErrorsList, "Save Errors"); 557 saveErrorsDialog.setVisible(false); 558 dvModelerFrame.getMenuItemSaveErrors().setEnabled(true); 559 560 int selectedValue = SaveErrorsDialog.showSaveErrorsDialog(dvModelerFrame, saveErrorsList, dialogType); 561 562 if (selectedValue == SaveErrorsDialog.SAVE_ANYWAY){ 563 cayenneProject.save(); 564 }else return selectedValue; 565 }else { 566 cayenneProject.save(); 567 } 568 } 569 catch (IOException ex) { 570 571 showExceptionDialog( 572 "Could not save Cayenne project", ex); 573 } 574 return -1; 575 } 576 577 private class DeleteAction extends AbstractAction { 578 private DeleteAction() { 579 super( 580 "Delete", 581 new ImageIcon (AddNewObjEntityViewAction.class.getResource("images/delete.gif"))); 582 setEnabled(false); 583 putValue(Action.ACTION_COMMAND_KEY, "delete"); 584 putValue(Action.SHORT_DESCRIPTION, "Remove Selected Item"); 585 586 } 587 public void actionPerformed(ActionEvent e) { 588 deleteAction(e); 589 } 590 } 591 592 private void deleteAction(ActionEvent e){ 593 JTabbedPane dataTabbedPane = dvModelerFrame.getDataTabbedPane(); 594 JTree dataMapTree = dvModelerFrame.getDataMapTree(); 595 JTree dataViewTree = dvModelerFrame.getDataViewTree(); 596 597 boolean dataViewTabSelected = (dataTabbedPane.getSelectedIndex() == 0); 598 Object node = (dataViewTabSelected ? 599 dataViewTree.getLastSelectedPathComponent() : 600 dataMapTree.getLastSelectedPathComponent()); 601 602 if (node instanceof DataView){ 603 DataView dataView = (DataView)node; 604 if (dataView.getObjEntityViewCount() != 0){ 605 int option = JOptionPane.showConfirmDialog( 606 dvModelerFrame, 607 "Do you really whant to delete the selected DataView?", 608 "DVModeler :: Deleting DataView", 609 JOptionPane.OK_CANCEL_OPTION 610 ); 611 612 if (option == JOptionPane.OK_OPTION){ 613 int index = cayenneProject.getDataViews().indexOf(dataView); 614 Map removingViews = cayenneProject.removeDataView(dataView); 615 616 DataViewTreeModel dataViewTreeModel = 617 (DataViewTreeModel)dataViewTree.getModel(); 618 dataViewTreeModel.dataViewRemoved(dataView, index); 619 620 DataMapTreeModel dataMapTreeModel = 621 (DataMapTreeModel)dataMapTree.getModel(); 622 dataMapTreeModel.objEntityViewsRemoved(removingViews); 623 624 if (cayenneProject.getDataViews().size() == 0){ 625 626 dvModelerFrame.getTopCardPanel().showPanel(null); 627 showBottomEmptyPanel(); 628 }else{ 629 if (index == 0){ 630 selectDataView((DataView)cayenneProject.getDataViews().get(0)); 631 } else { 632 selectDataView((DataView)cayenneProject.getDataViews().get(index - 1)); 633 } 634 } 635 } 636 } else { 637 int index = cayenneProject.getDataViews().indexOf(dataView); 638 cayenneProject.removeDataView(dataView); 639 640 DataViewTreeModel dataViewTreeModel = 641 (DataViewTreeModel)dataViewTree.getModel(); 642 dataViewTreeModel.dataViewRemoved(dataView, index); 643 644 if (cayenneProject.getDataViews().size() == 0){ 645 646 dvModelerFrame.getTopCardPanel().showPanel(null); 647 showBottomEmptyPanel(); 648 }else{ 649 if (index == 0){ 650 selectDataView((DataView)cayenneProject.getDataViews().get(0)); 651 } else { 652 selectDataView((DataView)cayenneProject.getDataViews().get(index - 1)); 653 } 654 } 655 } 656 657 658 } else if (node instanceof ObjEntityView){ 659 ObjEntityView view = (ObjEntityView)node; 660 DataView dataView = view.getDataView(); 661 int indexInDataView = dataView.getIndexOfObjEntityView(view); 662 ObjEntity entity = view.getObjEntity(); 663 664 int indexInDataMap = -1; 665 666 if (entity != null){ 667 indexInDataMap = entity.getIndexOfObjEntityView(view); 668 dataView.removeObjEntityView(view); 669 670 DataMapTreeModel dataMapTreeModel = 671 (DataMapTreeModel)dataMapTree.getModel(); 672 dataMapTreeModel.objEntityViewRemoved(entity, view, indexInDataMap); 673 674 } else { 675 dataView.removeObjEntityView(view); 676 } 677 678 DataViewTreeModel dataViewTreeModel = 679 (DataViewTreeModel)dataViewTree.getModel(); 680 dataViewTreeModel.objEntityViewRemoved(view, indexInDataView); 681 682 683 if (dataViewTabSelected){ 684 if (dataView.getObjEntityViewCount() == 0){ 685 selectDataView(dataView); 686 }else{ 687 if (indexInDataView == 0){ 688 selectObjEntityView(dataViewTabSelected, dataView.getObjEntityView(0)); 689 } else { 690 selectObjEntityView(dataViewTabSelected, dataView.getObjEntityView(indexInDataView - 1)); 691 } 692 } 693 }else{ 694 if (entity.getObjEntityViewCount() == 0){ 695 selectObjEntity(entity); 696 }else{ 697 if (indexInDataMap == 0){ 698 selectObjEntityView(dataViewTabSelected, entity.getObjEntityView(0)); 699 } else { 700 selectObjEntityView(dataViewTabSelected, entity.getObjEntityView(indexInDataMap - 1)); 701 } 702 } 703 } 704 } else if (node instanceof ObjEntityViewField){ 705 ObjEntityViewField field = (ObjEntityViewField)node; 706 ObjEntityView view = field.getObjEntityView(); 707 708 int index = view.getIndexOfObjEntityViewField(field); 709 view.removeObjEntityViewField(field); 710 711 DataViewTreeModel dataViewTreeModel = 712 (DataViewTreeModel)dataViewTree.getModel(); 713 dataViewTreeModel.fieldRemoved(field, index); 714 715 DataMapTreeModel dataMapTreeModel = 716 (DataMapTreeModel)dataMapTree.getModel(); 717 dataMapTreeModel.fieldRemoved(field, index); 718 719 JTable fieldsTable = dvModelerFrame.getFieldsTable(); 720 FieldsTableModel tableModel = (FieldsTableModel)fieldsTable.getModel(); 721 tableModel.fireTableRowsDeleted(index,index); 722 723 if (view.getObjEntityViewFieldCount() == 0){ 724 if (dataViewTabSelected){ 725 selectObjEntityViewInDataViewTree(view); 726 } else { 727 selectObjEntityViewInDataMapTree(view); 728 } 729 730 } else { 731 if (index == 0){ 732 selectField(dataViewTabSelected, view.getObjEntityViewField(0)); 733 } else { 734 selectField(dataViewTabSelected, view.getObjEntityViewField(index-1)); 735 736 } 737 } 738 } 739 } 740 741 742 private void selectDataView(DataView dataView){ 743 JTree dataViewTree = dvModelerFrame.getDataViewTree(); 744 745 DataViewTreeModel dataViewTreeModel = (DataViewTreeModel)dataViewTree.getModel(); 746 Object [] path = new Object [] {dataViewTreeModel.getRoot(), 747 dataView 748 }; 749 750 dataViewTree.setSelectionPath(new TreePath (path)); 751 } 752 753 private void selectObjEntity(ObjEntity entity){ 754 JTree dataMapTree = dvModelerFrame.getDataMapTree(); 755 756 DataMapTreeModel dataMapTreeModel = (DataMapTreeModel)dataMapTree.getModel(); 757 DataMap dataMap = entity.getDataMap(); 758 Object [] path = new Object [] {dataMapTreeModel.getRoot(), 759 dataMap, 760 entity 761 }; 762 763 dataMapTree.setSelectionPath(new TreePath (path)); 764 } 765 766 private void selectObjEntityView(boolean dataViewTabSelected, ObjEntityView view){ 767 DataView dataView = view.getDataView(); 768 JTree dataMapTree = dvModelerFrame.getDataMapTree(); 769 JTree dataViewTree = dvModelerFrame.getDataViewTree(); 770 771 DataViewTreeModel dataViewTreeModel = (DataViewTreeModel)dataViewTree.getModel(); 772 if (dataViewTabSelected){ 773 Object [] path = new Object []{dataViewTreeModel.getRoot(), 774 dataView, 775 view 776 }; 777 778 dataViewTree.setSelectionPath(new TreePath (path)); 779 } else { 780 ObjEntity entity = view.getObjEntity(); 781 DataMap dataMap = entity.getDataMap(); 782 DataMapTreeModel dataMapTreeModel = (DataMapTreeModel)dataMapTree.getModel(); 783 784 Object [] path = new Object []{dataMapTreeModel.getRoot(), 785 dataMap, 786 entity, 787 view 788 }; 789 dataMapTree.setSelectionPath(new TreePath (path)); 790 } 791 } 792 793 private void selectObjEntityViewInDataViewTree(ObjEntityView view){ 794 JTree dataViewTree = dvModelerFrame.getDataViewTree(); 795 796 DataViewTreeModel dataViewTreeModel = (DataViewTreeModel)dataViewTree.getModel(); 797 DataView dataView = view.getDataView(); 798 Object [] path = new Object [] {dataViewTreeModel.getRoot(), 799 dataView, 800 view 801 }; 802 803 dataViewTree.setSelectionPath(new TreePath (path)); 804 } 805 806 private void selectObjEntityViewInDataMapTree(ObjEntityView view){ 807 JTree dataMapTree = dvModelerFrame.getDataMapTree(); 808 809 DataMapTreeModel dataMapTreeModel = (DataMapTreeModel)dataMapTree.getModel(); 810 ObjEntity entity = view.getObjEntity(); 811 DataMap dataMap = entity.getDataMap(); 812 Object [] path = new Object [] {dataMapTreeModel.getRoot(), 813 dataMap, 814 entity, 815 view 816 }; 817 818 dataMapTree.setSelectionPath(new TreePath (path)); 819 } 820 private void selectField(boolean dataViewTabSelected, ObjEntityViewField field){ 821 JTree dataMapTree = dvModelerFrame.getDataMapTree(); 822 JTree dataViewTree = dvModelerFrame.getDataViewTree(); 823 824 ObjEntityView view = field.getObjEntityView(); 825 DataView dataView = view.getDataView(); 826 DataViewTreeModel dataViewTreeModel = (DataViewTreeModel)dataViewTree.getModel(); 827 if (dataViewTabSelected){ 828 Object [] path = new Object []{dataViewTreeModel.getRoot(), 829 dataView, 830 view, 831 field 832 }; 833 834 dataViewTree.setSelectionPath(new TreePath (path)); 835 } else { 836 ObjEntity entity = view.getObjEntity(); 837 DataMap dataMap = entity.getDataMap(); 838 DataMapTreeModel dataMapTreeModel = (DataMapTreeModel)dataMapTree.getModel(); 839 840 Object [] path = new Object []{dataMapTreeModel.getRoot(), 841 dataMap, 842 entity, 843 view, 844 field 845 }; 846 dataMapTree.setSelectionPath(new TreePath (path)); 847 } 848 } 849 850 public ActionListener getMenuItemLoadErrorsActionListenr(){ 851 return new ActionListener (){ 852 public void actionPerformed(ActionEvent e){ 853 loadErrorsAction(e); 854 } 855 }; 856 } 857 858 private void loadErrorsAction(ActionEvent e){ 859 loadErrorsDialog.setVisible(true); 860 } 861 862 public ActionListener getMenuItemSaveErrorsActionListener(){ 863 return new ActionListener (){ 864 public void actionPerformed(ActionEvent e){ 865 saveErrorsAction(e); 866 } 867 }; 868 } 869 870 private void saveErrorsAction(ActionEvent e){ 871 saveErrorsDialog.setVisible(true); 872 } 873 874 public TreeSelectionListener getDataMapTreeTreeSelectionListener(){ 875 return new TreeSelectionListener (){ 876 public void valueChanged(TreeSelectionEvent e){ 877 dataMapTreeNodeSelected(); 878 } 879 }; 880 } 881 882 private void dataMapTreeNodeSelected() { 883 JTree dataMapTree = dvModelerFrame.getDataMapTree(); 884 885 Object node = dataMapTree.getLastSelectedPathComponent(); 886 String topTitle = ""; 887 String bottomTitle = ""; 888 if (node == null){ 889 dvModelerFrame.getTopCardPanel().showPanel(null); 890 showBottomEmptyPanel(); 891 }else if (node instanceof DataMap){ 892 dvModelerFrame.getTopCardPanel().showPanel((DVObject)node); 893 showBottomEmptyPanel(); 894 topTitle = "Data View \"" + 895 node + 896 "\" :: Properties"; 897 } else if (node instanceof ObjEntity){ 898 dvModelerFrame.getTopCardPanel().showPanel((DVObject)node); 899 ObjEntity objEntity = (ObjEntity)node; 900 showRelatioshipsTable(objEntity); 901 topTitle = "Entity \"" + 902 node + 903 "\" :: Attributes"; 904 bottomTitle = "Entity \"" + 905 node + 906 "\" :: Relationships"; 907 } else if (node instanceof ObjEntityView){ 908 dvModelerFrame.getTopCardPanel().showPanel((DVObject)node); 909 showFieldsTable((ObjEntityView)node); 910 topTitle = "View \"" + 911 node + 912 "\" :: Properties"; 913 bottomTitle = "View \"" + 914 node + 915 "\" :: Fields"; 916 } else if (node instanceof ObjEntityViewField){ 917 dvModelerFrame.getTopCardPanel().showPanel((DVObject)node); 918 ObjEntityViewField field = (ObjEntityViewField)node; 919 920 showFieldsTable(field.getObjEntityView()); 921 int index = field.getObjEntityView().getIndexOfObjEntityViewField(field); 922 JTable fieldsTable = dvModelerFrame.getFieldsTable(); 923 fieldsTable.setRowSelectionInterval(index,index); 924 925 topTitle = "Field \"" + node + "\" :: Properties"; 926 bottomTitle = "View \"" + 927 ((ObjEntityViewField)node).getObjEntityView() + 928 "\" :: Fields"; 929 } 930 931 dvModelerFrame.getTopTile().setCaption(topTitle); 932 dvModelerFrame.getBottomTile().setCaption(bottomTitle); 933 enableControls(); 934 } 935 936 public TreeSelectionListener getDataViewTreeTreeSelectionListener(){ 937 return new TreeSelectionListener (){ 938 public void valueChanged(TreeSelectionEvent e){ 939 dataViewTreeNodeSelected(); 940 } 941 }; 942 } 943 944 private void dataViewTreeNodeSelected() { 945 JTree dataViewTree = dvModelerFrame.getDataViewTree(); 946 947 Object node = dataViewTree.getLastSelectedPathComponent(); 948 String topTitle = ""; 949 String bottomTitle = ""; 950 951 if (node == null){ 952 dvModelerFrame.getTopCardPanel().showPanel(null); 953 showBottomEmptyPanel(); 954 }else if (node instanceof DataView){ 955 dvModelerFrame.getTopCardPanel().showPanel((DVObject)node); 956 showBottomEmptyPanel(); 957 topTitle = "Data View \"" + 958 node + 959 "\" :: Properties"; 960 } else if (node instanceof ObjEntityView){ 961 dvModelerFrame.getTopCardPanel().showPanel((DVObject)node); 962 showFieldsTable((ObjEntityView)node); 963 topTitle = "View \"" + 964 node + 965 "\" :: Properties"; 966 bottomTitle = "View \"" + 967 node + 968 "\" :: Fields"; 969 } else if (node instanceof ObjEntityViewField){ 970 dvModelerFrame.getTopCardPanel().showPanel((DVObject)node); 971 ObjEntityViewField field = (ObjEntityViewField)node; 972 showFieldsTable(field.getObjEntityView()); 973 int index = field.getObjEntityView().getIndexOfObjEntityViewField(field); 974 975 JTable fieldsTable = dvModelerFrame.getFieldsTable(); 976 fieldsTable.setRowSelectionInterval(index,index); 977 topTitle = "Field \"" + node + "\" :: Properties"; 978 bottomTitle = "View \"" + 979 ((ObjEntityViewField)node).getObjEntityView() + 980 "\" :: Fields"; 981 } 982 dvModelerFrame.getTopTile().setCaption(topTitle); 983 dvModelerFrame.getBottomTile().setCaption(bottomTitle); 984 enableControls(); 985 } 986 987 988 private void showBottomEmptyPanel(){ 989 JPanel bottomCardPanel = dvModelerFrame.getBottomCardPanel(); 990 CardLayout cardLayout = (CardLayout )bottomCardPanel.getLayout(); 991 cardLayout.show(bottomCardPanel, "emptyPanel"); 992 } 993 994 private void showFieldsTable(ObjEntityView view){ 995 JTable fieldsTable = dvModelerFrame.getFieldsTable(); 996 FieldsTableModel fieldsTableModel = (FieldsTableModel)fieldsTable.getModel(); 997 fieldsTableModel.setObjEntityView(view); 998 999 JPanel bottomCardPanel = dvModelerFrame.getBottomCardPanel(); 1000 CardLayout cardLayout = (CardLayout )bottomCardPanel.getLayout(); 1001 cardLayout.show(bottomCardPanel, "fieldsTable"); 1002 } 1003 1004 private void showRelatioshipsTable(ObjEntity objEntity){ 1005 java.util.List relationships = new ArrayList (); 1006 relationships.addAll(objEntity.getDataMap().getObjRelationshipsBySource(objEntity)); 1007 relationships.addAll(objEntity.getDataMap().getObjRelationshipsByTarget(objEntity)); 1008 1009 JTable relationshipsTable = dvModelerFrame.getRelationshipsTable(); 1010 RelationshipsTableModel model = (RelationshipsTableModel)relationshipsTable.getModel(); 1011 model.setObjRelationships(relationships); 1012 1013 JPanel bottomCardPanel = dvModelerFrame.getBottomCardPanel(); 1014 CardLayout cardLayout = (CardLayout )bottomCardPanel.getLayout(); 1015 cardLayout.show(bottomCardPanel, "relationshipsTable"); 1016 } 1017 1018 private void configureFileChoosers() { 1019 openProjectFileChooser.setDialogType(JFileChooser.OPEN_DIALOG); 1020 openProjectFileChooser.setApproveButtonMnemonic('O'); 1021 openProjectFileChooser.setApproveButtonText("Open Project"); 1022 openProjectFileChooser.setApproveButtonToolTipText("Open Selected Cayenne Project"); 1023 openProjectFileChooser.setDialogTitle("DVModeler :: Open Cayenne Project"); 1024 openProjectFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); 1025 openProjectFileChooser.setMultiSelectionEnabled(false); 1026 openProjectFileChooser.setFileFilter(new javax.swing.filechooser.FileFilter () { 1027 public String getDescription() { 1028 return "(*.xml) Cayenne Project Files"; 1029 } 1030 public boolean accept(File file) { 1031 return (file.isDirectory() || 1032 file.getName().toLowerCase().endsWith(".xml")); 1033 } 1034 }); 1035 } 1036 1037 private class PropertyChangeMediator implements PropertyChangeListener { 1038 public void propertyChange(PropertyChangeEvent e) { 1039 Object source = e.getSource(); 1040 String propertyName = e.getPropertyName(); 1041 1042 JTree dataMapTree = dvModelerFrame.getDataMapTree(); 1043 JTree dataViewTree = dvModelerFrame.getDataViewTree(); 1044 1045 DataViewTreeModel dataViewTreeModel = 1046 (DataViewTreeModel)dataViewTree.getModel(); 1047 DataMapTreeModel dataMapTreeModel = 1048 (DataMapTreeModel)dataMapTree.getModel(); 1049 1050 JTable fieldsTable = dvModelerFrame.getFieldsTable(); 1051 1052 1053 if (source instanceof DataView) { 1054 if ("name".equals(propertyName)) { 1055 String topTitle = "Data View \"" + 1056 source + 1057 "\" :: Properties"; 1058 dvModelerFrame.getTopTile().setCaption(topTitle); 1059 dataViewTreeModel.dataViewChanged((DataView)source); 1060 } 1061 } else if (source instanceof ObjEntityView) { 1062 if ("name".equals(propertyName)) { 1063 String topTitle = "View \"" + 1064 source + 1065 "\" :: Properties"; 1066 String bottomTitle = "View \"" + 1067 source + 1068 "\" :: Fields"; 1069 dvModelerFrame.getTopTile().setCaption(topTitle); 1070 dvModelerFrame.getBottomTile().setCaption(bottomTitle); 1071 dataViewTreeModel.objEntityViewChanged((ObjEntityView)source); 1072 dataMapTreeModel.objEntityViewChanged((ObjEntityView)source); 1073 } 1074 } else if (source instanceof ObjEntityViewField) { 1075 1076 FieldsTableModel fieldsTableModel = (FieldsTableModel)fieldsTable.getModel(); 1077 ObjEntityViewField field = (ObjEntityViewField)source; 1078 1079 if ("name".equals(propertyName)) { 1080 String topTitle = "Field \"" + source + "\" :: Properties"; 1081 dvModelerFrame.getTopTile().setCaption(topTitle); 1082 dataViewTreeModel.fieldChanged(field); 1083 dataMapTreeModel.fieldChanged(field); 1084 fieldsTableModel.fireTableCellUpdated(field.getObjEntityView().getIndexOfObjEntityViewField(field), 0); 1085 } 1086 if ("dataType".equals(propertyName)){ 1087 fieldsTableModel.fireTableCellUpdated(field.getObjEntityView().getIndexOfObjEntityViewField(field), 1); 1088 } 1089 if ("calcType".equals(propertyName)){ 1090 fieldsTableModel.fireTableCellUpdated(field.getObjEntityView().getIndexOfObjEntityViewField(field), 2); 1091 } 1092 if ("objRelationship".equals(propertyName)){ 1093 1094 dataViewTreeModel.fieldChanged(field); 1095 dataMapTreeModel.fieldChanged(field); 1096 fieldsTableModel.fireTableCellUpdated(field.getObjEntityView().getIndexOfObjEntityViewField(field), 3); 1097 } 1098 if ("objAttribute".equals(propertyName)){ 1099 dataViewTreeModel.fieldChanged(field); 1100 dataMapTreeModel.fieldChanged(field); 1101 fieldsTableModel.fireTableCellUpdated(field.getObjEntityView().getIndexOfObjEntityViewField(field), 3); 1102 } 1103 if ("lookup".equals(propertyName)){ 1104 dataViewTreeModel.fieldChanged(field); 1105 dataMapTreeModel.fieldChanged(field); 1106 fieldsTableModel.fireTableCellUpdated(field.getObjEntityView().getIndexOfObjEntityViewField(field), 4); 1107 } 1108 } 1109 } 1110 } 1111 1112 private void showExceptionDialog(String description, Exception ex) { 1113 String message = "<html>" + description + "<br>"; 1114 message += ex.getClass() + ": " + ex.getMessage() + "<br>"; 1115 Exception cause = (Exception )ex.getCause(); 1116 if (cause != null) { 1117 message += "caused by " + cause.getClass() + ": " + cause.getMessage(); 1118 } 1119 message += "</html>"; 1120 JOptionPane.showMessageDialog( 1121 dvModelerFrame, message, "DVModeler :: Error", JOptionPane.ERROR_MESSAGE); 1122 } 1123 1124 1125 1126} 1127 | Popular Tags |