Project

General

Profile

Download (12.4 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.ui.section.classification;
11

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

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

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

    
43
    private boolean createNew = false;
44

    
45
	private EntitySelectionElement<Classification> selection_classification;
46

    
47
	private Classification classification;
48

    
49
	private TaxonNodeSelectionElement selection_parentTaxonNode;
50

    
51
	private ITaxonTreeNode parentTreeNode;
52

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

    
56
	private Taxon taxon;
57

    
58
	private TextWithLabelElement textNewTaxonName;
59

    
60
	private TaxonNodeAgentRelationCollectionSection selectionNodeAgentRelation;
61

    
62
	private CheckboxElement checkbox_openInEditor;
63

    
64
	private CheckboxElement checkbox_unplaced;
65

    
66
	private CheckboxElement checkbox_excluded;
67

    
68
	private MultilanguageTextElement multiLanguageTextExcludedNotes;
69

    
70
	private boolean openInEditor;
71

    
72
	private boolean complete;
73

    
74
	private EntitySelectionElement<TaxonNameBase> selection_reuseExistingName;
75

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

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

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

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

    
114
		}
115

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

    
127
		if (!isCreateNew()){
128
            selectionNodeAgentRelation = formFactory.createTaxonNodeAgentRelationCollectionSection(formElement, getConversationHolder(), style);
129
            selectionNodeAgentRelation.setEntity((TaxonNode)entity);
130
            selectionNodeAgentRelation.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
131
        }
132
		preFillTaxonName();
133

    
134
		if (entity instanceof TaxonNode) {
135
		    checkbox_excluded = formFactory.createCheckbox(formElement,
136
		            "Taxon is excluded", ((TaxonNode) entity).isExcluded(), style);
137
		    multiLanguageTextExcludedNotes = formFactory.createMultiLanguageTextElement(formElement, "Excluded Notes", ((TaxonNode)entity).getExcludedNote(), 50, style);
138
		    multiLanguageTextExcludedNotes.setEnabled(((TaxonNode) entity).isExcluded());
139
		}
140
		checkbox_openInEditor = formFactory.createCheckbox(formElement,
141
		        "Open in Editor", true, style);
142
		setOpenInEditor(true);
143
	}
144

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

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

    
192

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

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

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

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

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

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

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

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

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

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

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

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

    
298
	private void setOpenInEditor(boolean openInEditor) {
299
		this.openInEditor = openInEditor;
300
	}
301

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

    
309
	public Reference getReference(){
310
	    return selection_SecRef.getSelection();
311
	}
312

    
313
	public Control getMainControl(){
314
	    return textNewTaxonName.getMainControl();
315
	}
316

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

    
328
    public boolean isCreateNew() {
329
        return createNew;
330
    }
331

    
332
}
(10-10/12)