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