1
|
package eu.etaxonomy.taxeditor.editor.view.derivate;
|
2
|
|
3
|
import java.util.Collection;
|
4
|
import java.util.HashSet;
|
5
|
import java.util.Map.Entry;
|
6
|
import java.util.Set;
|
7
|
|
8
|
import org.eclipse.core.runtime.IProgressMonitor;
|
9
|
import org.eclipse.jface.action.MenuManager;
|
10
|
import org.eclipse.jface.util.LocalSelectionTransfer;
|
11
|
import org.eclipse.jface.viewers.AbstractTreeViewer;
|
12
|
import org.eclipse.jface.viewers.StructuredSelection;
|
13
|
import org.eclipse.jface.viewers.TreeNode;
|
14
|
import org.eclipse.jface.viewers.TreeViewer;
|
15
|
import org.eclipse.swt.SWT;
|
16
|
import org.eclipse.swt.dnd.DND;
|
17
|
import org.eclipse.swt.dnd.Transfer;
|
18
|
import org.eclipse.swt.widgets.Composite;
|
19
|
import org.eclipse.swt.widgets.Control;
|
20
|
import org.eclipse.swt.widgets.Menu;
|
21
|
import org.eclipse.swt.widgets.Tree;
|
22
|
import org.eclipse.ui.IEditorInput;
|
23
|
import org.eclipse.ui.IEditorPart;
|
24
|
import org.eclipse.ui.IEditorSite;
|
25
|
import org.eclipse.ui.IMemento;
|
26
|
import org.eclipse.ui.PartInitException;
|
27
|
import org.eclipse.ui.part.EditorPart;
|
28
|
|
29
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
30
|
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
|
31
|
import eu.etaxonomy.cdm.api.service.molecular.ISequenceService;
|
32
|
import eu.etaxonomy.cdm.model.molecular.Sequence;
|
33
|
import eu.etaxonomy.cdm.model.molecular.SingleRead;
|
34
|
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
|
35
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
36
|
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
|
37
|
import eu.etaxonomy.taxeditor.model.IContextListener;
|
38
|
import eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider;
|
39
|
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
|
40
|
import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
|
41
|
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
|
42
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
43
|
import eu.etaxonomy.taxeditor.view.derivateSearch.DerivateLabelProvider;
|
44
|
|
45
|
/**
|
46
|
* Displays the derivate hierarchy of all derivates having one single {@link FieldUnit} as root.
|
47
|
*
|
48
|
*/
|
49
|
public class DerivateView extends EditorPart implements IPartContentHasFactualData, IDirtyMarkableSelectionProvider,
|
50
|
IConversationEnabled, IPartContentHasDetails, IPartContentHasSupplementalData, IContextListener {
|
51
|
|
52
|
/**
|
53
|
* Key used for storing a single DerivateView in a memento
|
54
|
*/
|
55
|
private static final String DERIVATE_VIEW_MEMENTO_KEY = "derivateView";
|
56
|
|
57
|
/**
|
58
|
* Key to to group mementos for every single DerivateView
|
59
|
*/
|
60
|
private static final String DERIVATE_VIEWS_MEMENTO_KEY = "derivateViews";
|
61
|
|
62
|
public static final String ID = "eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView";
|
63
|
|
64
|
private ConversationHolder conversation;
|
65
|
|
66
|
private TreeViewer viewer;
|
67
|
|
68
|
private boolean isDirty;
|
69
|
|
70
|
private final int dndOperations = DND.DROP_MOVE;
|
71
|
|
72
|
private DerivateLabelProvider labelProvider;
|
73
|
|
74
|
private Set<SingleRead> multiLinkSingleReads;
|
75
|
|
76
|
/**
|
77
|
* Default constructor
|
78
|
*/
|
79
|
public DerivateView() {
|
80
|
CdmStore.getContextManager().addContextListener(this);
|
81
|
}
|
82
|
|
83
|
/* (non-Javadoc)
|
84
|
* @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
|
85
|
*/
|
86
|
@Override
|
87
|
public void createPartControl(Composite parent) {
|
88
|
viewer = new TreeViewer(new Tree(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION));
|
89
|
viewer.setContentProvider(new DerivateContentProvider());
|
90
|
labelProvider = new DerivateLabelProvider();
|
91
|
viewer.setLabelProvider(labelProvider);
|
92
|
viewer.setAutoExpandLevel(AbstractTreeViewer.ALL_LEVELS);
|
93
|
// Propagate selection from viewer
|
94
|
getSite().setSelectionProvider(viewer);
|
95
|
|
96
|
//create context menu
|
97
|
MenuManager menuManager = new MenuManager();
|
98
|
getSite().registerContextMenu(menuManager, viewer);
|
99
|
Control control = viewer.getControl();
|
100
|
Menu menu = menuManager.createContextMenu(control);
|
101
|
control.setMenu(menu);
|
102
|
|
103
|
generateMultiLinkSingleReads();
|
104
|
labelProvider.setMultiLinkSingleReads(multiLinkSingleReads);
|
105
|
IEditorInput editorInput = getEditorInput();
|
106
|
viewer.setInput(editorInput);
|
107
|
if(editorInput instanceof DerivateViewEditorInput){
|
108
|
SpecimenOrObservationBase<?> specimen = ((DerivateViewEditorInput) editorInput).getDerivate();
|
109
|
SpecimenOrObservationBase<?> rootElement = ((DerivateViewEditorInput) editorInput).getRootElement();
|
110
|
setPartName(DerivateLabelProvider.getDerivateText(rootElement));
|
111
|
viewer.setSelection(new StructuredSelection(new TreeNode(specimen)));
|
112
|
}
|
113
|
|
114
|
//add drag'n'drop support
|
115
|
Transfer[] transfers = new Transfer[] {LocalSelectionTransfer.getTransfer(),};
|
116
|
viewer.addDragSupport(dndOperations, transfers, new DerivateDragListener(this));
|
117
|
viewer.addDropSupport(dndOperations, transfers, new DerivateDropListener(this));
|
118
|
}
|
119
|
|
120
|
/* (non-Javadoc)
|
121
|
* @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.IProgressMonitor)
|
122
|
*/
|
123
|
@Override
|
124
|
public void doSave(IProgressMonitor monitor) {
|
125
|
monitor.beginTask("Saving Derivates", 3);
|
126
|
if (!conversation.isBound()) {
|
127
|
conversation.bind();
|
128
|
}
|
129
|
monitor.worked(1);
|
130
|
|
131
|
// commit the conversation and start a new transaction immediately
|
132
|
conversation.commit(true);
|
133
|
monitor.worked(1);
|
134
|
|
135
|
this.setDirty(false);
|
136
|
monitor.worked(1);
|
137
|
monitor.done();
|
138
|
firePropertyChange(PROP_DIRTY);
|
139
|
refreshTree();
|
140
|
}
|
141
|
|
142
|
/* (non-Javadoc)
|
143
|
* @see org.eclipse.ui.part.EditorPart#doSaveAs()
|
144
|
*/
|
145
|
@Override
|
146
|
public void doSaveAs() {
|
147
|
}
|
148
|
|
149
|
/* (non-Javadoc)
|
150
|
* @see org.eclipse.ui.part.EditorPart#getTitleToolTip()
|
151
|
*/
|
152
|
@Override
|
153
|
public String getTitleToolTip() {
|
154
|
return "Derivate View";
|
155
|
}
|
156
|
|
157
|
/* (non-Javadoc)
|
158
|
* @see org.eclipse.ui.part.EditorPart#init(org.eclipse.ui.IEditorSite, org.eclipse.ui.IEditorInput)
|
159
|
*/
|
160
|
@Override
|
161
|
public void init(IEditorSite site, IEditorInput input) throws PartInitException {
|
162
|
setSite(site);
|
163
|
setInput(input);
|
164
|
if(input instanceof DerivateViewEditorInput){
|
165
|
conversation = ((DerivateViewEditorInput) input).getConversationHolder();
|
166
|
}
|
167
|
}
|
168
|
|
169
|
/* (non-Javadoc)
|
170
|
* @see org.eclipse.ui.part.EditorPart#isDirty()
|
171
|
*/
|
172
|
@Override
|
173
|
public boolean isDirty() {
|
174
|
return isDirty;
|
175
|
}
|
176
|
|
177
|
/**
|
178
|
* @param isDirty the isDirty to set
|
179
|
*/
|
180
|
public void setDirty(boolean isDirty) {
|
181
|
this.isDirty = isDirty;
|
182
|
}
|
183
|
|
184
|
/* (non-Javadoc)
|
185
|
* @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
|
186
|
*/
|
187
|
@Override
|
188
|
public boolean isSaveAsAllowed() {
|
189
|
return false;
|
190
|
}
|
191
|
|
192
|
|
193
|
/* (non-Javadoc)
|
194
|
* @see org.eclipse.ui.part.WorkbenchPart#setFocus()
|
195
|
*/
|
196
|
@Override
|
197
|
public void setFocus() {
|
198
|
viewer.getControl().setFocus();
|
199
|
//make sure to bind again if maybe in another view the conversation was unbound
|
200
|
if(!conversation.isBound()){
|
201
|
conversation.bind();
|
202
|
}
|
203
|
}
|
204
|
|
205
|
/* (non-Javadoc)
|
206
|
* @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
|
207
|
*/
|
208
|
@Override
|
209
|
public void update(CdmDataChangeMap changeEvents) {
|
210
|
}
|
211
|
|
212
|
/* (non-Javadoc)
|
213
|
* @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
|
214
|
*/
|
215
|
@Override
|
216
|
public ConversationHolder getConversationHolder() {
|
217
|
return conversation;
|
218
|
}
|
219
|
|
220
|
/**
|
221
|
* @return the viewer
|
222
|
*/
|
223
|
public TreeViewer getViewer() {
|
224
|
return viewer;
|
225
|
}
|
226
|
|
227
|
/* (non-Javadoc)
|
228
|
* @see eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider#changed(java.lang.Object)
|
229
|
*/
|
230
|
@Override
|
231
|
public void changed(Object element) {
|
232
|
setDirty(true);
|
233
|
firePropertyChange(IEditorPart.PROP_DIRTY);
|
234
|
viewer.refresh();
|
235
|
}
|
236
|
|
237
|
/**
|
238
|
* Refreshes the derivate hierarchy tree
|
239
|
*/
|
240
|
public void refreshTree(){
|
241
|
generateMultiLinkSingleReads();
|
242
|
labelProvider.setMultiLinkSingleReads(multiLinkSingleReads);
|
243
|
viewer.refresh();
|
244
|
}
|
245
|
|
246
|
private void generateMultiLinkSingleReads() {
|
247
|
Set<SingleRead> multiLinkSingleReads = new HashSet<SingleRead>();
|
248
|
for(Entry<SingleRead, Collection<Sequence>> entry:CdmStore.getService(ISequenceService.class).getSingleReadSequencesMap().entrySet()){
|
249
|
if(entry.getValue().size()>1){
|
250
|
multiLinkSingleReads.add(entry.getKey());
|
251
|
}
|
252
|
}
|
253
|
this.multiLinkSingleReads = multiLinkSingleReads;
|
254
|
}
|
255
|
|
256
|
/**
|
257
|
* @return a set of {@link SingleRead}s that have multiple parents
|
258
|
*/
|
259
|
public Set<SingleRead> getMultiLinkSingleReads() {
|
260
|
return this.multiLinkSingleReads;
|
261
|
}
|
262
|
|
263
|
/* (non-Javadoc)
|
264
|
* @see eu.etaxonomy.taxeditor.model.IContextListener#contextAboutToStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
|
265
|
*/
|
266
|
@Override
|
267
|
public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
|
268
|
}
|
269
|
|
270
|
/* (non-Javadoc)
|
271
|
* @see eu.etaxonomy.taxeditor.model.IContextListener#contextStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
|
272
|
*/
|
273
|
@Override
|
274
|
public void contextStop(IMemento memento, IProgressMonitor monitor) {
|
275
|
}
|
276
|
|
277
|
/* (non-Javadoc)
|
278
|
* @see eu.etaxonomy.taxeditor.model.IContextListener#contextStart(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
|
279
|
*/
|
280
|
@Override
|
281
|
public void contextStart(IMemento memento, IProgressMonitor monitor) {
|
282
|
// if(memento == null){
|
283
|
// return;
|
284
|
// }
|
285
|
// IMemento editorMementos = memento.getChild(DERIVATE_VIEW_MEMENTO_KEY);
|
286
|
// DerivateViewEditorInput input = new DerivateViewEditorInput(UUID.fromString(editorInput.getString(DerivateViewEditorInput.UUID_MEMENTO_KEY)));
|
287
|
// try {
|
288
|
// EditorUtil.open(input);
|
289
|
// } catch (PartInitException e) {
|
290
|
// AbstractUtility.error(getClass(), "Error opening an editor window", e);
|
291
|
// }
|
292
|
}
|
293
|
|
294
|
/* (non-Javadoc)
|
295
|
* @see eu.etaxonomy.taxeditor.model.IContextListener#contextRefresh(org.eclipse.core.runtime.IProgressMonitor)
|
296
|
*/
|
297
|
@Override
|
298
|
public void contextRefresh(IProgressMonitor monitor) {
|
299
|
}
|
300
|
|
301
|
/* (non-Javadoc)
|
302
|
* @see eu.etaxonomy.taxeditor.model.IContextListener#workbenchShutdown(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
|
303
|
*/
|
304
|
@Override
|
305
|
public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
|
306
|
// if (!conversation.isBound()) {
|
307
|
// conversation.bind();
|
308
|
// }
|
309
|
// conversation.close();
|
310
|
// if(memento == null){
|
311
|
// return;
|
312
|
// }
|
313
|
//
|
314
|
// IEditorInput input = getEditorInput();
|
315
|
// if(input instanceof IPersistable){
|
316
|
// memento.createChild(DERIVATE_VIEW_MEMENTO_KEY);
|
317
|
// ((IPersistable) input).saveState(memento);
|
318
|
// }
|
319
|
}
|
320
|
|
321
|
}
|