ff9bb4e41271cbd7b6e71bf62525f6ef7fd3dff4
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / editor / TaxonEditorInput.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9
10 package eu.etaxonomy.taxeditor.editor;
11
12 import java.util.UUID;
13
14 import org.apache.log4j.Logger;
15 import org.eclipse.jface.dialogs.MessageDialog;
16 import org.eclipse.jface.resource.ImageDescriptor;
17 import org.eclipse.ui.IEditorInput;
18 import org.eclipse.ui.IMemento;
19 import org.eclipse.ui.IPersistableElement;
20
21 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
22 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
23 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
24 import eu.etaxonomy.cdm.model.taxon.Taxon;
25 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
26 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
27 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
28 import eu.etaxonomy.taxeditor.store.CdmStore;
29
30 /**
31 * @author n.hoffmann
32 * @created 19.03.2009
33 * @version 1.0
34 */
35 public class TaxonEditorInput implements IEditorInput, IConversationEnabled, IPersistableElement {
36 private static final Logger logger = Logger
37 .getLogger(TaxonEditorInput.class);
38
39 private ConversationHolder conversation;
40
41 private TaxonNode taxonNode;
42
43 private TaxonEditorInput(TaxonNode taxonNode, ConversationHolder conversation){
44 this.taxonNode = taxonNode;
45 this.conversation = conversation;
46 }
47
48 /**
49 *
50 * @param uuid
51 * @param conversation
52 * @return
53 */
54 public static TaxonEditorInput NewInstance(UUID uuid){
55 ConversationHolder conversation = CdmStore.NewTransactionalConversation();
56
57 TaxonNode taxonNode = CdmStore.getTaxonService().getTaxonNodeByUuid(uuid);
58
59 if(taxonNode == null){
60 MessageDialog.openWarning(EditorUtil.getShell(), "Not yet implemented", "Selected element is not if type TaxonBase.");
61 return null;
62 }
63
64 Taxon taxon;
65
66 // FIXME had to disable synonym handling for now while implementing taxonNode
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){
73 // String message;
74 // if(acceptedTaxa.size() == 0){
75 // message = "There is no accepted taxon for the chosen synonym. ";
76 // }else{
77 // message = "Multiple taxa found for the chosen synonym.";
78 // }
79 // MessageDialog.openWarning(EditorUtil.getShell(), "Not yet implemented", message);
80 // return null;
81 // }else{
82 // taxon = acceptedTaxa.toArray(new Taxon[0])[0];
83 // }
84 // } else {
85 // taxon = (Taxon) taxonBase;
86 // }
87
88
89 logger.trace(TaxonEditorInput.class.getSimpleName() + " created");
90
91 return new TaxonEditorInput(taxonNode, conversation);
92 }
93
94 public static TaxonEditorInput NewEmptyInstance(UUID parentTaxonNodeUuid){
95 ConversationHolder conversation = CdmStore.NewTransactionalConversation();
96
97 TaxonNameBase<?, ?> name = PreferencesUtil.getInstanceOfPreferredNameClass();
98
99 Taxon newTaxon = null;
100 TaxonNode newTaxonNode = null;
101
102 // FIXME adding of root taxon nodes disabled for now. Still have to find a way to
103 // which taxonomic tree the node shoul dbe added
104 // if(parentTaxonNodeUuid == null){
105 // newTaxon = Taxon.NewInstance(name, CdmStore.getDefault().getDefaultSec());
106 // newTaxonNode = new TaxonNode();
107 // }else{
108 TaxonNode parentTaxonNode = CdmStore.getTaxonService().getTaxonNodeByUuid(parentTaxonNodeUuid);
109
110 newTaxon = Taxon.NewInstance(name, parentTaxonNode.getTaxon().getSec());
111
112 newTaxonNode = parentTaxonNode.addChild(newTaxon, parentTaxonNode.getReferenceForParentChildRelation(), parentTaxonNode.getMicroReferenceForParentChildRelation());
113 // }
114 // add the new taxon to the editors persistence context
115 CdmStore.getTaxonService().saveTaxonNode(newTaxonNode);
116
117 return new TaxonEditorInput(newTaxonNode, conversation);
118 }
119
120 /* (non-Javadoc)
121 * @see org.eclipse.ui.IEditorInput#exists()
122 */
123 public boolean exists() {
124 return taxonNode != null;
125 }
126
127 /* (non-Javadoc)
128 * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
129 */
130 public ImageDescriptor getImageDescriptor() {
131 return null;
132 }
133
134 /* (non-Javadoc)
135 * @see org.eclipse.ui.IEditorInput#getName()
136 */
137 public String getName() {
138 TaxonNameBase<?, ?> name = getTaxon().getName();
139 if (name == null || name.getTitleCache() == null) {
140 return "New taxon";
141 } else {
142 return name.getTitleCache();
143 }
144 }
145
146 /* (non-Javadoc)
147 * @see org.eclipse.ui.IEditorInput#getPersistable()
148 */
149 public IPersistableElement getPersistable() {
150 return this;
151 }
152
153 /* (non-Javadoc)
154 * @see org.eclipse.ui.IEditorInput#getToolTipText()
155 */
156 public String getToolTipText() {
157 return getName();
158 }
159
160 /* (non-Javadoc)
161 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
162 */
163 public Object getAdapter(Class adapter) {
164
165 if (adapter == Taxon.class) {
166 return taxonNode.getTaxon();
167 }
168
169 if (adapter == TaxonNode.class) {
170 return taxonNode;
171 }
172
173 return null;
174 }
175
176 /**
177 * Overrides equals to ensure that a taxon can only be edited by
178 * one editor at a time.
179 *
180 * @return boolean
181 */
182 public boolean equals(Object obj) {
183 if (obj.getClass().equals(TaxonEditorInput.class)
184 && getTaxon().equals(((TaxonEditorInput) obj).getTaxon()))
185 return true;
186 return false;
187 }
188
189 /**
190 * @return the taxon
191 */
192 public Taxon getTaxon(){
193 return taxonNode.getTaxon();
194 }
195
196 /**
197 * @return the taxonNode
198 */
199 public TaxonNode getTaxonNode() {
200 return taxonNode;
201 }
202
203 /*
204 * (non-Javadoc)
205 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
206 */
207 public ConversationHolder getConversationHolder() {
208 return conversation;
209 }
210
211 /*
212 * (non-Javadoc)
213 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
214 */
215 public void update(CdmDataChangeMap events) {
216 // FIXME update the editor input
217 }
218
219 /* (non-Javadoc)
220 * @see org.eclipse.ui.IPersistableElement#getFactoryId()
221 */
222 public String getFactoryId() {
223 return TaxonEditorInputFactory.getFactoryId();
224 }
225
226 /* (non-Javadoc)
227 * @see org.eclipse.ui.IPersistable#saveState(org.eclipse.ui.IMemento)
228 */
229 public void saveState(IMemento memento) {
230 TaxonEditorInputFactory.saveState(memento, this);
231 }
232 }