af04f81b591ff47082752aacad6be95b0a2b6a8b
[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.Set;
13 import java.util.UUID;
14
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;
21
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;
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 Taxon taxon;
42 private ConversationHolder conversation;
43
44 private TaxonEditorInput(Taxon taxon, ConversationHolder conversation){
45 this.taxon = taxon;
46 this.conversation = conversation;
47 }
48
49 /**
50 *
51 * @param uuid
52 * @param conversation
53 * @return
54 */
55 public static TaxonEditorInput NewInstance(UUID uuid){
56 ConversationHolder conversation = CdmStore.NewTransactionalConversation();
57
58 TaxonBase<?> taxonBase = CdmStore.getTaxonService().getTaxonByUuid(uuid);
59
60 if(taxonBase == null){
61 MessageDialog.openWarning(EditorUtil.getShell(), "Not yet implemented", "Selected element is not if type TaxonBase.");
62 return null;
63 }
64
65 Taxon taxon;
66
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 return new TaxonEditorInput(taxon, conversation);
89 }
90
91 public static TaxonEditorInput NewEmptyInstance(UUID parentTaxonUuid){
92 ConversationHolder conversation = CdmStore.NewTransactionalConversation();
93
94 TaxonNameBase name = PreferencesUtil.getInstanceOfPreferredNameClass();
95
96 Taxon newTaxon = null;
97 if(parentTaxonUuid == null){
98 newTaxon = Taxon.NewInstance(name, CdmStore.getDefault().getDefaultSec());
99 }else{
100 Taxon parentTaxon = (Taxon) CdmStore.getTaxonService().getTaxonByUuid(parentTaxonUuid);
101 newTaxon = Taxon.NewInstance(name, parentTaxon.getSec());
102 parentTaxon.addTaxonomicChild(newTaxon, null, null);
103 }
104 // add the new taxon to the editors persistence context
105 CdmStore.getTaxonService().save(newTaxon);
106
107 return new TaxonEditorInput(newTaxon, conversation);
108 }
109
110 /* (non-Javadoc)
111 * @see org.eclipse.ui.IEditorInput#exists()
112 */
113 public boolean exists() {
114 return taxon != null;
115 }
116
117 /* (non-Javadoc)
118 * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
119 */
120 public ImageDescriptor getImageDescriptor() {
121 return null;
122 }
123
124 /* (non-Javadoc)
125 * @see org.eclipse.ui.IEditorInput#getName()
126 */
127 public String getName() {
128 TaxonNameBase name = taxon.getName();
129 if (name == null || name.getTitleCache() == null) {
130 return "New taxon";
131 } else {
132 return name.getTitleCache();
133 }
134 }
135
136 /* (non-Javadoc)
137 * @see org.eclipse.ui.IEditorInput#getPersistable()
138 */
139 public IPersistableElement getPersistable() {
140 return this;
141 }
142
143 /* (non-Javadoc)
144 * @see org.eclipse.ui.IEditorInput#getToolTipText()
145 */
146 public String getToolTipText() {
147 return getName();
148 }
149
150 /* (non-Javadoc)
151 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
152 */
153 public Object getAdapter(Class adapter) {
154
155 if (adapter == Taxon.class) {
156 return taxon;
157 }
158
159 return null;
160 }
161
162 /**
163 * Overrides equals to ensure that a taxon can only be edited by
164 * one editor at a time.
165 *
166 * @return boolean
167 */
168 public boolean equals(Object obj) {
169 if (obj.getClass().equals(TaxonEditorInput.class)
170 && taxon.equals(((TaxonEditorInput) obj).taxon))
171 return true;
172 return false;
173 }
174
175 public Taxon getTaxon(){
176 return taxon;
177 }
178
179 /*
180 * (non-Javadoc)
181 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
182 */
183 public ConversationHolder getConversationHolder() {
184 return conversation;
185 }
186
187 /*
188 * (non-Javadoc)
189 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
190 */
191 public void update(CdmDataChangeMap events) {
192 // FIXME update the editor input
193 }
194
195 /* (non-Javadoc)
196 * @see org.eclipse.ui.IPersistableElement#getFactoryId()
197 */
198 public String getFactoryId() {
199 return TaxonEditorInputFactory.getFactoryId();
200 }
201
202 /* (non-Javadoc)
203 * @see org.eclipse.ui.IPersistable#saveState(org.eclipse.ui.IMemento)
204 */
205 public void saveState(IMemento memento) {
206 TaxonEditorInputFactory.saveState(memento, this);
207 }
208 }