1 27 28 package org.nightlabs.editor2d.views; 29 30 import java.util.EventObject ; 31 import java.util.HashMap ; 32 import java.util.List ; 33 import java.util.Map ; 34 35 import org.apache.log4j.Logger; 36 import org.eclipse.gef.EditPart; 37 import org.eclipse.gef.EditPartListener; 38 import org.eclipse.gef.RootEditPart; 39 import org.eclipse.gef.commands.Command; 40 import org.eclipse.gef.commands.CommandStackListener; 41 import org.eclipse.jface.resource.ImageDescriptor; 42 import org.eclipse.jface.viewers.ISelection; 43 import org.eclipse.jface.viewers.ISelectionChangedListener; 44 import org.eclipse.jface.viewers.SelectionChangedEvent; 45 import org.eclipse.swt.SWT; 46 import org.eclipse.swt.custom.ScrolledComposite; 47 import org.eclipse.swt.events.DisposeEvent; 48 import org.eclipse.swt.events.DisposeListener; 49 import org.eclipse.swt.events.FocusAdapter; 50 import org.eclipse.swt.events.FocusEvent; 51 import org.eclipse.swt.events.SelectionAdapter; 52 import org.eclipse.swt.events.SelectionEvent; 53 import org.eclipse.swt.events.SelectionListener; 54 import org.eclipse.swt.graphics.Color; 55 import org.eclipse.swt.graphics.Image; 56 import org.eclipse.swt.layout.GridData; 57 import org.eclipse.swt.layout.GridLayout; 58 import org.eclipse.swt.widgets.Button; 59 import org.eclipse.swt.widgets.Composite; 60 import org.eclipse.swt.widgets.Control; 61 import org.eclipse.swt.widgets.Display; 62 import org.eclipse.swt.widgets.Label; 63 import org.eclipse.swt.widgets.Text; 64 import org.eclipse.ui.ISelectionListener; 65 import org.eclipse.ui.IWorkbenchPart; 66 import org.eclipse.ui.forms.widgets.Form; 67 import org.eclipse.ui.forms.widgets.FormToolkit; 68 import org.eclipse.ui.forms.widgets.ScrolledForm; 69 import org.eclipse.ui.forms.widgets.TableWrapLayout; 70 import org.eclipse.ui.part.ViewPart; 71 import org.nightlabs.base.form.XFormToolkit; 72 import org.nightlabs.base.resource.SharedImages; 73 import org.nightlabs.editor2d.AbstractEditor; 74 import org.nightlabs.editor2d.DrawComponent; 75 import org.nightlabs.editor2d.EditorPlugin; 76 import org.nightlabs.editor2d.Layer; 77 import org.nightlabs.editor2d.MultiLayerDrawComponent; 78 import org.nightlabs.editor2d.command.CreateLayerCommand; 79 import org.nightlabs.editor2d.command.DeleteLayerCommand; 80 import org.nightlabs.editor2d.command.DrawComponentReorderCommand; 81 import org.nightlabs.editor2d.edit.LayerEditPart; 82 import org.nightlabs.editor2d.edit.MultiLayerDrawComponentEditPart; 83 import org.nightlabs.editor2d.util.OrderUtil; 84 85 public class LayerView 86 extends ViewPart 87 implements ISelectionListener 88 { 89 public static final Logger LOGGER = Logger.getLogger(LayerView.class); 90 public static final String ID_VIEW = LayerView.class.getName(); 91 92 public static final Image DELETE_ICON = SharedImages.DELETE_16x16.createImage(); 93 public static final Image NEW_ICON = SharedImages.ADD_16x16.createImage(); 94 public static final Image EYE_ICON = SharedImages.getSharedImage(EditorPlugin.getDefault(), LayerView.class, "Eye"); 95 public static final Image EYE_INVISIBLE_ICON = SharedImages.getSharedImage(EditorPlugin.getDefault(), LayerView.class, "Eye-Invisible"); 96 public static final Image UP_ICON = SharedImages.getSharedImage(EditorPlugin.getDefault(), LayerView.class, "Up"); 97 public static final Image DOWN_ICON = SharedImages.getSharedImage(EditorPlugin.getDefault(), LayerView.class, "Down"); 98 public static final Image LOCK_ICON = SharedImages.getSharedImage(EditorPlugin.getDefault(), LayerView.class, "Lock"); 99 public static final Image UNLOCK_ICON = SharedImages.getSharedImage(EditorPlugin.getDefault(), LayerView.class, "Unlocked"); 100 101 protected MultiLayerDrawComponent mldc; 102 protected AbstractEditor editor; 103 protected Map button2Layer = new HashMap (); 104 protected Button buttonUp; 105 protected Button buttonDown; 106 protected Button buttonNew; 107 protected Button buttonDelete; 108 109 protected Color currentLayerColor; 110 protected Color getCurrentLayerColor() 111 { 112 if (currentLayerColor == null) 113 currentLayerColor = new Color(Display.getCurrent(), 232, 236, 240); 114 return currentLayerColor; 115 } 116 117 protected Color toolButtonColor; 118 protected Color getToolButtonColor() 119 { 120 if (toolButtonColor == null) 121 toolButtonColor = new Color(Display.getCurrent(), 0, 0, 0); 122 return toolButtonColor; 123 } 124 125 public LayerView() 126 { 127 super(); 128 } 129 130 protected void initMultiLayerDrawComponent() 131 { 132 if (getSite().getPage().getActiveEditor() instanceof AbstractEditor) 133 { 134 LOGGER.debug("getSite().getPage().getActiveEditor() instanceof Editor!"); 135 editor = (AbstractEditor) getSite().getPage().getActiveEditor(); 136 RootEditPart rootEditPart = editor.getOutlineGraphicalViewer().getRootEditPart(); 137 List children = rootEditPart.getChildren(); 138 if (!children.isEmpty()) { 139 EditPart editPart = (EditPart) children.get(0); 140 if (editPart instanceof MultiLayerDrawComponentEditPart) { 141 MultiLayerDrawComponentEditPart mldcEditPart = (MultiLayerDrawComponentEditPart) editPart; 142 mldcEditPart.addEditPartListener(mldcListener); 143 } 144 } 145 mldc = editor.getMultiLayerDrawComponent(); 146 } 147 } 148 149 190 protected void init() 191 { 192 initMultiLayerDrawComponent(); 194 getSite().getWorkbenchWindow().getSelectionService().addSelectionListener(this); 196 } 197 198 protected FormToolkit toolkit = null; 199 protected FormToolkit getToolkit() { 200 return toolkit; 201 } 202 203 protected ScrolledForm form = null; 204 protected ScrolledForm getForm() { 205 return form; 206 } 207 208 213 218 protected Composite layerComposite; 219 221 251 254 public void createPartControl(Composite parent) 255 { 256 init(); 257 258 parent.setLayout(new GridLayout()); 259 parent.setLayoutData(new GridData(GridData.FILL_BOTH)); 260 261 toolkit = new XFormToolkit(parent.getDisplay()); 262 form = getToolkit().createScrolledForm(parent); 263 form.setLayout(new GridLayout()); 264 form.setLayoutData(new GridData(GridData.FILL_BOTH)); 265 form.getBody().setLayout(new GridLayout()); 266 form.getBody().setLayoutData(new GridData(GridData.FILL_BOTH)); 267 268 273 parent.setBackground(toolkit.getColors().getBackground()); 274 275 layerComposite = getToolkit().createComposite(form.getBody(), SWT.NONE); 276 layerComposite.setLayout(new GridLayout()); 277 layerComposite.setLayoutData(new GridData(GridData.FILL_BOTH)); 278 layerComposite.setData(FormToolkit.KEY_DRAW_BORDER, FormToolkit.TEXT_BORDER); 279 toolkit.paintBordersFor(form.getBody()); 280 281 createTools(parent); 282 refresh(); 283 deactivateTools(true); 284 } 285 286 protected void createLayerEntry(Composite parent, Layer l) 287 { 288 int buttonStyle = SWT.TOGGLE; 289 290 Composite parentComposite = getToolkit().createComposite(parent, SWT.NONE); 291 parentComposite.setData(FormToolkit.KEY_DRAW_BORDER, FormToolkit.TEXT_BORDER); 292 getToolkit().paintBordersFor(parent); 293 getToolkit().paintBordersFor(parentComposite); 294 if (!parentComposite.isDisposed()) 295 { 296 parentComposite.setLayout(new GridLayout(2, false)); 297 parentComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 298 299 Button buttonVisible = getToolkit().createButton(parentComposite, EditorPlugin.getResourceString("layerView.buttonVisible.text"), buttonStyle); 301 buttonVisible.setSelection(!l.isVisible()); 302 if (l.isVisible()) { 303 buttonVisible.setImage(EYE_ICON); 304 buttonVisible.setToolTipText(EditorPlugin.getResourceString("layerView.buttonVisible.tooltip.invisible")); 305 } 306 else { 307 buttonVisible.setImage(EYE_INVISIBLE_ICON); 308 buttonVisible.setToolTipText(EditorPlugin.getResourceString("layerView.buttonVisible.tooltip.visible")); 309 } 310 buttonVisible.setLayoutData(new GridData(GridData.BEGINNING)); 311 312 buttonVisible.addSelectionListener(visibleListener); 313 buttonVisible.addDisposeListener(visibleDisposeListener); 314 315 331 Text text = getToolkit().createText(parentComposite, EditorPlugin.getResourceString("layerView.layer.name")); 333 if (l.getName() != null) 334 text.setText(l.getName()); 335 text.setEditable(true); 336 text.setLayoutData(new GridData(GridData.FILL_BOTH)); 337 text.addSelectionListener(textListener); 338 text.addFocusListener(focusListener); 339 text.addDisposeListener(textDisposeListener); 340 341 button2Layer.put(buttonVisible, l); 343 button2Layer.put(text, l); 345 346 if (l.equals(mldc.getCurrentLayer())) { 348 text.setBackground(getCurrentLayerColor()); 350 } 351 } 352 } 353 354 protected void createTools(Composite parent) 355 { 356 int buttonStyle = SWT.PUSH; 357 358 Composite toolsComposite = getToolkit().createComposite(parent); 359 toolsComposite.setLayout(new GridLayout(5, false)); 360 toolsComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 361 362 Label spacerLabel = getToolkit().createLabel(toolsComposite, ""); 364 spacerLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 365 366 buttonUp = getToolkit().createButton(toolsComposite, EditorPlugin.getResourceString("layerView.buttonUp.text"), 368 buttonStyle); 369 buttonUp.setToolTipText(EditorPlugin.getResourceString("layerView.buttonUp.tooltip")); 373 buttonUp.addSelectionListener(upListener); 374 buttonUp.addDisposeListener(upDisposeListener); 375 buttonUp.setBackground(getToolButtonColor()); 376 377 buttonDown = getToolkit().createButton(toolsComposite, EditorPlugin.getResourceString("layerView.buttonDown.text"), 378 buttonStyle); 379 buttonDown.setToolTipText(EditorPlugin.getResourceString("layerView.buttonDown.tooltip")); 383 buttonDown.addSelectionListener(downListener); 384 buttonDown.addDisposeListener(downDisposeListener); 385 buttonDown.setBackground(getToolButtonColor()); 386 387 buttonNew = getToolkit().createButton(toolsComposite, EditorPlugin.getResourceString("layerView.buttonNew.text"), 388 buttonStyle); 389 buttonNew.setToolTipText(EditorPlugin.getResourceString("layerView.buttonNew.tooltip")); 393 buttonNew.addSelectionListener(newListener); 394 buttonNew.addDisposeListener(newDisposeListener); 395 buttonNew.setBackground(getToolButtonColor()); 396 397 buttonDelete = getToolkit().createButton(toolsComposite, EditorPlugin.getResourceString("layerView.buttonDelete.text"), 398 buttonStyle); 399 buttonDelete.setToolTipText(EditorPlugin.getResourceString("layerView.buttonDelete.tooltip")); 403 buttonDelete.addSelectionListener(deleteListener); 404 buttonDelete.addDisposeListener(deleteDisposeListener); 405 buttonDelete.setBackground(getToolButtonColor()); 406 } 407 408 411 public void setFocus() 412 { 413 form.setFocus(); 414 } 415 416 protected FocusAdapter focusListener = new FocusAdapter() 417 { 418 public void focusGained(FocusEvent e) 419 { 420 Text t = (Text) e.getSource(); 421 if (button2Layer.containsKey(t)) 422 { 423 Layer currLayer = (Layer) button2Layer.get(t); 424 if (!mldc.getCurrentLayer().equals(currLayer)) { 425 mldc.setCurrentLayer(currLayer); 426 LOGGER.debug("Layer "+currLayer.getName()+" = CurrentLayer"); 427 refresh(); 428 } 429 } else { 430 throw new IllegalStateException ("There is no such Layer registered!"); 431 } 432 } 433 }; 434 435 protected SelectionListener textListener = new SelectionAdapter() 436 { 437 public void widgetDefaultSelected(SelectionEvent e) 438 { 439 if (e.getSource() instanceof Text) 440 { 441 Text text = (Text) e.getSource(); 442 String layerName = text.getText(); 443 LOGGER.debug("New LayerName = "+layerName); 444 Layer l = (Layer) button2Layer.get(text); 445 l.setName(layerName); 446 } 447 } 448 }; 449 450 protected SelectionListener visibleListener = new SelectionAdapter() 451 { 452 public void widgetSelected(SelectionEvent e) 453 { 454 LOGGER.debug("VISIBLE widgetSelected()"); 455 456 Button b = (Button) e.getSource(); 457 if (button2Layer.containsKey(b)) { 458 Layer l = (Layer) button2Layer.get(b); 459 if (!b.getSelection()) { 460 l.setVisible(true); 461 b.setImage(EYE_ICON); 462 b.setToolTipText(EditorPlugin.getResourceString("layerView.buttonVisible.tooltip.invisible")); 463 } else { 464 l.setVisible(false); 465 b.setImage(EYE_INVISIBLE_ICON); 466 b.setToolTipText(EditorPlugin.getResourceString("layerView.buttonVisible.tooltip.visible")); 467 } 468 updateViewer(); 469 } 470 else { 471 throw new IllegalStateException ("There is no such Layer registered!"); 472 } 473 } 474 }; 475 476 protected SelectionListener editableListener = new SelectionAdapter() 477 { 478 public void widgetSelected(SelectionEvent e) 479 { 480 LOGGER.debug("EDITABLE widgetSelected()"); 481 482 Button b = (Button) e.getSource(); 483 if (button2Layer.containsKey(b)) { 484 Layer l = (Layer) button2Layer.get(b); 485 if (b.getSelection()) { 486 l.setEditable(true); 487 b.setToolTipText(EditorPlugin.getResourceString("layerView.buttonLocked.tooltip.locked")); 488 b.setImage(UNLOCK_ICON); 489 } else { 490 l.setEditable(false); 491 b.setToolTipText(EditorPlugin.getResourceString("layerView.buttonLocked.tooltip.unlocked")); 492 b.setImage(LOCK_ICON); 493 } 494 updateViewer(); 495 } 496 else { 497 throw new IllegalStateException ("There is no such Layer registered!"); 498 } 499 } 500 }; 501 502 protected void updateViewer() 503 { 504 editor.getEditPartViewer().getRootEditPart().refresh(); 505 } 507 508 protected SelectionListener newListener = new SelectionAdapter() 509 { 510 public void widgetSelected(SelectionEvent e) 511 { 512 LOGGER.debug("NEW widgetSelected()"); 513 CreateLayerCommand addLayer = new CreateLayerCommand(mldc); 514 executeCommand(addLayer); 515 } 516 }; 517 518 protected SelectionListener deleteListener = new SelectionAdapter() 519 { 520 public void widgetSelected(SelectionEvent e) 521 { 522 LOGGER.debug("DELETE widgetSelected()"); 523 524 Layer currentLayer = mldc.getCurrentLayer(); 525 DeleteLayerCommand layerCommand = new DeleteLayerCommand(mldc, currentLayer); 526 executeCommand(layerCommand); 527 } 528 }; 529 530 protected Layer getCurrentLayer() 531 { 532 return mldc.getCurrentLayer(); 533 } 534 535 protected SelectionListener upListener = new SelectionAdapter() 536 { 537 public void widgetSelected(SelectionEvent e) 538 { 539 LOGGER.debug("UP widgetSelected()"); 540 int oldIndex = OrderUtil.indexOf(getCurrentLayer()); 541 int lastIndex = OrderUtil.getLastIndex(mldc); 542 int newIndex = oldIndex + 1; 543 if (!(lastIndex > newIndex)) { 544 newIndex = lastIndex; 545 } 546 DrawComponentReorderCommand cmd = new DrawComponentReorderCommand( 547 getCurrentLayer(), mldc, newIndex); 548 executeCommand(cmd); 549 } 550 }; 551 552 protected SelectionListener downListener = new SelectionAdapter() 553 { 554 public void widgetSelected(SelectionEvent e) 555 { 556 LOGGER.debug("DOWN widgetSelected()"); 557 int oldIndex = OrderUtil.indexOf(getCurrentLayer()); 558 int firstIndex = 0; 559 int newIndex = oldIndex - 1; 560 if (!(firstIndex < newIndex)) { 561 newIndex = firstIndex; 562 } 563 DrawComponentReorderCommand cmd = new DrawComponentReorderCommand( 564 getCurrentLayer(), mldc, newIndex); 565 executeCommand(cmd); 566 } 567 }; 568 569 protected void executeCommand(Command cmd) 570 { 571 editor.getOutlineEditDomain().getCommandStack().execute(cmd); 572 } 573 574 protected CommandStackListener commandStackListener = new CommandStackListener() 575 { 576 579 public void commandStackChanged(EventObject event) 580 { 581 editor.getEditPartViewer().getRootEditPart().refresh(); 582 refresh(); 583 } 584 }; 585 586 protected EditPartListener mldcListener = new EditPartListener.Stub() 587 { 588 591 public void childAdded(EditPart child, int index) 592 { 593 if (child instanceof LayerEditPart) { 594 MultiLayerDrawComponentEditPart parent = (MultiLayerDrawComponentEditPart) child.getParent(); 595 mldc = (MultiLayerDrawComponent) parent.getModel(); 596 refresh(); 597 } 598 } 599 600 603 public void removingChild(EditPart child, int index) 604 { 605 if (child instanceof LayerEditPart) { 606 MultiLayerDrawComponentEditPart parent = (MultiLayerDrawComponentEditPart) child.getParent(); 607 mldc = (MultiLayerDrawComponent) parent.getModel(); 608 refresh(); 609 } 610 } 611 }; 612 613 protected void deactivateTools(boolean newButton) 614 { 615 if (!buttonUp.isDisposed()) 616 buttonUp.setEnabled(false); 617 if (!buttonDown.isDisposed()) 618 buttonDown.setEnabled(false); 619 if (!buttonDelete.isDisposed()) 620 buttonDelete.setEnabled(false); 621 if (!buttonNew.isDisposed()) 622 buttonNew.setEnabled(newButton); 623 } 624 625 protected void activateTools() 626 { 627 if (!buttonUp.isDisposed()) 628 buttonUp.setEnabled(true); 629 if (!buttonDown.isDisposed()) 630 buttonDown.setEnabled(true); 631 if (!buttonDelete.isDisposed()) 632 buttonDelete.setEnabled(true); 633 if (!buttonNew.isDisposed()) 634 buttonNew.setEnabled(true); 635 } 636 637 640 public void selectionChanged(IWorkbenchPart part, ISelection selection) 641 { 642 if (part instanceof AbstractEditor) 643 { 644 this.editor = (AbstractEditor) part; 645 this.mldc = editor.getMultiLayerDrawComponent(); 646 editor.getOutlineEditDomain().getCommandStack().removeCommandStackListener(commandStackListener); 647 editor.getOutlineEditDomain().getCommandStack().addCommandStackListener(commandStackListener); 648 } 649 else if (!(getSite().getPage().getActiveEditor() instanceof AbstractEditor)) 650 { 651 editor = null; 652 mldc = null; 653 deactivateTools(false); 654 } 655 refresh(); 656 } 657 658 protected void createComposites() 659 { 660 if (layerComposite != null) 661 layerComposite.dispose(); 662 layerComposite = getToolkit().createComposite(form.getBody(), SWT.NONE); 663 layerComposite.setLayout(new GridLayout()); 664 layerComposite.setLayoutData(new GridData(GridData.FILL_BOTH)); 665 layerComposite.setData(FormToolkit.KEY_DRAW_BORDER, FormToolkit.TEXT_BORDER); 666 getToolkit().paintBordersFor(form.getBody()); 667 } 668 669 680 public void refresh() 681 { 682 if (layerComposite != null || !layerComposite.isDisposed()) 683 { 684 createComposites(); 685 button2Layer.clear(); 686 687 if (mldc != null) 688 { 689 for (int i = mldc.getDrawComponents().size()-1; i >= 0; --i) 690 { 691 DrawComponent dc = (DrawComponent) mldc.getDrawComponents().get(i); 692 if (dc instanceof Layer) { 693 Layer l = (Layer) dc; 694 createLayerEntry(layerComposite, l); 695 } else { 696 LOGGER.debug("dc NOT instanceof Layer, but instanceof "+dc.getClass()); 697 } 698 } 699 700 if (mldc.getDrawComponents().size() <= 1) 701 deactivateTools(true); 702 else 703 activateTools(); 704 705 form.reflow(true); 706 } 707 } 708 } 709 710 public void dispose() 711 { 712 getSite().getWorkbenchWindow().getSelectionService().removeSelectionListener(this); 713 super.dispose(); 714 } 715 716 protected DisposeListener visibleDisposeListener = new DisposeListener() { 717 public void widgetDisposed(DisposeEvent e) { 718 Button b = (Button) e.getSource(); 719 b.removeSelectionListener(visibleListener); 720 } 721 }; 722 723 protected DisposeListener editableDisposeListener = new DisposeListener() { 724 public void widgetDisposed(DisposeEvent e) { 725 Button b = (Button) e.getSource(); 726 b.removeSelectionListener(editableListener); 727 } 728 }; 729 730 protected DisposeListener textDisposeListener = new DisposeListener() { 731 public void widgetDisposed(DisposeEvent e) { 732 Text t = (Text) e.getSource(); 733 t.removeSelectionListener(textListener); 734 t.removeFocusListener(focusListener); 735 } 736 }; 737 738 protected DisposeListener upDisposeListener = new DisposeListener() { 739 public void widgetDisposed(DisposeEvent e) { 740 Button b = (Button) e.getSource(); 741 b.removeSelectionListener(upListener); 742 } 743 }; 744 745 protected DisposeListener downDisposeListener = new DisposeListener() { 746 public void widgetDisposed(DisposeEvent e) { 747 Button b = (Button) e.getSource(); 748 b.removeSelectionListener(downListener); 749 } 750 }; 751 752 protected DisposeListener newDisposeListener = new DisposeListener() { 753 public void widgetDisposed(DisposeEvent e) { 754 Button b = (Button) e.getSource(); 755 b.removeSelectionListener(newListener); 756 } 757 }; 758 759 protected DisposeListener deleteDisposeListener = new DisposeListener() { 760 public void widgetDisposed(DisposeEvent e) { 761 Button b = (Button) e.getSource(); 762 b.removeSelectionListener(newListener); 763 } 764 }; 765 766 } 767 | Popular Tags |