1
|
/**
|
2
|
* Copyright (C) 2009 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
|
package eu.etaxonomy.taxeditor.editor.definedterm.e4;
|
10
|
|
11
|
import javax.annotation.PostConstruct;
|
12
|
import javax.annotation.PreDestroy;
|
13
|
import javax.inject.Inject;
|
14
|
|
15
|
import org.eclipse.core.runtime.IProgressMonitor;
|
16
|
import org.eclipse.e4.core.contexts.ContextInjectionFactory;
|
17
|
import org.eclipse.e4.core.contexts.IEclipseContext;
|
18
|
import org.eclipse.e4.ui.di.Focus;
|
19
|
import org.eclipse.e4.ui.di.Persist;
|
20
|
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
|
21
|
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
|
22
|
import org.eclipse.e4.ui.services.EMenuService;
|
23
|
import org.eclipse.e4.ui.workbench.modeling.EPartService;
|
24
|
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
|
25
|
import org.eclipse.jface.viewers.ISelectionChangedListener;
|
26
|
import org.eclipse.jface.viewers.StructuredSelection;
|
27
|
import org.eclipse.jface.viewers.TreeViewer;
|
28
|
import org.eclipse.jface.viewers.Viewer;
|
29
|
import org.eclipse.jface.viewers.ViewerSorter;
|
30
|
import org.eclipse.swt.SWT;
|
31
|
import org.eclipse.swt.dnd.DND;
|
32
|
import org.eclipse.swt.dnd.Transfer;
|
33
|
import org.eclipse.swt.layout.FillLayout;
|
34
|
import org.eclipse.swt.widgets.Composite;
|
35
|
import org.eclipse.swt.widgets.Tree;
|
36
|
import org.eclipse.ui.IMemento;
|
37
|
|
38
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
39
|
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
|
40
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
41
|
import eu.etaxonomy.cdm.model.common.OrderedTermBase;
|
42
|
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
|
43
|
import eu.etaxonomy.taxeditor.editor.definedterm.TermContentProvider;
|
44
|
import eu.etaxonomy.taxeditor.editor.definedterm.TermLabelProvider;
|
45
|
import eu.etaxonomy.taxeditor.editor.definedterm.TermTransfer;
|
46
|
import eu.etaxonomy.taxeditor.editor.definedterm.input.TermEditorInput;
|
47
|
import eu.etaxonomy.taxeditor.model.AbstractUtility;
|
48
|
import eu.etaxonomy.taxeditor.model.IContextListener;
|
49
|
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
|
50
|
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
|
51
|
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
|
52
|
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
|
53
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
54
|
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
|
55
|
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
|
56
|
|
57
|
/**
|
58
|
*
|
59
|
* @author pplitzner
|
60
|
* @since Aug 22, 2017
|
61
|
*
|
62
|
*/
|
63
|
public class DefinedTermEditorE4 implements IConversationEnabled, IDirtyMarkable, IPostOperationEnabled,
|
64
|
IPartContentHasDetails, IPartContentHasSupplementalData, IE4SavablePart, IContextListener {
|
65
|
|
66
|
public static final String OPEN_COMMAND_ID = "eu.etaxonomy.taxeditor.store.openDefinedTermEditor";
|
67
|
|
68
|
protected TreeViewer viewer;
|
69
|
|
70
|
protected ConversationHolder conversation;
|
71
|
|
72
|
@Inject
|
73
|
private ESelectionService selService;
|
74
|
|
75
|
@Inject
|
76
|
private MDirtyable dirty;
|
77
|
|
78
|
private ISelectionChangedListener selectionChangedListener;
|
79
|
private final int dndOperations = DND.DROP_COPY | DND.DROP_MOVE;
|
80
|
|
81
|
private TermEditorInput input =null;
|
82
|
|
83
|
@Inject
|
84
|
private MPart thisPart;
|
85
|
|
86
|
@Inject
|
87
|
public DefinedTermEditorE4() {
|
88
|
CdmStore.getContextManager().addContextListener(this);
|
89
|
}
|
90
|
|
91
|
@PostConstruct
|
92
|
public void createPartControl(Composite parent, EMenuService menuService,
|
93
|
IEclipseContext context) {
|
94
|
if (CdmStore.isActive()){
|
95
|
if(conversation == null){
|
96
|
conversation = CdmStore.createConversation();
|
97
|
}
|
98
|
}
|
99
|
else{
|
100
|
return;
|
101
|
}
|
102
|
FillLayout layout = new FillLayout();
|
103
|
layout.marginHeight = 0;
|
104
|
layout.marginWidth = 0;
|
105
|
layout.type = SWT.VERTICAL;
|
106
|
|
107
|
parent.setLayout(layout);
|
108
|
viewer = new TreeViewer(new Tree(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION));
|
109
|
viewer.getControl().setLayoutData(LayoutConstants.FILL());
|
110
|
viewer.setContentProvider(new TermContentProvider());
|
111
|
viewer.setLabelProvider(new TermLabelProvider());
|
112
|
viewer.setSorter(new DefinedTermSorter());
|
113
|
|
114
|
Transfer[] transfers = new Transfer[] { TermTransfer.getInstance() };
|
115
|
viewer.addDragSupport(dndOperations, transfers, new DefinedTermDragListenerE4(viewer));
|
116
|
DefinedTermDropAdapterE4 dropListener = new DefinedTermDropAdapterE4(this);
|
117
|
ContextInjectionFactory.inject(dropListener, context);
|
118
|
viewer.addDropSupport(dndOperations, transfers, dropListener);
|
119
|
|
120
|
//propagate selection
|
121
|
selectionChangedListener = (event -> selService.setSelection(AbstractUtility.getElementsFromSelectionChangedEvent(event)));
|
122
|
viewer.addSelectionChangedListener(selectionChangedListener);
|
123
|
|
124
|
//create context menu
|
125
|
menuService.registerContextMenu(viewer.getControl(), "eu.etaxonomy.taxeditor.store.popupmenu.termeditor");
|
126
|
|
127
|
}
|
128
|
|
129
|
@Override
|
130
|
public boolean postOperation(CdmBase objectAffectedByOperation) {
|
131
|
viewer.refresh();
|
132
|
|
133
|
if(objectAffectedByOperation != null){
|
134
|
viewer.setSelection(new StructuredSelection(objectAffectedByOperation));
|
135
|
}
|
136
|
setDirty(true);
|
137
|
|
138
|
return true;
|
139
|
}
|
140
|
|
141
|
public void init(TermEditorInput input){
|
142
|
this.input = input;
|
143
|
|
144
|
input.initialiseVocabularies();
|
145
|
viewer.setInput(input.getVocabularies());
|
146
|
thisPart.setLabel(input.getName());
|
147
|
}
|
148
|
|
149
|
@Override
|
150
|
@Persist
|
151
|
public void save(IProgressMonitor monitor) {
|
152
|
getConversationHolder().commit();
|
153
|
input.merge();
|
154
|
setDirty(false);
|
155
|
input.initialiseVocabularies();
|
156
|
viewer.refresh();
|
157
|
}
|
158
|
|
159
|
public boolean isDirty() {
|
160
|
return dirty.isDirty();
|
161
|
}
|
162
|
|
163
|
@Focus
|
164
|
public void setFocus() {
|
165
|
if(viewer!=null && viewer.getControl() != null && !viewer.getControl().isDisposed()){
|
166
|
viewer.getControl().setFocus();
|
167
|
}
|
168
|
if(conversation!=null){
|
169
|
conversation.bind();
|
170
|
}
|
171
|
if(input!=null){
|
172
|
input.bind();
|
173
|
}
|
174
|
}
|
175
|
|
176
|
@Override
|
177
|
public void update(CdmDataChangeMap changeEvents) {
|
178
|
}
|
179
|
|
180
|
@Override
|
181
|
public ConversationHolder getConversationHolder() {
|
182
|
return conversation;
|
183
|
}
|
184
|
|
185
|
@Override
|
186
|
public void changed(Object element) {
|
187
|
setDirty(true);
|
188
|
viewer.update(element, null);
|
189
|
}
|
190
|
|
191
|
@Override
|
192
|
public void forceDirty() {
|
193
|
changed(null);
|
194
|
}
|
195
|
|
196
|
public void setDirty(boolean dirty) {
|
197
|
this.dirty.setDirty(dirty);
|
198
|
}
|
199
|
|
200
|
@Override
|
201
|
public boolean onComplete() {
|
202
|
return false;
|
203
|
}
|
204
|
|
205
|
public TreeViewer getViewer() {
|
206
|
return viewer;
|
207
|
}
|
208
|
|
209
|
private class DefinedTermSorter extends ViewerSorter {
|
210
|
|
211
|
@Override
|
212
|
public int compare(Viewer viewer, Object e1, Object e2) {
|
213
|
// the comparison value in this method determines the
|
214
|
// location <-> add term method used in the MoveDefinedTermOperation
|
215
|
// execute call
|
216
|
if(e1 instanceof OrderedTermBase && e2 instanceof OrderedTermBase) {
|
217
|
OrderedTermBase otbe1 = (OrderedTermBase)e1;
|
218
|
OrderedTermBase otbe2 = (OrderedTermBase)e2;
|
219
|
if(otbe1.getOrderIndex() == otbe2.getOrderIndex()) {
|
220
|
return 0;
|
221
|
} else if (otbe1.getOrderIndex() < otbe2.getOrderIndex()){
|
222
|
return -1;
|
223
|
} else{
|
224
|
return 1;
|
225
|
}
|
226
|
} else {
|
227
|
return super.compare(viewer, e1, e2);
|
228
|
}
|
229
|
}
|
230
|
}
|
231
|
|
232
|
@PreDestroy
|
233
|
public void dispose() {
|
234
|
if(conversation!=null){
|
235
|
conversation.unregisterForDataStoreChanges(this);
|
236
|
conversation.close();
|
237
|
}
|
238
|
if(input!=null){
|
239
|
input.dispose();
|
240
|
}
|
241
|
}
|
242
|
|
243
|
public TermEditorInput getDefinedTermEditorInput() {
|
244
|
return input;
|
245
|
}
|
246
|
|
247
|
/**
|
248
|
* {@inheritDoc}
|
249
|
*/
|
250
|
@Override
|
251
|
public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
|
252
|
}
|
253
|
|
254
|
/**
|
255
|
* {@inheritDoc}
|
256
|
*/
|
257
|
@Override
|
258
|
public void contextStop(IMemento memento, IProgressMonitor monitor) {
|
259
|
//close view when workbench closes
|
260
|
try{
|
261
|
thisPart.getContext().get(EPartService.class).hidePart(thisPart);
|
262
|
}
|
263
|
catch(Exception e){
|
264
|
//nothing
|
265
|
}
|
266
|
}
|
267
|
|
268
|
/**
|
269
|
* {@inheritDoc}
|
270
|
*/
|
271
|
@Override
|
272
|
public void contextStart(IMemento memento, IProgressMonitor monitor) {
|
273
|
}
|
274
|
|
275
|
/**
|
276
|
* {@inheritDoc}
|
277
|
*/
|
278
|
@Override
|
279
|
public void contextRefresh(IProgressMonitor monitor) {
|
280
|
}
|
281
|
|
282
|
/**
|
283
|
* {@inheritDoc}
|
284
|
*/
|
285
|
@Override
|
286
|
public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
|
287
|
}
|
288
|
|
289
|
}
|