Project

General

Profile

Download (4.85 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9

    
10
package eu.etaxonomy.taxeditor.editor.name.e4.container;
11

    
12
import java.util.ArrayList;
13
import java.util.List;
14

    
15
import org.eclipse.e4.core.contexts.IEclipseContext;
16
import org.eclipse.swt.SWT;
17
import org.eclipse.swt.dnd.DND;
18
import org.eclipse.swt.dnd.DropTarget;
19
import org.eclipse.swt.dnd.DropTargetListener;
20
import org.eclipse.swt.dnd.Transfer;
21
import org.eclipse.swt.graphics.Color;
22
import org.eclipse.swt.widgets.Composite;
23
import org.eclipse.swt.widgets.Display;
24
import org.eclipse.ui.forms.widgets.TableWrapData;
25
import org.eclipse.ui.forms.widgets.TableWrapLayout;
26

    
27
import eu.etaxonomy.taxeditor.editor.CdmDataTransfer;
28
import eu.etaxonomy.taxeditor.editor.name.e4.IDropTargetableE4;
29
import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
30
import eu.etaxonomy.taxeditor.editor.name.e4.dnd.NameEditorDropTargetEffect;
31
import eu.etaxonomy.taxeditor.editor.name.e4.dnd.NameEditorDropTargetListenerE4;
32
import eu.etaxonomy.taxeditor.model.AbstractUtility;
33
import eu.etaxonomy.taxeditor.preference.Resources;
34

    
35
/**
36
 *
37
 * @author pplitzner
38
 * @date Aug 24, 2017
39
 *
40
 */
41
public abstract class AbstractGroupE4 implements IDropTargetableE4{
42

    
43
	private Composite control;
44

    
45
	private IEclipseContext context;
46

    
47
	private final List<AbstractGroupedContainerE4> groupedContainers = new ArrayList<>();
48

    
49
	protected TaxonNameEditorE4 editor;
50

    
51
	private DropTarget target;
52
	private DropTargetListener dropListener;
53

    
54
	public AbstractGroupE4(TaxonNameEditorE4 editor){
55
		this.editor = editor;
56
		this.context = editor.getContext();
57
	}
58

    
59
	protected void createContent(){
60
		createControl();
61

    
62
		createContainers();
63

    
64
		// Drop functionality
65
		this.setDroppable(true);
66

    
67
		editor.getManagedForm().reflow(true);
68
	}
69

    
70
	protected void createControl() {
71
		control = editor.getToolkit().createComposite(editor.getControl());
72

    
73
		control.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB));
74
		TableWrapLayout layout = new TableWrapLayout();
75

    
76
		layout.topMargin = 0;
77
		layout.rightMargin = 0;
78
		layout.bottomMargin = 1;
79
		layout.leftMargin = 0;
80

    
81
		layout.verticalSpacing = 0;
82
		layout.horizontalSpacing = 0;
83
		control.setLayout(layout);
84

    
85
		control.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_GRAY));
86
	}
87

    
88
	public void add(AbstractGroupedContainerE4 groupedContainer){
89
		groupedContainers.add(groupedContainer);
90
	}
91

    
92
	public void remove(AbstractGroupedContainerE4 groupedContainer){
93
		groupedContainer.dispose();
94
		groupedContainers.remove(groupedContainer);
95

    
96
		if(!(this instanceof AcceptedGroupE4) && groupedContainers.isEmpty()){
97
			getEditor().removeGroup(this);
98
		}
99
	}
100

    
101
	public List<AbstractGroupedContainerE4> getGroupedContainers(){
102
		return groupedContainers;
103
	}
104

    
105

    
106
	public void setDroppable(boolean droppable) {
107
		if (droppable) {
108
			Transfer[] types = new Transfer[] {CdmDataTransfer.getInstance()};
109
			int operations = DND.DROP_MOVE;
110
			target = new DropTarget(control, operations);
111
			target.setTransfer(types);
112
			dropListener = new NameEditorDropTargetListenerE4(this);
113
			target.addDropListener(dropListener);
114

    
115
			target.setDropTargetEffect(new NameEditorDropTargetEffect(control));
116

    
117
		} else {
118
			if (dropListener != null) {
119
				target.removeDropListener(dropListener);
120
			}
121
		}
122
	}
123

    
124
	@Override
125
	public IEclipseContext getContext() {
126
	    return context;
127
	}
128

    
129
	@Override
130
    public TaxonNameEditorE4 getEditor() {
131
		return editor;
132
	}
133

    
134
	@Override
135
    public void dragEntered() {
136
		Color color = AbstractUtility.getColor(Resources.COLOR_DRAG_ENTER);
137

    
138
		for(AbstractGroupedContainerE4 container : groupedContainers){
139
			container.setBackground(color);
140
		}
141
	}
142

    
143
	@Override
144
    public void dragLeft() {
145
		for(AbstractGroupedContainerE4 container : groupedContainers){
146
			container.restoreColor();
147
		}
148
	}
149

    
150
	/** {@inheritDoc} */
151
	@Override
152
    public boolean postOperation(Object objectAffectedByOperation) {
153
		editor.changed(objectAffectedByOperation);
154
		redraw();
155
		return true;
156
	}
157

    
158
	public void redraw(){
159
	    emptyGroup();
160

    
161
		createContainers();
162
	}
163

    
164
	protected abstract void createContainers();
165

    
166
	@Override
167
	public Composite getControl() {
168
		return control;
169
	}
170

    
171
	@Override
172
	public boolean onComplete() {
173
		return true;
174
	}
175

    
176
	public void dispose(){
177
		if(getControl() != null){
178
		    getControl().setMenu(null);
179
			for(AbstractGroupedContainerE4 container : getGroupedContainers()){
180
				container.dispose();
181
			}
182
			getControl().dispose();
183
		}
184
	}
185

    
186
	protected void emptyGroup(){
187
		for(AbstractGroupedContainerE4 container : groupedContainers){
188
		    container.getControl().setMenu(null);
189
			container.dispose();
190
		}
191
		groupedContainers.clear();
192
	}
193

    
194
	protected void setMenuToGroup(){
195
        for(AbstractGroupedContainerE4 container : groupedContainers){
196
            container.setMenu();
197

    
198
        }
199

    
200
    }
201
}
(1-1/11)