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 if(CdmStore
.getCurrentSessionManager().isRemoting()) {
360 CdmStore
.getService(IOccurrenceService
.class).merge(new ArrayList
<SpecimenOrObservationBase
>(rootElements
), true);
364 this.setDirty(false);
367 firePropertyChange(PROP_DIRTY
);
372 public void doSaveAs() {
376 public String
getTitleToolTip() {
377 return Messages
.DerivateView_DERIVATIVE_EDITOR
;
381 public boolean isDirty() {
386 * @param isDirty the isDirty to set
388 public void setDirty(boolean isDirty
) {
389 this.isDirty
= isDirty
;
393 public boolean isSaveAsAllowed() {
398 public void setFocus() {
399 //make sure to bind again if maybe in another view the conversation was unbound
400 if(conversation
!=null && !conversation
.isBound()){
403 if(cdmEntitySession
!= null) {
404 cdmEntitySession
.bind();
406 derivateSearchCompositeController
.setFocusOnSearchField();
410 public void update(CdmDataChangeMap changeEvents
) {
414 public ConversationHolder
getConversationHolder() {
419 public void changed(Object element
) {
421 firePropertyChange(IEditorPart
.PROP_DIRTY
);
422 viewer
.update(new TreeNode(element
), null);
426 public void forceDirty() {
431 public Map
<Object
, List
<String
>> getPropertyPathsMap() {
432 List
<String
> specimenPropertyPaths
= Arrays
.asList(new String
[] {
434 "derivationEvents.derivates",
442 Map
<Object
, List
<String
>> specimenPropertyPathMap
=
443 new HashMap
<Object
, List
<String
>>();
444 specimenPropertyPathMap
.put(SpecimenOrObservationBase
.class,specimenPropertyPaths
);
445 return specimenPropertyPathMap
;
449 * Refreshes the derivate hierarchy tree and expands the tree
450 * to show and select the given object.
452 * @param expandTo the object to which the tree should be expanded
454 public void refreshTree(Object expandTo
){
456 TreeSelection selection
= (TreeSelection
) viewer
.getSelection();
457 viewer
.expandToLevel(selection
.getFirstElement(), 1);
458 viewer
.setSelection(new StructuredSelection(new TreeNode(expandTo
)));
462 * Refreshes the derivate hierarchy tree
464 public void refreshTree(){
465 if(!viewer
.getTree().isDisposed()){
470 //FIXME:Remoting hack to make this work for remoting
471 //This should actually be resolved using remoting post operations
472 public void remove(Object obj
) {
473 rootElements
.remove(obj
);
474 viewer
.setInput(rootElements
);
478 * @return a set of {@link SingleRead}s that have multiple parents
480 public Set
<SingleRead
> getMultiLinkSingleReads() {
481 return DerivateLabelProvider
.getMultiLinkSingleReads();
484 public Object
getSelectionInput() {
485 return selectedTaxon
;
488 public DerivateLabelProvider
getLabelProvider() {
489 return labelProvider
;
493 public boolean postOperation(CdmBase objectAffectedByOperation
) {
495 if(objectAffectedByOperation
!=null){
496 changed(objectAffectedByOperation
);
502 public boolean onComplete() {
508 public boolean canAttachMedia() {
512 public void addFieldUnit(FieldUnit fieldUnit
) {
513 rootElements
.add(fieldUnit
);
514 derivateToRootEntityMap
.put(fieldUnit
, fieldUnit
);
518 public ICdmEntitySession
getCdmEntitySession() {
519 return cdmEntitySession
;
523 public void dispose() {
525 if(conversation
!=null){
526 conversation
.close();
528 if(cdmEntitySession
!= null) {
529 cdmEntitySession
.dispose();
532 public void selectionChanged_internal(IWorkbenchPart part
, ISelection selection
) {
536 if(viewer
.getTree().isDisposed()){
539 if(listenToSelectionChange
){
540 if(part
instanceof MultiPageTaxonEditor
){
541 selectedTaxon
= ((MultiPageTaxonEditor
) part
).getTaxon();
543 else if(selection
instanceof IStructuredSelection
){
544 Object selectedElement
= ((IStructuredSelection
) selection
).getFirstElement();
545 if(selectedElement
instanceof CdmBase
){
546 if(((CdmBase
) selectedElement
).isInstanceOf(TaxonNode
.class)){
547 selectedTaxon
= HibernateProxyHelper
.deproxy(selectedElement
, TaxonNode
.class).getTaxon();
549 else if(((CdmBase
) selectedElement
).isInstanceOf(Taxon
.class)){
550 selectedTaxon
= HibernateProxyHelper
.deproxy(selectedElement
, Taxon
.class);
554 if(selectedTaxon
!=null){
555 Collection
<SpecimenOrObservationBase
> fieldUnits
= CdmStore
.getService(IOccurrenceService
.class).listFieldUnitsByAssociatedTaxon(selectedTaxon
, null, null);
556 Collection
<UUID
> uuids
= new HashSet
<UUID
>();
557 for (SpecimenOrObservationBase specimenOrObservationBase
: fieldUnits
) {
558 uuids
.add(specimenOrObservationBase
.getUuid());
560 checkWarnThreshold(uuids
);
561 updateRootEntities(uuids
);
562 setPartName("Derivative Editor: " + selectedTaxon
.getName());
568 public void selectionChanged(IWorkbenchPart part
, ISelection selection
) {
569 if(delaySelection
==null){
570 delaySelection
= new DelaySelection(part
, selection
);
572 delaySelection
.setPart(part
);
573 delaySelection
.setSelection(selection
);
576 Display
.getCurrent().asyncExec(delaySelection
);
581 private void checkWarnThreshold(Collection
<UUID
> uuids
) {
582 if(uuids
!=null && uuids
.size()>WARN_THRESHOLD
){
583 MessagingUtils
.warningDialog("Performance warning", this.getClass(), String
.format("Specimens will not be loaded!\n"
584 + "There are %d specimens associated with the current "
585 + "selection. If you really want to show all of them in the specimen editor please "
586 + "use the taxon filter in the search bar.", uuids
.size()));
591 public TreeViewer
getViewer() {
599 public List
<SpecimenOrObservationBase
<?
>> getRootEntities() {
600 return new ArrayList
<SpecimenOrObservationBase
<?
>>(rootElements
);
603 public void toggleListenToSelectionChange() {
604 listenToSelectionChange
= !listenToSelectionChange
;
605 derivateSearchCompositeController
.setEnabled(!listenToSelectionChange
);
606 if(!listenToSelectionChange
){
607 selectedTaxon
= null;
608 setPartName("Derivative Editor");
610 else if(selectedTaxon
==null){
611 setPartName("Derivative Editor [no taxon selected]");
615 public boolean isListenToSelectionChange(){
616 return listenToSelectionChange
;
623 public void contextAboutToStop(IMemento memento
, IProgressMonitor monitor
) {
630 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
631 derivateSearchCompositeController
.setEnabled(false);
632 if(!viewer
.getTree().isDisposed()) {
633 viewer
.getTree().setEnabled(false);
634 viewer
.setInput(null);
642 public void contextStart(IMemento memento
, IProgressMonitor monitor
) {
643 derivateSearchCompositeController
.setEnabled(!listenToSelectionChange
);
644 if(!viewer
.getTree().isDisposed()){
645 viewer
.getTree().setEnabled(true);
654 public void contextRefresh(IProgressMonitor monitor
) {
661 public void workbenchShutdown(IMemento memento
, IProgressMonitor monitor
) {