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
.Menu
;
31 import org
.eclipse
.swt
.widgets
.Tree
;
32 import org
.eclipse
.ui
.IEditorPart
;
33 import org
.eclipse
.ui
.IMemento
;
34 import org
.eclipse
.ui
.ISaveablePart2
;
35 import org
.eclipse
.ui
.IWorkbenchPart
;
37 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
38 import eu
.etaxonomy
.cdm
.api
.service
.IOccurrenceService
;
39 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
40 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
41 import eu
.etaxonomy
.cdm
.model
.molecular
.SingleRead
;
42 import eu
.etaxonomy
.cdm
.model
.occurrence
.FieldUnit
;
43 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
44 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
45 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
46 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
47 import eu
.etaxonomy
.taxeditor
.editor
.Messages
;
48 import eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.searchFilter
.DerivateSearchCompositeController
;
49 import eu
.etaxonomy
.taxeditor
.model
.IContextListener
;
50 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
51 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasFactualData
;
52 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasMedia
;
53 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasSupplementalData
;
54 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySession
;
55 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
56 import eu
.etaxonomy
.taxeditor
.view
.AbstractCdmViewPart
;
57 import eu
.etaxonomy
.taxeditor
.view
.derivateSearch
.DerivateContentProvider
;
58 import eu
.etaxonomy
.taxeditor
.view
.derivateSearch
.DerivateLabelProvider
;
61 * Displays the derivate hierarchy of the specimen specified in the editor input.
64 public class DerivateView
extends AbstractCdmViewPart
implements IPartContentHasFactualData
, ISaveablePart2
,
65 IPartContentHasDetails
, IPartContentHasSupplementalData
, IPartContentHasMedia
, IContextListener
{
66 public static final String ID
= "eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView"; //$NON-NLS-1$
68 public static final String YOU_NEED_TO_SAVE_BEFORE_PERFORMING_THIS_ACTION
= Messages
.DerivateView_YOU_NEED_TO_SAVE
;
69 public static final String VIEW_HAS_UNSAVED_CHANGES
= Messages
.DerivateView_UNSAVED_CHANGES
;
71 private static final List
<String
> SPECIMEN_INIT_STRATEGY
= Arrays
.asList(new String
[] {
79 "derivationEvents.derivatives.annotations",
80 "derivationEvents.derivatives.markers",
81 "derivationEvents.derivatives.credits",
82 "derivationEvents.derivatives.extensions",
83 "derivationEvents.derivatives.rights",
84 "derivationEvents.derivatives.sources"
87 private ConversationHolder conversation
;
89 private TreeViewer viewer
;
91 private boolean isDirty
;
93 private final int dndOperations
= DND
.DROP_MOVE
;
95 private DerivateLabelProvider labelProvider
;
97 private DerivateContentProvider contentProvider
;
99 private DerivateSearchCompositeController derivateSearchCompositeController
;
102 * A map with keys being the derivative entities belonging to the {@link UUID}s passed to the constructor
103 * and values being the root elements of the hierarchy (may be the same objects as the derivative entities)
105 private Map
<SpecimenOrObservationBase
<?
>, SpecimenOrObservationBase
<?
>> derivateToRootEntityMap
;
108 * The set of root elements
110 private Set
<SpecimenOrObservationBase
<?
>> rootElements
;
112 private ICdmEntitySession cdmEntitySession
;
115 * <code>true</code> if this view is listening to selection changes
117 private boolean listenToSelectionChange
;
119 private Taxon selectedTaxon
;
122 * Default constructor
124 public DerivateView() {
131 private void init() {
132 this.derivateToRootEntityMap
= new HashMap
<SpecimenOrObservationBase
<?
>, SpecimenOrObservationBase
<?
>>();
133 this.rootElements
= new HashSet
<SpecimenOrObservationBase
<?
>>();
135 if (CdmStore
.isActive() && conversation
== null) {
136 conversation
= CdmStore
.createConversation();
138 if (CdmStore
.isActive()) {
139 cdmEntitySession
= CdmStore
.getCurrentSessionManager().newSession(this, true);
141 //listen to context changes
142 CdmStore
.getContextManager().addContextListener(this);
146 public void createPartControl(Composite parent
) {
148 parent
.setLayout(new GridLayout());
150 //---search and filter---
151 derivateSearchCompositeController
= new DerivateSearchCompositeController(parent
, this);
152 GridData gridDataSearchBar
= new GridData();
153 gridDataSearchBar
.horizontalAlignment
= GridData
.FILL
;
154 gridDataSearchBar
.grabExcessHorizontalSpace
= true;
155 derivateSearchCompositeController
.setLayoutData(gridDataSearchBar
);
156 derivateSearchCompositeController
.setEnabled(CdmStore
.isActive());
159 viewer
= new TreeViewer(new Tree(parent
, SWT
.MULTI
| SWT
.H_SCROLL
| SWT
.V_SCROLL
| SWT
.FULL_SELECTION
));
160 GridData gridDataTree
= new GridData();
161 gridDataTree
.horizontalAlignment
= GridData
.FILL
;
162 gridDataTree
.verticalAlignment
= GridData
.FILL
;
163 gridDataTree
.grabExcessVerticalSpace
= true;
164 gridDataTree
.grabExcessHorizontalSpace
= true;
165 viewer
.getTree().setLayoutData(gridDataTree
);
166 contentProvider
= new DerivateContentProvider();
167 viewer
.setContentProvider(contentProvider
);
168 labelProvider
= new DerivateLabelProvider();
169 labelProvider
.setConversation(conversation
);
170 viewer
.setLabelProvider(labelProvider
);
171 viewer
.setAutoExpandLevel(AbstractTreeViewer
.ALL_LEVELS
);
172 viewer
.getTree().setEnabled(CdmStore
.isActive());
173 // Propagate selection from viewer
174 getSite().setSelectionProvider(viewer
);
176 //listen to selection changes
177 selectionService
= getSite().getWorkbenchWindow().getSelectionService();
178 selectionService
.addSelectionListener(this);
180 //create context menu
181 MenuManager menuManager
= new MenuManager();
182 getSite().registerContextMenu(menuManager
, viewer
);
183 Control control
= viewer
.getControl();
184 Menu menu
= menuManager
.createContextMenu(control
);
185 control
.setMenu(menu
);
188 updateRootEntities();
190 //add drag'n'drop support
191 Transfer
[] transfers
= new Transfer
[] {LocalSelectionTransfer
.getTransfer(),};
192 viewer
.addDragSupport(dndOperations
, transfers
, new DerivateDragListener(this));
193 viewer
.addDropSupport(dndOperations
, transfers
, new DerivateDropListener(this));
196 public void updateRootEntities() {
197 updateRootEntities(null);
200 public void updateRootEntities(Collection
<UUID
> derivativeUuids
) {
201 if(conversation
!=null){
202 if (!conversation
.isBound()) {
206 * If the active session is not the session of the Derivate Editor then we will
207 * save it, bind temporarily to our session and rebind to the original session.
208 * This happens e.g. if a selection change happens in the taxon editor and
209 * "Link with editor" is enabled. The selection change event and thus the
210 * loading in updateRootEntities() happens in the session of the taxon
213 ICdmEntitySession previousCdmEntitySession
= CdmStore
.getCurrentSessionManager().getActiveSession();
214 if(cdmEntitySession
!= null) {
215 cdmEntitySession
.bind();
218 if(derivativeUuids
!=null){
219 this.derivateToRootEntityMap
= new HashMap
<SpecimenOrObservationBase
<?
>, SpecimenOrObservationBase
<?
>>();
220 this.rootElements
= new HashSet
<SpecimenOrObservationBase
<?
>>();
221 for (UUID uuid
: derivativeUuids
) {
222 SpecimenOrObservationBase
<?
> derivate
= CdmStore
.getService(IOccurrenceService
.class).load(uuid
, SPECIMEN_INIT_STRATEGY
);
223 if(derivate
instanceof FieldUnit
){
224 derivateToRootEntityMap
.put(derivate
, derivate
);
227 SpecimenOrObservationBase
<?
> topMostDerivate
= EditorUtil
.getTopMostDerivate(derivate
);
228 if(topMostDerivate
!=null){
229 derivateToRootEntityMap
.put(derivate
, topMostDerivate
);
232 derivateToRootEntityMap
.put(derivate
, derivate
);
236 for (SpecimenOrObservationBase
<?
> specimen
: derivateToRootEntityMap
.values()) {
237 rootElements
.add(specimen
);
240 viewer
.setInput(rootElements
);
242 previousCdmEntitySession
.bind();
247 public void doSave(IProgressMonitor monitor
) {
248 String taskName
= Messages
.DerivateView_SAVING_HIERARCHY
;
249 monitor
.beginTask(taskName
, 3);
250 if (!conversation
.isBound()) {
255 // commit the conversation and start a new transaction immediately
256 conversation
.commit(true);
258 if(CdmStore
.getCurrentSessionManager().isRemoting()) {
259 CdmStore
.getService(IOccurrenceService
.class).merge(new ArrayList
<SpecimenOrObservationBase
>(rootElements
), true);
263 this.setDirty(false);
266 firePropertyChange(PROP_DIRTY
);
271 public void doSaveAs() {
275 public String
getTitleToolTip() {
276 return Messages
.DerivateView_DERIVATIVE_EDITOR
;
280 public boolean isDirty() {
285 * @param isDirty the isDirty to set
287 public void setDirty(boolean isDirty
) {
288 this.isDirty
= isDirty
;
292 public boolean isSaveAsAllowed() {
297 public void setFocus() {
298 viewer
.getControl().setFocus();
299 //make sure to bind again if maybe in another view the conversation was unbound
300 if(conversation
!=null && !conversation
.isBound()){
303 if(cdmEntitySession
!= null) {
304 cdmEntitySession
.bind();
309 public void update(CdmDataChangeMap changeEvents
) {
313 public ConversationHolder
getConversationHolder() {
321 public TreeViewer
getViewer() {
326 public void changed(Object element
) {
328 firePropertyChange(IEditorPart
.PROP_DIRTY
);
333 public void forceDirty() {
338 public Map
<Object
, List
<String
>> getPropertyPathsMap() {
339 List
<String
> specimenPropertyPaths
= Arrays
.asList(new String
[] {
341 "derivationEvents.derivates",
349 Map
<Object
, List
<String
>> specimenPropertyPathMap
=
350 new HashMap
<Object
, List
<String
>>();
351 specimenPropertyPathMap
.put(SpecimenOrObservationBase
.class,specimenPropertyPaths
);
352 return specimenPropertyPathMap
;
356 * Refreshes the derivate hierarchy tree and expands the tree
357 * to show and select the given object.
359 * @param expandTo the object to which the tree should be expanded
361 public void refreshTree(Object expandTo
){
363 TreeSelection selection
= (TreeSelection
) viewer
.getSelection();
364 viewer
.expandToLevel(selection
.getFirstElement(), 1);
365 viewer
.setSelection(new StructuredSelection(new TreeNode(expandTo
)));
369 * Refreshes the derivate hierarchy tree
371 public void refreshTree(){
376 * Refreshes the derivate hierarchy tree
377 * @param refreshViewer if <code>true</code> then also the
378 * viewer will be refreshed. This was implemented due to
379 * performance reasons. If passing <code>false</code>
380 * does what was expected use <code>false</code> preferably.
382 public void refreshTree(boolean refreshViewer
){
383 //refresh typedesignations
384 labelProvider
.refresh();
390 //FIXME:Remoting hack to make this work for remoting
391 //This should actually be resolved using remoting post operations
392 public void remove(Object obj
) {
393 rootElements
.remove(obj
);
394 viewer
.setInput(rootElements
);
398 * @return a set of {@link SingleRead}s that have multiple parents
400 public Set
<SingleRead
> getMultiLinkSingleReads() {
401 return DerivateLabelProvider
.getMultiLinkSingleReads();
404 public Object
getSelectionInput() {
405 return selectedTaxon
;
408 public DerivateLabelProvider
getLabelProvider() {
409 return labelProvider
;
413 public boolean postOperation(CdmBase objectAffectedByOperation
) {
415 if(objectAffectedByOperation
!=null){
416 changed(objectAffectedByOperation
);
422 public boolean onComplete() {
428 public boolean canAttachMedia() {
432 public void addFieldUnit(FieldUnit fieldUnit
) {
433 rootElements
.add(fieldUnit
);
434 derivateToRootEntityMap
.put(fieldUnit
, fieldUnit
);
438 public ICdmEntitySession
getCdmEntitySession() {
439 return cdmEntitySession
;
443 public void dispose() {
445 if(conversation
!=null){
446 conversation
.close();
448 if(cdmEntitySession
!= null) {
449 cdmEntitySession
.dispose();
454 public void selectionChanged(IWorkbenchPart part
, ISelection selection
) {
455 if(listenToSelectionChange
&& selection
instanceof IStructuredSelection
){
456 Object selectedElement
= ((IStructuredSelection
) selection
).getFirstElement();
457 if(selectedElement
instanceof CdmBase
&& ((CdmBase
) selectedElement
).isInstanceOf(Taxon
.class)){
458 selectedTaxon
= HibernateProxyHelper
.deproxy(selectedElement
, Taxon
.class);
459 Collection
<SpecimenOrObservationBase
> fieldUnits
= CdmStore
.getService(IOccurrenceService
.class).listFieldUnitsByAssociatedTaxon(selectedTaxon
, null, null);
460 Collection
<UUID
> uuids
= new HashSet
<UUID
>();
461 for (SpecimenOrObservationBase specimenOrObservationBase
: fieldUnits
) {
462 uuids
.add(specimenOrObservationBase
.getUuid());
464 updateRootEntities(uuids
);
465 setPartName("Derivative Editor: " + selectedTaxon
.getName());
474 public List
<SpecimenOrObservationBase
<?
>> getRootEntities() {
475 return new ArrayList
<SpecimenOrObservationBase
<?
>>(rootElements
);
479 public void createViewer(Composite parent
) {
480 // TODO Auto-generated method stub
485 public boolean isSaveOnCloseNeeded() {
490 public int promptToSaveOnClose() {
491 return ISaveablePart2
.DEFAULT
;
494 public void toggleListenToSelectionChange() {
495 listenToSelectionChange
= !listenToSelectionChange
;
496 derivateSearchCompositeController
.setEnabled(!listenToSelectionChange
);
497 if(!listenToSelectionChange
){
498 selectedTaxon
= null;
499 setPartName("Derivative Editor");
501 else if(selectedTaxon
==null){
502 setPartName("Derivative Editor [no taxon selected]");
506 public boolean isListenToSelectionChange(){
507 return listenToSelectionChange
;
514 public void contextAboutToStop(IMemento memento
, IProgressMonitor monitor
) {
521 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
522 derivateSearchCompositeController
.setEnabled(false);
523 viewer
.getTree().setEnabled(false);
524 viewer
.setInput(null);
531 public void contextStart(IMemento memento
, IProgressMonitor monitor
) {
533 derivateSearchCompositeController
.setEnabled(!listenToSelectionChange
);
534 viewer
.getTree().setEnabled(true);
542 public void contextRefresh(IProgressMonitor monitor
) {
549 public void workbenchShutdown(IMemento memento
, IProgressMonitor monitor
) {