conversations get closed explicitly now
[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.ITreeNode;
25 import eu.etaxonomy.cdm.model.taxon.Taxon;
26 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
27 import eu.etaxonomy.cdm.model.taxon.TaxonomicTree;
28 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
29 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
30 import eu.etaxonomy.taxeditor.store.CdmStore;
31
32 /**
33 * @author n.hoffmann
34 * @created 19.03.2009
35 * @version 1.0
36 */
37 public class TaxonEditorInput implements IEditorInput, IConversationEnabled, IPersistableElement {
38 private static final Logger logger = Logger
39 .getLogger(TaxonEditorInput.class);
40
41 private ConversationHolder conversation;
42
43 private TaxonNode taxonNode;
44
45 private TaxonEditorInput(TaxonNode taxonNode, ConversationHolder conversation){
46 this.taxonNode = taxonNode;
47 this.conversation = conversation;
48 }
49
50 /**
51 *
52 * @param taxonNodeUuid
53 * @param conversation
54 * @return
55 */
56 public static TaxonEditorInput NewInstance(UUID taxonNodeUuid){
57 ConversationHolder conversation = CdmStore.NewTransactionalConversation();
58
59 TaxonNode taxonNode = CdmStore.getTaxonTreeService().getTaxonNodeByUuid(taxonNodeUuid);
60
61 if(taxonNode == null){
62 MessageDialog.openWarning(EditorUtil.getShell(), "Not yet implemented", "Selected element is not type TaxonBase.");
63 return null;
64 }
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 parentNodeUuid){
95 ConversationHolder conversation = CdmStore.NewTransactionalConversation();
96
97 TaxonNameBase<?, ?> name = PreferencesUtil.getPreferredNomenclaturalCode().getNewTaxonNameInstance(null);
98 ITreeNode parentNode = CdmStore.getTaxonTreeService().getTreeNodeByUuid(parentNodeUuid);
99
100 Taxon newTaxon = Taxon.NewInstance(name, parentNode.getReference());
101 TaxonNode newTaxonNode = parentNode.addChildTaxon(newTaxon, parentNode.getReference(), parentNode.getMicroReference(), null);
102
103 // add the new taxon to the editors persistence context
104 CdmStore.getTaxonTreeService().saveTaxonNode(newTaxonNode);
105 // we pass on the conversation, will be closed when the editor is closed
106 return new TaxonEditorInput(newTaxonNode, conversation);
107 }
108
109 /* (non-Javadoc)
110 * @see org.eclipse.ui.IEditorInput#exists()
111 */
112 public boolean exists() {
113 return taxonNode != null;
114 }
115
116 /* (non-Javadoc)
117 * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
118 */
119 public ImageDescriptor getImageDescriptor() {
120 return null;
121 }
122
123 /* (non-Javadoc)
124 * @see org.eclipse.ui.IEditorInput#getName()
125 */
126 public String getName() {
127 TaxonNameBase<?, ?> name = getTaxon().getName();
128 if (name == null || name.getTitleCache() == null) {
129 return "New taxon";
130 } else {
131 return name.getTitleCache();
132 }
133 }
134
135 /* (non-Javadoc)
136 * @see org.eclipse.ui.IEditorInput#getPersistable()
137 */
138 public IPersistableElement getPersistable() {
139 ConversationHolder internalConversation = null;
140 if(conversation.isClosed())
141 internalConversation = CdmStore.NewTransactionalConversation();
142 try{
143 boolean isPersistable = CdmStore.getTaxonTreeService().getTaxonNodeByUuid(taxonNode.getUuid()) != null;
144 if (isPersistable) {
145 return this;
146 } else {
147 return null;
148 }
149 }finally{
150 if(internalConversation != null)
151 internalConversation.close();
152 }
153 }
154
155 /* (non-Javadoc)
156 * @see org.eclipse.ui.IEditorInput#getToolTipText()
157 */
158 public String getToolTipText() {
159 return getName();
160 }
161
162 /* (non-Javadoc)
163 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
164 */
165 public Object getAdapter(Class adapter) {
166
167 if (adapter == Taxon.class) {
168 return taxonNode.getTaxon();
169 }
170
171 if (adapter == TaxonNode.class) {
172 return taxonNode;
173 }
174
175 return null;
176 }
177
178 /**
179 * Overrides equals to ensure that a taxon can only be edited by
180 * one editor at a time.
181 *
182 * @return boolean
183 */
184 public boolean equals(Object obj) {
185 if (obj.getClass().equals(TaxonEditorInput.class)
186 && getTaxon().equals(((TaxonEditorInput) obj).getTaxon()))
187 return true;
188 return false;
189 }
190
191 /**
192 * @return the taxon
193 */
194 public Taxon getTaxon(){
195 return taxonNode.getTaxon();
196 }
197
198 /**
199 * @return the taxonNode
200 */
201 public TaxonNode getTaxonNode() {
202 return taxonNode;
203 }
204
205 /*
206 * (non-Javadoc)
207 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
208 */
209 public ConversationHolder getConversationHolder() {
210 return conversation;
211 }
212
213 /*
214 * (non-Javadoc)
215 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
216 */
217 public void update(CdmDataChangeMap events) {
218 // FIXME update the editor input
219 }
220
221 /* (non-Javadoc)
222 * @see org.eclipse.ui.IPersistableElement#getFactoryId()
223 */
224 public String getFactoryId() {
225 return TaxonEditorInputFactory.getFactoryId();
226 }
227
228 /* (non-Javadoc)
229 * @see org.eclipse.ui.IPersistable#saveState(org.eclipse.ui.IMemento)
230 */
231 public void saveState(IMemento memento) {
232 TaxonEditorInputFactory.saveState(memento, this);
233 }
234 }