Project

General

Profile

Download (12.1 KB) Statistics
| Branch: | Tag: | Revision:
1 8091dec0 n.hoffmann
/**
2
* Copyright (C) 2007 EDIT
3 f6a8dc56 Patric Plitzner
* European Distributed Institute of Taxonomy
4 8091dec0 n.hoffmann
* http://www.e-taxonomy.eu
5 f6a8dc56 Patric Plitzner
*
6 8091dec0 n.hoffmann
* 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 729887cf n.hoffmann
import java.util.HashSet;
13
import java.util.Set;
14 8091dec0 n.hoffmann
import java.util.UUID;
15
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 db5e366d n.hoffmann
import eu.etaxonomy.cdm.api.service.IClassificationService;
24
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
25
import eu.etaxonomy.cdm.api.service.ITaxonService;
26 bdb766b8 Andreas Müller
import eu.etaxonomy.cdm.model.common.CdmBase;
27 8091dec0 n.hoffmann
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
28 f6a8dc56 Patric Plitzner
import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
29 729887cf n.hoffmann
import eu.etaxonomy.cdm.model.taxon.Synonym;
30 8091dec0 n.hoffmann
import eu.etaxonomy.cdm.model.taxon.Taxon;
31 729887cf n.hoffmann
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
32 5dd980ce n.hoffmann
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
33 729887cf n.hoffmann
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
34
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
35 8091dec0 n.hoffmann
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
36 b455bf12 n.hoffmann
import eu.etaxonomy.taxeditor.model.DataChangeBridge;
37 41e2f693 Cherian Mathew
import eu.etaxonomy.taxeditor.model.MessagingUtils;
38 7e299dd8 p.ciardelli
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
39 8091dec0 n.hoffmann
import eu.etaxonomy.taxeditor.store.CdmStore;
40
41 f6a8dc56 Patric Plitzner
42 8091dec0 n.hoffmann
/**
43 3be6ef3e n.hoffmann
 * <p>TaxonEditorInput class.</p>
44
 *
45 8091dec0 n.hoffmann
 * @author n.hoffmann
46
 * @created 19.03.2009
47
 * @version 1.0
48
 */
49
public class TaxonEditorInput implements IEditorInput, IConversationEnabled, IPersistableElement {
50
51
	private ConversationHolder conversation;
52 f6a8dc56 Patric Plitzner
53 5dd980ce n.hoffmann
	private TaxonNode taxonNode;
54 b455bf12 n.hoffmann
55
	private TaxonEditorInputDataChangeBehaviour dataChangeBehavior;
56 f6a8dc56 Patric Plitzner
57 3fb05254 n.hoffmann
	private TaxonBase initiallySelectedTaxonBase;
58 f6a8dc56 Patric Plitzner
59 5dd980ce n.hoffmann
	private TaxonEditorInput(TaxonNode taxonNode, ConversationHolder conversation){
60 8091dec0 n.hoffmann
		this.conversation = conversation;
61 729887cf n.hoffmann
		this.taxonNode = taxonNode;
62
	}
63
64 f6a8dc56 Patric Plitzner
65
66 3be6ef3e n.hoffmann
	/**
67
	 * <p>NewInstance</p>
68
	 *
69
	 * @param taxonNodeUuid a {@link java.util.UUID} object.
70
	 * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
71
	 * @throws java.lang.Exception if any.
72
	 */
73 27b59ab1 n.hoffmann
	public static TaxonEditorInput NewInstance(UUID taxonNodeUuid) throws Exception{
74
		try{
75
			ConversationHolder conversation = CdmStore.createConversation();
76
			return NewInstance(taxonNodeUuid, conversation);
77
		}catch(Exception e){
78
			throw e;
79
		}
80 8091dec0 n.hoffmann
	}
81 f6a8dc56 Patric Plitzner
82 8091dec0 n.hoffmann
    /**
83 f6a8dc56 Patric Plitzner
     *
84 63eabeac p.ciardelli
     * @param taxonNodeUuid
85 8091dec0 n.hoffmann
     * @param conversation
86
     * @return
87
     */
88 729887cf n.hoffmann
    private static TaxonEditorInput NewInstance(UUID taxonNodeUuid, ConversationHolder conversation){
89 f6a8dc56 Patric Plitzner
90
91 db5e366d n.hoffmann
    	TaxonNode taxonNode = CdmStore.getService(ITaxonNodeService.class).load(taxonNodeUuid, null);
92 f6a8dc56 Patric Plitzner
93 5dd980ce n.hoffmann
		if(taxonNode == null){
94 41e2f693 Cherian Mathew
			MessagingUtils.warningDialog("Not yet implemented", TaxonEditorInput.class, "Selected element is not type TaxonBase.");
95 8091dec0 n.hoffmann
			return null;
96 f6a8dc56 Patric Plitzner
		}
97
98 5dd980ce n.hoffmann
    	return new TaxonEditorInput(taxonNode, conversation);
99 8091dec0 n.hoffmann
    }
100 f6a8dc56 Patric Plitzner
101 3be6ef3e n.hoffmann
    /**
102
     * <p>NewInstanceFromTaxonBase</p>
103
     *
104
     * @param taxonBaseUuid a {@link java.util.UUID} object.
105
     * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
106
     */
107 729887cf n.hoffmann
    public static TaxonEditorInput NewInstanceFromTaxonBase(UUID taxonBaseUuid){
108
    	ConversationHolder conversation = CdmStore.createConversation();
109 f6a8dc56 Patric Plitzner
110 3fb05254 n.hoffmann
    	TaxonEditorInput input = null;
111 f6a8dc56 Patric Plitzner
112 db5e366d n.hoffmann
    	TaxonBase taxonBase = CdmStore.getService(ITaxonService.class).find(taxonBaseUuid);
113 7e154cb6 Katja Luther
    	if (taxonBase != null){
114
	    	if(taxonBase.isOrphaned()) {
115 41e2f693 Cherian Mathew
	    		MessagingUtils.warningDialog("Orphaned Taxon", TaxonEditorInput.class, "This is an orphaned taxon i.e. a taxon that is not connected to a classification and not having any taxonomic relationships. Editing of orphaned taxon is currently not supported.");
116 7e154cb6 Katja Luther
	    	}
117
	    	else if(taxonBase.isInstanceOf(Taxon.class)){
118
	    		Taxon taxon = CdmBase.deproxy(taxonBase, Taxon.class);
119
	
120
	    		if (taxon.getTaxonNodes().size() == 0 && taxon.isMisapplication()){
121
	    			// TODO get accepted taxon
122 41e2f693 Cherian Mathew
	    			MessagingUtils.info("trying to open Mispplied Name ");
123 7e154cb6 Katja Luther
	
124
	    			Set<Taxon> acceptedTaxa = new HashSet<Taxon>();
125
	    			Set<TaxonRelationship> relations = taxon.getRelationsFromThisTaxon();
126
	    			for(TaxonRelationship relation : relations){
127
	    				if(relation.getType().equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR())){
128
	    					acceptedTaxa.add(relation.getToTaxon());
129
	    				}
130
	    			}
131
	    			input =  getInputForMultipleTaxa(conversation, acceptedTaxa);
132
	
133
	    		}else{
134
	    			input = getInputForMultipleNodes(conversation, taxon.getTaxonNodes());
135
	    		}
136
	    	}else if(taxonBase instanceof Synonym){
137
	    		Synonym synonym = (Synonym) taxonBase;
138
	
139
	    		Set<Taxon> taxa = synonym.getAcceptedTaxa();
140
	    		input = getInputForMultipleTaxa(conversation, taxa);
141
	    	}
142
	    	if (input != null){
143
	    		input.setInitiallySelectedTaxonBase(taxonBase);
144
	    	}
145 b4ca72fe Cherian Mathew
    	}
146 7e154cb6 Katja Luther
    	
147 f6a8dc56 Patric Plitzner
148 3fb05254 n.hoffmann
    	return input;
149 729887cf n.hoffmann
    }
150 f6a8dc56 Patric Plitzner
151 3fb05254 n.hoffmann
	private static TaxonEditorInput getInputForMultipleNodes(ConversationHolder conversation, Set<TaxonNode> taxonNodes){
152 729887cf n.hoffmann
    	if(taxonNodes.size() == 1){
153
    		TaxonNode taxonNode = taxonNodes.iterator().next();
154
    		return NewInstance(taxonNode.getUuid(), conversation);
155
    	}else if(taxonNodes.size() > 1){
156 70264b7f n.hoffmann
			TaxonNode taxonNode = ChooseFromMultipleTaxonNodesDialog.choose(taxonNodes);
157
			if(taxonNode != null){
158 f6a8dc56 Patric Plitzner
				return NewInstance(taxonNode.getUuid(), conversation);
159 70264b7f n.hoffmann
			}
160 729887cf n.hoffmann
		}else if(taxonNodes.size() == 0){
161
			// this is an undesired state
162 41e2f693 Cherian Mathew
			MessagingUtils.warningDialog("Incorrect state", TaxonEditorInput.class, "The accepted taxon is not part of any classification. This should not have happened.");
163 729887cf n.hoffmann
		}
164
    	return null;
165
    }
166 f6a8dc56 Patric Plitzner
167 729887cf n.hoffmann
    private static TaxonEditorInput getInputForMultipleTaxa(ConversationHolder conversation, Set<Taxon> taxa){
168
    	if(taxa.size() == 1){
169
    		Taxon taxon = taxa.iterator().next();
170
    		Set<TaxonNode> nodes = taxon.getTaxonNodes();
171
    		return getInputForMultipleNodes(conversation, nodes);
172
    	}else if(taxa.size() > 1){
173 4e4d11fb n.hoffmann
    		Set<TaxonNode> taxonNodes = new HashSet<TaxonNode>();
174
			for ( Taxon taxon : taxa ){
175
				taxonNodes.addAll(taxon.getTaxonNodes());
176
			}
177
			return getInputForMultipleNodes(conversation, taxonNodes);
178 729887cf n.hoffmann
		}else if(taxa.size() == 0){
179
			// this is an undesired state
180 41e2f693 Cherian Mathew
			MessagingUtils.warningDialog("Incorrect state", TaxonEditorInput.class, "Trying to open accepted taxon for a synonym or misapplication but" +
181 729887cf n.hoffmann
					" no accepted taxa are present. This should not have happened.");
182
		}
183
    	return null;
184
    }
185 f6a8dc56 Patric Plitzner
186 3be6ef3e n.hoffmann
    /**
187
     * <p>NewEmptyInstance</p>
188
     *
189
     * @param parentNodeUuid a {@link java.util.UUID} object.
190
     * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
191
     */
192 d2443357 n.hoffmann
    public static TaxonEditorInput NewEmptyInstance(UUID parentNodeUuid){
193 729887cf n.hoffmann
    	ConversationHolder conversation = CdmStore.createConversation();
194 f6a8dc56 Patric Plitzner
195 7e299dd8 p.ciardelli
		TaxonNameBase<?, ?> name = PreferencesUtil.getPreferredNomenclaturalCode().getNewTaxonNameInstance(null);
196 f6a8dc56 Patric Plitzner
		ITaxonTreeNode parentNode = CdmStore.getService(IClassificationService.class).getTreeNodeByUuid(parentNodeUuid);
197
198 3d331582 n.hoffmann
		Taxon newTaxon = Taxon.NewInstance(name, parentNode.getReference());
199 124424d0 Cherian Mathew
		TaxonNode newTaxonNode = parentNode.addChildTaxon(newTaxon, parentNode.getReference(), parentNode.getMicroReference());
200 f6a8dc56 Patric Plitzner
201 8091dec0 n.hoffmann
		// add the new taxon to the editors persistence context
202 db5e366d n.hoffmann
		UUID newTaxonNodeUuid = CdmStore.getService(ITaxonNodeService.class).save(newTaxonNode);
203 f6a8dc56 Patric Plitzner
204 5dd980ce n.hoffmann
		return new TaxonEditorInput(newTaxonNode, conversation);
205 8091dec0 n.hoffmann
    }
206 f6a8dc56 Patric Plitzner
207 8091dec0 n.hoffmann
	/* (non-Javadoc)
208
	 * @see org.eclipse.ui.IEditorInput#exists()
209
	 */
210 3be6ef3e n.hoffmann
	/**
211
	 * <p>exists</p>
212
	 *
213
	 * @return a boolean.
214
	 */
215 f6a8dc56 Patric Plitzner
	@Override
216
    public boolean exists() {
217 5dd980ce n.hoffmann
		return taxonNode != null;
218 8091dec0 n.hoffmann
	}
219
220
	/* (non-Javadoc)
221
	 * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
222
	 */
223 3be6ef3e n.hoffmann
	/**
224
	 * <p>getImageDescriptor</p>
225
	 *
226
	 * @return a {@link org.eclipse.jface.resource.ImageDescriptor} object.
227
	 */
228 f6a8dc56 Patric Plitzner
	@Override
229
    public ImageDescriptor getImageDescriptor() {
230 8091dec0 n.hoffmann
		return null;
231
	}
232
233
	/* (non-Javadoc)
234
	 * @see org.eclipse.ui.IEditorInput#getName()
235
	 */
236 3be6ef3e n.hoffmann
	/**
237
	 * <p>getName</p>
238
	 *
239
	 * @return a {@link java.lang.String} object.
240
	 */
241 f6a8dc56 Patric Plitzner
	@Override
242
    public String getName() {
243 b455bf12 n.hoffmann
		if(getTaxon() == null){
244
			return null;
245
		}
246 5dd980ce n.hoffmann
		TaxonNameBase<?, ?> name = getTaxon().getName();
247 8091dec0 n.hoffmann
		if (name == null || name.getTitleCache() == null) {
248
			return "New taxon";
249
		} else {
250
			return name.getTitleCache();
251 f6a8dc56 Patric Plitzner
		}
252 8091dec0 n.hoffmann
	}
253
254
	/* (non-Javadoc)
255
	 * @see org.eclipse.ui.IEditorInput#getPersistable()
256
	 */
257 3be6ef3e n.hoffmann
	/**
258
	 * <p>getPersistable</p>
259
	 *
260
	 * @return a {@link org.eclipse.ui.IPersistableElement} object.
261
	 */
262 f6a8dc56 Patric Plitzner
	@Override
263
    public IPersistableElement getPersistable() {
264 729887cf n.hoffmann
//		if(CdmStore.isActive()){
265
//			TaxonNode test = CdmStore.getTaxonTreeService().getTaxonNodeByUuid(taxonNode.getUuid());
266
//			boolean isPersistable = CdmStore.getTaxonTreeService().getTaxonNodeByUuid(taxonNode.getUuid()) != null;
267
//			if (isPersistable) {
268
//				return this;
269
//			}
270
//		}
271
		return null;
272 8091dec0 n.hoffmann
	}
273
274
	/* (non-Javadoc)
275
	 * @see org.eclipse.ui.IEditorInput#getToolTipText()
276
	 */
277 3be6ef3e n.hoffmann
	/**
278
	 * <p>getToolTipText</p>
279
	 *
280
	 * @return a {@link java.lang.String} object.
281
	 */
282 f6a8dc56 Patric Plitzner
	@Override
283
    public String getToolTipText() {
284 8091dec0 n.hoffmann
		return getName();
285
	}
286
287
	/* (non-Javadoc)
288
	 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
289
	 */
290 3be6ef3e n.hoffmann
	/** {@inheritDoc} */
291 f6a8dc56 Patric Plitzner
	@Override
292
    public Object getAdapter(Class adapter) {
293 8091dec0 n.hoffmann
294
		if (adapter == Taxon.class) {
295 5dd980ce n.hoffmann
			return taxonNode.getTaxon();
296
		}
297 f6a8dc56 Patric Plitzner
298 5dd980ce n.hoffmann
		if (adapter == TaxonNode.class) {
299
			return taxonNode;
300 8091dec0 n.hoffmann
		}
301 f6a8dc56 Patric Plitzner
302 8091dec0 n.hoffmann
		return null;
303
	}
304 f6a8dc56 Patric Plitzner
305 3be6ef3e n.hoffmann
	/**
306
	 * {@inheritDoc}
307
	 *
308
	 * Overrides equals to ensure that a taxon can only be edited by
309
	 * one editor at a time.
310
	 */
311 f6a8dc56 Patric Plitzner
	@Override
312
    public boolean equals(Object obj) {
313
		if (TaxonEditorInput.class.equals(obj.getClass())
314 b455bf12 n.hoffmann
				&& getTaxon() != null
315 1d9ed6ce n.hoffmann
				&& getTaxon().equals(((TaxonEditorInput) obj).getTaxon())){
316
			if(((TaxonEditorInput) obj).getInitiallySelectedTaxonBase() != null){
317
				setInitiallySelectedTaxonBase(((TaxonEditorInput) obj).getInitiallySelectedTaxonBase());
318
			}
319 8091dec0 n.hoffmann
			return true;
320 1d9ed6ce n.hoffmann
		}
321 8091dec0 n.hoffmann
		return false;
322
	}
323
324 5dd980ce n.hoffmann
	/**
325 3be6ef3e n.hoffmann
	 * <p>getTaxon</p>
326
	 *
327 5dd980ce n.hoffmann
	 * @return the taxon
328
	 */
329 8091dec0 n.hoffmann
	public Taxon getTaxon(){
330 5dd980ce n.hoffmann
		return taxonNode.getTaxon();
331
	}
332 f6a8dc56 Patric Plitzner
333 5dd980ce n.hoffmann
	/**
334 3be6ef3e n.hoffmann
	 * <p>Getter for the field <code>taxonNode</code>.</p>
335
	 *
336 5dd980ce n.hoffmann
	 * @return the taxonNode
337
	 */
338
	public TaxonNode getTaxonNode() {
339
		return taxonNode;
340 8091dec0 n.hoffmann
	}
341 f6a8dc56 Patric Plitzner
342 8091dec0 n.hoffmann
	/*
343
	 * (non-Javadoc)
344
	 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
345
	 */
346 3be6ef3e n.hoffmann
	/**
347
	 * <p>getConversationHolder</p>
348
	 *
349
	 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
350
	 */
351 f6a8dc56 Patric Plitzner
	@Override
352
    public ConversationHolder getConversationHolder() {
353 8091dec0 n.hoffmann
		return conversation;
354
	}
355
356
	/*
357
	 * (non-Javadoc)
358
	 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
359
	 */
360 3be6ef3e n.hoffmann
	/** {@inheritDoc} */
361 f6a8dc56 Patric Plitzner
	@Override
362
    public void update(CdmDataChangeMap events) {
363 b455bf12 n.hoffmann
		if(dataChangeBehavior == null){
364
			dataChangeBehavior = new TaxonEditorInputDataChangeBehaviour(this);
365
		}
366 f6a8dc56 Patric Plitzner
367 b455bf12 n.hoffmann
		DataChangeBridge.handleDataChange(events, dataChangeBehavior);
368 8091dec0 n.hoffmann
	}
369
370
	/* (non-Javadoc)
371
	 * @see org.eclipse.ui.IPersistableElement#getFactoryId()
372
	 */
373 3be6ef3e n.hoffmann
	/**
374
	 * <p>getFactoryId</p>
375
	 *
376
	 * @return a {@link java.lang.String} object.
377
	 */
378 f6a8dc56 Patric Plitzner
	@Override
379
    public String getFactoryId() {
380 8091dec0 n.hoffmann
		return TaxonEditorInputFactory.getFactoryId();
381
	}
382
383
	/* (non-Javadoc)
384
	 * @see org.eclipse.ui.IPersistable#saveState(org.eclipse.ui.IMemento)
385
	 */
386 3be6ef3e n.hoffmann
	/** {@inheritDoc} */
387 f6a8dc56 Patric Plitzner
	@Override
388
    public void saveState(IMemento memento) {
389 8091dec0 n.hoffmann
		TaxonEditorInputFactory.saveState(memento, this);
390
	}
391 3fb05254 n.hoffmann
392 f6a8dc56 Patric Plitzner
393 3be6ef3e n.hoffmann
    /**
394
     * <p>Setter for the field <code>initiallySelectedTaxonBase</code>.</p>
395
     *
396
     * @param taxonBase a {@link eu.etaxonomy.cdm.model.taxon.TaxonBase} object.
397
     */
398 1d9ed6ce n.hoffmann
    public void setInitiallySelectedTaxonBase(TaxonBase taxonBase) {
399 3fb05254 n.hoffmann
		this.initiallySelectedTaxonBase = taxonBase;
400
	}
401
402 3be6ef3e n.hoffmann
	/**
403
	 * <p>Getter for the field <code>initiallySelectedTaxonBase</code>.</p>
404
	 *
405
	 * @return a {@link eu.etaxonomy.cdm.model.taxon.TaxonBase} object.
406
	 */
407 3fb05254 n.hoffmann
	public TaxonBase getInitiallySelectedTaxonBase() {
408
		return initiallySelectedTaxonBase;
409
	}
410 f6a8dc56 Patric Plitzner
411 0af47024 n.hoffmann
	@Override
412
	public String toString() {
413
		return String.format("%s[%s]", this.getClass().getSimpleName(), getTaxon());
414
	}
415 8091dec0 n.hoffmann
}