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
;
13 import java
.util
.UUID
;
15 import org
.apache
.log4j
.Logger
;
16 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
17 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
18 import org
.eclipse
.ui
.IEditorInput
;
19 import org
.eclipse
.ui
.IMemento
;
20 import org
.eclipse
.ui
.IPersistableElement
;
22 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
23 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
24 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
25 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
26 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
27 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
28 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
29 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
30 import eu
.etaxonomy
.taxeditor
.store
.preference
.PreferencesUtil
;
37 public class TaxonEditorInput
implements IEditorInput
, IConversationEnabled
, IPersistableElement
{
38 private static final Logger logger
= Logger
39 .getLogger(TaxonEditorInput
.class);
42 private ConversationHolder conversation
;
44 private TaxonEditorInput(Taxon taxon
, ConversationHolder conversation
){
46 this.conversation
= conversation
;
55 public static TaxonEditorInput
NewInstance(UUID uuid
){
56 ConversationHolder conversation
= CdmStore
.NewTransactionalConversation();
58 TaxonBase
<?
> taxonBase
= CdmStore
.getTaxonService().getTaxonByUuid(uuid
);
60 if(taxonBase
== null){
61 MessageDialog
.openWarning(EditorUtil
.getShell(), "Not yet implemented", "Selected element is not if type TaxonBase.");
67 if (taxonBase
instanceof Synonym
) {
68 // TODO: in case of pro parte synonym or any other where we might have multiple
69 // accepted taxa we have to provide a mechanism that can deal with that
70 // TODO set focus to the synonym
71 Set
<Taxon
> acceptedTaxa
= ((Synonym
) taxonBase
).getAcceptedTaxa();
72 if(acceptedTaxa
.size() != 1){
74 if(acceptedTaxa
.size() == 0){
75 message
= "There is no accepted taxon for the chosen synonym. ";
77 message
= "Multiple taxa found for the chosen synonym.";
79 MessageDialog
.openWarning(EditorUtil
.getShell(), "Not yet implemented", message
);
82 taxon
= acceptedTaxa
.toArray(new Taxon
[0])[0];
85 taxon
= (Taxon
) taxonBase
;
88 return new TaxonEditorInput(taxon
, conversation
);
91 public static TaxonEditorInput
NewEmptyInstance(UUID parentTaxonUuid
){
92 ConversationHolder conversation
= CdmStore
.NewTransactionalConversation();
94 TaxonNameBase name
= PreferencesUtil
.getInstanceOfPreferredNameClass();
96 Taxon newTaxon
= null;
97 if(parentTaxonUuid
== null){
98 newTaxon
= Taxon
.NewInstance(name
, CdmStore
.getDefault().getDefaultSec());
100 Taxon parentTaxon
= (Taxon
) CdmStore
.getTaxonService().getTaxonByUuid(parentTaxonUuid
);
101 newTaxon
= Taxon
.NewInstance(name
, parentTaxon
.getSec());
102 parentTaxon
.addTaxonomicChild(newTaxon
, null, null);
104 // add the new taxon to the editors persistence context
105 CdmStore
.getTaxonService().save(newTaxon
);
107 return new TaxonEditorInput(newTaxon
, conversation
);
111 * @see org.eclipse.ui.IEditorInput#exists()
113 public boolean exists() {
114 return taxon
!= null;
118 * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
120 public ImageDescriptor
getImageDescriptor() {
125 * @see org.eclipse.ui.IEditorInput#getName()
127 public String
getName() {
128 TaxonNameBase name
= taxon
.getName();
129 if (name
== null || name
.getTitleCache() == null) {
132 return name
.getTitleCache();
137 * @see org.eclipse.ui.IEditorInput#getPersistable()
139 public IPersistableElement
getPersistable() {
144 * @see org.eclipse.ui.IEditorInput#getToolTipText()
146 public String
getToolTipText() {
151 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
153 public Object
getAdapter(Class adapter
) {
155 if (adapter
== Taxon
.class) {
163 * Overrides equals to ensure that a taxon can only be edited by
164 * one editor at a time.
168 public boolean equals(Object obj
) {
169 if (obj
.getClass().equals(TaxonEditorInput
.class)
170 && taxon
.equals(((TaxonEditorInput
) obj
).taxon
))
175 public Taxon
getTaxon(){
181 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
183 public ConversationHolder
getConversationHolder() {
189 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
191 public void update(CdmDataChangeMap events
) {
192 // FIXME update the editor input
196 * @see org.eclipse.ui.IPersistableElement#getFactoryId()
198 public String
getFactoryId() {
199 return TaxonEditorInputFactory
.getFactoryId();
203 * @see org.eclipse.ui.IPersistable#saveState(org.eclipse.ui.IMemento)
205 public void saveState(IMemento memento
) {
206 TaxonEditorInputFactory
.saveState(memento
, this);