Project

General

Profile

Download (12.3 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

    
55
	private Taxon taxon;
56

    
57
	private TextWithLabelElement textNewTaxonName;
58

    
59
	private TaxonNodeAgentRelationCollectionSection selectionNodeAgentRelation;
60

    
61
	private CheckboxElement checkbox_openInEditor;
62

    
63
	private CheckboxElement checkbox_unplaced;
64

    
65
	private CheckboxElement checkbox_excluded;
66

    
67
	private MultilanguageTextElement multiLanguageTextExcludedNotes;
68

    
69
	private boolean openInEditor;
70

    
71
	private boolean complete;
72

    
73
	private EntitySelectionElement<TaxonName> selection_reuseExistingName;
74

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

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

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

    
99
		selection_reuseExistingName = formFactory
100
				.createSelectionElement(TaxonName.class,//getConversationHolder(),
101
				        formElement,
102
						"Reuse existing name", null,
103
						EntitySelectionElement.DELETABLE, style);
104
		selection_SecRef = formFactory
105
                .createSelectionElement(Reference.class,//getConversationHolder(),
106
                        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){
151
                    TaxonName name = HibernateProxyHelper.deproxy(node.getTaxon().getName());
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(TaxonName 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)