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.HashSet;
|
12
|
import java.util.Set;
|
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.model.application.ui.MDirtyable;
|
24
|
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
|
25
|
import org.eclipse.e4.ui.services.EMenuService;
|
26
|
import org.eclipse.e4.ui.workbench.modeling.EPartService;
|
27
|
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
|
28
|
import org.eclipse.jface.dialogs.MessageDialog;
|
29
|
import org.eclipse.jface.util.LocalSelectionTransfer;
|
30
|
import org.eclipse.jface.viewers.ISelectionChangedListener;
|
31
|
import org.eclipse.jface.viewers.StructuredSelection;
|
32
|
import org.eclipse.jface.viewers.TreeViewer;
|
33
|
import org.eclipse.swt.SWT;
|
34
|
import org.eclipse.swt.dnd.DND;
|
35
|
import org.eclipse.swt.dnd.Transfer;
|
36
|
import org.eclipse.swt.layout.FillLayout;
|
37
|
import org.eclipse.swt.widgets.Composite;
|
38
|
import org.eclipse.swt.widgets.Tree;
|
39
|
import org.eclipse.ui.IMemento;
|
40
|
|
41
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
42
|
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
|
43
|
import eu.etaxonomy.cdm.api.service.ITermService;
|
44
|
import eu.etaxonomy.cdm.api.service.IVocabularyService;
|
45
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
46
|
import eu.etaxonomy.cdm.model.common.TermBase;
|
47
|
import eu.etaxonomy.cdm.model.common.TermVocabulary;
|
48
|
import eu.etaxonomy.cdm.persistence.dto.TermDto;
|
49
|
import eu.etaxonomy.cdm.persistence.dto.TermVocabularyDto;
|
50
|
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
|
51
|
import eu.etaxonomy.taxeditor.editor.definedterm.DefinedTermSorter;
|
52
|
import eu.etaxonomy.taxeditor.editor.definedterm.TermDtoContentProvider;
|
53
|
import eu.etaxonomy.taxeditor.editor.definedterm.TermDtoLabelProvider;
|
54
|
import eu.etaxonomy.taxeditor.editor.definedterm.input.TermEditorInput;
|
55
|
import eu.etaxonomy.taxeditor.event.EventUtility;
|
56
|
import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
|
57
|
import eu.etaxonomy.taxeditor.l10n.Messages;
|
58
|
import eu.etaxonomy.taxeditor.model.IContextListener;
|
59
|
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
|
60
|
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
|
61
|
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
|
62
|
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
|
63
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
64
|
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
|
65
|
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
|
66
|
|
67
|
/**
|
68
|
*
|
69
|
* @author pplitzner
|
70
|
* @since Aug 22, 2017
|
71
|
*
|
72
|
*/
|
73
|
public class DefinedTermEditorE4 implements IConversationEnabled, IDirtyMarkable, IPostOperationEnabled,
|
74
|
IPartContentHasDetails, IPartContentHasSupplementalData, IE4SavablePart, IContextListener {
|
75
|
|
76
|
public static final String OPEN_COMMAND_ID = "eu.etaxonomy.taxeditor.store.openDefinedTermEditor"; //$NON-NLS-1$
|
77
|
|
78
|
protected TreeViewer viewer;
|
79
|
|
80
|
protected ConversationHolder conversation;
|
81
|
|
82
|
@Inject
|
83
|
private ESelectionService selService;
|
84
|
|
85
|
@Inject
|
86
|
private MDirtyable dirty;
|
87
|
|
88
|
private ISelectionChangedListener selectionChangedListener;
|
89
|
private final int dndOperations = DND.DROP_COPY | DND.DROP_MOVE;
|
90
|
|
91
|
private TermEditorInput input =null;
|
92
|
|
93
|
@Inject
|
94
|
private MPart thisPart;
|
95
|
|
96
|
private Set<TermBase> changedTerms = new HashSet<>();
|
97
|
|
98
|
@Inject
|
99
|
public DefinedTermEditorE4() {
|
100
|
CdmStore.getContextManager().addContextListener(this);
|
101
|
}
|
102
|
|
103
|
@PostConstruct
|
104
|
public void createPartControl(Composite parent, EMenuService menuService,
|
105
|
IEclipseContext context) {
|
106
|
if (CdmStore.isActive()){
|
107
|
if(conversation == null){
|
108
|
conversation = CdmStore.createConversation();
|
109
|
}
|
110
|
}
|
111
|
else{
|
112
|
return;
|
113
|
}
|
114
|
FillLayout layout = new FillLayout();
|
115
|
layout.marginHeight = 0;
|
116
|
layout.marginWidth = 0;
|
117
|
layout.type = SWT.VERTICAL;
|
118
|
|
119
|
parent.setLayout(layout);
|
120
|
viewer = new TreeViewer(new Tree(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.MULTI));
|
121
|
viewer.getControl().setLayoutData(LayoutConstants.FILL());
|
122
|
viewer.setContentProvider(new TermDtoContentProvider());
|
123
|
viewer.setLabelProvider(new TermDtoLabelProvider());
|
124
|
viewer.setComparator(new DefinedTermSorter());
|
125
|
|
126
|
Transfer[] transfers = new Transfer[] { LocalSelectionTransfer.getTransfer() };
|
127
|
viewer.addDragSupport(dndOperations, transfers, new DefinedTermDragListenerE4(viewer));
|
128
|
DefinedTermDropAdapterE4 dropListener = new DefinedTermDropAdapterE4(this);
|
129
|
ContextInjectionFactory.inject(dropListener, context);
|
130
|
viewer.addDropSupport(dndOperations, transfers, dropListener);
|
131
|
|
132
|
//propagate selection
|
133
|
selectionChangedListener = (event -> selService.setSelection(event.getSelection()));
|
134
|
viewer.addSelectionChangedListener(selectionChangedListener);
|
135
|
|
136
|
//create context menu
|
137
|
menuService.registerContextMenu(viewer.getControl(), "eu.etaxonomy.taxeditor.store.popupmenu.termeditor"); //$NON-NLS-1$
|
138
|
|
139
|
}
|
140
|
|
141
|
@Override
|
142
|
public boolean postOperation(Object objectAffectedByOperation) {
|
143
|
if(objectAffectedByOperation instanceof TermVocabulary){
|
144
|
input.initialiseVocabularies();
|
145
|
}
|
146
|
viewer.refresh();
|
147
|
if(objectAffectedByOperation != null){
|
148
|
StructuredSelection selection = new StructuredSelection(objectAffectedByOperation);
|
149
|
viewer.setSelection(selection);
|
150
|
viewer.expandToLevel(objectAffectedByOperation, 1);
|
151
|
}
|
152
|
return true;
|
153
|
}
|
154
|
|
155
|
public void init(TermEditorInput input){
|
156
|
this.input = input;
|
157
|
|
158
|
input.initialiseVocabularies();
|
159
|
viewer.setInput(input.getVocabularies());
|
160
|
thisPart.setLabel(input.getName());
|
161
|
}
|
162
|
|
163
|
/**
|
164
|
* Checks the dirty flag and, if set, prompts the user to optionally save
|
165
|
* the editor
|
166
|
*
|
167
|
* @return <code>false</code> if the editor is not dirty anymore, either
|
168
|
* because it wasn't beforehand or because it has been saved.
|
169
|
* <code>true</code> otherwise
|
170
|
*/
|
171
|
public boolean checkDirty(){
|
172
|
if (isDirty()){
|
173
|
boolean proceed = MessageDialog.openQuestion(null,
|
174
|
Messages.DefinedTermEditorE4_SAVE_TITLE, Messages.DefinedTermEditorE4_SAVE_MESSAGE);
|
175
|
if (proceed) {
|
176
|
save(null);
|
177
|
return false;
|
178
|
}
|
179
|
else{
|
180
|
return true;
|
181
|
}
|
182
|
}
|
183
|
else{
|
184
|
return false;
|
185
|
}
|
186
|
}
|
187
|
|
188
|
@Override
|
189
|
@Persist
|
190
|
public void save(IProgressMonitor monitor) {
|
191
|
getConversationHolder().commit();
|
192
|
for(TermBase term:changedTerms){
|
193
|
if(term.isInstanceOf(DefinedTermBase.class)){
|
194
|
CdmStore.getService(ITermService.class).merge((DefinedTermBase) term);
|
195
|
}
|
196
|
else if(term.isInstanceOf(TermVocabulary.class)){
|
197
|
CdmStore.getService(IVocabularyService.class).merge((TermVocabulary) term);
|
198
|
}
|
199
|
}
|
200
|
changedTerms.clear();
|
201
|
setDirty(false);
|
202
|
input.initialiseVocabularies();
|
203
|
viewer.refresh();
|
204
|
EventUtility.postEvent(WorkbenchEventConstants.REFRESH_SUPPLEMENTAL, true);
|
205
|
}
|
206
|
|
207
|
public boolean isDirty() {
|
208
|
return dirty.isDirty();
|
209
|
}
|
210
|
|
211
|
@Focus
|
212
|
public void setFocus() {
|
213
|
if(viewer!=null && viewer.getControl() != null && !viewer.getControl().isDisposed()){
|
214
|
viewer.getControl().setFocus();
|
215
|
}
|
216
|
if(conversation!=null){
|
217
|
conversation.bind();
|
218
|
}
|
219
|
if(input!=null){
|
220
|
input.bind();
|
221
|
}
|
222
|
}
|
223
|
|
224
|
@Override
|
225
|
public void update(CdmDataChangeMap changeEvents) {
|
226
|
}
|
227
|
|
228
|
@Override
|
229
|
public ConversationHolder getConversationHolder() {
|
230
|
return conversation;
|
231
|
}
|
232
|
|
233
|
@Override
|
234
|
public void changed(Object element) {
|
235
|
if(element instanceof DefinedTermBase){
|
236
|
DefinedTermBase definedTermBase = (DefinedTermBase) element;
|
237
|
TermDto termDto = TermDto.fromTerm(definedTermBase);
|
238
|
viewer.update(termDto, null);
|
239
|
changedTerms.add(definedTermBase);
|
240
|
}
|
241
|
else if(element instanceof TermVocabulary){
|
242
|
TermVocabulary vocabulary = (TermVocabulary)element;
|
243
|
TermVocabularyDto vocabularyDto = new TermVocabularyDto(vocabulary.getUuid(), vocabulary.getRepresentations());
|
244
|
viewer.update(vocabularyDto, null);
|
245
|
changedTerms.add(vocabulary);
|
246
|
}
|
247
|
setDirty(true);
|
248
|
viewer.update(element, null);
|
249
|
}
|
250
|
|
251
|
@Override
|
252
|
public void forceDirty() {
|
253
|
changed(null);
|
254
|
}
|
255
|
|
256
|
public void setDirty(boolean dirty) {
|
257
|
this.dirty.setDirty(dirty);
|
258
|
}
|
259
|
|
260
|
@Override
|
261
|
public boolean onComplete() {
|
262
|
return false;
|
263
|
}
|
264
|
|
265
|
public TreeViewer getViewer() {
|
266
|
return viewer;
|
267
|
}
|
268
|
|
269
|
@PreDestroy
|
270
|
public void dispose() {
|
271
|
if(conversation!=null){
|
272
|
conversation.unregisterForDataStoreChanges(this);
|
273
|
conversation.close();
|
274
|
conversation = null;
|
275
|
}
|
276
|
if(input!=null){
|
277
|
input.dispose();
|
278
|
}
|
279
|
this.dirty.setDirty(false);
|
280
|
}
|
281
|
|
282
|
public TermEditorInput getDefinedTermEditorInput() {
|
283
|
return input;
|
284
|
}
|
285
|
|
286
|
@Override
|
287
|
public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
|
288
|
}
|
289
|
|
290
|
@Override
|
291
|
public void contextStop(IMemento memento, IProgressMonitor monitor) {
|
292
|
//close view when workbench closes
|
293
|
try{
|
294
|
thisPart.getContext().get(EPartService.class).hidePart(thisPart);
|
295
|
}
|
296
|
catch(Exception e){
|
297
|
//nothing
|
298
|
}
|
299
|
}
|
300
|
|
301
|
@Override
|
302
|
public void contextStart(IMemento memento, IProgressMonitor monitor) {
|
303
|
}
|
304
|
|
305
|
@Override
|
306
|
public void contextRefresh(IProgressMonitor monitor) {
|
307
|
}
|
308
|
|
309
|
@Override
|
310
|
public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
|
311
|
}
|
312
|
|
313
|
}
|