2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.editor
;
12 import java
.util
.Arrays
;
13 import java
.util
.HashSet
;
14 import java
.util
.List
;
17 import java
.util
.UUID
;
19 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
20 import org
.eclipse
.ui
.IEditorInput
;
21 import org
.eclipse
.ui
.IMemento
;
22 import org
.eclipse
.ui
.IPersistableElement
;
24 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
25 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
26 import eu
.etaxonomy
.cdm
.api
.service
.IClassificationService
;
27 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonNodeService
;
28 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
29 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
30 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
31 import eu
.etaxonomy
.cdm
.model
.taxon
.ITaxonTreeNode
;
32 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
33 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationship
;
37 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationshipType
;
38 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
39 import eu
.etaxonomy
.taxeditor
.model
.DataChangeBridge
;
40 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
41 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
42 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
46 * <p>TaxonEditorInput class.</p>
52 public class TaxonEditorInput
extends CdmEntitySessionInput
implements IEditorInput
, IConversationEnabled
, IPersistableElement
{
54 private final ConversationHolder conversation
;
56 private TaxonNode taxonNode
;
58 private TaxonEditorInputDataChangeBehaviour dataChangeBehavior
;
60 private TaxonBase initiallySelectedTaxonBase
;
62 private enum CdmType
{
68 private TaxonEditorInput(UUID uuid
, CdmType type
) {
69 this.conversation
= CdmStore
.createConversation();
71 case PARENT_TAXON_NODE
:
72 initForParentTaxonNode(uuid
);
75 initForTaxonBase(uuid
);
78 initForTaxonNode(uuid
);
83 private void init(TaxonNode taxonNode
) {
84 this.taxonNode
= taxonNode
;
91 * @param taxonNodeUuid a {@link java.util.UUID} object.
92 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
93 * @throws java.lang.Exception if any.
95 private void initForTaxonNode(UUID taxonNodeUuid
) {
97 List
<String
> taxonNodePropertyPaths
= Arrays
.asList(new String
[] {
104 "taxon.descriptions",
105 "taxon.synonymRelations",
106 "taxon.relationsToThisTaxon",
107 "taxon.relationsFromThisTaxon",
110 "taxon.name.homotypicalGroup.typifiedNames",
111 "taxon.name.taxonBases",
112 "taxon.name.descriptions.descriptionElements",
113 "taxon.name.descriptions.markers"
118 TaxonNode taxonNode
= CdmStore
.getService(ITaxonNodeService
.class).load(taxonNodeUuid
, taxonNodePropertyPaths
);
120 if(taxonNode
== null){
121 MessagingUtils
.warningDialog("Not yet implemented", TaxonEditorInput
.class, "Selected element is not type TaxonBase.");
127 private void initForTaxonBase(UUID taxonBaseUuid
) {
129 TaxonBase taxonBase
= CdmStore
.getService(ITaxonService
.class).load(taxonBaseUuid
);
130 if (taxonBase
!= null){
131 if(taxonBase
.isInstanceOf(Taxon
.class)){
132 Taxon taxon
= CdmBase
.deproxy(taxonBase
, Taxon
.class);
134 if (taxon
.getTaxonNodes().size() == 0 && taxon
.isMisapplication()){
135 // TODO get accepted taxon
136 MessagingUtils
.info("trying to open Mispplied Name ");
138 Set
<Taxon
> acceptedTaxa
= new HashSet
<Taxon
>();
139 Set
<TaxonRelationship
> relations
= taxon
.getRelationsFromThisTaxon();
140 for(TaxonRelationship relation
: relations
){
141 if(relation
.getType().equals(TaxonRelationshipType
.MISAPPLIED_NAME_FOR())){
142 acceptedTaxa
.add(relation
.getToTaxon());
145 setInputForMultipleTaxa(conversation
, acceptedTaxa
);
148 setInputForMultipleNodes(conversation
, taxon
.getTaxonNodes());
150 }else if(taxonBase
instanceof Synonym
){
151 Synonym synonym
= (Synonym
) taxonBase
;
153 Set
<Taxon
> taxa
= synonym
.getAcceptedTaxa();
154 setInputForMultipleTaxa(conversation
, taxa
);
161 * <p>NewEmptyInstance</p>
163 * @param parentNodeUuid a {@link java.util.UUID} object.
164 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
166 private void initForParentTaxonNode(UUID parentNodeUuid
){
169 TaxonNameBase
<?
, ?
> name
= PreferencesUtil
.getPreferredNomenclaturalCode().getNewTaxonNameInstance(null);
170 ITaxonTreeNode parentNode
= CdmStore
.getService(IClassificationService
.class).getTreeNodeByUuid(parentNodeUuid
);
172 Taxon newTaxon
= Taxon
.NewInstance(name
, parentNode
.getReference());
173 TaxonNode newTaxonNode
= parentNode
.addChildTaxon(newTaxon
, parentNode
.getReference(), parentNode
.getMicroReference());
175 // add the new taxon to the editors persistence context
176 UUID newTaxonNodeUuid
= CdmStore
.getService(ITaxonNodeService
.class).save(newTaxonNode
).getUuid();
178 initForTaxonNode(newTaxonNodeUuid
);
184 private void setInputForMultipleNodes(ConversationHolder conversation
, Set
<TaxonNode
> taxonNodes
){
185 if(taxonNodes
.size() == 1){
186 TaxonNode taxonNode
= taxonNodes
.iterator().next();
188 }else if(taxonNodes
.size() > 1){
189 TaxonNode taxonNode
= ChooseFromMultipleTaxonNodesDialog
.choose(taxonNodes
);
190 if(taxonNode
!= null){
193 }else if(taxonNodes
.size() == 0){
194 // this is an undesired state
195 MessagingUtils
.warningDialog("Incorrect state", TaxonEditorInput
.class, "The accepted taxon is not part of any classification. This should not have happened.");
199 private void setInputForMultipleTaxa(ConversationHolder conversation
, Set
<Taxon
> taxa
){
200 if(taxa
.size() == 1){
201 Taxon taxon
= taxa
.iterator().next();
202 Set
<TaxonNode
> nodes
= taxon
.getTaxonNodes();
203 setInputForMultipleNodes(conversation
, nodes
);
204 }else if(taxa
.size() > 1){
205 Set
<TaxonNode
> taxonNodes
= new HashSet
<TaxonNode
>();
206 for ( Taxon taxon
: taxa
){
207 taxonNodes
.addAll(taxon
.getTaxonNodes());
209 setInputForMultipleNodes(conversation
, taxonNodes
);
210 }else if(taxa
.size() == 0){
211 // this is an undesired state
212 MessagingUtils
.warningDialog("Incorrect state", TaxonEditorInput
.class, "Trying to open accepted taxon for a synonym or misapplication but" +
213 " no accepted taxa are present. This should not have happened.");
220 * @param taxonNodeUuid a {@link java.util.UUID} object.
221 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
222 * @throws java.lang.Exception if any.
224 public static TaxonEditorInput
NewInstance(UUID taxonNodeUuid
) throws Exception
{
225 return new TaxonEditorInput(taxonNodeUuid
, CdmType
.TAXON_NODE
);
230 * <p>NewInstanceFromTaxonBase</p>
232 * @param taxonBaseUuid a {@link java.util.UUID} object.
233 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
235 public static TaxonEditorInput
NewInstanceFromTaxonBase(UUID taxonBaseUuid
){
236 return new TaxonEditorInput(taxonBaseUuid
, CdmType
.TAXON_BASE
);
241 * <p>NewEmptyInstance</p>
243 * @param parentNodeUuid a {@link java.util.UUID} object.
244 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
246 public static TaxonEditorInput
NewEmptyInstance(UUID parentNodeUuid
){
247 return new TaxonEditorInput(parentNodeUuid
, CdmType
.PARENT_TAXON_NODE
);
251 * @see org.eclipse.ui.IEditorInput#exists()
259 public boolean exists() {
260 return taxonNode
!= null;
264 * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
267 * <p>getImageDescriptor</p>
269 * @return a {@link org.eclipse.jface.resource.ImageDescriptor} object.
272 public ImageDescriptor
getImageDescriptor() {
277 * @see org.eclipse.ui.IEditorInput#getName()
282 * @return a {@link java.lang.String} object.
285 public String
getName() {
286 if(getTaxon() == null){
289 TaxonNameBase
<?
, ?
> name
= getTaxon().getName();
290 if (name
== null || name
.getTitleCache() == null) {
293 return name
.getTitleCache();
298 * @see org.eclipse.ui.IEditorInput#getPersistable()
301 * <p>getPersistable</p>
303 * @return a {@link org.eclipse.ui.IPersistableElement} object.
306 public IPersistableElement
getPersistable() {
307 // if(CdmStore.isActive()){
308 // TaxonNode test = CdmStore.getTaxonTreeService().getTaxonNodeByUuid(taxonNode.getUuid());
309 // boolean isPersistable = CdmStore.getTaxonTreeService().getTaxonNodeByUuid(taxonNode.getUuid()) != null;
310 // if (isPersistable) {
318 * @see org.eclipse.ui.IEditorInput#getToolTipText()
321 * <p>getToolTipText</p>
323 * @return a {@link java.lang.String} object.
326 public String
getToolTipText() {
331 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
335 public Object
getAdapter(Class adapter
) {
337 if (adapter
== Taxon
.class) {
341 if (adapter
== TaxonNode
.class) {
351 * Overrides equals to ensure that a taxon can only be edited by
352 * one editor at a time.
355 public boolean equals(Object obj
) {
356 if (TaxonEditorInput
.class.equals(obj
.getClass())
357 && getTaxon() != null
358 && getTaxon().equals(((TaxonEditorInput
) obj
).getTaxon())){
359 if(((TaxonEditorInput
) obj
).getInitiallySelectedTaxonBase() != null){
360 setInitiallySelectedTaxonBase(((TaxonEditorInput
) obj
).getInitiallySelectedTaxonBase());
372 public Taxon
getTaxon(){
373 Taxon taxon
= CdmBase
.deproxy(taxonNode
.getTaxon(), Taxon
.class);
378 * <p>Getter for the field <code>taxonNode</code>.</p>
380 * @return the taxonNode
382 public TaxonNode
getTaxonNode() {
388 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
391 * <p>getConversationHolder</p>
393 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
396 public ConversationHolder
getConversationHolder() {
402 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
406 public void update(CdmDataChangeMap events
) {
407 if(dataChangeBehavior
== null){
408 dataChangeBehavior
= new TaxonEditorInputDataChangeBehaviour(this);
411 DataChangeBridge
.handleDataChange(events
, dataChangeBehavior
);
415 * @see org.eclipse.ui.IPersistableElement#getFactoryId()
418 * <p>getFactoryId</p>
420 * @return a {@link java.lang.String} object.
423 public String
getFactoryId() {
424 return TaxonEditorInputFactory
.getFactoryId();
428 * @see org.eclipse.ui.IPersistable#saveState(org.eclipse.ui.IMemento)
432 public void saveState(IMemento memento
) {
433 TaxonEditorInputFactory
.saveState(memento
, this);
438 * <p>Setter for the field <code>initiallySelectedTaxonBase</code>.</p>
440 * @param taxonBase a {@link eu.etaxonomy.cdm.model.taxon.TaxonBase} object.
442 public void setInitiallySelectedTaxonBase(TaxonBase taxonBase
) {
443 this.initiallySelectedTaxonBase
= taxonBase
;
447 * <p>Getter for the field <code>initiallySelectedTaxonBase</code>.</p>
449 * @return a {@link eu.etaxonomy.cdm.model.taxon.TaxonBase} object.
451 public TaxonBase
getInitiallySelectedTaxonBase() {
452 return initiallySelectedTaxonBase
;
456 // public String toString() {
457 // return String.format("%s[%s]", this.getClass().getSimpleName(), getTaxon());
462 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getRootEntities()
465 public List
<TaxonNode
> getRootEntities() {
466 return Arrays
.asList(taxonNode
);
470 * @see eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput#merge()
473 public void merge() {
474 CdmStore
.getService(ITaxonNodeService
.class).merge(taxonNode
);
478 public Map
<Object
, List
<String
>> getPropertyPathsMap() {
479 // Map<Object, List<String>> propertyPathsMap = new HashMap<Object, List<String>>();
480 // List<String> taxonNameBasePropertyPaths = Arrays.asList(new String[] {
482 // "taxonBases.taxonNodes",
483 // "homotypicalGroup.typifiedNames.taxonBases.synonymRelations",
484 // "taxonBases.relationsToThisTaxon",
485 // "taxonBases.relationsFromThisTaxon",
488 // propertyPathsMap.put(TaxonNameBase.class, taxonNameBasePropertyPaths);
490 // List<String> taxonBasePropertyPaths = Arrays.asList(new String[] {
491 // "taxon.annotations",
494 // "taxon.extensions",
497 // "taxon.descriptions"
499 // propertyPathsMap.put(TaxonNode.class, taxonBasePropertyPaths);
500 // return propertyPathsMap;