1 package eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
;
3 import java
.util
.ArrayList
;
4 import java
.util
.Arrays
;
5 import java
.util
.Collection
;
6 import java
.util
.HashMap
;
7 import java
.util
.HashSet
;
10 import java
.util
.Map
.Entry
;
12 import java
.util
.UUID
;
14 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
15 import org
.eclipse
.jface
.action
.MenuManager
;
16 import org
.eclipse
.jface
.util
.LocalSelectionTransfer
;
17 import org
.eclipse
.jface
.viewers
.AbstractTreeViewer
;
18 import org
.eclipse
.jface
.viewers
.ISelection
;
19 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
20 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
21 import org
.eclipse
.jface
.viewers
.TreeNode
;
22 import org
.eclipse
.jface
.viewers
.TreeSelection
;
23 import org
.eclipse
.jface
.viewers
.TreeViewer
;
24 import org
.eclipse
.swt
.SWT
;
25 import org
.eclipse
.swt
.dnd
.DND
;
26 import org
.eclipse
.swt
.dnd
.Transfer
;
27 import org
.eclipse
.swt
.layout
.GridData
;
28 import org
.eclipse
.swt
.layout
.GridLayout
;
29 import org
.eclipse
.swt
.widgets
.Composite
;
30 import org
.eclipse
.swt
.widgets
.Control
;
31 import org
.eclipse
.swt
.widgets
.Menu
;
32 import org
.eclipse
.swt
.widgets
.Tree
;
33 import org
.eclipse
.ui
.IEditorPart
;
34 import org
.eclipse
.ui
.IMemento
;
35 import org
.eclipse
.ui
.ISaveablePart2
;
36 import org
.eclipse
.ui
.IWorkbenchPart
;
38 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
39 import eu
.etaxonomy
.cdm
.api
.service
.IOccurrenceService
;
40 import eu
.etaxonomy
.cdm
.api
.service
.molecular
.ISequenceService
;
41 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
42 import eu
.etaxonomy
.cdm
.model
.molecular
.Sequence
;
43 import eu
.etaxonomy
.cdm
.model
.molecular
.SingleRead
;
44 import eu
.etaxonomy
.cdm
.model
.occurrence
.FieldUnit
;
45 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
46 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
47 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
48 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
49 import eu
.etaxonomy
.taxeditor
.editor
.Messages
;
50 import eu
.etaxonomy
.taxeditor
.editor
.MultiPageTaxonEditor
;
51 import eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.searchFilter
.DerivateSearchCompositeController
;
52 import eu
.etaxonomy
.taxeditor
.model
.IContextListener
;
53 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
54 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasFactualData
;
55 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasMedia
;
56 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasSupplementalData
;
57 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySession
;
58 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
59 import eu
.etaxonomy
.taxeditor
.view
.AbstractCdmViewPart
;
60 import eu
.etaxonomy
.taxeditor
.view
.derivateSearch
.DerivateContentProvider
;
61 import eu
.etaxonomy
.taxeditor
.view
.derivateSearch
.DerivateLabelProvider
;
64 * Displays the derivate hierarchy of the specimen specified in the editor input.
67 public class DerivateView
extends AbstractCdmViewPart
implements IPartContentHasFactualData
, ISaveablePart2
,
68 IPartContentHasDetails
, IPartContentHasSupplementalData
, IPartContentHasMedia
, IContextListener
{
69 public static final String ID
= "eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView"; //$NON-NLS-1$
71 public static final String YOU_NEED_TO_SAVE_BEFORE_PERFORMING_THIS_ACTION
= Messages
.DerivateView_YOU_NEED_TO_SAVE
;
72 public static final String VIEW_HAS_UNSAVED_CHANGES
= Messages
.DerivateView_UNSAVED_CHANGES
;
74 private static final List
<String
> SPECIMEN_INIT_STRATEGY
= Arrays
.asList(new String
[] {
82 "derivationEvents.derivatives.annotations",
83 "derivationEvents.derivatives.markers",
84 "derivationEvents.derivatives.credits",
85 "derivationEvents.derivatives.extensions",
86 "derivationEvents.derivatives.rights",
87 "derivationEvents.derivatives.sources"
90 private ConversationHolder conversation
;
92 private TreeViewer viewer
;
94 private boolean isDirty
;
96 private final int dndOperations
= DND
.DROP_MOVE
;
98 private DerivateLabelProvider labelProvider
;
100 private Set
<SingleRead
> multiLinkSingleReads
;
102 private ISelection selection
= null;
105 private DerivateContentProvider contentProvider
;
107 private DerivateSearchCompositeController derivateSearchCompositeController
;
110 * A map with keys being the derivative entities belonging to the {@link UUID}s passed to the constructor
111 * and values being the root elements of the hierarchy (may be the same objects as the derivative entities)
113 private Map
<SpecimenOrObservationBase
<?
>, SpecimenOrObservationBase
<?
>> derivateToRootEntityMap
;
116 * The set of root elements
118 private Set
<SpecimenOrObservationBase
<?
>> rootElements
;
120 private ICdmEntitySession cdmEntitySession
;
123 * <code>true</code> if this view is listening to selection changes
125 private boolean listenToSelectionChange
;
128 * Default constructor
130 public DerivateView() {
137 private void init() {
138 this.derivateToRootEntityMap
= new HashMap
<SpecimenOrObservationBase
<?
>, SpecimenOrObservationBase
<?
>>();
139 this.rootElements
= new HashSet
<SpecimenOrObservationBase
<?
>>();
141 if (CdmStore
.isActive() && conversation
== null) {
142 conversation
= CdmStore
.createConversation();
144 if (CdmStore
.isActive()) {
145 cdmEntitySession
= CdmStore
.getCurrentSessionManager().newSession(this, true);
147 //listen to context changes
148 CdmStore
.getContextManager().addContextListener(this);
152 public void createPartControl(Composite parent
) {
154 parent
.setLayout(new GridLayout());
156 //---search and filter---
157 derivateSearchCompositeController
= new DerivateSearchCompositeController(parent
, this);
158 derivateSearchCompositeController
.setEnabled(CdmStore
.isActive());
159 GridData gridDataSearchBar
= new GridData();
160 gridDataSearchBar
.horizontalAlignment
= GridData
.FILL
;
161 gridDataSearchBar
.grabExcessHorizontalSpace
= true;
162 derivateSearchCompositeController
.setLayoutData(gridDataSearchBar
);
165 viewer
= new TreeViewer(new Tree(parent
, SWT
.MULTI
| SWT
.H_SCROLL
| SWT
.V_SCROLL
| SWT
.FULL_SELECTION
));
166 GridData gridDataTree
= new GridData();
167 gridDataTree
.horizontalAlignment
= GridData
.FILL
;
168 gridDataTree
.verticalAlignment
= GridData
.FILL
;
169 gridDataTree
.grabExcessVerticalSpace
= true;
170 gridDataTree
.grabExcessHorizontalSpace
= true;
171 viewer
.getTree().setLayoutData(gridDataTree
);
172 contentProvider
= new DerivateContentProvider();
173 viewer
.setContentProvider(contentProvider
);
174 labelProvider
= new DerivateLabelProvider();
175 labelProvider
.setConversation(conversation
);
176 viewer
.setLabelProvider(labelProvider
);
177 viewer
.setAutoExpandLevel(AbstractTreeViewer
.ALL_LEVELS
);
178 // Propagate selection from viewer
179 getSite().setSelectionProvider(viewer
);
181 //listen to selection changes
182 selectionService
= getSite().getWorkbenchWindow().getSelectionService();
183 selectionService
.addSelectionListener(this);
185 //create context menu
186 MenuManager menuManager
= new MenuManager();
187 getSite().registerContextMenu(menuManager
, viewer
);
188 Control control
= viewer
.getControl();
189 Menu menu
= menuManager
.createContextMenu(control
);
190 control
.setMenu(menu
);
193 updateRootEntities();
195 //add drag'n'drop support
196 Transfer
[] transfers
= new Transfer
[] {LocalSelectionTransfer
.getTransfer(),};
197 viewer
.addDragSupport(dndOperations
, transfers
, new DerivateDragListener(this));
198 viewer
.addDropSupport(dndOperations
, transfers
, new DerivateDropListener(this));
201 public void updateRootEntities() {
202 updateRootEntities(null);
205 public void updateRootEntities(Collection
<UUID
> derivativeUuids
) {
206 if(conversation
!=null){
207 if (!conversation
.isBound()) {
211 * If the active session is not the session of the Derivate Editor then we will
212 * save it, bind temporarily to our session and rebind to the original session.
213 * This happens e.g. if a selection change happens in the taxon editor and
214 * "Link with editor" is enabled. The selection change event and thus the
215 * loading in updateRootEntities() happens in the session of the taxon
218 ICdmEntitySession previousCdmEntitySession
= CdmStore
.getCurrentSessionManager().getActiveSession();
219 if(cdmEntitySession
!= null) {
220 cdmEntitySession
.bind();
223 if(derivativeUuids
!=null){
224 this.derivateToRootEntityMap
= new HashMap
<SpecimenOrObservationBase
<?
>, SpecimenOrObservationBase
<?
>>();
225 this.rootElements
= new HashSet
<SpecimenOrObservationBase
<?
>>();
226 for (UUID uuid
: derivativeUuids
) {
227 SpecimenOrObservationBase
<?
> derivate
= CdmStore
.getService(IOccurrenceService
.class).load(uuid
, SPECIMEN_INIT_STRATEGY
);
228 if(derivate
instanceof FieldUnit
){
229 derivateToRootEntityMap
.put(derivate
, derivate
);
232 SpecimenOrObservationBase
<?
> topMostDerivate
= EditorUtil
.getTopMostDerivate(derivate
);
233 if(topMostDerivate
!=null){
234 derivateToRootEntityMap
.put(derivate
, topMostDerivate
);
237 derivateToRootEntityMap
.put(derivate
, derivate
);
241 for (SpecimenOrObservationBase
<?
> specimen
: derivateToRootEntityMap
.values()) {
242 rootElements
.add(specimen
);
245 viewer
.setInput(rootElements
);
247 previousCdmEntitySession
.bind();
252 public void doSave(IProgressMonitor monitor
) {
253 String taskName
= Messages
.DerivateView_SAVING_HIERARCHY
;
254 monitor
.beginTask(taskName
, 3);
255 if (!conversation
.isBound()) {
260 // commit the conversation and start a new transaction immediately
261 conversation
.commit(true);
263 if(CdmStore
.getCurrentSessionManager().isRemoting()) {
264 CdmStore
.getService(IOccurrenceService
.class).merge(new ArrayList
<SpecimenOrObservationBase
>(rootElements
), true);
268 this.setDirty(false);
271 firePropertyChange(PROP_DIRTY
);
276 public void doSaveAs() {
280 public String
getTitleToolTip() {
281 return Messages
.DerivateView_DERIVATIVE_EDITOR
;
285 public boolean isDirty() {
290 * @param isDirty the isDirty to set
292 public void setDirty(boolean isDirty
) {
293 this.isDirty
= isDirty
;
297 public boolean isSaveAsAllowed() {
302 public void setFocus() {
303 viewer
.getControl().setFocus();
304 //make sure to bind again if maybe in another view the conversation was unbound
305 if(conversation
!=null && !conversation
.isBound()){
308 if(cdmEntitySession
!= null) {
309 cdmEntitySession
.bind();
314 public void update(CdmDataChangeMap changeEvents
) {
318 public ConversationHolder
getConversationHolder() {
326 public TreeViewer
getViewer() {
331 public void changed(Object element
) {
333 firePropertyChange(IEditorPart
.PROP_DIRTY
);
338 public void forceDirty() {
343 public Map
<Object
, List
<String
>> getPropertyPathsMap() {
344 List
<String
> specimenPropertyPaths
= Arrays
.asList(new String
[] {
346 "derivationEvents.derivates",
354 Map
<Object
, List
<String
>> specimenPropertyPathMap
=
355 new HashMap
<Object
, List
<String
>>();
356 specimenPropertyPathMap
.put(SpecimenOrObservationBase
.class,specimenPropertyPaths
);
357 return specimenPropertyPathMap
;
361 * Refreshes the derivate hierarchy tree and expands the tree
362 * to show and select the given object.
364 * @param expandTo the object to which the tree should be expanded
366 public void refreshTree(Object expandTo
){
368 TreeSelection selection
= (TreeSelection
) viewer
.getSelection();
369 viewer
.expandToLevel(selection
.getFirstElement(), 1);
370 viewer
.setSelection(new StructuredSelection(new TreeNode(expandTo
)));
374 * Refreshes the derivate hierarchy tree
376 public void refreshTree(){
377 generateMultiLinkSingleReads();
378 labelProvider
.setMultiLinkSingleReads(multiLinkSingleReads
);
382 //FIXME:Remoting hack to make this work for remoting
383 //This should actually be resolved using remoting post operations
384 public void remove(Object obj
) {
385 rootElements
.remove(obj
);
386 viewer
.setInput(rootElements
);
389 private void generateMultiLinkSingleReads() {
390 Set
<SingleRead
> multiLinkSingleReads
= new HashSet
<SingleRead
>();
391 for(Entry
<SingleRead
, Collection
<Sequence
>> entry
:CdmStore
.getService(ISequenceService
.class).getSingleReadSequencesMap().entrySet()){
392 if(entry
.getValue().size()>1){
393 multiLinkSingleReads
.add(entry
.getKey());
396 this.multiLinkSingleReads
= multiLinkSingleReads
;
400 * @return a set of {@link SingleRead}s that have multiple parents
402 public Set
<SingleRead
> getMultiLinkSingleReads() {
403 return this.multiLinkSingleReads
;
406 public ISelection
getSelection() {
410 public DerivateLabelProvider
getLabelProvider() {
411 return labelProvider
;
415 public boolean postOperation(CdmBase objectAffectedByOperation
) {
417 if(objectAffectedByOperation
!=null){
418 changed(objectAffectedByOperation
);
424 public boolean onComplete() {
430 public boolean canAttachMedia() {
434 public void removeHierarchy(SpecimenOrObservationBase
<?
> specimenOrObservationBase
) {
435 SpecimenOrObservationBase
<?
> rootElement
= derivateToRootEntityMap
.remove(specimenOrObservationBase
);
436 rootElements
.remove(rootElement
);
439 public void addHierarchy(FieldUnit fieldUnit
) {
440 rootElements
.add(fieldUnit
);
441 derivateToRootEntityMap
.put(fieldUnit
, fieldUnit
);
445 public ICdmEntitySession
getCdmEntitySession() {
446 return cdmEntitySession
;
450 public void dispose() {
452 if(conversation
!=null){
453 conversation
.close();
455 if(cdmEntitySession
!= null) {
456 cdmEntitySession
.dispose();
461 public void selectionChanged(IWorkbenchPart part
, ISelection selection
) {
462 if(listenToSelectionChange
&& selection
instanceof IStructuredSelection
){
463 Object selectedElement
= ((IStructuredSelection
) selection
).getFirstElement();
464 if(selectedElement
instanceof Taxon
){
465 Collection
<SpecimenOrObservationBase
> fieldUnits
= CdmStore
.getService(IOccurrenceService
.class).listFieldUnitsByAssociatedTaxon((Taxon
) selectedElement
, null, null);
466 Collection
<UUID
> uuids
= new HashSet
<UUID
>();
467 for (SpecimenOrObservationBase specimenOrObservationBase
: fieldUnits
) {
468 uuids
.add(specimenOrObservationBase
.getUuid());
470 updateRootEntities(uuids
);
473 if(part
instanceof MultiPageTaxonEditor
){
474 Taxon taxon
= ((MultiPageTaxonEditor
) part
).getTaxon();
475 derivateSearchCompositeController
.setTaxon(taxon
);
483 public List
<SpecimenOrObservationBase
<?
>> getRootEntities() {
484 return new ArrayList
<SpecimenOrObservationBase
<?
>>(rootElements
);
488 public void createViewer(Composite parent
) {
489 // TODO Auto-generated method stub
494 public boolean isSaveOnCloseNeeded() {
499 public int promptToSaveOnClose() {
500 return ISaveablePart2
.DEFAULT
;
503 public void toggleListenToSelectionChange() {
504 listenToSelectionChange
= !listenToSelectionChange
;
505 derivateSearchCompositeController
.setEnabled(!listenToSelectionChange
);
506 derivateSearchCompositeController
.setTaxon(null);
513 public void contextAboutToStop(IMemento memento
, IProgressMonitor monitor
) {
520 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
521 derivateSearchCompositeController
.setEnabled(false);
522 viewer
.setInput(null);
529 public void contextStart(IMemento memento
, IProgressMonitor monitor
) {
531 derivateSearchCompositeController
.setEnabled(!listenToSelectionChange
);
539 public void contextRefresh(IProgressMonitor monitor
) {
546 public void workbenchShutdown(IMemento memento
, IProgressMonitor monitor
) {