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
;
11 import java
.util
.UUID
;
13 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
14 import org
.eclipse
.jface
.action
.MenuManager
;
15 import org
.eclipse
.jface
.util
.LocalSelectionTransfer
;
16 import org
.eclipse
.jface
.viewers
.AbstractTreeViewer
;
17 import org
.eclipse
.jface
.viewers
.ISelection
;
18 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
19 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
20 import org
.eclipse
.jface
.viewers
.TreeNode
;
21 import org
.eclipse
.jface
.viewers
.TreeSelection
;
22 import org
.eclipse
.jface
.viewers
.TreeViewer
;
23 import org
.eclipse
.swt
.SWT
;
24 import org
.eclipse
.swt
.dnd
.DND
;
25 import org
.eclipse
.swt
.dnd
.Transfer
;
26 import org
.eclipse
.swt
.layout
.GridData
;
27 import org
.eclipse
.swt
.layout
.GridLayout
;
28 import org
.eclipse
.swt
.widgets
.Composite
;
29 import org
.eclipse
.swt
.widgets
.Control
;
30 import org
.eclipse
.swt
.widgets
.Display
;
31 import org
.eclipse
.swt
.widgets
.Menu
;
32 import org
.eclipse
.swt
.widgets
.Tree
;
33 import org
.eclipse
.ui
.IEditorInput
;
34 import org
.eclipse
.ui
.IEditorPart
;
35 import org
.eclipse
.ui
.IEditorSite
;
36 import org
.eclipse
.ui
.IMemento
;
37 import org
.eclipse
.ui
.ISelectionListener
;
38 import org
.eclipse
.ui
.ISelectionService
;
39 import org
.eclipse
.ui
.IWorkbenchPart
;
40 import org
.eclipse
.ui
.PartInitException
;
41 import org
.eclipse
.ui
.part
.EditorPart
;
43 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
44 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
45 import eu
.etaxonomy
.cdm
.api
.service
.IOccurrenceService
;
46 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
47 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
48 import eu
.etaxonomy
.cdm
.model
.molecular
.SingleRead
;
49 import eu
.etaxonomy
.cdm
.model
.occurrence
.FieldUnit
;
50 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
51 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
52 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
53 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
54 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
55 import eu
.etaxonomy
.taxeditor
.editor
.Messages
;
56 import eu
.etaxonomy
.taxeditor
.editor
.MultiPageTaxonEditor
;
57 import eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.searchFilter
.DerivateSearchCompositeController
;
58 import eu
.etaxonomy
.taxeditor
.model
.IContextListener
;
59 import eu
.etaxonomy
.taxeditor
.model
.IDirtyMarkable
;
60 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
61 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasFactualData
;
62 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasMedia
;
63 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasSupplementalData
;
64 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
65 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
66 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySession
;
67 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionEnabled
;
68 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
69 import eu
.etaxonomy
.taxeditor
.view
.derivateSearch
.DerivateContentProvider
;
70 import eu
.etaxonomy
.taxeditor
.view
.derivateSearch
.DerivateLabelProvider
;
73 * Displays the derivate hierarchy of the specimen specified in the editor input.
76 public class DerivateView
extends EditorPart
implements IPartContentHasFactualData
, IConversationEnabled
,
77 ICdmEntitySessionEnabled
, IDirtyMarkable
, IPostOperationEnabled
, IPartContentHasDetails
, IPartContentHasSupplementalData
, IPartContentHasMedia
,
78 IContextListener
, ISelectionListener
{
79 public static final String ID
= "eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView"; //$NON-NLS-1$
81 public static final String YOU_NEED_TO_SAVE_BEFORE_PERFORMING_THIS_ACTION
= Messages
.DerivateView_YOU_NEED_TO_SAVE
;
82 public static final String VIEW_HAS_UNSAVED_CHANGES
= Messages
.DerivateView_UNSAVED_CHANGES
;
84 private static final List
<String
> SPECIMEN_INIT_STRATEGY
= Arrays
.asList(new String
[] {
92 "derivationEvents.derivatives.annotations",
93 "derivationEvents.derivatives.markers",
94 "derivationEvents.derivatives.credits",
95 "derivationEvents.derivatives.extensions",
96 "derivationEvents.derivatives.rights",
97 "derivationEvents.derivatives.sources"
100 private static final int WARN_THRESHOLD
= 200;
102 private DelaySelection delaySelection
= null;
104 * This is the monitor for the DelaySelection runnable.
105 * If it is <code>true</code> then it is currently delaying a selection.
107 private boolean isInDelay
;
111 * This class invokes internal_selectionChanged() in a separate thread.
112 * This allows an asynchronous and/or delayed handling of selection changes
114 private class DelaySelection
implements Runnable
{
115 private IWorkbenchPart part
;
116 private ISelection selection
;
118 public DelaySelection(IWorkbenchPart part
, ISelection selection
) {
121 this.selection
= selection
;
127 selectionChanged_internal(part
, selection
);
134 public synchronized void setSelection(ISelection selection
) {
135 this.selection
= selection
;
138 public synchronized void setPart(IWorkbenchPart part
) {
144 private ConversationHolder conversation
;
146 private TreeViewer viewer
;
148 private boolean isDirty
;
150 private final int dndOperations
= DND
.DROP_MOVE
;
152 private DerivateLabelProvider labelProvider
;
154 private DerivateContentProvider contentProvider
;
156 private DerivateSearchCompositeController derivateSearchCompositeController
;
159 * A map with keys being the derivative entities belonging to the {@link UUID}s passed to the constructor
160 * and values being the root elements of the hierarchy (may be the same objects as the derivative entities)
162 private Map
<SpecimenOrObservationBase
<?
>, SpecimenOrObservationBase
<?
>> derivateToRootEntityMap
;
165 * The set of root elements
167 private Set
<SpecimenOrObservationBase
<?
>> rootElements
;
169 private ICdmEntitySession cdmEntitySession
;
172 * <code>true</code> if this view is listening to selection changes
174 private boolean listenToSelectionChange
;
176 private Taxon selectedTaxon
;
178 private ISelectionService selectionService
;
181 * Default constructor
183 public DerivateView() {
191 public void init(IEditorSite site
, IEditorInput input
) throws PartInitException
{
193 this.setInput(input
);
194 this.derivateToRootEntityMap
= new HashMap
<SpecimenOrObservationBase
<?
>, SpecimenOrObservationBase
<?
>>();
195 this.rootElements
= new HashSet
<SpecimenOrObservationBase
<?
>>();
197 if (CdmStore
.isActive() && conversation
== null) {
198 conversation
= CdmStore
.createConversation();
200 if (CdmStore
.isActive()) {
201 cdmEntitySession
= CdmStore
.getCurrentSessionManager().newSession(this, true);
203 //listen to context changes
204 CdmStore
.getContextManager().addContextListener(this);
208 public void createPartControl(Composite parent
) {
210 parent
.setLayout(new GridLayout());
212 //---search and filter---
213 derivateSearchCompositeController
= new DerivateSearchCompositeController(parent
, this);
214 GridData gridDataSearchBar
= new GridData();
215 gridDataSearchBar
.horizontalAlignment
= GridData
.FILL
;
216 gridDataSearchBar
.grabExcessHorizontalSpace
= true;
217 derivateSearchCompositeController
.setLayoutData(gridDataSearchBar
);
218 derivateSearchCompositeController
.setEnabled(CdmStore
.isActive());
221 viewer
= new TreeViewer(new Tree(parent
, SWT
.MULTI
| SWT
.H_SCROLL
| SWT
.V_SCROLL
| SWT
.FULL_SELECTION
));
222 GridData gridDataTree
= new GridData();
223 gridDataTree
.horizontalAlignment
= GridData
.FILL
;
224 gridDataTree
.verticalAlignment
= GridData
.FILL
;
225 gridDataTree
.grabExcessVerticalSpace
= true;
226 gridDataTree
.grabExcessHorizontalSpace
= true;
227 viewer
.getTree().setLayoutData(gridDataTree
);
228 contentProvider
= new DerivateContentProvider();
229 viewer
.setContentProvider(contentProvider
);
230 labelProvider
= new DerivateLabelProvider();
231 labelProvider
.setConversation(conversation
);
232 viewer
.setLabelProvider(labelProvider
);
233 viewer
.setAutoExpandLevel(AbstractTreeViewer
.ALL_LEVELS
);
234 viewer
.getTree().setEnabled(CdmStore
.isActive());
235 // Propagate selection from viewer
236 getSite().setSelectionProvider(viewer
);
238 //listen to selection changes
239 selectionService
= getSite().getWorkbenchWindow().getSelectionService();
240 selectionService
.addSelectionListener(this);
242 //create context menu
243 MenuManager menuManager
= new MenuManager();
244 menuManager
.setRemoveAllWhenShown(true);
245 getSite().registerContextMenu(menuManager
, viewer
);
246 Control control
= viewer
.getControl();
247 Menu menu
= menuManager
.createContextMenu(control
);
248 control
.setMenu(menu
);
251 Collection
<UUID
> derivativeUuids
= ((DerivateViewEditorInput
)getEditorInput()).getDerivativeUuids();
252 checkWarnThreshold(derivativeUuids
);
253 updateRootEntities(derivativeUuids
);
255 derivateSearchCompositeController
.setTaxonFilter(((DerivateViewEditorInput
) getEditorInput()).getTaxonUuid());
257 getEditorSite().getActionBars().getStatusLineManager().setMessage("");
259 //add drag'n'drop support
260 Transfer
[] transfers
= new Transfer
[] {LocalSelectionTransfer
.getTransfer(),};
261 viewer
.addDragSupport(dndOperations
, transfers
, new DerivateDragListener(this));
262 viewer
.addDropSupport(dndOperations
, transfers
, new DerivateDropListener(this));
265 public void updateRootEntities() {
266 updateRootEntities((Collection
)null);
269 public void updateRootEntities(Collection
<UUID
> derivativeUuids
) {
270 if(conversation
!=null){
271 if (!conversation
.isBound()) {
275 * If the active session is not the session of the Derivative Editor
276 * then we will save the active session for later, bind temporarily
277 * to our session and rebind to the original session when we are
278 * done. This happens e.g. if a selection change happens in the
279 * taxon editor and "Link with editor" is enabled. The selection
280 * change event and thus the loading in updateRootEntities() happens
281 * in the session of the taxon editor.
283 ICdmEntitySession previousCdmEntitySession
= CdmStore
.getCurrentSessionManager().getActiveSession();
284 if(cdmEntitySession
!= null) {
285 cdmEntitySession
.bind();
288 List
<SpecimenOrObservationBase
> derivates
= null;
289 if(derivativeUuids
!=null){
290 this.derivateToRootEntityMap
= new HashMap
<SpecimenOrObservationBase
<?
>, SpecimenOrObservationBase
<?
>>();
291 this.rootElements
= new HashSet
<SpecimenOrObservationBase
<?
>>();
292 derivates
= CdmStore
.getService(IOccurrenceService
.class).load(new ArrayList(derivativeUuids
), SPECIMEN_INIT_STRATEGY
);
294 updateRootEntities(derivates
);
295 previousCdmEntitySession
.bind();
300 public void updateRootEntities(List
<SpecimenOrObservationBase
> derivates
) {
302 this.derivateToRootEntityMap
= new HashMap
<SpecimenOrObservationBase
<?
>, SpecimenOrObservationBase
<?
>>();
303 this.rootElements
= new HashSet
<SpecimenOrObservationBase
<?
>>();
304 for (SpecimenOrObservationBase derivate
: derivates
) {
306 if(derivate
instanceof FieldUnit
){
307 derivateToRootEntityMap
.put(derivate
, derivate
);
310 SpecimenOrObservationBase
<?
> topMostDerivate
= EditorUtil
.getTopMostDerivate(derivate
);
311 if(topMostDerivate
!=null){
312 derivateToRootEntityMap
.put(derivate
, topMostDerivate
);
315 derivateToRootEntityMap
.put(derivate
, derivate
);
319 for (SpecimenOrObservationBase
<?
> specimen
: derivateToRootEntityMap
.values()) {
320 rootElements
.add(specimen
);
323 labelProvider
.updateLabelCache(rootElements
);
324 viewer
.setInput(rootElements
);
326 getEditorSite().getActionBars().getStatusLineManager().setMessage(rootElements
.size() +" derivative hierarchies found");
328 //set selection to derivatives if the filter criteria
329 //taxon assignment or derivative type are set
330 if(derivates
!=null && !derivateSearchCompositeController
.isDefaultSearch()){
331 List
<TreeNode
> nodesToSelect
= new ArrayList
<TreeNode
>();
332 for (SpecimenOrObservationBase specimenOrObservationBase
: derivates
) {
333 nodesToSelect
.add(new TreeNode(specimenOrObservationBase
));
335 viewer
.setSelection(new StructuredSelection(nodesToSelect
));
338 viewer
.setSelection(null);
343 public void updateLabelCache(){
344 labelProvider
.updateLabelCache(rootElements
);
348 public void doSave(IProgressMonitor monitor
) {
349 String taskName
= Messages
.DerivateView_SAVING_HIERARCHY
;
350 monitor
.beginTask(taskName
, 3);
351 if (!conversation
.isBound()) {
356 // commit the conversation and start a new transaction immediately
357 conversation
.commit(true);
359 CdmStore
.getService(IOccurrenceService
.class).merge(new ArrayList
<SpecimenOrObservationBase
>(rootElements
), true);
363 this.setDirty(false);
366 firePropertyChange(PROP_DIRTY
);
371 public void doSaveAs() {
375 public String
getTitleToolTip() {
376 return Messages
.DerivateView_DERIVATIVE_EDITOR
;
380 public boolean isDirty() {
385 * @param isDirty the isDirty to set
387 public void setDirty(boolean isDirty
) {
388 this.isDirty
= isDirty
;
392 public boolean isSaveAsAllowed() {
397 public void setFocus() {
398 //make sure to bind again if maybe in another view the conversation was unbound
399 if(conversation
!=null && !conversation
.isBound()){
402 if(cdmEntitySession
!= null) {
403 cdmEntitySession
.bind();
405 derivateSearchCompositeController
.setFocusOnSearchField();
409 public void update(CdmDataChangeMap changeEvents
) {
413 public ConversationHolder
getConversationHolder() {
418 public void changed(Object element
) {
420 firePropertyChange(IEditorPart
.PROP_DIRTY
);
421 viewer
.update(new TreeNode(element
), null);
425 public void forceDirty() {
430 public Map
<Object
, List
<String
>> getPropertyPathsMap() {
431 List
<String
> specimenPropertyPaths
= Arrays
.asList(new String
[] {
433 "derivationEvents.derivates",
441 Map
<Object
, List
<String
>> specimenPropertyPathMap
=
442 new HashMap
<Object
, List
<String
>>();
443 specimenPropertyPathMap
.put(SpecimenOrObservationBase
.class,specimenPropertyPaths
);
444 return specimenPropertyPathMap
;
448 * Refreshes the derivate hierarchy tree and expands the tree
449 * to show and select the given object.
451 * @param expandTo the object to which the tree should be expanded
453 public void refreshTree(Object expandTo
){
455 TreeSelection selection
= (TreeSelection
) viewer
.getSelection();
456 viewer
.expandToLevel(selection
.getFirstElement(), 1);
457 viewer
.setSelection(new StructuredSelection(new TreeNode(expandTo
)));
461 * Refreshes the derivate hierarchy tree
463 public void refreshTree(){
464 if(!viewer
.getTree().isDisposed()){
469 //FIXME:Remoting hack to make this work for remoting
470 //This should actually be resolved using remoting post operations
471 public void remove(Object obj
) {
472 rootElements
.remove(obj
);
473 viewer
.setInput(rootElements
);
477 * @return a set of {@link SingleRead}s that have multiple parents
479 public Set
<SingleRead
> getMultiLinkSingleReads() {
480 return DerivateLabelProvider
.getMultiLinkSingleReads();
483 public Object
getSelectionInput() {
484 return selectedTaxon
;
487 public DerivateLabelProvider
getLabelProvider() {
488 return labelProvider
;
492 public boolean postOperation(CdmBase objectAffectedByOperation
) {
494 if(objectAffectedByOperation
!=null){
495 changed(objectAffectedByOperation
);
501 public boolean onComplete() {
507 public boolean canAttachMedia() {
511 public void addFieldUnit(FieldUnit fieldUnit
) {
512 rootElements
.add(fieldUnit
);
513 derivateToRootEntityMap
.put(fieldUnit
, fieldUnit
);
517 public ICdmEntitySession
getCdmEntitySession() {
518 return cdmEntitySession
;
522 public void dispose() {
524 if(conversation
!=null){
525 conversation
.close();
527 if(cdmEntitySession
!= null) {
528 cdmEntitySession
.dispose();
531 public void selectionChanged_internal(IWorkbenchPart part
, ISelection selection
) {
535 if(viewer
.getTree().isDisposed()){
538 if(listenToSelectionChange
){
539 if(part
instanceof MultiPageTaxonEditor
){
540 selectedTaxon
= ((MultiPageTaxonEditor
) part
).getTaxon();
542 else if(selection
instanceof IStructuredSelection
){
543 Object selectedElement
= ((IStructuredSelection
) selection
).getFirstElement();
544 if(selectedElement
instanceof CdmBase
){
545 if(((CdmBase
) selectedElement
).isInstanceOf(TaxonNode
.class)){
546 selectedTaxon
= HibernateProxyHelper
.deproxy(selectedElement
, TaxonNode
.class).getTaxon();
548 else if(((CdmBase
) selectedElement
).isInstanceOf(Taxon
.class)){
549 selectedTaxon
= HibernateProxyHelper
.deproxy(selectedElement
, Taxon
.class);
553 if(selectedTaxon
!=null){
554 Collection
<SpecimenOrObservationBase
> fieldUnits
= CdmStore
.getService(IOccurrenceService
.class).listFieldUnitsByAssociatedTaxon(selectedTaxon
, null, null);
555 Collection
<UUID
> uuids
= new HashSet
<UUID
>();
556 for (SpecimenOrObservationBase specimenOrObservationBase
: fieldUnits
) {
557 uuids
.add(specimenOrObservationBase
.getUuid());
559 checkWarnThreshold(uuids
);
560 updateRootEntities(uuids
);
561 setPartName("Derivative Editor: " + selectedTaxon
.getName());
567 public void selectionChanged(IWorkbenchPart part
, ISelection selection
) {
568 if(delaySelection
==null){
569 delaySelection
= new DelaySelection(part
, selection
);
571 delaySelection
.setPart(part
);
572 delaySelection
.setSelection(selection
);
575 Display
.getCurrent().asyncExec(delaySelection
);
580 private void checkWarnThreshold(Collection
<UUID
> uuids
) {
581 if(uuids
!=null && uuids
.size()>WARN_THRESHOLD
){
582 MessagingUtils
.warningDialog("Performance warning", this.getClass(), String
.format("Specimens will not be loaded!\n"
583 + "There are %d specimens associated with the current "
584 + "selection. If you really want to show all of them in the specimen editor please "
585 + "use the taxon filter in the search bar.", uuids
.size()));
590 public TreeViewer
getViewer() {
598 public List
<SpecimenOrObservationBase
<?
>> getRootEntities() {
599 return new ArrayList
<SpecimenOrObservationBase
<?
>>(rootElements
);
602 public void toggleListenToSelectionChange() {
603 listenToSelectionChange
= !listenToSelectionChange
;
604 derivateSearchCompositeController
.setEnabled(!listenToSelectionChange
);
605 if(!listenToSelectionChange
){
606 selectedTaxon
= null;
607 setPartName("Derivative Editor");
609 else if(selectedTaxon
==null){
610 setPartName("Derivative Editor [no taxon selected]");
614 public boolean isListenToSelectionChange(){
615 return listenToSelectionChange
;
622 public void contextAboutToStop(IMemento memento
, IProgressMonitor monitor
) {
629 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
630 derivateSearchCompositeController
.setEnabled(false);
631 if(!viewer
.getTree().isDisposed()) {
632 viewer
.getTree().setEnabled(false);
633 viewer
.setInput(null);
641 public void contextStart(IMemento memento
, IProgressMonitor monitor
) {
642 derivateSearchCompositeController
.setEnabled(!listenToSelectionChange
);
643 if(!viewer
.getTree().isDisposed()){
644 viewer
.getTree().setEnabled(true);
653 public void contextRefresh(IProgressMonitor monitor
) {
660 public void workbenchShutdown(IMemento memento
, IProgressMonitor monitor
) {