Project

General

Profile

Download (20.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.ArrayList;
13
import java.util.Collection;
14
import java.util.HashMap;
15
import java.util.Map;
16

    
17
import org.apache.commons.lang.StringUtils;
18
import org.eclipse.swt.SWT;
19
import org.eclipse.swt.widgets.Control;
20
import org.eclipse.swt.widgets.Text;
21

    
22
import eu.etaxonomy.cdm.common.CdmUtils;
23
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
24
import eu.etaxonomy.cdm.model.common.Language;
25
import eu.etaxonomy.cdm.model.common.LanguageString;
26
import eu.etaxonomy.cdm.model.metadata.CdmPreference;
27
import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
28
import eu.etaxonomy.cdm.model.metadata.PublishEnum;
29
import eu.etaxonomy.cdm.model.name.TaxonName;
30
import eu.etaxonomy.cdm.model.reference.Reference;
31
import eu.etaxonomy.cdm.model.taxon.Classification;
32
import eu.etaxonomy.cdm.model.taxon.Taxon;
33
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
34
import eu.etaxonomy.cdm.model.taxon.TaxonNodeStatus;
35
import eu.etaxonomy.taxeditor.preference.CdmPreferenceCache;
36
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
37
import eu.etaxonomy.taxeditor.store.CdmStore;
38
import eu.etaxonomy.taxeditor.store.StoreUtil;
39
import eu.etaxonomy.taxeditor.ui.combo.EnumComboElement;
40
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
41
import eu.etaxonomy.taxeditor.ui.element.CheckboxElement;
42
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
43
import eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement;
44
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
45
import eu.etaxonomy.taxeditor.ui.element.MultilanguageTextElement;
46
import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
47
import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
48
import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
49
import eu.etaxonomy.taxeditor.ui.selection.TaxonNodeSelectionElement;
50

    
51
/**
52
 *
53
 * @author n.hoffmann
54
 * @created Sep 27, 2010
55
 * @version 1.0
56
 */
57
public class TaxonNodeDetailElement extends AbstractCdmDetailElement<TaxonNode> {
58

    
59
    private boolean createNew = false;
60
	private Classification classification;
61
	private TaxonNode parentNode;
62
	private Taxon taxon;
63

    
64
	private TaxonNodeSelectionElement selection_parentTaxonNode;
65
	private EntitySelectionElement<Taxon> selection_reuseExistingTaxon;
66
	private EntitySelectionElement<TaxonName> selection_reuseExistingName;
67

    
68
    private EntitySelectionElement<Reference> selection_SecRef;
69
	private EntitySelectionElement<Reference> selection_Ref;
70

    
71

    
72
    TextWithLabelElement textTaxonSec;
73

    
74
	private TextWithLabelElement microReference;
75

    
76

    
77
    private TextWithLabelElement textNewTaxonName;
78

    
79
	private TaxonNodeAgentRelationCollectionSection selectionNodeAgentRelation;
80

    
81
	private EnumComboElement combo_status;
82
//	private CheckboxElement checkbox_excluded;
83
	private CheckboxElement checkbox_publish;
84
//	private CheckboxElement checkbox_placementDoubtful;
85

    
86
    private MultilanguageTextElement multiLanguageTextStatusNotes;
87
    private Map<Language, LanguageString> multilanguageTextCache;
88

    
89

    
90
	private boolean complete;
91

    
92
	private TaxonNodeStatus status = null;
93

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

    
100

    
101
	public Map<Language, LanguageString> getMultiLanguageTextExcludedNotes() {
102
        return multiLanguageTextStatusNotes.getMultilanguageText();
103
    }
104

    
105

    
106

    
107
    public boolean isExcluded() {
108
        if (status == null){
109
            return false;
110
        }
111
        return status.equals(TaxonNodeStatus.EXCLUDED);
112
    }
113

    
114

    
115

    
116
    public boolean isUnplaced() {
117
        if (status == null){
118
            return false;
119
        }
120
        return status.equals(TaxonNodeStatus.UNPLACED);
121
    }
122

    
123

    
124

    
125
    public boolean isPlacementDoubtful() {
126
        if (status == null){
127
            return false;
128
        }
129
        return status.equals(TaxonNodeStatus.DOUBTFUL);
130
    }
131

    
132
    public Reference getRef() {
133
        return selection_Ref.getEntity();
134
    }
135
    public TaxonName getExistingName() {
136
        return selection_reuseExistingName.getEntity();
137
    }
138
    public String getMicroReference() {
139
        return microReference.getText();
140
    }
141

    
142
	/** {@inheritDoc} */
143
	@Override
144
	protected void createControls(ICdmFormElement formElement,
145
	        TaxonNode entity, int style) {
146
//	    taxon = entity.getTaxon();
147

    
148
	    if (isCreateNew()){
149
//	        setParentTreeNode(entity);
150
    		selection_parentTaxonNode = formFactory
151
    				.createTaxonNodeSelectionElement(getConversationHolder(), formElement, "Parent", parentNode,
152
    						EntitySelectionElement.DELETABLE, style, 100);
153

    
154
    		selection_Ref = formFactory
155
                    .createSelectionElement(Reference.class,
156
                            formElement, "Parent Child Relation Source", null,
157
                            EntitySelectionElement.DELETABLE, style, 100);
158

    
159
            microReference = formFactory.createTextWithLabelElement(formElement,"Detail", "", style);
160
            microReference.setText(parentNode.getMicroReference());
161

    
162
    		selection_reuseExistingTaxon = formFactory
163
    				.createSelectionElement(Taxon.class,//getConversationHolder(),
164
    				        formElement,
165
    						"Reuse existing taxon", null,
166
    						EntitySelectionElement.DELETABLE, style);
167

    
168
    		selection_reuseExistingName = formFactory
169
    				.createSelectionElement(TaxonName.class,//getConversationHolder(),
170
    				        formElement,
171
    						"Reuse existing name", taxon != null? taxon.getName(): null,
172
    						EntitySelectionElement.DELETABLE, style);
173
    		selection_SecRef = formFactory
174
                    .createSelectionElement(Reference.class,//getConversationHolder(),
175
                            formElement, "Secundum Reference", taxon != null? taxon.getSec(): null,
176
                            EntitySelectionElement.DELETABLE, style, 100);
177

    
178
    		if (isCreateNew()){
179
    		    textNewTaxonName = formFactory.createTextWithLabelElement(formElement,"New Taxon", "", style);
180
    		    textNewTaxonName.setFocus();
181
    		} else{
182
    		    textNewTaxonName = formFactory.createTextWithLabelElement(formElement,"Taxon", "", style);
183

    
184
    		}
185
	    }else{
186
	        taxon = getEntity().getTaxon();
187
	        if (entity.getParent().getTaxon() == null){
188
	            TextWithLabelElement textParent = formFactory.createTextWithLabelElement(formElement,"Classification", entity.getClassification().getTitleCache(), style);
189
                textParent.setEnabled(false);
190
	        }else{
191
	            TextWithLabelElement textParent = formFactory.createTextWithLabelElement(formElement,"Parent", entity.getParent().getTaxon().getTitleCache(), style);
192
	            textParent.setEnabled(false);
193
	        }
194

    
195
	        selection_Ref = formFactory
196
                    .createSelectionElement(Reference.class,
197
                            formElement, "Parent Child Relation Source", null,
198
                            EntitySelectionElement.DELETABLE, style);
199

    
200
	        selection_reuseExistingTaxon = formFactory
201
                  .createSelectionElement(Taxon.class,//getConversationHolder(),
202
                          formElement,
203
                          "Taxon", taxon,
204
                          EntitySelectionElement.DELETABLE, style);
205

    
206
	        textTaxonSec = formFactory.createTextWithLabelElement(formElement,"Secundum Reference", "", style);
207
            if (entity.getTaxon().getSec() != null){
208
                textTaxonSec.setText(taxon.getSec().getTitleCache());
209
            }
210
            textTaxonSec.setEnabled(false);
211

    
212

    
213

    
214
	    }
215

    
216

    
217
        if (!isCreateNew()){
218
            if (entity.getReference() != null){
219
                selection_Ref.setEntity(entity.getReference());
220
            }
221

    
222
        }
223

    
224

    
225
        Taxon tax = null;
226
        if (entity.getTaxon() != null){
227
            tax = entity.getTaxon();
228
        }
229
        checkbox_publish = formFactory.createCheckbox(formElement,
230
                "Taxon is published", tax != null? tax.isPublish(): true, style);
231

    
232

    
233
        checkbox_publish.setEnabled(isCreateNew());
234

    
235
        combo_status = formFactory.createEnumComboElement(TaxonNodeStatus.class, formElement, style, true);
236
        if (!isCreateNew()){
237
            combo_status.setSelection(entity.getStatus());
238
        }
239

    
240
        if(isCreateNew()){
241

    
242

    
243
		   String defaultPublish = PreferencesUtil.getStringValue(PreferencePredicate.DefaultBehaviourForPublishFlag.getKey());
244
	        if (defaultPublish != null && defaultPublish.equals(PublishEnum.Publish.getKey())){
245
	            checkbox_publish.setSelection(true);
246
	        } else if (defaultPublish != null && defaultPublish.equals(PublishEnum.NotPublish.getKey())){
247
	            checkbox_publish.setSelection(false);
248
	        }else{
249
	            if(this.getParentTreeNode() != null &&  this.getParentTreeNode().isInstanceOf(TaxonNode.class) && this.getParentTreeNode().getTaxon() != null){
250
	                checkbox_publish.setSelection(this.getParentTreeNode().getTaxon().isPublish());
251
	            }else{
252
	                checkbox_publish.setSelection(true);
253
	            }
254
	        }
255

    
256
	    } else{
257
            setTreeNode(entity);
258
            complete = true;
259
        }
260

    
261

    
262
		if (isCreateNew()){
263
		    preFillTaxonName();
264
		}
265

    
266

    
267
	    multiLanguageTextStatusNotes = formFactory.createMultiLanguageTextElement(formElement, "Status Notes", null, 50, style);
268
	    if (!isCreateNew()){
269
	        multiLanguageTextStatusNotes.setMultilanguageText(entity.getStatusNote());
270
	        multiLanguageTextStatusNotes.setEnabled(entity.getStatus() != null);
271
	    }
272

    
273

    
274
		if (!isCreateNew()){
275
		    selectionNodeAgentRelation = formFactory.createTaxonNodeAgentRelationCollectionSection(formElement, getConversationHolder(), StoreUtil.getSectionStyle(TaxonNodeAgentRelationCollectionSection.class, entity.getClass().getCanonicalName()));
276
		    selectionNodeAgentRelation.setEntity(entity);
277
		    selectionNodeAgentRelation.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
278
		}
279

    
280
	}
281

    
282
	private void preFillTaxonName() {
283
        TaxonNode node = parentNode;
284
        if(node.getTaxon()!=null){
285
            Taxon taxon = HibernateProxyHelper.deproxy(node.getTaxon(), Taxon.class);
286
            if(taxon.getName()!=null){
287
                TaxonName name = HibernateProxyHelper.deproxy(node.getTaxon().getName());
288
                if (!isCreateNew()){
289
                    textNewTaxonName.setText(name.getNameCache());
290
                } else if( ! name.isSupraGeneric() && name.getRank() != null){
291
                    String taxonName = "";
292
                    if(name.isGenus() || name.isInfraGeneric()|| name.isSpeciesAggregate() ){
293
                        taxonName = name.getGenusOrUninomial();
294
                    }
295
                    else if(name.isSpecies() || name.isInfraSpecific() ){
296
                        taxonName = CdmUtils.concat(" ", name.getGenusOrUninomial(),name.getSpecificEpithet());
297
                    }
298
                    if (StringUtils.isNotBlank(taxonName)){
299
                    	textNewTaxonName.setText(taxonName + " ");
300
                    	if(textNewTaxonName.getMainControl() instanceof Text){
301
                    		Text text = (Text)textNewTaxonName.getMainControl();
302
                    		text.setSelection(textNewTaxonName.getText().length());
303
                    	}
304
                    }
305
                }
306
            }
307
        }
308

    
309
    }
310

    
311
	/** {@inheritDoc} */
312
	@Override
313
	public void handleEvent(Object eventSource) {
314
		if (eventSource == selection_parentTaxonNode) {
315
		    setParentTreeNode(selection_parentTaxonNode.getEntity());
316
		} else if (eventSource == selection_reuseExistingTaxon) {
317
		    if (selection_reuseExistingTaxon.getEntity() != null){
318
		        setTaxon(selection_reuseExistingTaxon.getEntity());
319
		        if (getTaxon().getSec()!= null){
320
		            if (isCreateNew()){
321
		               selection_SecRef.setEntity(getTaxon().getSec());
322
		            }else{
323
		               textTaxonSec.setText((getTaxon().getSec().getTitleCache()));
324
		            }
325
		        }else{
326
		            if (isCreateNew()){
327
	                       selection_SecRef.setEntity(null);
328
	                    }else{
329
	                       textTaxonSec.setText("");
330
	                    }
331
		        }
332
		        checkbox_publish.setSelected(getTaxon().isPublish());
333
            }
334
		    boolean enabled = selection_reuseExistingTaxon.getEntity() == null;
335
		    if (selection_reuseExistingName != null){
336
		        selection_reuseExistingName.setEnabled(enabled);
337
            }
338
		    if (textNewTaxonName != null){
339
		        textNewTaxonName.setEnabled(enabled);
340
		    }
341
			complete = true;
342
		} else if (eventSource == selection_reuseExistingName) {
343
		    boolean enabled = selection_reuseExistingName.getEntity() == null;
344
			setTaxon(HibernateProxyHelper.deproxy(selection_reuseExistingName.getEntity()));
345
			if (selection_reuseExistingTaxon != null){
346
			    selection_reuseExistingTaxon.setEnabled(enabled);
347
			}
348
			textNewTaxonName.setEnabled(enabled);
349
			complete = !textNewTaxonName.getText().isEmpty();
350
		}
351

    
352
        if (eventSource == selection_Ref) {
353
            if(!isCreateNew()){
354
                getEntity().setReference(selection_Ref.getEntity());
355
            }
356
        }else if (eventSource == selection_SecRef) {
357
            if (taxon != null){
358
                taxon.setSec(selection_SecRef.getEntity());
359
            }
360
        }else if (eventSource == microReference) {
361
            if(!isCreateNew()){
362
                getEntity().setMicroReference(microReference.getText());
363
            }
364
        }  else if (eventSource == checkbox_publish) {
365
            if (taxon != null){
366
                taxon.setPublish(checkbox_publish.getSelection());
367
            }
368
        }
369

    
370
		if (eventSource == combo_status) {
371
            status = (TaxonNodeStatus) combo_status.getSelection();
372
            if (status == null){
373
                if (multiLanguageTextStatusNotes.getMultilanguageText() != null && !multiLanguageTextStatusNotes.getMultilanguageText().isEmpty()){
374
                    multilanguageTextCache = new HashMap<>();
375
                    for (LanguageString langString: multiLanguageTextStatusNotes.getMultilanguageText().values()){
376
                        multilanguageTextCache.put(langString.getLanguage(), langString);
377
                    }
378
//                    multilanguageTextCache = multiLanguageTextStatusNotes.getMultilanguageText();
379
                }
380
                multiLanguageTextStatusNotes.setMultilanguageText(new HashMap<>());
381
//                multiLanguageTextStatusNotes.refresh();
382

    
383
            }else if (multilanguageTextCache != null ){
384

    
385
                multiLanguageTextStatusNotes.setMultilanguageText(multilanguageTextCache);
386
                multilanguageTextCache = null;
387
            }
388
            if (!isCreateNew()){
389
                getEntity().setStatus(status);
390
                if (status == null){
391
                    getEntity().getStatusNote().clear();
392
                    multiLanguageTextStatusNotes.setMultilanguageText(new HashMap<>());
393
                    multiLanguageTextStatusNotes.refresh();
394
                }else{
395
                    if (multiLanguageTextStatusNotes.getMultilanguageText() != null){
396
                        for (LanguageString lang:multiLanguageTextStatusNotes.getMultilanguageText().values()){
397
                            getEntity().putStatusNote(lang);
398
                        }
399
                    }
400
                }
401
            }
402

    
403

    
404
            multiLanguageTextStatusNotes.setEnabled(status != null);
405

    
406
            complete = true;
407
        }
408

    
409
		if (eventSource == textNewTaxonName) {
410
            boolean enabled = CdmUtils.isBlank(textNewTaxonName.getText());
411
            if (selection_reuseExistingTaxon != null){
412
                selection_reuseExistingTaxon.setEnabled(enabled);
413
            }
414
            selection_reuseExistingName.setEnabled(enabled);
415

    
416
            complete = CdmUtils.isNotBlank(textNewTaxonName.getText());
417
        }
418

    
419
		if (eventSource == selectionNodeAgentRelation){
420
		   boolean allComplete = true;
421
		   for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
422
		       allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
423
		   }
424
		    complete = !isCreateNew() && allComplete;
425
		}
426
	}
427

    
428
	public Classification getClassification() {
429
		return classification;
430
	}
431

    
432

    
433

    
434
	public TaxonNode getParentTreeNode() {
435
		return parentNode;
436
	}
437

    
438
	public void setParentTreeNode(TaxonNode parentTreeNode) {
439
		this.parentNode = parentTreeNode;
440
		updateContent();
441
		if (parentTreeNode.getTaxon() == null) {
442
			classification = parentTreeNode.getClassification();
443
			if (selection_parentTaxonNode != null){
444
    			selection_parentTaxonNode.setEntity(classification.getRootNode());
445
    			selection_parentTaxonNode.setClassification(classification);
446
    			selection_SecRef.setEntity(classification.getReference());
447
			}
448
		} else  {
449
			classification = HibernateProxyHelper
450
					.deproxy(parentTreeNode.getClassification());
451
			if (selection_parentTaxonNode != null){
452
			    selection_parentTaxonNode.setEntity(HibernateProxyHelper.deproxy(parentTreeNode));
453
			    selection_parentTaxonNode.setClassification(classification);
454
	            selection_SecRef.setEntity(parentTreeNode.getTaxon().getSec());
455
			}
456

    
457

    
458
		}
459
	}
460
	@Override
461
	protected void updateControlStates(){
462
        Collection<Object> except = new ArrayList<Object>();
463
        for(ICdmFormElement formElement:getElements()){
464
            if(formElement instanceof IEnableableFormElement && !((IEnableableFormElement) formElement).isEnabled()){
465
                except.add(formElement);
466
            }
467
        }
468
        if (isCreateNew() ){
469
            enabled = true;
470
        }else{
471
            enabled = getEntity() != null && CdmStore.currentAuthentiationHasPermission(StoreUtil.getCdmEntity(getEntity()), requiredCrud);
472
        }
473
        setEnabled(enabled, except);
474
    }
475

    
476
	private void setTreeNode(TaxonNode treeNode) {
477
        classification = HibernateProxyHelper
478
                    .deproxy(treeNode.getClassification());
479
        if (isCreateNew()){
480
           selection_parentTaxonNode.setEntity(treeNode.getParent());
481
           selection_parentTaxonNode.setClassification(treeNode.getParent().getClassification());
482
           selection_SecRef.setEntity(treeNode.getTaxon().getSec());
483
        }
484
	}
485

    
486
	private void setTaxon(Taxon taxon) {
487
		this.taxon = taxon;
488
		if (!isCreateNew()){
489
		    getEntity().setTaxon(taxon);
490
		}
491
		this.set_publish(taxon.isPublish());
492
		if (isCreateNew()){
493
		    textNewTaxonName.setText(taxon.getName().getTitleCache());
494
		    if (taxon.getSec() != null){
495
		        selection_SecRef.setEntity(taxon.getSec());
496
		    }
497
		}else{
498
		    if (taxon.getSec() != null) {
499
                this.textTaxonSec.setText(taxon.getSec().getTitleCache());
500
            }
501
		}
502

    
503

    
504
	}
505

    
506
	private void setTaxon(TaxonName taxonName) {
507
		Reference secundum = null;
508
		if (getParentTreeNode() != null) {
509
			if (this.selection_SecRef.getEntity() != null){
510
				secundum = this.selection_SecRef.getEntity();
511
			}
512
		}
513
		if (taxonName != null){
514
		    taxon = Taxon.NewInstance(taxonName, secundum);
515
		    if (textNewTaxonName.getText() == null || !taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
516
		    	 textNewTaxonName.setText(taxonName.getTitleCache());
517
		    }
518
		    if (!isCreateNew()){
519
		        getEntity().setTaxon(taxon);
520
		    }
521

    
522
		} else {
523
		    textNewTaxonName.setText(null);
524
		}
525
	}
526

    
527
	public String getTaxonName() {
528
        return textNewTaxonName.getText();
529
    }
530
	public Taxon getTaxon() {
531
        return taxon;
532
    }
533

    
534
	public Reference getSecReference(){
535
	    return selection_SecRef.getSelection();
536
	}
537

    
538
	public Control getMainControl(){
539
	    return textNewTaxonName.getMainControl();
540
	}
541

    
542
	public boolean isComplete() {
543
	    boolean allComplete = true;
544
	    if (!isCreateNew()){
545
	        for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
546
	            allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
547
	        }
548
	        complete = allComplete;
549
	    }
550
		return complete;
551
	}
552

    
553
    public boolean isCreateNew() {
554
        return createNew;
555
    }
556

    
557
    public boolean is_publish() {
558
        boolean isPublishPreference = true;
559
        CdmPreference defaultPublish = CdmPreferenceCache.instance().get(PreferencePredicate.DefaultBehaviourForPublishFlag.getKey());
560
        if (defaultPublish != null && defaultPublish.getValue()!= null && defaultPublish.getValue().equals(PublishEnum.NotPublish.getKey())){
561
            isPublishPreference = false;
562
        }
563
        return checkbox_publish!=null?checkbox_publish.getSelection():isPublishPreference;
564
    }
565

    
566
    public void set_publish(boolean publish) {
567
        this.checkbox_publish.setSelection(publish);
568
    }
569

    
570
    /**
571
     * Updates all widgets to display the latest data
572
     */
573
    @Override
574
    protected void updateContent() {
575
        removeElements();
576
        if (isCreateNew()){
577
            createControls(this, parentNode, SWT.NONE);
578
        }else{
579
            createControls(this, getEntity(), SWT.NONE);
580
        }
581
        updateControlStates();
582
    }
583

    
584

    
585
}
(10-10/12)