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