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
;
16 import java
.util
.UUID
;
18 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
19 import org
.eclipse
.ui
.IEditorInput
;
20 import org
.eclipse
.ui
.IMemento
;
21 import org
.eclipse
.ui
.IPersistableElement
;
23 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
24 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
25 import eu
.etaxonomy
.cdm
.api
.service
.IClassificationService
;
26 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonNodeService
;
27 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
28 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
29 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
30 import eu
.etaxonomy
.cdm
.model
.taxon
.ITaxonTreeNode
;
31 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
32 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
33 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationship
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationshipType
;
37 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
38 import eu
.etaxonomy
.taxeditor
.model
.DataChangeBridge
;
39 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
40 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
41 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
45 * <p>TaxonEditorInput class.</p>
51 public class TaxonEditorInput
extends CdmEntitySessionInput
implements IEditorInput
, IConversationEnabled
, IPersistableElement
{
53 private final ConversationHolder conversation
;
55 private TaxonNode taxonNode
;
57 private TaxonEditorInputDataChangeBehaviour dataChangeBehavior
;
59 private TaxonBase initiallySelectedTaxonBase
;
61 private final ITaxonNodeService service
;
63 private enum CdmType
{
69 private TaxonEditorInput(UUID uuid
, CdmType type
) {
70 this.conversation
= CdmStore
.createConversation();
71 service
= CdmStore
.getService(ITaxonNodeService
.class);
73 case PARENT_TAXON_NODE
:
74 initForParentTaxonNode(uuid
);
77 initForTaxonBase(uuid
);
80 initForTaxonNode(uuid
);
85 private void init(TaxonNode taxonNode
) {
86 this.taxonNode
= taxonNode
;
93 * @param taxonNodeUuid a {@link java.util.UUID} object.
94 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
95 * @throws java.lang.Exception if any.
97 private void initForTaxonNode(UUID taxonNodeUuid
) {
100 TaxonNode taxonNode
= getCdmEntitySession().remoteLoad(service
,taxonNodeUuid
, null);
102 if(taxonNode
== null){
103 MessagingUtils
.warningDialog("Not yet implemented", TaxonEditorInput
.class, "Selected element is not type TaxonBase.");
109 private void initForTaxonBase(UUID taxonBaseUuid
) {
111 TaxonBase taxonBase
= getCdmEntitySession().remoteLoad(CdmStore
.getService(ITaxonService
.class),taxonBaseUuid
);
112 if (taxonBase
!= null){
113 if(taxonBase
.isInstanceOf(Taxon
.class)){
114 Taxon taxon
= CdmBase
.deproxy(taxonBase
, Taxon
.class);
116 if (taxon
.getTaxonNodes().size() == 0 && taxon
.isMisapplication()){
117 // TODO get accepted taxon
118 MessagingUtils
.info("trying to open Mispplied Name ");
120 Set
<Taxon
> acceptedTaxa
= new HashSet
<Taxon
>();
121 Set
<TaxonRelationship
> relations
= taxon
.getRelationsFromThisTaxon();
122 for(TaxonRelationship relation
: relations
){
123 if(relation
.getType().equals(TaxonRelationshipType
.MISAPPLIED_NAME_FOR())){
124 acceptedTaxa
.add(relation
.getToTaxon());
127 setInputForMultipleTaxa(conversation
, acceptedTaxa
);
130 setInputForMultipleNodes(conversation
, taxon
.getTaxonNodes());
132 }else if(taxonBase
instanceof Synonym
){
133 Synonym synonym
= (Synonym
) taxonBase
;
135 Set
<Taxon
> taxa
= synonym
.getAcceptedTaxa();
136 setInputForMultipleTaxa(conversation
, taxa
);
143 * <p>NewEmptyInstance</p>
145 * @param parentNodeUuid a {@link java.util.UUID} object.
146 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
148 private void initForParentTaxonNode(UUID parentNodeUuid
){
151 TaxonNameBase
<?
, ?
> name
= PreferencesUtil
.getPreferredNomenclaturalCode().getNewTaxonNameInstance(null);
152 ITaxonTreeNode parentNode
= CdmStore
.getService(IClassificationService
.class).getTreeNodeByUuid(parentNodeUuid
);
154 Taxon newTaxon
= Taxon
.NewInstance(name
, parentNode
.getReference());
155 TaxonNode newTaxonNode
= parentNode
.addChildTaxon(newTaxon
, parentNode
.getReference(), parentNode
.getMicroReference());
157 // add the new taxon to the editors persistence context
158 UUID newTaxonNodeUuid
= getCdmEntitySession().remoteSave(CdmStore
.getService(ITaxonNodeService
.class),newTaxonNode
);
160 initForTaxonNode(newTaxonNodeUuid
);
164 private void setInputForMultipleNodes(ConversationHolder conversation
, Set
<TaxonNode
> taxonNodes
){
165 if(taxonNodes
.size() == 1){
166 TaxonNode taxonNode
= taxonNodes
.iterator().next();
168 }else if(taxonNodes
.size() > 1){
169 TaxonNode taxonNode
= ChooseFromMultipleTaxonNodesDialog
.choose(taxonNodes
);
170 if(taxonNode
!= null){
173 }else if(taxonNodes
.size() == 0){
174 // this is an undesired state
175 MessagingUtils
.warningDialog("Incorrect state", TaxonEditorInput
.class, "The accepted taxon is not part of any classification. This should not have happened.");
179 private void setInputForMultipleTaxa(ConversationHolder conversation
, Set
<Taxon
> taxa
){
180 if(taxa
.size() == 1){
181 Taxon taxon
= taxa
.iterator().next();
182 Set
<TaxonNode
> nodes
= taxon
.getTaxonNodes();
183 setInputForMultipleNodes(conversation
, nodes
);
184 }else if(taxa
.size() > 1){
185 Set
<TaxonNode
> taxonNodes
= new HashSet
<TaxonNode
>();
186 for ( Taxon taxon
: taxa
){
187 taxonNodes
.addAll(taxon
.getTaxonNodes());
189 setInputForMultipleNodes(conversation
, taxonNodes
);
190 }else if(taxa
.size() == 0){
191 // this is an undesired state
192 MessagingUtils
.warningDialog("Incorrect state", TaxonEditorInput
.class, "Trying to open accepted taxon for a synonym or misapplication but" +
193 " no accepted taxa are present. This should not have happened.");
200 * @param taxonNodeUuid a {@link java.util.UUID} object.
201 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
202 * @throws java.lang.Exception if any.
204 public static TaxonEditorInput
NewInstance(UUID taxonNodeUuid
) throws Exception
{
205 return new TaxonEditorInput(taxonNodeUuid
, CdmType
.TAXON_NODE
);
210 * <p>NewInstanceFromTaxonBase</p>
212 * @param taxonBaseUuid a {@link java.util.UUID} object.
213 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
215 public static TaxonEditorInput
NewInstanceFromTaxonBase(UUID taxonBaseUuid
){
216 return new TaxonEditorInput(taxonBaseUuid
, CdmType
.TAXON_BASE
);
223 * <p>NewEmptyInstance</p>
225 * @param parentNodeUuid a {@link java.util.UUID} object.
226 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
228 public static TaxonEditorInput
NewEmptyInstance(UUID parentNodeUuid
){
229 return new TaxonEditorInput(parentNodeUuid
, CdmType
.PARENT_TAXON_NODE
);
233 * @see org.eclipse.ui.IEditorInput#exists()
241 public boolean exists() {
242 return taxonNode
!= null;
246 * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
249 * <p>getImageDescriptor</p>
251 * @return a {@link org.eclipse.jface.resource.ImageDescriptor} object.
254 public ImageDescriptor
getImageDescriptor() {
259 * @see org.eclipse.ui.IEditorInput#getName()
264 * @return a {@link java.lang.String} object.
267 public String
getName() {
268 if(getTaxon() == null){
271 TaxonNameBase
<?
, ?
> name
= getTaxon().getName();
272 if (name
== null || name
.getTitleCache() == null) {
275 return name
.getTitleCache();
280 * @see org.eclipse.ui.IEditorInput#getPersistable()
283 * <p>getPersistable</p>
285 * @return a {@link org.eclipse.ui.IPersistableElement} object.
288 public IPersistableElement
getPersistable() {
289 // if(CdmStore.isActive()){
290 // TaxonNode test = CdmStore.getTaxonTreeService().getTaxonNodeByUuid(taxonNode.getUuid());
291 // boolean isPersistable = CdmStore.getTaxonTreeService().getTaxonNodeByUuid(taxonNode.getUuid()) != null;
292 // if (isPersistable) {
300 * @see org.eclipse.ui.IEditorInput#getToolTipText()
303 * <p>getToolTipText</p>
305 * @return a {@link java.lang.String} object.
308 public String
getToolTipText() {
313 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
317 public Object
getAdapter(Class adapter
) {
319 if (adapter
== Taxon
.class) {
323 if (adapter
== TaxonNode
.class) {
333 * Overrides equals to ensure that a taxon can only be edited by
334 * one editor at a time.
337 public boolean equals(Object obj
) {
338 if (TaxonEditorInput
.class.equals(obj
.getClass())
339 && getTaxon() != null
340 && getTaxon().equals(((TaxonEditorInput
) obj
).getTaxon())){
341 if(((TaxonEditorInput
) obj
).getInitiallySelectedTaxonBase() != null){
342 setInitiallySelectedTaxonBase(((TaxonEditorInput
) obj
).getInitiallySelectedTaxonBase());
354 public Taxon
getTaxon(){
355 Taxon taxon
= CdmBase
.deproxy(taxonNode
.getTaxon(), Taxon
.class);
360 * <p>Getter for the field <code>taxonNode</code>.</p>
362 * @return the taxonNode
364 public TaxonNode
getTaxonNode() {
370 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
373 * <p>getConversationHolder</p>
375 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
378 public ConversationHolder
getConversationHolder() {
384 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
388 public void update(CdmDataChangeMap events
) {
389 if(dataChangeBehavior
== null){
390 dataChangeBehavior
= new TaxonEditorInputDataChangeBehaviour(this);
393 DataChangeBridge
.handleDataChange(events
, dataChangeBehavior
);
397 * @see org.eclipse.ui.IPersistableElement#getFactoryId()
400 * <p>getFactoryId</p>
402 * @return a {@link java.lang.String} object.
405 public String
getFactoryId() {
406 return TaxonEditorInputFactory
.getFactoryId();
410 * @see org.eclipse.ui.IPersistable#saveState(org.eclipse.ui.IMemento)
414 public void saveState(IMemento memento
) {
415 TaxonEditorInputFactory
.saveState(memento
, this);
420 * <p>Setter for the field <code>initiallySelectedTaxonBase</code>.</p>
422 * @param taxonBase a {@link eu.etaxonomy.cdm.model.taxon.TaxonBase} object.
424 public void setInitiallySelectedTaxonBase(TaxonBase taxonBase
) {
425 this.initiallySelectedTaxonBase
= taxonBase
;
429 * <p>Getter for the field <code>initiallySelectedTaxonBase</code>.</p>
431 * @return a {@link eu.etaxonomy.cdm.model.taxon.TaxonBase} object.
433 public TaxonBase
getInitiallySelectedTaxonBase() {
434 return initiallySelectedTaxonBase
;
438 public String
toString() {
439 return String
.format("%s[%s]", this.getClass().getSimpleName(), getTaxon());
443 * @see eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput#getService()
446 public ITaxonNodeService
getService() {
452 * @see eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput#getRootEntities()
455 public List
<TaxonNode
> getRootEntities() {
456 return Arrays
.asList(taxonNode
);
461 * @see eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput#update()
464 public <T
extends CdmBase
> void update() {
465 taxonNode
= getCdmEntitySession().remoteUpdate(service
, taxonNode
);