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
.IEditorInput
;
33 import org
.eclipse
.ui
.IEditorPart
;
34 import org
.eclipse
.ui
.IEditorSite
;
35 import org
.eclipse
.ui
.IMemento
;
36 import org
.eclipse
.ui
.ISelectionListener
;
37 import org
.eclipse
.ui
.ISelectionService
;
38 import org
.eclipse
.ui
.IWorkbenchPart
;
39 import org
.eclipse
.ui
.PartInitException
;
40 import org
.eclipse
.ui
.part
.EditorPart
;
42 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
43 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
44 import eu
.etaxonomy
.cdm
.api
.service
.IOccurrenceService
;
45 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
46 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
47 import eu
.etaxonomy
.cdm
.model
.molecular
.SingleRead
;
48 import eu
.etaxonomy
.cdm
.model
.occurrence
.FieldUnit
;
49 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
50 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
51 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
52 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
53 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
54 import eu
.etaxonomy
.taxeditor
.editor
.Messages
;
55 import eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.searchFilter
.DerivateSearchCompositeController
;
56 import eu
.etaxonomy
.taxeditor
.model
.IContextListener
;
57 import eu
.etaxonomy
.taxeditor
.model
.IDirtyMarkable
;
58 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
59 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasFactualData
;
60 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasMedia
;
61 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasSupplementalData
;
62 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
63 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySession
;
64 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionEnabled
;
65 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
66 import eu
.etaxonomy
.taxeditor
.view
.derivateSearch
.DerivateContentProvider
;
67 import eu
.etaxonomy
.taxeditor
.view
.derivateSearch
.DerivateLabelProvider
;
70 * Displays the derivate hierarchy of the specimen specified in the editor input.
73 public class DerivateView
extends EditorPart
implements IPartContentHasFactualData
, IConversationEnabled
,
74 ICdmEntitySessionEnabled
, IDirtyMarkable
, IPostOperationEnabled
, IPartContentHasDetails
, IPartContentHasSupplementalData
, IPartContentHasMedia
,
75 IContextListener
, ISelectionListener
{
76 public static final String ID
= "eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView"; //$NON-NLS-1$
78 public static final String YOU_NEED_TO_SAVE_BEFORE_PERFORMING_THIS_ACTION
= Messages
.DerivateView_YOU_NEED_TO_SAVE
;
79 public static final String VIEW_HAS_UNSAVED_CHANGES
= Messages
.DerivateView_UNSAVED_CHANGES
;
81 private static final List
<String
> SPECIMEN_INIT_STRATEGY
= Arrays
.asList(new String
[] {
89 "derivationEvents.derivatives.annotations",
90 "derivationEvents.derivatives.markers",
91 "derivationEvents.derivatives.credits",
92 "derivationEvents.derivatives.extensions",
93 "derivationEvents.derivatives.rights",
94 "derivationEvents.derivatives.sources"
97 private ConversationHolder conversation
;
99 private TreeViewer viewer
;
101 private boolean isDirty
;
103 private final int dndOperations
= DND
.DROP_MOVE
;
105 private DerivateLabelProvider labelProvider
;
107 private DerivateContentProvider contentProvider
;
109 private DerivateSearchCompositeController derivateSearchCompositeController
;
112 * A map with keys being the derivative entities belonging to the {@link UUID}s passed to the constructor
113 * and values being the root elements of the hierarchy (may be the same objects as the derivative entities)
115 private Map
<SpecimenOrObservationBase
<?
>, SpecimenOrObservationBase
<?
>> derivateToRootEntityMap
;
118 * The set of root elements
120 private Set
<SpecimenOrObservationBase
<?
>> rootElements
;
122 private ICdmEntitySession cdmEntitySession
;
125 * <code>true</code> if this view is listening to selection changes
127 private boolean listenToSelectionChange
;
129 private Taxon selectedTaxon
;
131 private ISelectionService selectionService
;
134 * Default constructor
136 public DerivateView() {
144 public void init(IEditorSite site
, IEditorInput input
) throws PartInitException
{
146 this.setInput(input
);
147 this.derivateToRootEntityMap
= new HashMap
<SpecimenOrObservationBase
<?
>, SpecimenOrObservationBase
<?
>>();
148 this.rootElements
= new HashSet
<SpecimenOrObservationBase
<?
>>();
150 if (CdmStore
.isActive() && conversation
== null) {
151 conversation
= CdmStore
.createConversation();
153 if (CdmStore
.isActive()) {
154 cdmEntitySession
= CdmStore
.getCurrentSessionManager().newSession(this, true);
156 //listen to context changes
157 CdmStore
.getContextManager().addContextListener(this);
161 public void createPartControl(Composite parent
) {
163 parent
.setLayout(new GridLayout());
165 //---search and filter---
166 derivateSearchCompositeController
= new DerivateSearchCompositeController(parent
, this);
167 GridData gridDataSearchBar
= new GridData();
168 gridDataSearchBar
.horizontalAlignment
= GridData
.FILL
;
169 gridDataSearchBar
.grabExcessHorizontalSpace
= true;
170 derivateSearchCompositeController
.setLayoutData(gridDataSearchBar
);
171 derivateSearchCompositeController
.setEnabled(CdmStore
.isActive());
174 viewer
= new TreeViewer(new Tree(parent
, SWT
.MULTI
| SWT
.H_SCROLL
| SWT
.V_SCROLL
| SWT
.FULL_SELECTION
));
175 GridData gridDataTree
= new GridData();
176 gridDataTree
.horizontalAlignment
= GridData
.FILL
;
177 gridDataTree
.verticalAlignment
= GridData
.FILL
;
178 gridDataTree
.grabExcessVerticalSpace
= true;
179 gridDataTree
.grabExcessHorizontalSpace
= true;
180 viewer
.getTree().setLayoutData(gridDataTree
);
181 contentProvider
= new DerivateContentProvider();
182 viewer
.setContentProvider(contentProvider
);
183 labelProvider
= new DerivateLabelProvider();
184 labelProvider
.setConversation(conversation
);
185 viewer
.setLabelProvider(labelProvider
);
186 viewer
.setAutoExpandLevel(AbstractTreeViewer
.ALL_LEVELS
);
187 viewer
.getTree().setEnabled(CdmStore
.isActive());
188 // Propagate selection from viewer
189 getSite().setSelectionProvider(viewer
);
191 //listen to selection changes
192 selectionService
= getSite().getWorkbenchWindow().getSelectionService();
193 selectionService
.addSelectionListener(this);
195 //create context menu
196 MenuManager menuManager
= new MenuManager();
197 getSite().registerContextMenu(menuManager
, viewer
);
198 Control control
= viewer
.getControl();
199 Menu menu
= menuManager
.createContextMenu(control
);
200 control
.setMenu(menu
);
203 updateRootEntities();
205 //add drag'n'drop support
206 Transfer
[] transfers
= new Transfer
[] {LocalSelectionTransfer
.getTransfer(),};
207 viewer
.addDragSupport(dndOperations
, transfers
, new DerivateDragListener(this));
208 viewer
.addDropSupport(dndOperations
, transfers
, new DerivateDropListener(this));
211 public void updateRootEntities() {
212 updateRootEntities(null);
215 public void updateRootEntities(Collection
<UUID
> derivativeUuids
) {
216 if(conversation
!=null){
217 if (!conversation
.isBound()) {
221 * If the active session is not the session of the Derivate Editor then we will
222 * save it, bind temporarily to our session and rebind to the original session.
223 * This happens e.g. if a selection change happens in the taxon editor and
224 * "Link with editor" is enabled. The selection change event and thus the
225 * loading in updateRootEntities() happens in the session of the taxon
228 ICdmEntitySession previousCdmEntitySession
= CdmStore
.getCurrentSessionManager().getActiveSession();
229 if(cdmEntitySession
!= null) {
230 cdmEntitySession
.bind();
233 if(derivativeUuids
!=null){
234 this.derivateToRootEntityMap
= new HashMap
<SpecimenOrObservationBase
<?
>, SpecimenOrObservationBase
<?
>>();
235 this.rootElements
= new HashSet
<SpecimenOrObservationBase
<?
>>();
236 for (UUID uuid
: derivativeUuids
) {
237 SpecimenOrObservationBase
<?
> derivate
= CdmStore
.getService(IOccurrenceService
.class).load(uuid
, SPECIMEN_INIT_STRATEGY
);
238 if(derivate
instanceof FieldUnit
){
239 derivateToRootEntityMap
.put(derivate
, derivate
);
242 SpecimenOrObservationBase
<?
> topMostDerivate
= EditorUtil
.getTopMostDerivate(derivate
);
243 if(topMostDerivate
!=null){
244 derivateToRootEntityMap
.put(derivate
, topMostDerivate
);
247 derivateToRootEntityMap
.put(derivate
, derivate
);
251 for (SpecimenOrObservationBase
<?
> specimen
: derivateToRootEntityMap
.values()) {
252 rootElements
.add(specimen
);
255 viewer
.setInput(rootElements
);
257 previousCdmEntitySession
.bind();
262 public void doSave(IProgressMonitor monitor
) {
263 String taskName
= Messages
.DerivateView_SAVING_HIERARCHY
;
264 monitor
.beginTask(taskName
, 3);
265 if (!conversation
.isBound()) {
270 // commit the conversation and start a new transaction immediately
271 conversation
.commit(true);
273 if(CdmStore
.getCurrentSessionManager().isRemoting()) {
274 CdmStore
.getService(IOccurrenceService
.class).merge(new ArrayList
<SpecimenOrObservationBase
>(rootElements
), true);
278 this.setDirty(false);
281 firePropertyChange(PROP_DIRTY
);
286 public void doSaveAs() {
290 public String
getTitleToolTip() {
291 return Messages
.DerivateView_DERIVATIVE_EDITOR
;
295 public boolean isDirty() {
300 * @param isDirty the isDirty to set
302 public void setDirty(boolean isDirty
) {
303 this.isDirty
= isDirty
;
307 public boolean isSaveAsAllowed() {
312 public void setFocus() {
313 viewer
.getControl().setFocus();
314 //make sure to bind again if maybe in another view the conversation was unbound
315 if(conversation
!=null && !conversation
.isBound()){
318 if(cdmEntitySession
!= null) {
319 cdmEntitySession
.bind();
324 public void update(CdmDataChangeMap changeEvents
) {
328 public ConversationHolder
getConversationHolder() {
333 public void changed(Object element
) {
335 firePropertyChange(IEditorPart
.PROP_DIRTY
);
340 public void forceDirty() {
345 public Map
<Object
, List
<String
>> getPropertyPathsMap() {
346 List
<String
> specimenPropertyPaths
= Arrays
.asList(new String
[] {
348 "derivationEvents.derivates",
356 Map
<Object
, List
<String
>> specimenPropertyPathMap
=
357 new HashMap
<Object
, List
<String
>>();
358 specimenPropertyPathMap
.put(SpecimenOrObservationBase
.class,specimenPropertyPaths
);
359 return specimenPropertyPathMap
;
363 * Refreshes the derivate hierarchy tree and expands the tree
364 * to show and select the given object.
366 * @param expandTo the object to which the tree should be expanded
368 public void refreshTree(Object expandTo
){
370 TreeSelection selection
= (TreeSelection
) viewer
.getSelection();
371 viewer
.expandToLevel(selection
.getFirstElement(), 1);
372 viewer
.setSelection(new StructuredSelection(new TreeNode(expandTo
)));
376 * Refreshes the derivate hierarchy tree
378 public void refreshTree(){
383 * Refreshes the derivate hierarchy tree
384 * @param refreshViewer if <code>true</code> then also the
385 * viewer will be refreshed. This was implemented due to
386 * performance reasons. If passing <code>false</code>
387 * does what was expected use <code>false</code> preferably.
389 public void refreshTree(boolean refreshViewer
){
390 //refresh typedesignations
391 labelProvider
.refresh();
397 //FIXME:Remoting hack to make this work for remoting
398 //This should actually be resolved using remoting post operations
399 public void remove(Object obj
) {
400 rootElements
.remove(obj
);
401 viewer
.setInput(rootElements
);
405 * @return a set of {@link SingleRead}s that have multiple parents
407 public Set
<SingleRead
> getMultiLinkSingleReads() {
408 return DerivateLabelProvider
.getMultiLinkSingleReads();
411 public Object
getSelectionInput() {
412 return selectedTaxon
;
415 public DerivateLabelProvider
getLabelProvider() {
416 return labelProvider
;
420 public boolean postOperation(CdmBase objectAffectedByOperation
) {
422 if(objectAffectedByOperation
!=null){
423 changed(objectAffectedByOperation
);
429 public boolean onComplete() {
435 public boolean canAttachMedia() {
439 public void addFieldUnit(FieldUnit fieldUnit
) {
440 rootElements
.add(fieldUnit
);
441 derivateToRootEntityMap
.put(fieldUnit
, fieldUnit
);
445 public ICdmEntitySession
getCdmEntitySession() {
446 return cdmEntitySession
;
450 public void dispose() {
452 if(conversation
!=null){
453 conversation
.close();
455 if(cdmEntitySession
!= null) {
456 cdmEntitySession
.dispose();
461 public void selectionChanged(IWorkbenchPart part
, ISelection selection
) {
462 if(viewer
.getTree().isDisposed()){
465 if(listenToSelectionChange
&& selection
instanceof IStructuredSelection
){
466 Object selectedElement
= ((IStructuredSelection
) selection
).getFirstElement();
467 if(selectedElement
instanceof CdmBase
){
468 if(((CdmBase
) selectedElement
).isInstanceOf(TaxonNode
.class)){
469 selectedTaxon
= HibernateProxyHelper
.deproxy(selectedElement
, TaxonNode
.class).getTaxon();
471 else if(((CdmBase
) selectedElement
).isInstanceOf(Taxon
.class)){
472 selectedTaxon
= HibernateProxyHelper
.deproxy(selectedElement
, Taxon
.class);
474 Collection
<SpecimenOrObservationBase
> fieldUnits
= CdmStore
.getService(IOccurrenceService
.class).listFieldUnitsByAssociatedTaxon(selectedTaxon
, null, null);
475 Collection
<UUID
> uuids
= new HashSet
<UUID
>();
476 for (SpecimenOrObservationBase specimenOrObservationBase
: fieldUnits
) {
477 uuids
.add(specimenOrObservationBase
.getUuid());
479 updateRootEntities(uuids
);
480 setPartName("Derivative Editor: " + selectedTaxon
.getName());
485 public TreeViewer
getViewer() {
493 public List
<SpecimenOrObservationBase
<?
>> getRootEntities() {
494 return new ArrayList
<SpecimenOrObservationBase
<?
>>(rootElements
);
497 public void toggleListenToSelectionChange() {
498 listenToSelectionChange
= !listenToSelectionChange
;
499 derivateSearchCompositeController
.setEnabled(!listenToSelectionChange
);
500 if(!listenToSelectionChange
){
501 selectedTaxon
= null;
502 setPartName("Derivative Editor");
504 else if(selectedTaxon
==null){
505 setPartName("Derivative Editor [no taxon selected]");
509 public boolean isListenToSelectionChange(){
510 return listenToSelectionChange
;
517 public void contextAboutToStop(IMemento memento
, IProgressMonitor monitor
) {
524 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
525 derivateSearchCompositeController
.setEnabled(false);
526 viewer
.getTree().setEnabled(false);
527 viewer
.setInput(null);
534 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
) {