Project

General

Profile

Download (18.8 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.store.StoreUtil;
33
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
34
import eu.etaxonomy.taxeditor.ui.element.CheckboxElement;
35
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
36
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
37
import eu.etaxonomy.taxeditor.ui.element.MultilanguageTextElement;
38
import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
39
import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
40
import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
41
import eu.etaxonomy.taxeditor.ui.selection.TaxonNodeSelectionElement;
42

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

    
51
    private boolean createNew = false;
52

    
53
//	private EntitySelectionElement<Classification> selection_classification;
54

    
55
	private Classification classification;
56

    
57
	private TaxonNodeSelectionElement selection_parentTaxonNode;
58

    
59
	private TaxonNode parentTreeNode;
60

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

    
67
	private Taxon taxon;
68

    
69
	private TextWithLabelElement textNewTaxonName;
70

    
71
	private TaxonNodeAgentRelationCollectionSection selectionNodeAgentRelation;
72

    
73
//	private CheckboxElement checkbox_openInEditor;
74

    
75
	private CheckboxElement checkbox_unplaced;
76

    
77
	private CheckboxElement checkbox_excluded;
78
	private CheckboxElement checkbox_publish;
79

    
80

    
81

    
82
    private MultilanguageTextElement multiLanguageTextExcludedNotes;
83

    
84
//	private boolean openInEditor;
85

    
86
	private boolean complete;
87

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

    
91
	private EntitySelectionElement<TaxonName> selection_reuseExistingName;
92

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

    
99

    
100

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

    
112
    		selection_parentTaxonNode = formFactory
113
    				.createTaxonNodeSelectionElement(getConversationHolder(), formElement, "Parent", parentTreeNode,
114
    						EntitySelectionElement.DELETABLE, style);
115

    
116

    
117

    
118
    		selection_Ref = formFactory
119
                    .createSelectionElement(Reference.class,
120
                            formElement, "Parent Child Relation Source", null,
121
                            EntitySelectionElement.DELETABLE, style);
122

    
123

    
124
            microReference = formFactory.createTextWithLabelElement(formElement,"Detail", "", style);
125
            microReference.setText(getEntity().getMicroReference());
126

    
127
    		selection_reuseExistingTaxon = formFactory
128
    				.createSelectionElement(Taxon.class,//getConversationHolder(),
129
    				        formElement,
130
    						"Reuse existing taxon", null,
131
    						EntitySelectionElement.DELETABLE, style);
132

    
133
    		selection_reuseExistingName = formFactory
134
    				.createSelectionElement(TaxonName.class,//getConversationHolder(),
135
    				        formElement,
136
    						"Reuse existing name", taxon.getName(),
137
    						EntitySelectionElement.DELETABLE, style);
138
    		selection_SecRef = formFactory
139
                    .createSelectionElement(Reference.class,//getConversationHolder(),
140
                            formElement, "Secundum Reference", taxon.getSec(),
141
                            EntitySelectionElement.DELETABLE, style);
142

    
143
    		if (isCreateNew()){
144
    		    textNewTaxonName = formFactory.createTextWithLabelElement(formElement,"New Taxon", "", style);
145
    		    textNewTaxonName.setFocus();
146
    		} else{
147
    		    textNewTaxonName = formFactory.createTextWithLabelElement(formElement,"Taxon", "", style);
148

    
149
    		}
150
	    }else{
151
	        if (entity.getParent().getTaxon() == null){
152
	            TextWithLabelElement textParent = formFactory.createTextWithLabelElement(formElement,"Classification", entity.getClassification().getTitleCache(), style);
153
                textParent.setEnabled(false);
154
	        }else{
155
	            TextWithLabelElement textParent = formFactory.createTextWithLabelElement(formElement,"Parent", entity.getParent().getTaxon().getTitleCache(), style);
156
	            textParent.setEnabled(false);
157
	        }
158
//	        TextWithLabelElement textTaxon = formFactory.createTextWithLabelElement(formElement,"Taxon", taxon.getTitleCache(), style);
159
//	        textTaxon.setEnabled(false);
160
	        selection_Ref = formFactory
161
                    .createSelectionElement(Reference.class,
162
                            formElement, "Parent Child Relation Source", null,
163
                            EntitySelectionElement.DELETABLE, style);
164

    
165
	        selection_reuseExistingTaxon = formFactory
166
                  .createSelectionElement(Taxon.class,//getConversationHolder(),
167
                          formElement,
168
                          "Taxon", taxon,
169
                          EntitySelectionElement.DELETABLE, style);
170

    
171
	        textTaxonSec = formFactory.createTextWithLabelElement(formElement,"Secundum Reference", "", style);
172
            if (entity.getTaxon().getSec() != null){
173
                textTaxonSec.setText(taxon.getSec().getTitleCache());
174
            }
175
            textTaxonSec.setEnabled(false);
176

    
177

    
178

    
179
	    }
180

    
181

    
182
        if (!isCreateNew()){
183
            if (entity.getReference() != null){
184
                selection_Ref.setEntity(entity.getReference());
185
            }
186
        }
187
        parentTreeNode = entity.getParent();
188

    
189
		if (entity instanceof TaxonNode ) {
190
		    checkbox_publish = formFactory.createCheckbox(formElement,
191
		            "Taxon is published", entity.getTaxon().isPublish(), style);
192
		}else{
193
		    checkbox_publish = formFactory.createCheckbox(formElement,
194
                    "Taxon is published", true, style);
195
		}
196

    
197
		checkbox_publish.setEnabled(isCreateNew());
198
		if(isCreateNew()){
199
		   setParentTreeNode(entity.getParent());
200

    
201
		   String defaultPublish = PreferencesUtil.getStringValue(PreferencePredicate.DefaultBehaviourForPublishFlag.getKey());
202
	        if (defaultPublish != null && defaultPublish.equals(PublishEnum.Publish.getKey())){
203
	            checkbox_publish.setSelection(true);
204
	        } else if (defaultPublish != null && defaultPublish.equals(PublishEnum.NotPublish.getKey())){
205
	            checkbox_publish.setSelection(false);
206
	        }else{
207
	            if(this.getParentTreeNode() != null &&  this.getParentTreeNode().isInstanceOf(TaxonNode.class) && ((TaxonNode)this.getParentTreeNode()).getTaxon() != null){
208
	                checkbox_publish.setSelection(((TaxonNode)this.getParentTreeNode()).getTaxon().isPublish());
209
	            }else{
210
	                checkbox_publish.setSelection(true);
211
	            }
212
	        }
213

    
214
	    } else{
215
            setTreeNode(entity);
216

    
217
            complete = true;
218
        }
219
		if (entity instanceof TaxonNode) {
220
		    checkbox_unplaced = formFactory.createCheckbox(formElement,
221
		            "Taxon is unplaced", entity.isUnplaced(), style);
222
		}else{
223
		    checkbox_unplaced = formFactory.createCheckbox(formElement,
224
                    "Taxon is unplaced", false, style);
225
		}
226
		if (isCreateNew()){
227
		    preFillTaxonName();
228
		}
229

    
230
		if (entity instanceof TaxonNode) {
231
		    checkbox_excluded = formFactory.createCheckbox(formElement,
232
		            "Taxon is excluded", entity.isExcluded(), style);
233
		    multiLanguageTextExcludedNotes = formFactory.createMultiLanguageTextElement(formElement, "Excluded Notes", entity.getExcludedNote(), 50, style);
234
		    multiLanguageTextExcludedNotes.setEnabled(entity.isExcluded());
235
		}else{
236
		    checkbox_excluded = formFactory.createCheckbox(formElement,
237
                    "Taxon is excluded", false, style);
238
		    multiLanguageTextExcludedNotes = formFactory.createMultiLanguageTextElement(formElement, "Excluded Notes", new HashMap<Language, LanguageString>(), 50, style);
239
            multiLanguageTextExcludedNotes.setEnabled(false);
240
		}
241
		if (!isCreateNew()){
242
		    selectionNodeAgentRelation = formFactory.createTaxonNodeAgentRelationCollectionSection(formElement, getConversationHolder(), StoreUtil.getSectionStyle(TaxonNodeAgentRelationCollectionSection.class, entity.getClass().getCanonicalName()));
243
		    selectionNodeAgentRelation.setEntity(entity);
244
		    selectionNodeAgentRelation.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
245
		}
246

    
247
	}
248

    
249
	private void preFillTaxonName() {
250
        if(getEntity() instanceof TaxonNode ){
251
            TaxonNode node = parentTreeNode;
252
            if(node.getTaxon()!=null){
253
                Taxon taxon = HibernateProxyHelper.deproxy(node.getTaxon(), Taxon.class);
254
                if(taxon.getName()!=null){
255
                    TaxonName name = HibernateProxyHelper.deproxy(node.getTaxon().getName());
256
                    if (!isCreateNew()){
257
                        textNewTaxonName.setText(name.getNameCache());
258
                    } else if( ! name.isSupraGeneric() && name.getRank() != null){
259
                        String taxonName = "";
260
                        if(name.isGenus() || name.isInfraGeneric()|| name.isSpeciesAggregate() ){
261
                            taxonName = name.getGenusOrUninomial();
262
                        }
263
                        else if(name.isSpecies() || name.isInfraSpecific() ){
264
                            taxonName = CdmUtils.concat(" ", name.getGenusOrUninomial(),name.getSpecificEpithet());
265
                        }
266
                        if (StringUtils.isNotBlank(taxonName)){
267
                        	textNewTaxonName.setText(taxonName + " ");
268
                        	if(textNewTaxonName.getMainControl() instanceof Text){
269
                        		Text text = (Text)textNewTaxonName.getMainControl();
270
                        		text.setSelection(textNewTaxonName.getText().length());
271
                        	}
272
                        }
273
                    }
274
                }
275
            }
276
        }
277
    }
278

    
279
	/** {@inheritDoc} */
280
	@Override
281
	public void handleEvent(Object eventSource) {
282
		if (eventSource == selection_parentTaxonNode) {
283
		    setParentTreeNode(selection_parentTaxonNode.getEntity());
284
		} else if (eventSource == selection_reuseExistingTaxon) {
285
		    if (selection_reuseExistingTaxon.getEntity() != null){
286
		        setTaxon(selection_reuseExistingTaxon.getEntity());
287
		        if (getTaxon().getSec()!= null){
288
		            if (isCreateNew()){
289
		               selection_SecRef.setEntity(getTaxon().getSec());
290
		            }else{
291
		                textTaxonSec.setText((getTaxon().getSec().getTitleCache()));
292
		            }
293
		        }else{
294
		            if (isCreateNew()){
295
	                       selection_SecRef.setEntity(null);
296
	                    }else{
297
	                        textTaxonSec.setText("");
298
	                    }
299
		        }
300
		        checkbox_publish.setSelected(getTaxon().isPublish());
301
            }
302
		    boolean enabled = selection_reuseExistingTaxon.getEntity() == null;
303
		    if (selection_reuseExistingName != null){
304
		        selection_reuseExistingName.setEnabled(enabled);
305
            }
306
	        textNewTaxonName.setEnabled(enabled);
307
			complete = true;
308
		} else if (eventSource == selection_reuseExistingName) {
309
		    boolean enabled = selection_reuseExistingName.getEntity() == null;
310
			setTaxon(HibernateProxyHelper.deproxy(selection_reuseExistingName.getEntity()));
311
			if (selection_reuseExistingTaxon != null){
312
			    selection_reuseExistingTaxon.setEnabled(enabled);
313
			}
314
			textNewTaxonName.setEnabled(enabled);
315
			complete = !textNewTaxonName.getText().isEmpty();
316
		}
317

    
318
        if (eventSource == selection_Ref) {
319
            getEntity().setReference(selection_Ref.getEntity());
320
        }else if (eventSource == selection_SecRef) {
321
            taxon.setSec(selection_SecRef.getEntity());
322
        }else if (eventSource == microReference) {
323
            getEntity().setMicroReference(microReference.getText());
324
        }  else if (eventSource == checkbox_publish) {
325
            taxon.setPublish(checkbox_publish.getSelection());
326
        }
327

    
328
		if (eventSource == checkbox_excluded) {
329
            excluded = checkbox_excluded.getSelection();
330

    
331
            getEntity().setExcluded(excluded);
332

    
333
            multiLanguageTextExcludedNotes.setEnabled(excluded);
334
        }
335
		if (eventSource == checkbox_unplaced) {
336
            unplaced = checkbox_unplaced.getSelection();
337
            getEntity().setUnplaced(unplaced);
338

    
339
        }
340
		if (eventSource == textNewTaxonName) {
341
            boolean enabled = CdmUtils.isBlank(textNewTaxonName.getText());
342
            if (selection_reuseExistingTaxon != null){
343
                selection_reuseExistingTaxon.setEnabled(enabled);
344
            }
345
            selection_reuseExistingName.setEnabled(enabled);
346

    
347
            complete = CdmUtils.isNotBlank(textNewTaxonName.getText());
348
        }
349

    
350
		if (eventSource == selectionNodeAgentRelation){
351
		   boolean allComplete = true;
352
		   for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
353
		       allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
354
		   }
355
		    complete = !isCreateNew() && allComplete;
356
		}
357
	}
358

    
359
	public Classification getClassification() {
360
		return classification;
361
	}
362

    
363

    
364

    
365
	public ITaxonTreeNode getParentTreeNode() {
366
		return parentTreeNode;
367
	}
368

    
369
	private void setClassification(Classification classification) {
370
		this.classification = classification;
371
		//setParentTreeNode(classification);
372
	}
373

    
374
	private void setParentTreeNode(TaxonNode parentTreeNode) {
375
		this.parentTreeNode = parentTreeNode;
376

    
377
		if (parentTreeNode.getTaxon() == null) {
378
			classification = parentTreeNode.getClassification();
379
//			if (selection_classification != null){
380
//			    selection_classification.setEntity(classification);
381
//			}
382
			selection_parentTaxonNode.setEntity(classification.getRootNode());
383
			selection_parentTaxonNode.setClassification(classification);
384
			selection_SecRef.setEntity(classification.getReference());
385
		} else  {
386
			classification = HibernateProxyHelper
387
					.deproxy(parentTreeNode.getClassification());
388
//			if (selection_classification != null){
389
//			    selection_classification.setEntity(classification);
390
//			}
391
			selection_parentTaxonNode.setEntity(HibernateProxyHelper
392
                    .deproxy(parentTreeNode));
393
			selection_parentTaxonNode.setClassification(classification);
394
			selection_SecRef.setEntity(parentTreeNode.getTaxon().getSec());
395

    
396
		}
397
	}
398

    
399
	private void setTreeNode(TaxonNode treeNode) {
400
        classification = HibernateProxyHelper
401
                    .deproxy(treeNode.getClassification());
402
        if (isCreateNew()){
403
           //selection_classification.setEntity(classification);
404
            selection_parentTaxonNode.setEntity(treeNode.getParent());
405
            selection_parentTaxonNode.setClassification(treeNode.getParent().getClassification());
406
            selection_SecRef.setEntity(treeNode.getTaxon().getSec());
407
        }
408
	}
409

    
410
	private void setTaxon(Taxon taxon) {
411
		this.taxon = taxon;
412
		getEntity().setTaxon(taxon);
413
		this.set_publish(taxon.isPublish());
414
		if (isCreateNew()){
415
		    textNewTaxonName.setText(taxon.getName().getTitleCache());
416
		    if (taxon.getSec() != null){
417
		        selection_SecRef.setEntity(taxon.getSec());
418
		    }
419
		}else{
420
		    if (taxon.getSec() != null) {
421
                this.textTaxonSec.setText(taxon.getSec().getTitleCache());
422
            }
423
		}
424

    
425

    
426
	}
427

    
428
	private void setTaxon(TaxonName taxonName) {
429
		Reference secundum = null;
430
		if (getParentTreeNode() != null) {
431
			if (this.selection_SecRef.getEntity() != null){
432
				secundum = this.selection_SecRef.getEntity();
433
			}
434
		}
435
		if (taxonName != null){
436
		    taxon = Taxon.NewInstance(taxonName, secundum);
437
		    if (textNewTaxonName.getText() == null || !taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
438
		    	 textNewTaxonName.setText(taxonName.getTitleCache());
439
		    }
440
		    getEntity().setTaxon(taxon);
441
//		    if (!taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
442
//		        textNewTaxonName.setText(taxonName.getTitleCache());
443
//		    }
444
		} else {
445
		    textNewTaxonName.setText(null);
446
		}
447
	}
448

    
449
//	private void setOpenInEditor(boolean openInEditor) {
450
//		this.openInEditor = openInEditor;
451
//	}
452

    
453
	public String getTaxonName() {
454
        return textNewTaxonName.getText();
455
    }
456
	public Taxon getTaxon() {
457
        return taxon;
458
    }
459

    
460
	public Reference getReference(){
461
	    return selection_SecRef.getSelection();
462
	}
463

    
464
	public Control getMainControl(){
465
	    return textNewTaxonName.getMainControl();
466
	}
467

    
468
	public boolean isComplete() {
469
	    boolean allComplete = true;
470
	    if (!isCreateNew()){
471
	        for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
472
	            allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
473
	        }
474
	        complete = allComplete;
475
	    }
476
		return complete;
477
	}
478

    
479
    public boolean isCreateNew() {
480
        return createNew;
481
    }
482

    
483
    public boolean isExcluded() {
484
        return excluded;
485
    }
486

    
487
    public boolean isUnplaced() {
488
        return unplaced;
489
    }
490

    
491
    public boolean is_publish() {
492
        boolean isPublishPreference = true;
493
        CdmPreference defaultPublish = PreferencesUtil.getPreferenceFromDB(PreferencePredicate.DefaultBehaviourForPublishFlag);
494
        if (defaultPublish != null && defaultPublish.getValue().equals(PublishEnum.NotPublish.getKey())){
495
            isPublishPreference = false;
496
        }
497
        return checkbox_publish!=null?checkbox_publish.getSelection():isPublishPreference;
498
    }
499

    
500
    public void set_publish(boolean publish) {
501
        this.checkbox_publish.setSelection(publish);
502
    }
503

    
504

    
505
}
(10-10/12)