Project

General

Profile

Download (17.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 java.util.HashMap;
13

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

    
18
import eu.etaxonomy.cdm.common.CdmUtils;
19
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
20
import eu.etaxonomy.cdm.model.common.Language;
21
import eu.etaxonomy.cdm.model.common.LanguageString;
22
import eu.etaxonomy.cdm.model.metadata.CdmPreference;
23
import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
24
import eu.etaxonomy.cdm.model.metadata.PublishEnum;
25
import eu.etaxonomy.cdm.model.name.TaxonName;
26
import eu.etaxonomy.cdm.model.reference.Reference;
27
import eu.etaxonomy.cdm.model.taxon.Classification;
28
import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
29
import eu.etaxonomy.cdm.model.taxon.Taxon;
30
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
31
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
32
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
33
import eu.etaxonomy.taxeditor.ui.element.CheckboxElement;
34
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
35
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
36
import eu.etaxonomy.taxeditor.ui.element.MultilanguageTextElement;
37
import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
38
import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
39
import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
40
import eu.etaxonomy.taxeditor.ui.selection.TaxonNodeSelectionElement;
41

    
42
/**
43
 *
44
 * @author n.hoffmann
45
 * @created Sep 27, 2010
46
 * @version 1.0
47
 */
48
public class TaxonNodeDetailElement extends AbstractCdmDetailElement<TaxonNode> {
49

    
50
    private boolean createNew = false;
51

    
52
//	private EntitySelectionElement<Classification> selection_classification;
53

    
54
	private Classification classification;
55

    
56
	private TaxonNodeSelectionElement selection_parentTaxonNode;
57

    
58
	private TaxonNode parentTreeNode;
59

    
60
	private EntitySelectionElement<Taxon> selection_reuseExistingTaxon;
61
	private EntitySelectionElement<Reference> selection_SecRef;
62
	private EntitySelectionElement<Reference> selection_Ref;
63
	TextWithLabelElement textTaxonSec;
64
	private TextWithLabelElement microReference;
65

    
66
	private Taxon taxon;
67

    
68
	private TextWithLabelElement textNewTaxonName;
69

    
70
	private TaxonNodeAgentRelationCollectionSection selectionNodeAgentRelation;
71

    
72
//	private CheckboxElement checkbox_openInEditor;
73

    
74
	private CheckboxElement checkbox_unplaced;
75

    
76
	private CheckboxElement checkbox_excluded;
77
	private CheckboxElement checkbox_publish;
78

    
79

    
80

    
81
    private MultilanguageTextElement multiLanguageTextExcludedNotes;
82

    
83
//	private boolean openInEditor;
84

    
85
	private boolean complete;
86

    
87
	private boolean excluded = false;
88
	private boolean unplaced = false;
89

    
90
	private EntitySelectionElement<TaxonName> selection_reuseExistingName;
91

    
92
	public TaxonNodeDetailElement(CdmFormFactory formFactory,
93
			ICdmFormElement formElement, boolean isCreateNew) {
94
		super(formFactory, formElement);
95
		createNew = isCreateNew;
96
	}
97

    
98

    
99

    
100
	/** {@inheritDoc} */
101
	@Override
102
	protected void createControls(ICdmFormElement formElement,
103
	        TaxonNode entity, int style) {
104
	    taxon = entity.getTaxon();
105
	    if (isCreateNew()){
106
//    		selection_classification = formFactory
107
//    				.createSelectionElement(Classification.class,//getConversationHolder(),
108
//    				        formElement, "Classification",
109
//    						null, EntitySelectionElement.DELETABLE, style);
110
    		selection_parentTaxonNode = formFactory
111
    				.createTaxonNodeSelectionElement(getConversationHolder(), formElement, "Parent", parentTreeNode,
112
    						EntitySelectionElement.DELETABLE, style);
113

    
114
//    		selection_reuseExistingTaxon = formFactory
115
//    				.createSelectionElement(Taxon.class,//getConversationHolder(),
116
//    				        formElement,
117
//    						"Reuse existing taxon", null,
118
//    						EntitySelectionElement.DELETABLE, style);
119

    
120
    		selection_reuseExistingName = formFactory
121
    				.createSelectionElement(TaxonName.class,//getConversationHolder(),
122
    				        formElement,
123
    						"Reuse existing name", taxon.getName(),
124
    						EntitySelectionElement.DELETABLE, style);
125
    		selection_SecRef = formFactory
126
                    .createSelectionElement(Reference.class,//getConversationHolder(),
127
                            formElement, "Secundum Reference", taxon.getSec(),
128
                            EntitySelectionElement.DELETABLE, style);
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
	    }else{
138
	        TextWithLabelElement textClassification = formFactory.createTextWithLabelElement(formElement,"Classification", entity.getClassification().getTitleCache(), style);
139
	        textClassification.setEnabled(false);
140

    
141
//	        TextWithLabelElement textTaxon = formFactory.createTextWithLabelElement(formElement,"Taxon", taxon.getTitleCache(), style);
142
//	        textTaxon.setEnabled(false);
143

    
144
	        selection_reuseExistingTaxon = formFactory
145
                  .createSelectionElement(Taxon.class,//getConversationHolder(),
146
                          formElement,
147
                          "Reuse existing taxon", taxon,
148
                          EntitySelectionElement.DELETABLE, style);
149

    
150
	        textTaxonSec = formFactory.createTextWithLabelElement(formElement,"Secundum Reference", "", style);
151
            if (entity.getTaxon().getSec() != null){
152
                textTaxonSec.setText(taxon.getSec().getTitleCache());
153
            }
154
            textTaxonSec.setEnabled(false);
155

    
156

    
157

    
158
	    }
159

    
160
	    selection_Ref = formFactory
161
                .createSelectionElement(Reference.class,//getConversationHolder(),
162
                        formElement, "Parent Child Relation Source", null,
163
                        EntitySelectionElement.DELETABLE, style);
164
        microReference = formFactory.createTextWithLabelElement(formElement,"Detail", "", style);
165
        microReference.setText(getEntity().getMicroReference());
166
        if (!isCreateNew()){
167
            selection_Ref.setEntity(entity.getReference());
168
        }
169
        parentTreeNode = entity.getParent();
170
		if (entity instanceof TaxonNode && parentTreeNode.getTaxon() != null) {
171
		    checkbox_publish = formFactory.createCheckbox(formElement,
172
		            "Taxon is public", parentTreeNode.getTaxon().isPublish(), style);
173
		}else{
174
		    checkbox_publish = formFactory.createCheckbox(formElement,
175
                    "Taxon is public", true, style);
176
		}
177
		checkbox_publish.setEnabled(isCreateNew());
178
		if(isCreateNew()){
179
		   setParentTreeNode(entity.getParent());
180

    
181
		   String defaultPublish = PreferencesUtil.getStringValue(PreferencePredicate.DefaultBehaviourForPublishFlag.getKey());
182
	        if (defaultPublish != null && defaultPublish.equals(PublishEnum.Publish.getKey())){
183
	            checkbox_publish.setSelection(true);
184
	        } else if (defaultPublish != null && defaultPublish.equals(PublishEnum.NotPublish.getKey())){
185
	            checkbox_publish.setSelection(false);
186
	        }else{
187
	            if(this.getParentTreeNode() != null &&  this.getParentTreeNode().isInstanceOf(TaxonNode.class) && ((TaxonNode)this.getParentTreeNode()).getTaxon() != null){
188
	                checkbox_publish.setSelection(((TaxonNode)this.getParentTreeNode()).getTaxon().isPublish());
189
	            }else{
190
	                checkbox_publish.setSelection(true);
191
	            }
192
	        }
193

    
194
	    } else{
195
            setTreeNode(entity);
196
            complete = true;
197
        }
198
		if (entity instanceof TaxonNode) {
199
		    checkbox_unplaced = formFactory.createCheckbox(formElement,
200
		            "Taxon is unplaced", entity.isUnplaced(), style);
201
		}else{
202
		    checkbox_unplaced = formFactory.createCheckbox(formElement,
203
                    "Taxon is unplaced", false, style);
204
		}
205
		if (isCreateNew()){
206
		    preFillTaxonName();
207
		}
208

    
209
		if (entity instanceof TaxonNode) {
210
		    checkbox_excluded = formFactory.createCheckbox(formElement,
211
		            "Taxon is excluded", entity.isExcluded(), style);
212
		    multiLanguageTextExcludedNotes = formFactory.createMultiLanguageTextElement(formElement, "Excluded Notes", entity.getExcludedNote(), 50, style);
213
		    multiLanguageTextExcludedNotes.setEnabled(entity.isExcluded());
214
		}else{
215
		    checkbox_excluded = formFactory.createCheckbox(formElement,
216
                    "Taxon is excluded", false, style);
217
		    multiLanguageTextExcludedNotes = formFactory.createMultiLanguageTextElement(formElement, "Excluded Notes", new HashMap<Language, LanguageString>(), 50, style);
218
            multiLanguageTextExcludedNotes.setEnabled(false);
219
		}
220
		if (!isCreateNew()){
221
		    selectionNodeAgentRelation = formFactory.createTaxonNodeAgentRelationCollectionSection(formElement, getConversationHolder(), style);
222
		    selectionNodeAgentRelation.setEntity(entity);
223
		    selectionNodeAgentRelation.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
224
		}
225
//		checkbox_openInEditor = formFactory.createCheckbox(formElement,
226
//		        "Open in Editor", true, style);
227
//		setOpenInEditor(true);
228
	}
229

    
230
	private void preFillTaxonName() {
231
        if(getEntity() instanceof TaxonNode ){
232
            TaxonNode node = parentTreeNode;
233
            if(node.getTaxon()!=null){
234
                Taxon taxon = HibernateProxyHelper.deproxy(node.getTaxon(), Taxon.class);
235
                if(taxon.getName()!=null){
236
                    TaxonName name = HibernateProxyHelper.deproxy(node.getTaxon().getName());
237
                    if (!isCreateNew()){
238
                        textNewTaxonName.setText(name.getNameCache());
239
                    } else if( ! name.isSupraGeneric() && name.getRank() != null){
240
                        String taxonName = "";
241
                        if(name.isGenus() || name.isInfraGeneric()|| name.isSpeciesAggregate() ){
242
                            taxonName = name.getGenusOrUninomial();
243
                        }
244
                        else if(name.isSpecies() || name.isInfraSpecific() ){
245
                            taxonName = CdmUtils.concat(" ", name.getGenusOrUninomial(),name.getSpecificEpithet());
246
                        }
247
                        if (StringUtils.isNotBlank(taxonName)){
248
                        	textNewTaxonName.setText(taxonName + " ");
249
                        	if(textNewTaxonName.getMainControl() instanceof Text){
250
                        		Text text = (Text)textNewTaxonName.getMainControl();
251
                        		text.setSelection(textNewTaxonName.getText().length());
252
                        	}
253
                        }
254
                    }
255
                }
256
            }
257
        }
258
    }
259

    
260
	/** {@inheritDoc} */
261
	@Override
262
	public void handleEvent(Object eventSource) {
263
		if (eventSource == selection_parentTaxonNode) {
264
		    setParentTreeNode(selection_parentTaxonNode.getEntity());
265
		} else if (eventSource == selection_reuseExistingTaxon) {
266
		    if (selection_reuseExistingTaxon.getEntity() != null){
267
		        setTaxon(selection_reuseExistingTaxon.getEntity());
268
            }
269
			complete = true;
270
		} else if (eventSource == selection_reuseExistingName) {
271
		    boolean enabled = selection_reuseExistingName.getEntity() == null;
272
			setTaxon(HibernateProxyHelper.deproxy(selection_reuseExistingName.getEntity()));
273
			if (selection_reuseExistingTaxon != null){
274
			    selection_reuseExistingTaxon.setEnabled(enabled);
275
			}
276
			textNewTaxonName.setEnabled(enabled);
277
			complete = !textNewTaxonName.getText().isEmpty();
278
		}
279

    
280
        if (eventSource == selection_Ref) {
281
            getEntity().setReference(selection_Ref.getEntity());
282
        }else if (eventSource == selection_SecRef) {
283
            taxon.setSec(selection_SecRef.getEntity());
284
        }else if (eventSource == microReference) {
285
            getEntity().setMicroReference(microReference.getText());
286
        }  else if (eventSource == checkbox_publish) {
287
            taxon.setPublish(checkbox_publish.getSelection());
288
        }
289

    
290
		if (eventSource == checkbox_excluded) {
291
            excluded = checkbox_excluded.getSelection();
292

    
293
            getEntity().setExcluded(excluded);
294

    
295
            multiLanguageTextExcludedNotes.setEnabled(excluded);
296
        }
297
		if (eventSource == checkbox_unplaced) {
298
            unplaced = checkbox_unplaced.getSelection();
299
            getEntity().setUnplaced(unplaced);
300

    
301
        }
302
		if (eventSource == textNewTaxonName) {
303
            boolean enabled = CdmUtils.isBlank(textNewTaxonName.getText());
304
            if (selection_reuseExistingTaxon != null){
305
                selection_reuseExistingTaxon.setEnabled(enabled);
306
            }
307
            selection_reuseExistingName.setEnabled(enabled);
308

    
309
            complete = CdmUtils.isNotBlank(textNewTaxonName.getText());
310
        }
311

    
312
		if (eventSource == selectionNodeAgentRelation){
313
		   boolean allComplete = true;
314
		   for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
315
		       allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
316
		   }
317
		    complete = !isCreateNew() && allComplete;
318
		}
319
	}
320

    
321
	public Classification getClassification() {
322
		return classification;
323
	}
324

    
325
//	public boolean isOpenInEditor() {
326
//		return openInEditor;
327
//	}
328

    
329
	public ITaxonTreeNode getParentTreeNode() {
330
		return parentTreeNode;
331
	}
332

    
333
	private void setClassification(Classification classification) {
334
		this.classification = classification;
335
		//setParentTreeNode(classification);
336
	}
337

    
338
	private void setParentTreeNode(TaxonNode parentTreeNode) {
339
		this.parentTreeNode = parentTreeNode;
340

    
341
		if (parentTreeNode.getTaxon() == null) {
342
			classification = parentTreeNode.getClassification();
343
//			if (selection_classification != null){
344
//			    selection_classification.setEntity(classification);
345
//			}
346
			selection_parentTaxonNode.setEntity(classification.getRootNode());
347
			selection_parentTaxonNode.setClassification(classification);
348
			selection_SecRef.setEntity(classification.getReference());
349
		} else  {
350
			classification = HibernateProxyHelper
351
					.deproxy(parentTreeNode.getClassification());
352
//			if (selection_classification != null){
353
//			    selection_classification.setEntity(classification);
354
//			}
355
			selection_parentTaxonNode.setEntity(HibernateProxyHelper
356
                    .deproxy(parentTreeNode));
357
			selection_parentTaxonNode.setClassification(classification);
358
			selection_SecRef.setEntity(parentTreeNode.getTaxon().getSec());
359

    
360
		}
361
	}
362

    
363
	private void setTreeNode(TaxonNode treeNode) {
364
        classification = HibernateProxyHelper
365
                    .deproxy(treeNode.getClassification());
366
        if (isCreateNew()){
367
           //selection_classification.setEntity(classification);
368
            selection_parentTaxonNode.setEntity(treeNode.getParent());
369
            selection_parentTaxonNode.setClassification(treeNode.getParent().getClassification());
370
            selection_SecRef.setEntity(treeNode.getTaxon().getSec());
371
        }
372
	}
373

    
374
	private void setTaxon(Taxon taxon) {
375
		this.taxon = taxon;
376
		getEntity().setTaxon(taxon);
377
		if (isCreateNew()){
378
		    textNewTaxonName.setText(taxon.getName().getTitleCache());
379
		    if (taxon.getSec() != null){
380
		        selection_SecRef.setEntity(taxon.getSec());
381
		    }
382
		}else{
383
		    if (taxon.getSec() != null) {
384
                this.textTaxonSec.setText(taxon.getSec().getTitleCache());
385
            }
386
		}
387

    
388

    
389
	}
390

    
391
	private void setTaxon(TaxonName taxonName) {
392
		Reference secundum = null;
393
		if (getParentTreeNode() != null) {
394
			if (this.selection_SecRef.getEntity() != null){
395
				secundum = this.selection_SecRef.getEntity();
396
			}
397
		}
398
		if (taxonName != null){
399
		    taxon = Taxon.NewInstance(taxonName, secundum);
400
		    if (textNewTaxonName.getText() == null || !taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
401
		    	 textNewTaxonName.setText(taxonName.getTitleCache());
402
		    }
403
		    getEntity().setTaxon(taxon);
404
//		    if (!taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
405
//		        textNewTaxonName.setText(taxonName.getTitleCache());
406
//		    }
407
		} else {
408
		    textNewTaxonName.setText(null);
409
		}
410
	}
411

    
412
//	private void setOpenInEditor(boolean openInEditor) {
413
//		this.openInEditor = openInEditor;
414
//	}
415

    
416
	public String getTaxonName() {
417
        return textNewTaxonName.getText();
418
    }
419
	public Taxon getTaxon() {
420
        return taxon;
421
    }
422

    
423
	public Reference getReference(){
424
	    return selection_SecRef.getSelection();
425
	}
426

    
427
	public Control getMainControl(){
428
	    return textNewTaxonName.getMainControl();
429
	}
430

    
431
	public boolean isComplete() {
432
	    boolean allComplete = true;
433
	    if (!isCreateNew()){
434
	        for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
435
	            allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
436
	        }
437
	        complete = allComplete;
438
	    }
439
		return complete;
440
	}
441

    
442
    public boolean isCreateNew() {
443
        return createNew;
444
    }
445

    
446
    public boolean isExcluded() {
447
        return excluded;
448
    }
449

    
450
    public boolean isUnplaced() {
451
        return unplaced;
452
    }
453

    
454
    public boolean is_publish() {
455
        boolean isPublishPreference = true;
456
        CdmPreference defaultPublish = PreferencesUtil.getPreferenceFromDB(PreferencePredicate.DefaultBehaviourForPublishFlag);
457
        if (defaultPublish != null && defaultPublish.getValue().equals(PublishEnum.NotPublish.getKey())){
458
            isPublishPreference = false;
459
        }
460
        return checkbox_publish!=null?checkbox_publish.getSelection():isPublishPreference;
461
    }
462

    
463
    public void set_publish(boolean publish) {
464
        this.checkbox_publish.setSelection(publish);
465
    }
466

    
467

    
468
}
(10-10/12)