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