Project

General

Profile

Download (13.5 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.TaxonName;
19
import eu.etaxonomy.cdm.model.reference.Reference;
20
import eu.etaxonomy.cdm.model.taxon.Classification;
21
import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
22
import eu.etaxonomy.cdm.model.taxon.Taxon;
23
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
24
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
25
import eu.etaxonomy.taxeditor.ui.element.CheckboxElement;
26
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
27
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
28
import eu.etaxonomy.taxeditor.ui.element.MultilanguageTextElement;
29
import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
30
import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
31
import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
32
import eu.etaxonomy.taxeditor.ui.selection.TaxonNodeSelectionElement;
33

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

    
42
    private boolean createNew = false;
43

    
44
	private EntitySelectionElement<Classification> selection_classification;
45

    
46
	private Classification classification;
47

    
48
	private TaxonNodeSelectionElement selection_parentTaxonNode;
49

    
50
	private ITaxonTreeNode parentTreeNode;
51

    
52
	private EntitySelectionElement<Taxon> selection_reuseExistingTaxon;
53
	private EntitySelectionElement<Reference> selection_SecRef;
54
	private EntitySelectionElement<Reference> selection_Ref;
55
	private TextWithLabelElement microReference;
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 boolean excluded = false;
76
	private boolean unplaced = false;
77

    
78
	private EntitySelectionElement<TaxonName> selection_reuseExistingName;
79

    
80
	public TaxonNodeDetailElement(CdmFormFactory formFactory,
81
			ICdmFormElement formElement, boolean isCreateNew) {
82
		super(formFactory, formElement);
83
		createNew = isCreateNew;
84
	}
85

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

    
98
		selection_reuseExistingTaxon = formFactory
99
				.createSelectionElement(Taxon.class,//getConversationHolder(),
100
				        formElement,
101
						"Reuse existing taxon", null,
102
						EntitySelectionElement.DELETABLE, style);
103

    
104
		selection_reuseExistingName = formFactory
105
				.createSelectionElement(TaxonName.class,//getConversationHolder(),
106
				        formElement,
107
						"Reuse existing name", null,
108
						EntitySelectionElement.DELETABLE, style);
109
		selection_SecRef = formFactory
110
                .createSelectionElement(Reference.class,//getConversationHolder(),
111
                        formElement, "Secundum Reference", null,
112
                        EntitySelectionElement.DELETABLE, style);
113
		selection_Ref = formFactory
114
                .createSelectionElement(Reference.class,//getConversationHolder(),
115
                        formElement, "Parent Child Relationship Reference", null,
116
                        EntitySelectionElement.DELETABLE, style);
117
		microReference = formFactory.createTextWithLabelElement(formElement,"Detail", "", style);
118
		if (!isCreateNew()){
119
		    selection_Ref.setEntity(entity.getReference());
120
        }
121
		if (isCreateNew()){
122
		    textNewTaxonName = formFactory.createTextWithLabelElement(formElement,"New Taxon", "", style);
123
		    textNewTaxonName.setFocus();
124
		} else{
125
		    textNewTaxonName = formFactory.createTextWithLabelElement(formElement,"Taxon", "", style);
126

    
127
		}
128

    
129
		if (isCreateNew()){
130
            setParentTreeNode(entity);
131
        } else{
132
            setTreeNode(entity);
133
            complete = true;
134
        }
135
		if (entity instanceof TaxonNode) {
136
		    checkbox_unplaced = formFactory.createCheckbox(formElement,
137
		            "Taxon is unplaced", ((TaxonNode)entity).isUnplaced(), style);
138
		}
139

    
140
		if (!isCreateNew()){
141
            selectionNodeAgentRelation = formFactory.createTaxonNodeAgentRelationCollectionSection(formElement, getConversationHolder(), style);
142
            selectionNodeAgentRelation.setEntity((TaxonNode)entity);
143
            selectionNodeAgentRelation.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
144
        }
145
		preFillTaxonName();
146

    
147
		if (entity instanceof TaxonNode) {
148
		    checkbox_excluded = formFactory.createCheckbox(formElement,
149
		            "Taxon is excluded", ((TaxonNode)entity).isExcluded(), style);
150
		    multiLanguageTextExcludedNotes = formFactory.createMultiLanguageTextElement(formElement, "Excluded Notes", ((TaxonNode)entity).getExcludedNote(), 50, style);
151
		    multiLanguageTextExcludedNotes.setEnabled(((TaxonNode) entity).isExcluded());
152
		}
153
		checkbox_openInEditor = formFactory.createCheckbox(formElement,
154
		        "Open in Editor", true, style);
155
		setOpenInEditor(true);
156
	}
157

    
158
	private void preFillTaxonName() {
159
        if(getEntity() instanceof TaxonNode ){
160
            TaxonNode node = (TaxonNode)getEntity();
161
            if(node.getTaxon()!=null){
162
                Taxon taxon = HibernateProxyHelper.deproxy(node.getTaxon(), Taxon.class);
163
                if(taxon.getName()!=null){
164
                    TaxonName name = HibernateProxyHelper.deproxy(node.getTaxon().getName());
165
                    if (!isCreateNew()){
166
                        textNewTaxonName.setText(name.getNameCache());
167
                    } else if( ! name.isSupraGeneric() && name.getRank() != null){
168
                        String taxonName = "";
169
                        if(name.isGenus() || name.isInfraGeneric()|| name.isSpeciesAggregate() ){
170
                            taxonName = name.getGenusOrUninomial();
171
                        }
172
                        else if(name.isSpecies() || name.isInfraSpecific() ){
173
                            taxonName = CdmUtils.concat(" ", name.getGenusOrUninomial(),name.getSpecificEpithet());
174
                        }
175
                        if (StringUtils.isNotBlank(taxonName)){
176
                        	textNewTaxonName.setText(taxonName + " ");
177
                        	if(textNewTaxonName.getMainControl() instanceof Text){
178
                        		Text text = (Text)textNewTaxonName.getMainControl();
179
                        		text.setSelection(textNewTaxonName.getText().length());
180
                        	}
181
                        }
182
                    }
183
                }
184
            }
185
        }
186
    }
187

    
188
	/** {@inheritDoc} */
189
	@Override
190
	public void handleEvent(Object eventSource) {
191
		if (eventSource == selection_classification) {
192
			setClassification(selection_classification.getEntity());
193
		} else if (eventSource == selection_parentTaxonNode) {
194
			setParentTreeNode(selection_parentTaxonNode.getEntity());
195
		} else if (eventSource == selection_reuseExistingTaxon) {
196
				boolean enabled = selection_reuseExistingTaxon.getEntity() == null;
197
				selection_reuseExistingName.setEnabled(enabled);
198
				if (!enabled){
199
                    setTaxon(selection_reuseExistingTaxon.getEntity());
200
                }else{
201
			        textNewTaxonName.setText(null);
202
			    }
203
				complete = !textNewTaxonName.getText().isEmpty();
204

    
205

    
206
		} else if (eventSource == selection_reuseExistingName) {
207
		    boolean enabled = selection_reuseExistingName.getEntity() == null;
208
			setTaxon(selection_reuseExistingName.getEntity());
209
			selection_reuseExistingTaxon.setEnabled(enabled);
210
			textNewTaxonName.setEnabled(enabled);
211

    
212
			complete = !textNewTaxonName.getText().isEmpty();
213
		}else if (eventSource == selection_Ref) {
214
		    ((TaxonNode) getEntity()).setReference(selection_Ref.getEntity());
215
        }else if (eventSource == selection_SecRef) {
216
            ((TaxonNode) getEntity()).getTaxon().setSec(selection_SecRef.getEntity());
217
        }else if (eventSource == microReference) {
218
            ((TaxonNode) getEntity()).setMicroReference(microReference.getText());
219
        } else if (eventSource == textNewTaxonName) {
220
			boolean enabled = CdmUtils.isBlank(textNewTaxonName.getText());
221
			selection_reuseExistingTaxon.setEnabled(enabled);
222
			selection_reuseExistingName.setEnabled(enabled);
223

    
224
			complete = CdmUtils.isNotBlank(textNewTaxonName.getText());
225
		} else if (eventSource == checkbox_excluded) {
226
		    excluded = checkbox_excluded.getSelection();
227
		    ((TaxonNode)getEntity()).setExcluded(excluded);
228

    
229
            multiLanguageTextExcludedNotes.setEnabled(excluded);
230
		} else if (eventSource == checkbox_unplaced) {
231
		    unplaced = checkbox_unplaced.getSelection();
232
            ((TaxonNode)getEntity()).setUnplaced(unplaced);
233
		} else if (eventSource == checkbox_openInEditor) {
234
			setOpenInEditor(checkbox_openInEditor.getSelection());
235
		} else if (eventSource == selectionNodeAgentRelation){
236
		   boolean allComplete = true;
237
		   for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
238
		       allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
239
		   }
240
		    complete = !isCreateNew() && allComplete;
241
		}
242
	}
243

    
244
	public Classification getClassification() {
245
		return classification;
246
	}
247

    
248
	public boolean isOpenInEditor() {
249
		return openInEditor;
250
	}
251

    
252
	public ITaxonTreeNode getParentTreeNode() {
253
		return parentTreeNode;
254
	}
255

    
256
	private void setClassification(Classification classification) {
257
		this.classification = classification;
258
		setParentTreeNode(classification);
259
	}
260

    
261
	private void setParentTreeNode(ITaxonTreeNode parentTreeNode) {
262
		this.parentTreeNode = parentTreeNode;
263

    
264
		if (parentTreeNode instanceof Classification) {
265
			classification = (Classification) parentTreeNode;
266
			selection_classification.setEntity(classification);
267
			selection_parentTaxonNode.setEntity(null);
268
			selection_parentTaxonNode.setClassification(classification);
269
			selection_SecRef.setEntity(classification.getReference());
270
		} else if (parentTreeNode instanceof TaxonNode) {
271
			classification = HibernateProxyHelper
272
					.deproxy(((TaxonNode) parentTreeNode).getClassification());
273
			selection_classification.setEntity(classification);
274
			selection_parentTaxonNode.setEntity((TaxonNode) parentTreeNode);
275
			selection_parentTaxonNode.setClassification(classification);
276
			selection_SecRef.setEntity(((TaxonNode)parentTreeNode).getTaxon().getSec());
277
		} else if(parentTreeNode == null){
278
			this.parentTreeNode = selection_classification.getEntity();
279
		}
280
	}
281

    
282
	private void setTreeNode(ITaxonTreeNode treeNode) {
283
        classification = HibernateProxyHelper
284
                    .deproxy(((TaxonNode) treeNode).getClassification());
285
        selection_classification.setEntity(classification);
286
        selection_parentTaxonNode.setEntity(((TaxonNode) treeNode).getParent());
287
        selection_parentTaxonNode.setClassification(((TaxonNode) treeNode).getParent().getClassification());
288
        selection_SecRef.setEntity(((TaxonNode)treeNode).getTaxon().getSec());
289
	}
290

    
291
	private void setTaxon(Taxon taxon) {
292
		this.taxon = taxon;
293
		if (isCreateNew()){
294
		    textNewTaxonName.setText(taxon.getName().getTitleCache());
295
		}
296
		selection_SecRef.setEntity(taxon.getSec());
297
	}
298

    
299
	private void setTaxon(TaxonName taxonName) {
300
		Reference secundum = null;
301
		if (getParentTreeNode() != null) {
302
			if (this.selection_SecRef.getEntity() != null){
303
				secundum = this.selection_SecRef.getEntity();
304
			}
305
		}
306
		if (taxonName != null){
307
		    taxon = Taxon.NewInstance(taxonName, secundum);
308
		    if (textNewTaxonName.getText() == null || !taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
309
		    	 textNewTaxonName.setText(taxonName.getTitleCache());
310
		    }
311
//		    if (!taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
312
//		        textNewTaxonName.setText(taxonName.getTitleCache());
313
//		    }
314
		} else {
315
		    textNewTaxonName.setText(null);
316
		}
317
	}
318

    
319
	private void setOpenInEditor(boolean openInEditor) {
320
		this.openInEditor = openInEditor;
321
	}
322

    
323
	public String getTaxonName() {
324
        return textNewTaxonName.getText();
325
    }
326
	public Taxon getTaxon() {
327
        return taxon;
328
    }
329

    
330
	public Reference getReference(){
331
	    return selection_SecRef.getSelection();
332
	}
333

    
334
	public Control getMainControl(){
335
	    return textNewTaxonName.getMainControl();
336
	}
337

    
338
	public boolean isComplete() {
339
	    boolean allComplete = true;
340
	    if (!isCreateNew()){
341
	        for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
342
	            allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
343
	        }
344
	        complete = allComplete;
345
	    }
346
		return complete;
347
	}
348

    
349
    public boolean isCreateNew() {
350
        return createNew;
351
    }
352

    
353
    public boolean isExcluded() {
354
        return excluded;
355
    }
356

    
357
    public boolean isUnplaced() {
358
        return unplaced;
359
    }
360

    
361
}
(10-10/12)