1 package eu
.etaxonomy
.taxeditor
.editor
;
3 import org
.eclipse
.swt
.SWT
;
4 import org
.eclipse
.swt
.dnd
.DND
;
5 import org
.eclipse
.swt
.dnd
.DragSource
;
6 import org
.eclipse
.swt
.dnd
.DragSourceAdapter
;
7 import org
.eclipse
.swt
.dnd
.DragSourceEvent
;
8 import org
.eclipse
.swt
.dnd
.DragSourceListener
;
9 import org
.eclipse
.swt
.dnd
.Transfer
;
10 import org
.eclipse
.swt
.widgets
.Composite
;
11 import org
.eclipse
.swt
.widgets
.Control
;
13 import eu
.etaxonomy
.taxeditor
.actions
.WidgetTransfer
;
16 * A composite which can be dragged between different EditorGroupComposite. The user
17 * uses the draggable control for dragging the whole composite.
19 * @see eu.etaxonomy.taxeditor.editor.EditorGroupComposite
24 abstract public class GroupedComposite
extends Composite
{
26 IParentDataAdapter parentDataAdapter
;
27 private Control draggableControl
;
29 public GroupedComposite(Composite parent
) {
30 super(parent
, SWT
.NONE
);
35 abstract protected void createContent();
37 protected void setDraggableControl(Control control
) {
38 draggableControl
= control
;
41 public void setDraggable(boolean draggable
) {
45 if (dragger
!= null) {
50 if (draggableControl
== null) {
51 throw new NullPointerException(
52 "Draggable control must be set to add draggability");
55 Transfer
[] types
= new Transfer
[] { WidgetTransfer
.getInstance() };
56 int operations
= DND
.DROP_MOVE
| DND
.DROP_COPY
| DND
.DROP_LINK
;
58 dragger
= new DragSource(draggableControl
, operations
);
59 dragger
.setTransfer(types
);
60 dragger
.addDragListener(dragSourceListener
);
68 * Drag listener which passes the Composite as the data in a drag event.
70 DragSourceListener dragSourceListener
= new DragSourceAdapter() {
72 public void dragStart(DragSourceEvent event
) {
73 GroupedComposite
.this.setFocus();
77 public void dragSetData(DragSourceEvent event
) {
78 WidgetTransfer
.getInstance().setWidget(GroupedComposite
.this);
81 private DragSource dragger
;
84 * Override of setParent: dispose of parent if this is its last child
87 * @see org.eclipse.swt.widgets.Control#setParent(org.eclipse.swt.widgets.Composite)
89 public boolean setParent(Composite parent
) {
91 Composite oldParent
= this.getParent();
92 Composite oldGrandparent
= oldParent
.getParent();
94 if (super.setParent(parent
)) {
95 if (oldParent
instanceof EditorGroupComposite
96 && oldParent
.getChildren().length
== 0) {
101 // Redraw the component holding the group and grouped composites
102 parent
.getParent().layout();
104 // In case we are dragging between different views, redraw the
106 if (!oldGrandparent
.equals(parent
.getParent())) {
107 oldGrandparent
.layout();
115 public void dispose () {
116 Composite parent
= this.getParent();
117 Composite grandParent
= null;
118 if (parent
!= null) {
119 grandParent
= parent
.getParent();
122 if (parent
instanceof Composite
123 && parent
.getChildren().length
== 0) {
127 // Redraw the component holding the group and grouped composites
128 if (grandParent
!= null) {
129 grandParent
.layout();
134 * A grouped composite is usually grouped according to data held by its
135 * parent. When the composite changes parents, this method gets called to
136 * update data as needed.
138 protected void adaptParentData() {
139 if (parentDataAdapter
!= null) {
140 parentDataAdapter
.adaptParentData();
144 public void setParentDataAdapter(IParentDataAdapter parentDataAdapter
) {
145 this.parentDataAdapter
= parentDataAdapter
;