1
|
/**
|
2
|
* Copyright (C) 2015 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.navigation.navigator.e4.handler;
|
10
|
|
11
|
import java.util.HashSet;
|
12
|
import java.util.Set;
|
13
|
import java.util.UUID;
|
14
|
|
15
|
import javax.inject.Named;
|
16
|
|
17
|
import org.eclipse.core.commands.operations.AbstractOperation;
|
18
|
import org.eclipse.core.runtime.IStatus;
|
19
|
import org.eclipse.core.runtime.Status;
|
20
|
import org.eclipse.e4.core.di.annotations.CanExecute;
|
21
|
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
|
22
|
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
|
23
|
import org.eclipse.e4.ui.services.IServiceConstants;
|
24
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
25
|
import org.eclipse.jface.viewers.TreeSelection;
|
26
|
import org.eclipse.swt.widgets.Display;
|
27
|
import org.eclipse.swt.widgets.Shell;
|
28
|
|
29
|
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
|
30
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
31
|
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
|
32
|
import eu.etaxonomy.taxeditor.event.EventUtility;
|
33
|
import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
|
34
|
import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
|
35
|
import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
|
36
|
import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigatorLabels;
|
37
|
import eu.etaxonomy.taxeditor.navigation.navigator.operation.MoveFactualDataOperation;
|
38
|
import eu.etaxonomy.taxeditor.operation.e4.CdmHandlerE4;
|
39
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
40
|
import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
|
41
|
|
42
|
/**
|
43
|
* @author cmathew
|
44
|
* @date 19 Jun 2015
|
45
|
*
|
46
|
*/
|
47
|
public class MoveFactualDataSetNameInSourceHandlerE5 extends CdmHandlerE4 {
|
48
|
|
49
|
private TaxonNodeDto sourceTaxonNode;
|
50
|
private TaxonNode targetTaxonNode;
|
51
|
private boolean setNameInSource = false;
|
52
|
|
53
|
public MoveFactualDataSetNameInSourceHandlerE5() {
|
54
|
super(TaxonNavigatorLabels.MOVE_FACTUAL_DATA_LABEL);
|
55
|
}
|
56
|
|
57
|
@Override
|
58
|
public IStatus allowOperations(IStructuredSelection selection,
|
59
|
Shell shell,
|
60
|
MPart activePart,
|
61
|
MHandledMenuItem menuItem) {
|
62
|
// check that only a single taxon tree node has been selected
|
63
|
if(selection.size() > 1) {
|
64
|
return new Status(IStatus.ERROR,
|
65
|
"unknown", //$NON-NLS-1$
|
66
|
TaxonNavigatorLabels.SINGLE_TAXON_SELECTION_MESSAGE);
|
67
|
}
|
68
|
|
69
|
// check for no taxon tree node selected
|
70
|
if(selection.size() == 0) {
|
71
|
return new Status(IStatus.ERROR,
|
72
|
"unknown", //$NON-NLS-1$
|
73
|
TaxonNavigatorLabels.NO_TAXON_SELECTION_MESSAGE);
|
74
|
}
|
75
|
|
76
|
// check that selected object is a taxon node
|
77
|
Object obj = selection.iterator().next();
|
78
|
if(obj instanceof TaxonNodeDto) {
|
79
|
sourceTaxonNode = (TaxonNodeDto)obj;
|
80
|
} else {
|
81
|
return new Status(IStatus.ERROR,
|
82
|
"unknown", //$NON-NLS-1$
|
83
|
TaxonNavigatorLabels.SELECTED_OBJECT_NOT_TREE_NODE_MESSAGE);
|
84
|
}
|
85
|
|
86
|
if(NavigationUtil.isDirty(sourceTaxonNode, partService)) {
|
87
|
return new Status(IStatus.ERROR,
|
88
|
"unknown", //$NON-NLS-1$
|
89
|
TaxonNavigatorLabels.UNSAVED_CHANGES_MESSAGE);
|
90
|
}
|
91
|
|
92
|
Set<UUID> excludeTaxa = new HashSet<>();
|
93
|
excludeTaxa.add(sourceTaxonNode.getUuid());
|
94
|
|
95
|
targetTaxonNode = TaxonNodeSelectionDialog.select(shell,
|
96
|
// new ConversationHolderMock(),
|
97
|
Messages.RemotingMoveFactualDataHandler_CHOOSE_TAXA,
|
98
|
excludeTaxa,
|
99
|
CdmStore.getService(ITaxonNodeService.class).find(sourceTaxonNode.getUuid()),
|
100
|
sourceTaxonNode.getClassificationUUID());
|
101
|
|
102
|
if(targetTaxonNode == null) {
|
103
|
return new Status(IStatus.CANCEL,
|
104
|
"unknown", //$NON-NLS-1$
|
105
|
""); //$NON-NLS-1$
|
106
|
}
|
107
|
if(NavigationUtil.isDirty(targetTaxonNode, partService)){
|
108
|
return new Status(IStatus.ERROR,
|
109
|
"unknown", //$NON-NLS-1$
|
110
|
TaxonNavigatorLabels.UNSAVED_CHANGES_MESSAGE);
|
111
|
}
|
112
|
return Status.OK_STATUS;
|
113
|
}
|
114
|
|
115
|
@Override
|
116
|
public AbstractOperation prepareOperation(IStructuredSelection selection,
|
117
|
Shell shell,
|
118
|
MPart activePart,
|
119
|
MHandledMenuItem menuItem) {
|
120
|
if (menuItem.getElementId().equals("eu.etaxonomy.taxeditor.navigation.handledmenuitem.movefactualdataSetNameInSource")){
|
121
|
setNameInSource = true;
|
122
|
}
|
123
|
return new MoveFactualDataOperation(getTrigger(),
|
124
|
false,
|
125
|
sourceTaxonNode.getTaxonUuid(),
|
126
|
targetTaxonNode.getTaxon().getUuid(), setNameInSource);
|
127
|
}
|
128
|
|
129
|
@CanExecute
|
130
|
private boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)TreeSelection selection, MHandledMenuItem menuItem){
|
131
|
boolean canExecute = false;
|
132
|
canExecute = selection.size()==1
|
133
|
&& selection.getFirstElement() instanceof TaxonNodeDto
|
134
|
&& ((TaxonNodeDto)selection.getFirstElement()).getTaxonUuid() != null;
|
135
|
menuItem.setVisible(canExecute);
|
136
|
return canExecute;
|
137
|
}
|
138
|
|
139
|
@Override
|
140
|
public void onComplete() {
|
141
|
Display.getDefault().asyncExec(new Runnable(){
|
142
|
@Override
|
143
|
public void run() {
|
144
|
|
145
|
EventUtility.postEvent(WorkbenchEventConstants.REFRESH_NAME_EDITOR, sourceTaxonNode.getTaxonUuid());
|
146
|
EventUtility.postEvent(WorkbenchEventConstants.REFRESH_NAME_EDITOR, targetTaxonNode.getTaxon().getUuid());
|
147
|
//FIXME E4 refresh factual view via events or similar
|
148
|
// try {
|
149
|
// //close and re-open to refresh factual data view
|
150
|
// MultiPageTaxonEditor sourceEditor = (MultiPageTaxonEditor) EditorUtil.findEditorByTaxonNodeUuid(sourceTaxonNode.getUuid());
|
151
|
// MultiPageTaxonEditor targetEditor = (MultiPageTaxonEditor) EditorUtil.findEditorByTaxonNodeUuid(targetTaxonNode.getUuid());
|
152
|
// if(targetEditor != null){
|
153
|
// AbstractUtility.close(sourceEditor);
|
154
|
// AbstractUtility.close(targetEditor);
|
155
|
// }
|
156
|
// EditorUtil.openTaxonNodeE4(sourceTaxonNode.getUuid());
|
157
|
// EditorUtil.openTaxonNodeE4(targetTaxonNode.getUuid());
|
158
|
// } catch (PartInitException e) {
|
159
|
// MessagingUtils.error(this.getClass(), e);
|
160
|
// throw new RuntimeException(e);
|
161
|
// } catch (Exception e) {
|
162
|
// MessagingUtils.warningDialog(Messages.RemotingMoveFactualDataHandler_CREATE_FAILED, this, e.getMessage());
|
163
|
// }
|
164
|
}
|
165
|
|
166
|
});
|
167
|
}
|
168
|
|
169
|
/**
|
170
|
* {@inheritDoc}
|
171
|
*/
|
172
|
@Override
|
173
|
protected Object getTrigger() {
|
174
|
return this;
|
175
|
}
|
176
|
}
|