Project

General

Profile

Download (5.76 KB) Statistics
| Branch: | Tag: | Revision:
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.propertysheet.name.AcceptedTaxonPropertySource;
30
import eu.etaxonomy.taxeditor.store.CdmStore;
31
import eu.etaxonomy.taxeditor.store.preference.PreferencesUtil;
32

    
33
/**
34
 * @author n.hoffmann
35
 * @created 19.03.2009
36
 * @version 1.0
37
 */
38
public class TaxonEditorInput implements IEditorInput, IConversationEnabled, IPersistableElement {
39
	private static final Logger logger = Logger
40
			.getLogger(TaxonEditorInput.class);
41

    
42
	private Taxon taxon;
43
	private ConversationHolder conversation;
44
	
45
	private TaxonEditorInput(Taxon taxon, ConversationHolder conversation){
46
		this.taxon = taxon;
47
		this.conversation = conversation;
48
	}
49
	
50
    /**
51
     * 
52
     * @param uuid
53
     * @param conversation
54
     * @return
55
     */
56
    public static TaxonEditorInput NewInstance(UUID uuid){
57
    	ConversationHolder conversation = CdmStore.NewTransactionalConversation();
58
    	
59
		TaxonBase<?> taxonBase = CdmStore.getTaxonService().getTaxonByUuid(uuid);
60
		
61
		if(taxonBase == null){
62
			MessageDialog.openWarning(EditorUtil.getShell(), "Not yet implemented", "Selected element is not if type TaxonBase.");
63
			return null;
64
		}
65
		
66
		Taxon taxon;
67
		
68
		if (taxonBase instanceof Synonym) {
69
			// TODO: in case of pro parte synonym or any other where we might have multiple
70
			// accepted taxa we have to provide a mechanism that can deal with that
71
			// TODO set focus to the synonym
72
			Set<Taxon> acceptedTaxa = ((Synonym) taxonBase).getAcceptedTaxa();
73
			if(acceptedTaxa.size() != 1){
74
				String message;
75
				if(acceptedTaxa.size() == 0){
76
					message = "There is no accepted taxon for the chosen synonym. ";
77
				}else{
78
					message = "Multiple taxa found for the chosen synonym.";
79
				}
80
				MessageDialog.openWarning(EditorUtil.getShell(), "Not yet implemented", message);
81
				return null;
82
			}else{
83
				taxon = acceptedTaxa.toArray(new Taxon[0])[0];
84
			}
85
		} else {
86
			taxon = (Taxon) taxonBase;
87
		}	
88
    	
89
    	return new TaxonEditorInput(taxon, conversation);
90
    }
91
    
92
    public static TaxonEditorInput NewEmptyInstance(UUID parentTaxonUuid){
93
    	ConversationHolder conversation = CdmStore.NewTransactionalConversation();
94
		
95
		TaxonNameBase name = PreferencesUtil.getInstanceOfPreferredNameClass();
96
		
97
		Taxon newTaxon = null;
98
		if(parentTaxonUuid == null){
99
			newTaxon = Taxon.NewInstance(name, CdmStore.getDefault().getDefaultSec());
100
		}else{
101
			Taxon parentTaxon = (Taxon) CdmStore.getTaxonService().getTaxonByUuid(parentTaxonUuid);
102
			newTaxon = Taxon.NewInstance(name, parentTaxon.getSec());
103
			parentTaxon.addTaxonomicChild(newTaxon, null, null);
104
		}
105
		// add the new taxon to the editors persistence context
106
		CdmStore.getTaxonService().save(newTaxon);
107
		
108
		return new TaxonEditorInput(newTaxon, conversation);
109
    }
110
	
111
	/* (non-Javadoc)
112
	 * @see org.eclipse.ui.IEditorInput#exists()
113
	 */
114
	public boolean exists() {
115
		return taxon != null;
116
	}
117

    
118
	/* (non-Javadoc)
119
	 * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
120
	 */
121
	public ImageDescriptor getImageDescriptor() {
122
		return null;
123
	}
124

    
125
	/* (non-Javadoc)
126
	 * @see org.eclipse.ui.IEditorInput#getName()
127
	 */
128
	public String getName() {
129
		TaxonNameBase name = taxon.getName();
130
		if (name == null || name.getTitleCache() == null) {
131
			return "New taxon";
132
		} else {
133
			return name.getTitleCache();
134
		}	
135
	}
136

    
137
	/* (non-Javadoc)
138
	 * @see org.eclipse.ui.IEditorInput#getPersistable()
139
	 */
140
	public IPersistableElement getPersistable() {
141
		return this;
142
	}
143

    
144
	/* (non-Javadoc)
145
	 * @see org.eclipse.ui.IEditorInput#getToolTipText()
146
	 */
147
	public String getToolTipText() {
148
		return getName();
149
	}
150

    
151
	/* (non-Javadoc)
152
	 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
153
	 */
154
	public Object getAdapter(Class adapter) {
155

    
156
		if (adapter == Taxon.class) {
157
			return taxon;
158
		}
159
		
160
		return null;
161
	}
162
	
163
    /**
164
     * Overrides equals to ensure that a taxon can only be edited by
165
     * one editor at a time.
166
     * 
167
     * @return boolean
168
     */	
169
	public boolean equals(Object obj) {
170
		if (obj.getClass().equals(TaxonEditorInput.class) 
171
				&& taxon.equals(((TaxonEditorInput) obj).taxon))
172
			return true;
173
		return false;
174
	}
175

    
176
	public Taxon getTaxon(){
177
		return taxon;
178
	}
179
	
180
	/*
181
	 * (non-Javadoc)
182
	 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
183
	 */
184
	public ConversationHolder getConversationHolder() {
185
		return conversation;
186
	}
187

    
188
	/*
189
	 * (non-Javadoc)
190
	 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
191
	 */
192
	public void update(CdmDataChangeMap events) {
193
		// FIXME update the editor input
194
	}
195

    
196
	/* (non-Javadoc)
197
	 * @see org.eclipse.ui.IPersistableElement#getFactoryId()
198
	 */
199
	public String getFactoryId() {
200
		return TaxonEditorInputFactory.getFactoryId();
201
	}
202

    
203
	/* (non-Javadoc)
204
	 * @see org.eclipse.ui.IPersistable#saveState(org.eclipse.ui.IMemento)
205
	 */
206
	public void saveState(IMemento memento) {
207
		TaxonEditorInputFactory.saveState(memento, this);
208
	}
209
}
(24-24/30)