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
.hibernate
.HibernateProxyHelper
;
42 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
43 import eu
.etaxonomy
.cdm
.model
.molecular
.Sequence
;
44 import eu
.etaxonomy
.cdm
.model
.molecular
.SingleRead
;
45 import eu
.etaxonomy
.cdm
.model
.occurrence
.FieldUnit
;
46 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
47 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
48 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
49 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
50 import eu
.etaxonomy
.taxeditor
.editor
.Messages
;
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 DerivateContentProvider contentProvider
;
104 private DerivateSearchCompositeController derivateSearchCompositeController
;
107 * A map with keys being the derivative entities belonging to the {@link UUID}s passed to the constructor
108 * and values being the root elements of the hierarchy (may be the same objects as the derivative entities)
110 private Map
<SpecimenOrObservationBase
<?
>, SpecimenOrObservationBase
<?
>> derivateToRootEntityMap
;
113 * The set of root elements
115 private Set
<SpecimenOrObservationBase
<?
>> rootElements
;
117 private ICdmEntitySession cdmEntitySession
;
120 * <code>true</code> if this view is listening to selection changes
122 private boolean listenToSelectionChange
;
124 private Taxon selectedTaxon
;
127 * Default constructor
129 public DerivateView() {
136 private void init() {
137 this.derivateToRootEntityMap
= new HashMap
<SpecimenOrObservationBase
<?
>, SpecimenOrObservationBase
<?
>>();
138 this.rootElements
= new HashSet
<SpecimenOrObservationBase
<?
>>();
140 if (CdmStore
.isActive() && conversation
== null) {
141 conversation
= CdmStore
.createConversation();
143 if (CdmStore
.isActive()) {
144 cdmEntitySession
= CdmStore
.getCurrentSessionManager().newSession(this, true);
146 //listen to context changes
147 CdmStore
.getContextManager().addContextListener(this);
151 public void createPartControl(Composite parent
) {
153 parent
.setLayout(new GridLayout());
155 //---search and filter---
156 derivateSearchCompositeController
= new DerivateSearchCompositeController(parent
, this);
157 GridData gridDataSearchBar
= new GridData();
158 gridDataSearchBar
.horizontalAlignment
= GridData
.FILL
;
159 gridDataSearchBar
.grabExcessHorizontalSpace
= true;
160 derivateSearchCompositeController
.setLayoutData(gridDataSearchBar
);
161 derivateSearchCompositeController
.setEnabled(CdmStore
.isActive());
164 viewer
= new TreeViewer(new Tree(parent
, SWT
.MULTI
| SWT
.H_SCROLL
| SWT
.V_SCROLL
| SWT
.FULL_SELECTION
));
165 GridData gridDataTree
= new GridData();
166 gridDataTree
.horizontalAlignment
= GridData
.FILL
;
167 gridDataTree
.verticalAlignment
= GridData
.FILL
;
168 gridDataTree
.grabExcessVerticalSpace
= true;
169 gridDataTree
.grabExcessHorizontalSpace
= true;
170 viewer
.getTree().setLayoutData(gridDataTree
);
171 contentProvider
= new DerivateContentProvider();
172 viewer
.setContentProvider(contentProvider
);
173 labelProvider
= new DerivateLabelProvider();
174 labelProvider
.setConversation(conversation
);
175 viewer
.setLabelProvider(labelProvider
);
176 viewer
.setAutoExpandLevel(AbstractTreeViewer
.ALL_LEVELS
);
177 viewer
.getTree().setEnabled(CdmStore
.isActive());
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 Object
getSelectionInput() {
407 return selectedTaxon
;
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 addFieldUnit(FieldUnit fieldUnit
) {
435 rootElements
.add(fieldUnit
);
436 derivateToRootEntityMap
.put(fieldUnit
, fieldUnit
);
440 public ICdmEntitySession
getCdmEntitySession() {
441 return cdmEntitySession
;
445 public void dispose() {
447 if(conversation
!=null){
448 conversation
.close();
450 if(cdmEntitySession
!= null) {
451 cdmEntitySession
.dispose();
456 public void selectionChanged(IWorkbenchPart part
, ISelection selection
) {
457 if(listenToSelectionChange
&& selection
instanceof IStructuredSelection
){
458 Object selectedElement
= ((IStructuredSelection
) selection
).getFirstElement();
459 if(selectedElement
instanceof CdmBase
&& ((CdmBase
) selectedElement
).isInstanceOf(Taxon
.class)){
460 selectedTaxon
= HibernateProxyHelper
.deproxy(selectedElement
, Taxon
.class);
461 Collection
<SpecimenOrObservationBase
> fieldUnits
= CdmStore
.getService(IOccurrenceService
.class).listFieldUnitsByAssociatedTaxon(selectedTaxon
, null, null);
462 Collection
<UUID
> uuids
= new HashSet
<UUID
>();
463 for (SpecimenOrObservationBase specimenOrObservationBase
: fieldUnits
) {
464 uuids
.add(specimenOrObservationBase
.getUuid());
466 updateRootEntities(uuids
);
467 setPartName("Derivative Editor: " + selectedTaxon
.getName());
476 public List
<SpecimenOrObservationBase
<?
>> getRootEntities() {
477 return new ArrayList
<SpecimenOrObservationBase
<?
>>(rootElements
);
481 public void createViewer(Composite parent
) {
482 // TODO Auto-generated method stub
487 public boolean isSaveOnCloseNeeded() {
492 public int promptToSaveOnClose() {
493 return ISaveablePart2
.DEFAULT
;
496 public void toggleListenToSelectionChange() {
497 listenToSelectionChange
= !listenToSelectionChange
;
498 derivateSearchCompositeController
.setEnabled(!listenToSelectionChange
);
499 if(!listenToSelectionChange
){
500 selectedTaxon
= null;
501 setPartName("Derivative Editor");
503 else if(selectedTaxon
==null){
504 setPartName("Derivative Editor [no taxon selected]");
508 public boolean isListenToSelectionChange(){
509 return listenToSelectionChange
;
516 public void contextAboutToStop(IMemento memento
, IProgressMonitor monitor
) {
523 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
524 derivateSearchCompositeController
.setEnabled(false);
525 viewer
.getTree().setEnabled(false);
526 viewer
.setInput(null);
533 public void contextStart(IMemento memento
, IProgressMonitor monitor
) {
535 derivateSearchCompositeController
.setEnabled(!listenToSelectionChange
);
536 viewer
.getTree().setEnabled(true);
544 public void contextRefresh(IProgressMonitor monitor
) {
551 public void workbenchShutdown(IMemento memento
, IProgressMonitor monitor
) {