Project

General

Profile

Download (15.2 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.metadata.CdmPreference;
19
import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
20
import eu.etaxonomy.cdm.model.name.TaxonName;
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.preference.IPreferenceKeys;
27
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
28
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
29
import eu.etaxonomy.taxeditor.ui.element.CheckboxElement;
30
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
31
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
32
import eu.etaxonomy.taxeditor.ui.element.MultilanguageTextElement;
33
import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
34
import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
35
import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
36
import eu.etaxonomy.taxeditor.ui.selection.TaxonNodeSelectionElement;
37

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

    
46
    private boolean createNew = false;
47

    
48
	private EntitySelectionElement<Classification> selection_classification;
49

    
50
	private Classification classification;
51

    
52
	private TaxonNodeSelectionElement selection_parentTaxonNode;
53

    
54
	private ITaxonTreeNode parentTreeNode;
55

    
56
	private EntitySelectionElement<Taxon> selection_reuseExistingTaxon;
57
	private EntitySelectionElement<Reference> selection_SecRef;
58
	private EntitySelectionElement<Reference> selection_Ref;
59
	private TextWithLabelElement microReference;
60

    
61
	private Taxon taxon;
62

    
63
	private TextWithLabelElement textNewTaxonName;
64

    
65
	private TaxonNodeAgentRelationCollectionSection selectionNodeAgentRelation;
66

    
67
	private CheckboxElement checkbox_openInEditor;
68

    
69
	private CheckboxElement checkbox_unplaced;
70

    
71
	private CheckboxElement checkbox_excluded;
72
	private CheckboxElement checkbox_publish;
73

    
74

    
75

    
76
    private MultilanguageTextElement multiLanguageTextExcludedNotes;
77

    
78
	private boolean openInEditor;
79

    
80
	private boolean complete;
81

    
82
	private boolean excluded = false;
83
	private boolean unplaced = false;
84

    
85
	private EntitySelectionElement<TaxonName> selection_reuseExistingName;
86

    
87
	public TaxonNodeDetailElement(CdmFormFactory formFactory,
88
			ICdmFormElement formElement, boolean isCreateNew) {
89
		super(formFactory, formElement);
90
		createNew = isCreateNew;
91
	}
92

    
93

    
94

    
95
	/** {@inheritDoc} */
96
	@Override
97
	protected void createControls(ICdmFormElement formElement,
98
	        ITaxonTreeNode entity, int style) {
99
		selection_classification = formFactory
100
				.createSelectionElement(Classification.class,//getConversationHolder(),
101
				        formElement, "Classification",
102
						null, EntitySelectionElement.DELETABLE, style);
103
		selection_parentTaxonNode = formFactory
104
				.createTaxonNodeSelectionElement(getConversationHolder(), formElement, "Parent", null,
105
						EntitySelectionElement.DELETABLE, style);
106

    
107
		selection_reuseExistingTaxon = formFactory
108
				.createSelectionElement(Taxon.class,//getConversationHolder(),
109
				        formElement,
110
						"Reuse existing taxon", null,
111
						EntitySelectionElement.DELETABLE, style);
112

    
113
		selection_reuseExistingName = formFactory
114
				.createSelectionElement(TaxonName.class,//getConversationHolder(),
115
				        formElement,
116
						"Reuse existing name", null,
117
						EntitySelectionElement.DELETABLE, style);
118
		selection_SecRef = formFactory
119
                .createSelectionElement(Reference.class,//getConversationHolder(),
120
                        formElement, "Secundum Reference", null,
121
                        EntitySelectionElement.DELETABLE, style);
122
		selection_Ref = formFactory
123
                .createSelectionElement(Reference.class,//getConversationHolder(),
124
                        formElement, "Parent Child Relationship Reference", null,
125
                        EntitySelectionElement.DELETABLE, style);
126
		microReference = formFactory.createTextWithLabelElement(formElement,"Detail", "", style);
127
		if (!isCreateNew()){
128
		    selection_Ref.setEntity(entity.getReference());
129
        }
130
		if (isCreateNew()){
131
		    textNewTaxonName = formFactory.createTextWithLabelElement(formElement,"New Taxon", "", style);
132
		    textNewTaxonName.setFocus();
133
		} else{
134
		    textNewTaxonName = formFactory.createTextWithLabelElement(formElement,"Taxon", "", style);
135

    
136
		}
137
		if (entity instanceof TaxonNode) {
138
		    checkbox_publish = formFactory.createCheckbox(formElement,
139
		            "Taxon is public", ((TaxonNode)entity).getTaxon().isPublish(), style);
140

    
141
		    if(isCreateNew()){
142
		        CdmPreference defaultPublish = PreferencesUtil.getPreferenceFromDB(PreferencePredicate.DefaultBehaviourForPublishFlag);
143
		        if (defaultPublish != null && defaultPublish.getValue().equals(IPreferenceKeys.PUBLISH)){
144
		            checkbox_publish.setSelection(true);
145
		        } else if (defaultPublish != null && defaultPublish.getValue().equals(IPreferenceKeys.DO_NOT_PUBLISH)){
146
		            checkbox_publish.setSelection(false);
147
		        }else{
148
		            if((this.getParentTreeNode()).isInstanceOf(TaxonNode.class)){
149
		                checkbox_publish.setSelection(((TaxonNode)this.getParentTreeNode()).getTaxon().isPublish());
150
		            }else{
151
		                checkbox_publish.setSelection(true);
152
		            }
153
		        }
154
		    }
155
		}
156

    
157
		if (isCreateNew()){
158
            setParentTreeNode(entity);
159
        } else{
160
            setTreeNode(entity);
161
            complete = true;
162
        }
163
		if (entity instanceof TaxonNode) {
164
		    checkbox_unplaced = formFactory.createCheckbox(formElement,
165
		            "Taxon is unplaced", ((TaxonNode)entity).isUnplaced(), style);
166
		}
167

    
168
		preFillTaxonName();
169

    
170
		if (entity instanceof TaxonNode) {
171
		    checkbox_excluded = formFactory.createCheckbox(formElement,
172
		            "Taxon is excluded", ((TaxonNode)entity).isExcluded(), style);
173
		    multiLanguageTextExcludedNotes = formFactory.createMultiLanguageTextElement(formElement, "Excluded Notes", ((TaxonNode)entity).getExcludedNote(), 50, style);
174
		    multiLanguageTextExcludedNotes.setEnabled(((TaxonNode) entity).isExcluded());
175
		}
176
		if (!isCreateNew()){
177
		    selectionNodeAgentRelation = formFactory.createTaxonNodeAgentRelationCollectionSection(formElement, getConversationHolder(), style);
178
		    selectionNodeAgentRelation.setEntity((TaxonNode)entity);
179
		    selectionNodeAgentRelation.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
180
		}
181
		checkbox_openInEditor = formFactory.createCheckbox(formElement,
182
		        "Open in Editor", true, style);
183
		setOpenInEditor(true);
184
	}
185

    
186
	private void preFillTaxonName() {
187
        if(getEntity() instanceof TaxonNode ){
188
            TaxonNode node = (TaxonNode)getEntity();
189
            if(node.getTaxon()!=null){
190
                Taxon taxon = HibernateProxyHelper.deproxy(node.getTaxon(), Taxon.class);
191
                if(taxon.getName()!=null){
192
                    TaxonName name = HibernateProxyHelper.deproxy(node.getTaxon().getName());
193
                    if (!isCreateNew()){
194
                        textNewTaxonName.setText(name.getNameCache());
195
                    } else if( ! name.isSupraGeneric() && name.getRank() != null){
196
                        String taxonName = "";
197
                        if(name.isGenus() || name.isInfraGeneric()|| name.isSpeciesAggregate() ){
198
                            taxonName = name.getGenusOrUninomial();
199
                        }
200
                        else if(name.isSpecies() || name.isInfraSpecific() ){
201
                            taxonName = CdmUtils.concat(" ", name.getGenusOrUninomial(),name.getSpecificEpithet());
202
                        }
203
                        if (StringUtils.isNotBlank(taxonName)){
204
                        	textNewTaxonName.setText(taxonName + " ");
205
                        	if(textNewTaxonName.getMainControl() instanceof Text){
206
                        		Text text = (Text)textNewTaxonName.getMainControl();
207
                        		text.setSelection(textNewTaxonName.getText().length());
208
                        	}
209
                        }
210
                    }
211
                }
212
            }
213
        }
214
    }
215

    
216
	/** {@inheritDoc} */
217
	@Override
218
	public void handleEvent(Object eventSource) {
219
		if (eventSource == selection_classification) {
220
			setClassification(selection_classification.getEntity());
221
		} else if (eventSource == selection_parentTaxonNode) {
222
			setParentTreeNode(selection_parentTaxonNode.getEntity());
223
		} else if (eventSource == selection_reuseExistingTaxon) {
224
				boolean enabled = selection_reuseExistingTaxon.getEntity() == null;
225
				selection_reuseExistingName.setEnabled(enabled);
226
				if (!enabled){
227
                    setTaxon(selection_reuseExistingTaxon.getEntity());
228
                }else{
229
			        textNewTaxonName.setText(null);
230
			    }
231
				complete = !textNewTaxonName.getText().isEmpty();
232

    
233

    
234
		} else if (eventSource == selection_reuseExistingName) {
235
		    boolean enabled = selection_reuseExistingName.getEntity() == null;
236
			setTaxon(selection_reuseExistingName.getEntity());
237
			selection_reuseExistingTaxon.setEnabled(enabled);
238
			textNewTaxonName.setEnabled(enabled);
239

    
240
			complete = !textNewTaxonName.getText().isEmpty();
241
		}
242
		if(getEntity() instanceof TaxonNode){
243
		    if (eventSource == selection_Ref) {
244
		        ((TaxonNode) getEntity()).setReference(selection_Ref.getEntity());
245
		    }else if (eventSource == selection_SecRef) {
246
		        ((TaxonNode) getEntity()).getTaxon().setSec(selection_SecRef.getEntity());
247
		    }else if (eventSource == microReference) {
248
		        ((TaxonNode) getEntity()).setMicroReference(microReference.getText());
249
		    } else if (eventSource == textNewTaxonName) {
250
		        boolean enabled = CdmUtils.isBlank(textNewTaxonName.getText());
251
		        selection_reuseExistingTaxon.setEnabled(enabled);
252
		        selection_reuseExistingName.setEnabled(enabled);
253

    
254
		        complete = CdmUtils.isNotBlank(textNewTaxonName.getText());
255
		    } else if (eventSource == checkbox_excluded) {
256
		        excluded = checkbox_excluded.getSelection();
257
		        ((TaxonNode)getEntity()).setExcluded(excluded);
258

    
259
		        multiLanguageTextExcludedNotes.setEnabled(excluded);
260
		    } else if (eventSource == checkbox_unplaced) {
261
		        unplaced = checkbox_unplaced.getSelection();
262
		        ((TaxonNode)getEntity()).setUnplaced(unplaced);
263
		    } else if (eventSource == checkbox_openInEditor) {
264
		        setOpenInEditor(checkbox_openInEditor.getSelection());
265
		    } else if (eventSource == checkbox_publish) {
266
		        ((TaxonNode)getEntity()).getTaxon().setPublish(checkbox_publish.getSelection());
267
		    }
268
		}
269
		else if (eventSource == selectionNodeAgentRelation){
270
		   boolean allComplete = true;
271
		   for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
272
		       allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
273
		   }
274
		    complete = !isCreateNew() && allComplete;
275
		}
276
	}
277

    
278
	public Classification getClassification() {
279
		return classification;
280
	}
281

    
282
	public boolean isOpenInEditor() {
283
		return openInEditor;
284
	}
285

    
286
	public ITaxonTreeNode getParentTreeNode() {
287
		return parentTreeNode;
288
	}
289

    
290
	private void setClassification(Classification classification) {
291
		this.classification = classification;
292
		setParentTreeNode(classification);
293
	}
294

    
295
	private void setParentTreeNode(ITaxonTreeNode parentTreeNode) {
296
		this.parentTreeNode = parentTreeNode;
297

    
298
		if (parentTreeNode instanceof Classification) {
299
			classification = (Classification) parentTreeNode;
300
			selection_classification.setEntity(classification);
301
			selection_parentTaxonNode.setEntity(null);
302
			selection_parentTaxonNode.setClassification(classification);
303
			selection_SecRef.setEntity(classification.getReference());
304
		} else if (parentTreeNode instanceof TaxonNode) {
305
			classification = HibernateProxyHelper
306
					.deproxy(((TaxonNode) parentTreeNode).getClassification());
307
			selection_classification.setEntity(classification);
308
			selection_parentTaxonNode.setEntity(HibernateProxyHelper
309
                    .deproxy((TaxonNode) parentTreeNode));
310
			selection_parentTaxonNode.setClassification(classification);
311
			selection_SecRef.setEntity(((TaxonNode)parentTreeNode).getTaxon().getSec());
312
		} else if(parentTreeNode == null){
313
			this.parentTreeNode = selection_classification.getEntity();
314
		}
315
	}
316

    
317
	private void setTreeNode(ITaxonTreeNode treeNode) {
318
        classification = HibernateProxyHelper
319
                    .deproxy(((TaxonNode) treeNode).getClassification());
320
        selection_classification.setEntity(classification);
321
        selection_parentTaxonNode.setEntity(((TaxonNode) treeNode).getParent());
322
        selection_parentTaxonNode.setClassification(((TaxonNode) treeNode).getParent().getClassification());
323
        selection_SecRef.setEntity(((TaxonNode)treeNode).getTaxon().getSec());
324
	}
325

    
326
	private void setTaxon(Taxon taxon) {
327
		this.taxon = taxon;
328
		if (isCreateNew()){
329
		    textNewTaxonName.setText(taxon.getName().getTitleCache());
330
		}
331
		selection_SecRef.setEntity(taxon.getSec());
332
	}
333

    
334
	private void setTaxon(TaxonName taxonName) {
335
		Reference secundum = null;
336
		if (getParentTreeNode() != null) {
337
			if (this.selection_SecRef.getEntity() != null){
338
				secundum = this.selection_SecRef.getEntity();
339
			}
340
		}
341
		if (taxonName != null){
342
		    taxon = Taxon.NewInstance(taxonName, secundum);
343
		    if (textNewTaxonName.getText() == null || !taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
344
		    	 textNewTaxonName.setText(taxonName.getTitleCache());
345
		    }
346
//		    if (!taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
347
//		        textNewTaxonName.setText(taxonName.getTitleCache());
348
//		    }
349
		} else {
350
		    textNewTaxonName.setText(null);
351
		}
352
	}
353

    
354
	private void setOpenInEditor(boolean openInEditor) {
355
		this.openInEditor = openInEditor;
356
	}
357

    
358
	public String getTaxonName() {
359
        return textNewTaxonName.getText();
360
    }
361
	public Taxon getTaxon() {
362
        return taxon;
363
    }
364

    
365
	public Reference getReference(){
366
	    return selection_SecRef.getSelection();
367
	}
368

    
369
	public Control getMainControl(){
370
	    return textNewTaxonName.getMainControl();
371
	}
372

    
373
	public boolean isComplete() {
374
	    boolean allComplete = true;
375
	    if (!isCreateNew()){
376
	        for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
377
	            allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
378
	        }
379
	        complete = allComplete;
380
	    }
381
		return complete;
382
	}
383

    
384
    public boolean isCreateNew() {
385
        return createNew;
386
    }
387

    
388
    public boolean isExcluded() {
389
        return excluded;
390
    }
391

    
392
    public boolean isUnplaced() {
393
        return unplaced;
394
    }
395

    
396
    public boolean is_publish() {
397
        return checkbox_publish!=null?checkbox_publish.getSelection():false;
398
    }
399

    
400
    public void set_publish(boolean publish) {
401
        this.checkbox_publish.setSelection(publish);
402
    }
403

    
404
}
(10-10/12)