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
.Collections
;
7 import java
.util
.HashMap
;
8 import java
.util
.HashSet
;
12 import java
.util
.UUID
;
14 import javax
.annotation
.PostConstruct
;
15 import javax
.annotation
.PreDestroy
;
16 import javax
.inject
.Inject
;
17 import javax
.inject
.Named
;
19 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
20 import org
.eclipse
.e4
.core
.di
.annotations
.Optional
;
21 import org
.eclipse
.e4
.ui
.di
.Focus
;
22 import org
.eclipse
.e4
.ui
.di
.Persist
;
23 import org
.eclipse
.e4
.ui
.model
.application
.ui
.MDirtyable
;
24 import org
.eclipse
.e4
.ui
.model
.application
.ui
.basic
.MPart
;
25 import org
.eclipse
.e4
.ui
.services
.EMenuService
;
26 import org
.eclipse
.e4
.ui
.services
.IServiceConstants
;
27 import org
.eclipse
.e4
.ui
.workbench
.modeling
.EPartService
;
28 import org
.eclipse
.e4
.ui
.workbench
.modeling
.ESelectionService
;
29 import org
.eclipse
.jface
.util
.LocalSelectionTransfer
;
30 import org
.eclipse
.jface
.viewers
.AbstractTreeViewer
;
31 import org
.eclipse
.jface
.viewers
.ISelection
;
32 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
33 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
34 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
35 import org
.eclipse
.jface
.viewers
.TreeNode
;
36 import org
.eclipse
.jface
.viewers
.TreeSelection
;
37 import org
.eclipse
.jface
.viewers
.TreeViewer
;
38 import org
.eclipse
.swt
.SWT
;
39 import org
.eclipse
.swt
.dnd
.DND
;
40 import org
.eclipse
.swt
.dnd
.Transfer
;
41 import org
.eclipse
.swt
.layout
.GridData
;
42 import org
.eclipse
.swt
.layout
.GridLayout
;
43 import org
.eclipse
.swt
.widgets
.Composite
;
44 import org
.eclipse
.swt
.widgets
.Tree
;
45 import org
.eclipse
.ui
.IMemento
;
47 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
48 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
49 import eu
.etaxonomy
.cdm
.api
.service
.IOccurrenceService
;
50 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
51 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
52 import eu
.etaxonomy
.cdm
.model
.molecular
.SingleRead
;
53 import eu
.etaxonomy
.cdm
.model
.occurrence
.FieldUnit
;
54 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
55 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
56 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
57 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
58 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
59 import eu
.etaxonomy
.taxeditor
.editor
.MultiPageTaxonEditor
;
60 import eu
.etaxonomy
.taxeditor
.editor
.l10n
.Messages
;
61 import eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.searchFilter
.DerivateSearchCompositeController
;
62 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
63 import eu
.etaxonomy
.taxeditor
.model
.IContextListener
;
64 import eu
.etaxonomy
.taxeditor
.model
.IDirtyMarkable
;
65 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
66 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasFactualData
;
67 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasMedia
;
68 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasSupplementalData
;
69 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
70 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
71 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySession
;
72 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionEnabled
;
73 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
74 import eu
.etaxonomy
.taxeditor
.view
.derivateSearch
.DerivateContentProvider
;
75 import eu
.etaxonomy
.taxeditor
.view
.derivateSearch
.DerivateLabelProvider
;
76 import eu
.etaxonomy
.taxeditor
.workbench
.part
.IE4SavablePart
;
79 * Displays the derivate hierarchy of the specimen specified in the editor input.
82 public class DerivateView
implements IPartContentHasFactualData
, IConversationEnabled
,
83 ICdmEntitySessionEnabled
, IDirtyMarkable
, IPostOperationEnabled
, IPartContentHasDetails
, IPartContentHasSupplementalData
, IPartContentHasMedia
,
84 IContextListener
, IE4SavablePart
{
86 private static final String SPECIMEN_EDITOR
= Messages
.DerivateView_SPECIMEN_EDITOR
;
88 public static final String ID
= "eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView"; //$NON-NLS-1$
89 public static final String INPUT_ID
= ID
+".editorInput"; //$NON-NLS-1$
91 public static final String YOU_NEED_TO_SAVE_BEFORE_PERFORMING_THIS_ACTION
= Messages
.DerivateView_YOU_NEED_TO_SAVE
;
92 public static final String VIEW_HAS_UNSAVED_CHANGES
= Messages
.DerivateView_UNSAVED_CHANGES
;
94 private static final List
<String
> SPECIMEN_INIT_STRATEGY
= Arrays
.asList(new String
[] {
95 "descriptions", //$NON-NLS-1$
96 "annotations", //$NON-NLS-1$
97 "markers", //$NON-NLS-1$
98 "credits", //$NON-NLS-1$
99 "extensions", //$NON-NLS-1$
100 "rights", //$NON-NLS-1$
101 "sources", //$NON-NLS-1$
102 "derivationEvents.derivatives.annotations", //$NON-NLS-1$
103 "derivationEvents.derivatives.markers", //$NON-NLS-1$
104 "derivationEvents.derivatives.credits", //$NON-NLS-1$
105 "derivationEvents.derivatives.extensions", //$NON-NLS-1$
106 "derivationEvents.derivatives.rights", //$NON-NLS-1$
107 "derivationEvents.derivatives.sources" //$NON-NLS-1$
110 private static final int WARN_THRESHOLD
= 200;
113 private ConversationHolder conversation
;
115 private TreeViewer viewer
;
117 private final int dndOperations
= DND
.DROP_MOVE
;
119 private DerivateLabelProvider labelProvider
;
121 private DerivateContentProvider contentProvider
;
123 private DerivateSearchCompositeController derivateSearchCompositeController
;
126 * A map with keys being the derivative entities belonging to the {@link UUID}s passed to the constructor
127 * and values being the root elements of the hierarchy (may be the same objects as the derivative entities)
129 private Map
<SpecimenOrObservationBase
<?
>, SpecimenOrObservationBase
<?
>> derivateToRootEntityMap
;
132 * The set of root elements
134 private Set
<SpecimenOrObservationBase
<?
>> rootElements
;
136 private ICdmEntitySession cdmEntitySession
;
139 * <code>true</code> if this view is listening to selection changes
141 private boolean listenToSelectionChange
;
143 private Taxon selectedTaxon
;
146 private ESelectionService selService
;
149 private MDirtyable dirty
;
152 private EPartService partService
;
154 private ISelectionChangedListener selectionChangedListener
;
157 * Default constructor
160 public DerivateView() {
166 public void init(DerivateViewEditorInput editorInput
){
167 this.derivateToRootEntityMap
= new HashMap
<SpecimenOrObservationBase
<?
>, SpecimenOrObservationBase
<?
>>();
168 this.rootElements
= new HashSet
<SpecimenOrObservationBase
<?
>>();
171 Collection
<UUID
> derivativeUuids
= editorInput
.getDerivativeUuids();
172 checkWarnThreshold(derivativeUuids
);
173 updateRootEntities(derivativeUuids
);
175 derivateSearchCompositeController
.setTaxonFilter(editorInput
.getTaxonUuid());
178 // getEditorSite().getActionBars().getStatusLineManager().setMessage(""); //$NON-NLS-1$
182 public void createPartControl(Composite parent
, EMenuService menuService
) {
183 if (CdmStore
.isActive() && conversation
== null) {
184 conversation
= CdmStore
.createConversation();
186 if (CdmStore
.isActive()) {
187 cdmEntitySession
= CdmStore
.getCurrentSessionManager().newSession(this, true);
189 //listen to context changes
190 CdmStore
.getContextManager().addContextListener(this);
192 parent
.setLayout(new GridLayout());
194 //---search and filter---
195 derivateSearchCompositeController
= new DerivateSearchCompositeController(parent
, this);
196 GridData gridDataSearchBar
= new GridData();
197 gridDataSearchBar
.horizontalAlignment
= GridData
.FILL
;
198 gridDataSearchBar
.grabExcessHorizontalSpace
= true;
199 derivateSearchCompositeController
.setLayoutData(gridDataSearchBar
);
200 derivateSearchCompositeController
.setEnabled(CdmStore
.isActive());
203 viewer
= new TreeViewer(new Tree(parent
, SWT
.MULTI
| SWT
.H_SCROLL
| SWT
.V_SCROLL
| SWT
.FULL_SELECTION
));
204 GridData gridDataTree
= new GridData();
205 gridDataTree
.horizontalAlignment
= GridData
.FILL
;
206 gridDataTree
.verticalAlignment
= GridData
.FILL
;
207 gridDataTree
.grabExcessVerticalSpace
= true;
208 gridDataTree
.grabExcessHorizontalSpace
= true;
209 viewer
.getTree().setLayoutData(gridDataTree
);
210 contentProvider
= new DerivateContentProvider();
211 viewer
.setContentProvider(contentProvider
);
212 labelProvider
= new DerivateLabelProvider();
213 labelProvider
.setConversation(conversation
);
214 viewer
.setLabelProvider(labelProvider
);
215 viewer
.setAutoExpandLevel(AbstractTreeViewer
.ALL_LEVELS
);
216 viewer
.getTree().setEnabled(CdmStore
.isActive());
218 //propagate selection
219 selectionChangedListener
= (event
-> selService
.setSelection(AbstractUtility
.getElementsFromSelectionChangedEvent(event
)));
220 viewer
.addSelectionChangedListener(selectionChangedListener
);
222 //create context menu
223 menuService
.registerContextMenu(viewer
.getControl(), "eu.etaxonomy.taxeditor.editor.popupmenu.specimeneditor");
225 //add drag'n'drop support
226 Transfer
[] transfers
= new Transfer
[] {LocalSelectionTransfer
.getTransfer(),};
227 viewer
.addDragSupport(dndOperations
, transfers
, new DerivateDragListener(this));
228 viewer
.addDropSupport(dndOperations
, transfers
, new DerivateDropListener(this));
231 public void updateRootEntities() {
232 updateRootEntities((Collection
)null);
235 public void updateRootEntities(Collection
<UUID
> derivativeUuids
) {
236 if(conversation
!=null){
237 if (!conversation
.isBound()) {
241 * If the active session is not the session of the Derivative Editor
242 * then we will save the active session for later, bind temporarily
243 * to our session and rebind to the original session when we are
244 * done. This happens e.g. if a selection change happens in the
245 * taxon editor and "Link with editor" is enabled. The selection
246 * change event and thus the loading in updateRootEntities() happens
247 * in the session of the taxon editor.
249 ICdmEntitySession previousCdmEntitySession
= CdmStore
.getCurrentSessionManager().getActiveSession();
250 if(cdmEntitySession
!= null) {
251 cdmEntitySession
.bind();
254 List
<SpecimenOrObservationBase
> derivates
= null;
255 if(derivativeUuids
!=null){
256 this.derivateToRootEntityMap
= new HashMap
<SpecimenOrObservationBase
<?
>, SpecimenOrObservationBase
<?
>>();
257 this.rootElements
= new HashSet
<SpecimenOrObservationBase
<?
>>();
258 derivates
= CdmStore
.getService(IOccurrenceService
.class).load(new ArrayList(derivativeUuids
), SPECIMEN_INIT_STRATEGY
);
260 updateRootEntities(derivates
);
261 previousCdmEntitySession
.bind();
266 public void updateRootEntities(List
<SpecimenOrObservationBase
> derivates
) {
268 this.derivateToRootEntityMap
= new HashMap
<SpecimenOrObservationBase
<?
>, SpecimenOrObservationBase
<?
>>();
269 this.rootElements
= new HashSet
<SpecimenOrObservationBase
<?
>>();
270 for (SpecimenOrObservationBase derivate
: derivates
) {
272 if(derivate
instanceof FieldUnit
){
273 derivateToRootEntityMap
.put(derivate
, derivate
);
276 SpecimenOrObservationBase
<?
> topMostDerivate
= EditorUtil
.getTopMostDerivate(derivate
);
277 if(topMostDerivate
!=null){
278 derivateToRootEntityMap
.put(derivate
, topMostDerivate
);
281 derivateToRootEntityMap
.put(derivate
, derivate
);
285 for (SpecimenOrObservationBase
<?
> specimen
: derivateToRootEntityMap
.values()) {
286 rootElements
.add(specimen
);
289 labelProvider
.updateLabelCache(rootElements
);
290 viewer
.setInput(rootElements
);
293 // getEditorSite().getActionBars().getStatusLineManager().setMessage(String.format(Messages.DerivateView_CNT_DERIVATIVES_FOUND, rootElements.size()));
295 //set selection to derivatives if the filter criteria
296 //taxon assignment or derivative type are set
297 if(derivates
!=null && !derivateSearchCompositeController
.isDefaultSearch()){
298 List
<TreeNode
> nodesToSelect
= new ArrayList
<TreeNode
>();
299 for (SpecimenOrObservationBase specimenOrObservationBase
: derivates
) {
300 nodesToSelect
.add(new TreeNode(specimenOrObservationBase
));
302 setSelection(new StructuredSelection(nodesToSelect
));
309 private void setSelection(StructuredSelection selection
){
310 viewer
.removeSelectionChangedListener(selectionChangedListener
);
311 viewer
.setSelection(selection
);
312 viewer
.addSelectionChangedListener(selectionChangedListener
);
315 public void updateLabelCache(){
316 labelProvider
.updateLabelCache(rootElements
);
321 public void save(IProgressMonitor monitor
) {
322 String taskName
= Messages
.DerivateView_SAVING_HIERARCHY
;
323 monitor
.beginTask(taskName
, 3);
324 if (!conversation
.isBound()) {
329 // commit the conversation and start a new transaction immediately
330 conversation
.commit(true);
332 CdmStore
.getService(IOccurrenceService
.class).merge(new ArrayList
<SpecimenOrObservationBase
>(rootElements
), true);
336 this.setDirty(false);
339 dirty
.setDirty(false);
344 * @param isDirty the isDirty to set
346 public void setDirty(boolean isDirty
) {
347 dirty
.setDirty(isDirty
);
351 public void setFocus() {
352 //make sure to bind again if maybe in another view the conversation was unbound
353 if(conversation
!=null && !conversation
.isBound()){
356 if(cdmEntitySession
!= null) {
357 cdmEntitySession
.bind();
359 derivateSearchCompositeController
.setFocusOnSearchField();
363 public void update(CdmDataChangeMap changeEvents
) {
367 public ConversationHolder
getConversationHolder() {
372 public void changed(Object element
) {
374 //firePropertyChange(IEditorPart.PROP_DIRTY);
375 viewer
.update(new TreeNode(element
), null);
379 public void forceDirty() {
384 public Map
<Object
, List
<String
>> getPropertyPathsMap() {
385 List
<String
> specimenPropertyPaths
= Arrays
.asList(new String
[] {
386 "descriptions", //$NON-NLS-1$
387 "derivationEvents.derivates", //$NON-NLS-1$
388 "annotations", //$NON-NLS-1$
389 "markers", //$NON-NLS-1$
390 "credits", //$NON-NLS-1$
391 "extensions", //$NON-NLS-1$
392 "rights", //$NON-NLS-1$
393 "sources" //$NON-NLS-1$
395 Map
<Object
, List
<String
>> specimenPropertyPathMap
=
396 new HashMap
<Object
, List
<String
>>();
397 specimenPropertyPathMap
.put(SpecimenOrObservationBase
.class,specimenPropertyPaths
);
398 return specimenPropertyPathMap
;
402 * Refreshes the derivate hierarchy tree and expands the tree
403 * to show and select the given object.
405 * @param expandTo the object to which the tree should be expanded
407 public void refreshTree(Object expandTo
){
409 TreeSelection selection
= (TreeSelection
) viewer
.getSelection();
410 viewer
.expandToLevel(selection
.getFirstElement(), 1);
411 viewer
.setSelection(new StructuredSelection(new TreeNode(expandTo
)));
415 * Refreshes the derivate hierarchy tree
417 public void refreshTree(){
418 if(!viewer
.getTree().isDisposed()){
423 //FIXME:Remoting hack to make this work for remoting
424 //This should actually be resolved using remoting post operations
425 public void remove(Object obj
) {
426 if (obj
instanceof TreeNode
){
427 obj
= ((TreeNode
)obj
).getValue();
429 rootElements
.remove(obj
);
430 Object o
= this.derivateToRootEntityMap
.remove(obj
);
431 viewer
.setInput(rootElements
);
435 * @return a set of {@link SingleRead}s that have multiple parents
437 public Set
<SingleRead
> getMultiLinkSingleReads() {
438 return DerivateLabelProvider
.getMultiLinkSingleReads();
441 public Object
getSelectionInput() {
442 return selectedTaxon
;
445 public DerivateLabelProvider
getLabelProvider() {
446 return labelProvider
;
450 public boolean postOperation(CdmBase objectAffectedByOperation
) {
452 if(objectAffectedByOperation
!=null){
453 changed(objectAffectedByOperation
);
459 public boolean onComplete() {
465 public boolean canAttachMedia() {
469 public void addFieldUnit(FieldUnit fieldUnit
) {
470 rootElements
.add(fieldUnit
);
471 derivateToRootEntityMap
.put(fieldUnit
, fieldUnit
);
475 public ICdmEntitySession
getCdmEntitySession() {
476 return cdmEntitySession
;
480 public void dispose() {
481 if(conversation
!=null){
482 conversation
.close();
484 if(cdmEntitySession
!= null) {
485 cdmEntitySession
.dispose();
492 public void selectionChanged(@Optional @Named(IServiceConstants
.ACTIVE_SELECTION
) ISelection selection
,
493 @Named(IServiceConstants
.ACTIVE_PART
) MPart activePart
, MPart thisPart
)
495 if(activePart
== this || viewer
==null){
498 if(viewer
.getTree().isDisposed()){
501 if(listenToSelectionChange
){
502 selectedTaxon
= null;
503 if(activePart
instanceof MultiPageTaxonEditor
){
504 selectedTaxon
= ((MultiPageTaxonEditor
) activePart
).getTaxon();
506 else if(selection
instanceof IStructuredSelection
){
507 Object selectedElement
= ((IStructuredSelection
) selection
).getFirstElement();
508 if(selectedElement
instanceof CdmBase
){
509 if(((CdmBase
) selectedElement
).isInstanceOf(TaxonNode
.class)){
510 selectedTaxon
= HibernateProxyHelper
.deproxy(selectedElement
, TaxonNode
.class).getTaxon();
512 else if(((CdmBase
) selectedElement
).isInstanceOf(Taxon
.class)){
513 selectedTaxon
= HibernateProxyHelper
.deproxy(selectedElement
, Taxon
.class);
517 if(selectedTaxon
!=null){
518 Collection
<SpecimenOrObservationBase
> fieldUnits
= CdmStore
.getService(IOccurrenceService
.class).listFieldUnitsByAssociatedTaxon(selectedTaxon
, null, null);
519 Collection
<UUID
> uuids
= new HashSet
<UUID
>();
520 for (SpecimenOrObservationBase specimenOrObservationBase
: fieldUnits
) {
521 uuids
.add(specimenOrObservationBase
.getUuid());
523 checkWarnThreshold(uuids
);
524 updateRootEntities(uuids
);
526 thisPart
.setLabel(SPECIMEN_EDITOR
+": " + selectedTaxon
.getName()); //$NON-NLS-1$
529 updateRootEntities((Collection
<UUID
>)Collections
.EMPTY_LIST
);
534 private void checkWarnThreshold(Collection
<UUID
> uuids
) {
535 if(uuids
!=null && uuids
.size()>WARN_THRESHOLD
){
536 MessagingUtils
.warningDialog(Messages
.DerivateView_PERF_WARNING
, this.getClass(), String
.format(Messages
.DerivateView_PERF_WARNING_MESSAGE
, uuids
.size()));
541 public TreeViewer
getViewer() {
549 public List
<SpecimenOrObservationBase
<?
>> getRootEntities() {
550 return new ArrayList
<SpecimenOrObservationBase
<?
>>(rootElements
);
553 public void toggleListenToSelectionChange(MPart part
) {
554 listenToSelectionChange
= !listenToSelectionChange
;
555 derivateSearchCompositeController
.setEnabled(!listenToSelectionChange
);
556 if(!listenToSelectionChange
){
557 selectedTaxon
= null;
558 part
.setLabel(SPECIMEN_EDITOR
);
560 else if(selectedTaxon
==null){
561 part
.setLabel(SPECIMEN_EDITOR
+Messages
.DerivateView_NO_TAXON_SELECTED
);
565 public boolean isListenToSelectionChange(){
566 return listenToSelectionChange
;
573 public void contextAboutToStop(IMemento memento
, IProgressMonitor monitor
) {
580 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
581 derivateSearchCompositeController
.setEnabled(false);
582 if(!viewer
.getTree().isDisposed()) {
583 viewer
.getTree().setEnabled(false);
584 viewer
.setInput(null);
592 public void contextStart(IMemento memento
, IProgressMonitor monitor
) {
593 derivateSearchCompositeController
.setEnabled(!listenToSelectionChange
);
594 if(!viewer
.getTree().isDisposed()){
595 viewer
.getTree().setEnabled(true);
604 public void contextRefresh(IProgressMonitor monitor
) {
611 public void workbenchShutdown(IMemento memento
, IProgressMonitor monitor
) {
615 public boolean isDirty() {
616 return dirty
.isDirty();