1
|
/**
|
2
|
* Copyright (C) 2007 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
|
|
10
|
package eu.etaxonomy.taxeditor.featuretree.e4;
|
11
|
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.Arrays;
|
14
|
import java.util.HashMap;
|
15
|
import java.util.List;
|
16
|
import java.util.Map;
|
17
|
|
18
|
import javax.annotation.PostConstruct;
|
19
|
import javax.annotation.PreDestroy;
|
20
|
import javax.inject.Inject;
|
21
|
|
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.services.EMenuService;
|
26
|
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
|
27
|
import org.eclipse.jface.viewers.ISelectionChangedListener;
|
28
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
29
|
import org.eclipse.jface.viewers.SelectionChangedEvent;
|
30
|
import org.eclipse.jface.viewers.TreeViewer;
|
31
|
import org.eclipse.swt.SWT;
|
32
|
import org.eclipse.swt.events.ModifyEvent;
|
33
|
import org.eclipse.swt.events.ModifyListener;
|
34
|
import org.eclipse.swt.events.SelectionAdapter;
|
35
|
import org.eclipse.swt.events.SelectionEvent;
|
36
|
import org.eclipse.swt.widgets.Composite;
|
37
|
|
38
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
39
|
import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
|
40
|
import eu.etaxonomy.cdm.model.description.FeatureNode;
|
41
|
import eu.etaxonomy.cdm.model.description.FeatureTree;
|
42
|
import eu.etaxonomy.taxeditor.model.AbstractUtility;
|
43
|
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
|
44
|
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
|
45
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
46
|
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
|
47
|
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
|
48
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
49
|
import eu.etaxonomy.taxeditor.ui.dialog.selection.FeatureTreeSelectionDialog;
|
50
|
import eu.etaxonomy.taxeditor.workbench.part.IE4ViewerPart;
|
51
|
|
52
|
/**
|
53
|
*
|
54
|
* @author pplitzner
|
55
|
* @date 06.06.2017
|
56
|
*
|
57
|
*/
|
58
|
public class FeatureTreeEditor implements ICdmEntitySessionEnabled,
|
59
|
ModifyListener, ISelectionChangedListener, IE4ViewerPart, IPartContentHasDetails, IPartContentHasSupplementalData {
|
60
|
|
61
|
private ConversationHolder conversation;
|
62
|
|
63
|
private ICdmEntitySession cdmEntitySession;
|
64
|
|
65
|
@Inject
|
66
|
private ESelectionService selService;
|
67
|
|
68
|
@Inject
|
69
|
private MDirtyable dirty;
|
70
|
|
71
|
private FeatureTreeEditorComposite composite;
|
72
|
|
73
|
@Inject
|
74
|
public FeatureTreeEditor() {
|
75
|
}
|
76
|
|
77
|
/** {@inheritDoc} */
|
78
|
@PostConstruct
|
79
|
public void createControl(Composite parent, EMenuService menuService){
|
80
|
if (CdmStore.isActive()){
|
81
|
if(conversation == null){
|
82
|
conversation = CdmStore.createConversation();
|
83
|
}
|
84
|
if(cdmEntitySession!=null){
|
85
|
cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
|
86
|
}
|
87
|
}
|
88
|
else{
|
89
|
return;
|
90
|
}
|
91
|
composite = new FeatureTreeEditorComposite(parent, SWT.NULL);
|
92
|
composite.init(new FeatureNodeDragListener(composite.getViewer()),
|
93
|
new FeatureNodeDropAdapter(dirty, composite.getViewer()), this, new SelectionAdapter() {
|
94
|
@Override
|
95
|
public void widgetSelected(SelectionEvent e) {
|
96
|
if(isDirty()){
|
97
|
if(MessagingUtils.confirmDialog("Editor has to be saved", "You have to save before loading another feature tree. Save now?")){
|
98
|
save();
|
99
|
}
|
100
|
else{
|
101
|
return;
|
102
|
}
|
103
|
|
104
|
}
|
105
|
FeatureTree tree = FeatureTreeSelectionDialog.select(composite.getDisplay().getActiveShell(), conversation, null);
|
106
|
if (tree != null) {
|
107
|
composite.setSelectedTree(tree, FeatureTreeEditor.this);
|
108
|
}
|
109
|
}
|
110
|
});
|
111
|
|
112
|
//create context menu
|
113
|
menuService.registerContextMenu(composite.getViewer().getControl(), "eu.etaxonomy.taxeditor.store.popupmenu.featureTreeEditor");
|
114
|
}
|
115
|
|
116
|
public void setDirty(boolean isDirty){
|
117
|
this.dirty.setDirty(isDirty);
|
118
|
}
|
119
|
|
120
|
public boolean isDirty(){
|
121
|
return dirty.isDirty();
|
122
|
}
|
123
|
|
124
|
public FeatureTree getSelectedFeatureTree(){
|
125
|
return composite.getFeatureTree();
|
126
|
}
|
127
|
|
128
|
/** {@inheritDoc} */
|
129
|
@Override
|
130
|
public void modifyText(ModifyEvent e) {
|
131
|
composite.getFeatureTree().setTitleCache(composite.getText_title().getText(), true);
|
132
|
setDirty(true);
|
133
|
}
|
134
|
|
135
|
/** {@inheritDoc} */
|
136
|
@Override
|
137
|
public void selectionChanged(SelectionChangedEvent event) {
|
138
|
//propagate selection
|
139
|
selService.setSelection(AbstractUtility.getElementsFromSelectionChangedEvent(event));
|
140
|
}
|
141
|
|
142
|
@Focus
|
143
|
public void focus(){
|
144
|
if(composite!=null){
|
145
|
composite.getViewer().getControl().setFocus();
|
146
|
}
|
147
|
if(conversation!=null && !conversation.isBound()){
|
148
|
conversation.bind();
|
149
|
}
|
150
|
if(cdmEntitySession != null) {
|
151
|
cdmEntitySession.bind();
|
152
|
}
|
153
|
}
|
154
|
|
155
|
@Override
|
156
|
public void refresh(){
|
157
|
composite.getViewer().refresh();
|
158
|
}
|
159
|
|
160
|
public TreeViewer getViewer(){
|
161
|
return composite.getViewer();
|
162
|
}
|
163
|
|
164
|
/**
|
165
|
* {@inheritDoc}
|
166
|
*/
|
167
|
@Override
|
168
|
public IStructuredSelection getSelection() {
|
169
|
return (IStructuredSelection) composite.getViewer().getSelection();
|
170
|
}
|
171
|
|
172
|
@Persist
|
173
|
public void save(){
|
174
|
if (!conversation.isBound()) {
|
175
|
conversation.bind();
|
176
|
}
|
177
|
|
178
|
// commit the conversation and start a new transaction immediately
|
179
|
conversation.commit(true);
|
180
|
|
181
|
CdmStore.getService(IFeatureTreeService.class).saveOrUpdate(composite.getFeatureTree());
|
182
|
|
183
|
this.setDirty(false);
|
184
|
}
|
185
|
|
186
|
@PreDestroy
|
187
|
public void dispose(){
|
188
|
if(conversation!=null){
|
189
|
conversation.close();
|
190
|
}
|
191
|
if(cdmEntitySession != null) {
|
192
|
cdmEntitySession.dispose();
|
193
|
}
|
194
|
}
|
195
|
|
196
|
@Override
|
197
|
public ICdmEntitySession getCdmEntitySession() {
|
198
|
return cdmEntitySession;
|
199
|
}
|
200
|
|
201
|
@Override
|
202
|
public Map<Object, List<String>> getPropertyPathsMap() {
|
203
|
List<String> propertyPaths = Arrays.asList(new String[] {
|
204
|
"children", //$NON-NLS-1$
|
205
|
"feature", //$NON-NLS-1$
|
206
|
"featureTree", //$NON-NLS-1$
|
207
|
});
|
208
|
Map<Object, List<String>> propertyPathMap =
|
209
|
new HashMap<Object, List<String>>();
|
210
|
propertyPathMap.put(FeatureNode.class,propertyPaths);
|
211
|
return propertyPathMap;
|
212
|
}
|
213
|
|
214
|
/**
|
215
|
* {@inheritDoc}
|
216
|
*/
|
217
|
@Override
|
218
|
public List<FeatureTree> getRootEntities() {
|
219
|
List<FeatureTree> root = new ArrayList<>();
|
220
|
root.add(composite.getFeatureTree());
|
221
|
return root;
|
222
|
}
|
223
|
|
224
|
}
|