Project

General

Profile

Download (12.2 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
 * Copyright (C) 2007 EDIT
4
 * European Distributed Institute of Taxonomy
5
 * http://www.e-taxonomy.eu
6
 *
7
 * The contents of this file are subject to the Mozilla Public License Version 1.1
8
 * See LICENSE.TXT at the top of this package for the full license terms.
9
 */
10

    
11
package eu.etaxonomy.taxeditor.ui.section.classification;
12

    
13
import org.apache.commons.lang.StringUtils;
14
import org.eclipse.swt.widgets.Control;
15
import org.eclipse.swt.widgets.Text;
16

    
17
import eu.etaxonomy.cdm.common.CdmUtils;
18
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
19
import eu.etaxonomy.cdm.model.name.NonViralName;
20
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
21
import eu.etaxonomy.cdm.model.reference.Reference;
22
import eu.etaxonomy.cdm.model.taxon.Classification;
23
import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
24
import eu.etaxonomy.cdm.model.taxon.Taxon;
25
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
26
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
27
import eu.etaxonomy.taxeditor.ui.element.CheckboxElement;
28
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
29
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
30
import eu.etaxonomy.taxeditor.ui.element.MultilanguageTextElement;
31
import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
32
import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
33
import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
34
import eu.etaxonomy.taxeditor.ui.selection.TaxonNodeSelectionElement;
35

    
36
/**
37
 *
38
 * @author n.hoffmann
39
 * @created Sep 27, 2010
40
 * @version 1.0
41
 */
42
public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITaxonTreeNode> {
43

    
44
    private boolean createNew = false;
45

    
46
	private EntitySelectionElement<Classification> selection_classification;
47

    
48
	private Classification classification;
49

    
50
	private TaxonNodeSelectionElement selection_parentTaxonNode;
51

    
52
	private ITaxonTreeNode parentTreeNode;
53

    
54
	private EntitySelectionElement<Taxon> selection_reuseExistingTaxon;
55
	private EntitySelectionElement<Reference> selection_SecRef;
56

    
57
	private Taxon taxon;
58

    
59
	private TextWithLabelElement textNewTaxonName;
60

    
61
	private TaxonNodeAgentRelationCollectionSection selectionNodeAgentRelation;
62

    
63
	private CheckboxElement checkbox_openInEditor;
64

    
65
	private CheckboxElement checkbox_unplaced;
66

    
67
	private CheckboxElement checkbox_excluded;
68

    
69
	private MultilanguageTextElement multiLanguageTextExcludedNotes;
70

    
71
	private boolean openInEditor;
72

    
73
	private boolean complete;
74

    
75
	private EntitySelectionElement<TaxonNameBase> selection_reuseExistingName;
76

    
77
	public TaxonNodeDetailElement(CdmFormFactory formFactory,
78
			ICdmFormElement formElement, boolean isCreateNew) {
79
		super(formFactory, formElement);
80
		createNew = isCreateNew;
81
	}
82

    
83
	/** {@inheritDoc} */
84
	@Override
85
	protected void createControls(ICdmFormElement formElement,
86
	        ITaxonTreeNode entity, int style) {
87
		selection_classification = formFactory
88
				.createSelectionElement(Classification.class,
89
						getConversationHolder(), formElement, "Classification",
90
						null, EntitySelectionElement.DELETABLE, style);
91
		selection_parentTaxonNode = formFactory
92
				.createTaxonNodeSelectionElement(getConversationHolder(), formElement, "Parent", null,
93
						EntitySelectionElement.DELETABLE, style);
94

    
95
		selection_reuseExistingTaxon = formFactory
96
				.createSelectionElement(Taxon.class,
97
						getConversationHolder(), formElement,
98
						"Reuse existing taxon", null,
99
						EntitySelectionElement.DELETABLE, style);
100

    
101
		selection_reuseExistingName = formFactory
102
				.createSelectionElement(TaxonNameBase.class,
103
						getConversationHolder(), formElement,
104
						"Reuse existing name", null,
105
						EntitySelectionElement.DELETABLE, style);
106
		selection_SecRef = formFactory
107
                .createSelectionElement(Reference.class,getConversationHolder(), formElement, "Secundum Reference", null,
108
                        EntitySelectionElement.DELETABLE, style);
109
		if (isCreateNew()){
110
		    textNewTaxonName = formFactory.createTextWithLabelElement(formElement,"New Taxon", "", style);
111
		    textNewTaxonName.setFocus();
112
		} else{
113
		    textNewTaxonName = formFactory.createTextWithLabelElement(formElement,"Taxon", "", style);
114

    
115
		}
116

    
117
		if (isCreateNew()){
118
            setParentTreeNode(entity);
119
        } else{
120
            setTreeNode(entity);
121
            complete = true;
122
        }
123
		if (entity instanceof TaxonNode) {
124
		    checkbox_unplaced = formFactory.createCheckbox(formElement,
125
		            "Taxon is unplaced", ((TaxonNode) entity).isUnplaced(), style);
126
		}
127

    
128
		if (!isCreateNew()){
129
            selectionNodeAgentRelation = formFactory.createTaxonNodeAgentRelationCollectionSection(formElement, getConversationHolder(), style);
130
            selectionNodeAgentRelation.setEntity((TaxonNode)entity);
131
            selectionNodeAgentRelation.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
132
        }
133
		preFillTaxonName();
134
		checkbox_openInEditor = formFactory.createCheckbox(formElement,
135
				"Open in Editor", true, style);
136
		setOpenInEditor(true);
137

    
138
		if (entity instanceof TaxonNode) {
139
		    checkbox_excluded = formFactory.createCheckbox(formElement,
140
		            "Taxon is excluded", ((TaxonNode) entity).isExcluded(), style);
141
		    multiLanguageTextExcludedNotes = formFactory.createMultiLanguageTextElement(formElement, "Excluded Notes", ((TaxonNode)entity).getExcludedNote(), 50, style);
142
		    multiLanguageTextExcludedNotes.setEnabled(((TaxonNode) entity).isExcluded());
143
		}
144
	}
145

    
146
	private void preFillTaxonName() {
147
        if(getEntity() instanceof TaxonNode ){
148
            TaxonNode node = (TaxonNode)getEntity();
149
            if(node.getTaxon()!=null){
150
                Taxon taxon = HibernateProxyHelper.deproxy(node.getTaxon(), Taxon.class);
151
                if(taxon.getName()!=null && taxon.getName().isInstanceOf(NonViralName.class)){
152
                    NonViralName<?> name = HibernateProxyHelper.deproxy(node.getTaxon().getName(), NonViralName.class);
153
                    if (!isCreateNew()){
154
                        textNewTaxonName.setText(name.getNameCache());
155
                    } else if( ! name.isSupraGeneric() && name.getRank() != null){
156
                        String taxonName = "";
157
                        if(name.isGenus() || name.isInfraGeneric()|| name.isSpeciesAggregate() ){
158
                            taxonName = name.getGenusOrUninomial();
159
                        }
160
                        else if(name.isSpecies() || name.isInfraSpecific() ){
161
                            taxonName = CdmUtils.concat(" ", name.getGenusOrUninomial(),name.getSpecificEpithet());
162
                        }
163
                        if (StringUtils.isNotBlank(taxonName)){
164
                        	textNewTaxonName.setText(taxonName + " ");
165
                        	if(textNewTaxonName.getMainControl() instanceof Text){
166
                        		Text text = (Text)textNewTaxonName.getMainControl();
167
                        		text.setSelection(textNewTaxonName.getText().length());
168
                        	}
169
                        }
170
                    }
171
                }
172
            }
173
        }
174
    }
175

    
176
	/** {@inheritDoc} */
177
	@Override
178
	public void handleEvent(Object eventSource) {
179
		if (eventSource == selection_classification) {
180
			setClassification(selection_classification.getEntity());
181
		} else if (eventSource == selection_parentTaxonNode) {
182
			setParentTreeNode(selection_parentTaxonNode.getEntity());
183
		} else if (eventSource == selection_reuseExistingTaxon) {
184
				boolean enabled = selection_reuseExistingTaxon.getEntity() == null;
185
				selection_reuseExistingName.setEnabled(enabled);
186
				if (!enabled){
187
                    setTaxon(selection_reuseExistingTaxon.getEntity());
188
                }else{
189
			        textNewTaxonName.setText(null);
190
			    }
191
				complete = !textNewTaxonName.getText().isEmpty();
192

    
193

    
194
		} else if (eventSource == selection_reuseExistingName) {
195
		    boolean enabled = selection_reuseExistingName.getEntity() == null;
196
			setTaxon(selection_reuseExistingName.getEntity());
197
			selection_reuseExistingTaxon.setEnabled(enabled);
198
			textNewTaxonName.setEnabled(enabled);
199

    
200
			complete = !textNewTaxonName.getText().isEmpty();
201
		} else if (eventSource == textNewTaxonName) {
202
			boolean enabled = CdmUtils.isBlank(textNewTaxonName.getText());
203
			selection_reuseExistingTaxon.setEnabled(enabled);
204
			selection_reuseExistingName.setEnabled(enabled);
205

    
206
			complete = CdmUtils.isNotBlank(textNewTaxonName.getText());
207
		} else if (eventSource == checkbox_excluded) {
208
		    boolean isExcluded = checkbox_excluded.getSelection();
209
            ((TaxonNode) getEntity()).setExcluded(isExcluded);
210
            multiLanguageTextExcludedNotes.setEnabled(isExcluded);
211
		} else if (eventSource == checkbox_unplaced) {
212
		    ((TaxonNode) getEntity()).setUnplaced(checkbox_unplaced.getSelection());
213
		} else if (eventSource == checkbox_openInEditor) {
214
			setOpenInEditor(checkbox_openInEditor.getSelection());
215
		} else if (eventSource == selectionNodeAgentRelation){
216
		   boolean allComplete = true;
217
		   for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
218
		       allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
219
		   }
220
		    complete = !isCreateNew() && allComplete;
221
		}
222
	}
223

    
224
	public Classification getClassification() {
225
		return classification;
226
	}
227

    
228
	public boolean isOpenInEditor() {
229
		return openInEditor;
230
	}
231

    
232
	public ITaxonTreeNode getParentTreeNode() {
233
		return parentTreeNode;
234
	}
235

    
236
	private void setClassification(Classification classification) {
237
		this.classification = classification;
238
		setParentTreeNode(classification);
239
	}
240

    
241
	private void setParentTreeNode(ITaxonTreeNode parentTreeNode) {
242
		this.parentTreeNode = parentTreeNode;
243

    
244
		if (parentTreeNode instanceof Classification) {
245
			classification = (Classification) parentTreeNode;
246
			selection_classification.setEntity(classification);
247
			selection_parentTaxonNode.setEntity(null);
248
			selection_parentTaxonNode.setClassification(classification);
249
			selection_SecRef.setEntity(classification.getReference());
250
		} else if (parentTreeNode instanceof TaxonNode) {
251
			classification = HibernateProxyHelper
252
					.deproxy(((TaxonNode) parentTreeNode).getClassification());
253
			selection_classification.setEntity(classification);
254
			selection_parentTaxonNode.setEntity((TaxonNode) parentTreeNode);
255
			selection_parentTaxonNode.setClassification(classification);
256
			selection_SecRef.setEntity(((TaxonNode)parentTreeNode).getTaxon().getSec());
257
		} else if(parentTreeNode == null){
258
			this.parentTreeNode = selection_classification.getEntity();
259
		}
260
	}
261

    
262
	private void setTreeNode(ITaxonTreeNode treeNode) {
263
        classification = HibernateProxyHelper
264
                    .deproxy(((TaxonNode) treeNode).getClassification());
265
        selection_classification.setEntity(classification);
266
        selection_parentTaxonNode.setEntity(((TaxonNode) treeNode).getParent());
267
        selection_parentTaxonNode.setClassification(((TaxonNode) treeNode).getParent().getClassification());
268
        selection_SecRef.setEntity(((TaxonNode)treeNode).getTaxon().getSec());
269
	}
270

    
271
	private void setTaxon(Taxon taxon) {
272
		this.taxon = taxon;
273
		if (isCreateNew()){
274
		    textNewTaxonName.setText(taxon.getName().getTitleCache());
275
		}
276
		selection_SecRef.setEntity(taxon.getSec());
277
	}
278

    
279
	private void setTaxon(TaxonNameBase taxonName) {
280
		Reference secundum = null;
281
		if (getParentTreeNode() != null) {
282
			if (this.selection_SecRef.getEntity() != null){
283
				secundum = this.selection_SecRef.getEntity();
284
			}
285
		}
286
		if (taxonName != null){
287
		    taxon = Taxon.NewInstance(taxonName, secundum);
288
		    if (!taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
289
		        textNewTaxonName.setText(taxonName.getTitleCache());
290
		    }
291
		} else {
292
		    textNewTaxonName.setText(null);
293
		}
294
	}
295

    
296
	private void setOpenInEditor(boolean openInEditor) {
297
		this.openInEditor = openInEditor;
298
	}
299

    
300
	public String getTaxonName() {
301
        return textNewTaxonName.getText();
302
    }
303
	public Taxon getTaxon() {
304
        return taxon;
305
    }
306

    
307
	public Reference getReference(){
308
	    return selection_SecRef.getSelection();
309
	}
310

    
311
	public Control getMainControl(){
312
	    return textNewTaxonName.getMainControl();
313
	}
314

    
315
	public boolean isComplete() {
316
	    boolean allComplete = true;
317
	    if (!isCreateNew()){
318
	        for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
319
	            allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
320
	        }
321
	        complete = allComplete;
322
	    }
323
		return complete;
324
	}
325

    
326
    public boolean isCreateNew() {
327
        return createNew;
328
    }
329

    
330
}
(10-10/12)