Project

General

Profile

Download (20.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.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
 * @author n.hoffmann
53
 * @created Sep 27, 2010
54
 */
55
public class TaxonNodeDetailElement extends AbstractCdmDetailElement<TaxonNode> {
56

    
57
    private boolean createNew = false;
58
	private Classification classification;
59
	private TaxonNode parentNode;
60
	private Taxon taxon;
61
    private boolean complete;
62
    private TaxonNodeStatus status = null;
63
    private Map<Language, LanguageString> multilanguageTextCache;
64

    
65
	private TaxonNodeSelectionElement selection_parentTaxonNode;
66
	private EntitySelectionElement<Taxon> selection_reuseExistingTaxon;
67
	private EntitySelectionElement<TaxonName> selection_reuseExistingName;
68
    private EntitySelectionElement<Reference> selection_SecRef;
69
	private EntitySelectionElement<Reference> selection_Ref;
70
    private TextWithLabelElement textTaxonSec;
71
	private TextWithLabelElement microReference;
72
    private TextWithLabelElement textNewTaxonName;
73
	private TaxonNodeAgentRelationCollectionSection selectionNodeAgentRelation;
74
	private EnumComboElement<TaxonNodeStatus> combo_status;
75
	private CheckboxElement checkbox_publish;
76
    private MultilanguageTextElement multiLanguageTextStatusNotes;
77

    
78
	public TaxonNodeDetailElement(CdmFormFactory formFactory,
79
			ICdmFormElement formElement, boolean isCreateNew) {
80
		super(formFactory, formElement);
81
		createNew = isCreateNew;
82
	}
83

    
84
	public Map<Language, LanguageString> getMultiLanguageTextExcludedNotes() {
85
        return multiLanguageTextStatusNotes.getMultilanguageText();
86
    }
87

    
88
    public boolean isExcluded() {
89
        return status == null? false : status.equals(TaxonNodeStatus.EXCLUDED);
90
    }
91

    
92
    public boolean isUnplaced() {
93
        return status == null? false : status.equals(TaxonNodeStatus.UNPLACED);
94
    }
95

    
96
    public boolean isPlacementDoubtful() {
97
        return status == null? false : status.equals(TaxonNodeStatus.DOUBTFUL);
98
    }
99

    
100
    public Reference getRef() {
101
        return selection_Ref.getEntity();
102
    }
103
    public TaxonName getExistingName() {
104
        return selection_reuseExistingName.getEntity();
105
    }
106
    public String getMicroReference() {
107
        return microReference.getText();
108
    }
109

    
110
	@Override
111
	protected void createControls(ICdmFormElement formElement,
112
	        TaxonNode entity, int style) {
113
//	    taxon = entity.getTaxon();
114

    
115
	    if (isCreateNew()){
116
//	        setParentTreeNode(entity);
117
    		selection_parentTaxonNode = formFactory
118
    				.createTaxonNodeSelectionElement(getConversationHolder(), formElement, "Parent", parentNode,
119
    						EntitySelectionElement.DELETABLE, style, 100);
120

    
121
    		selection_Ref = formFactory
122
                    .createSelectionElement(Reference.class,
123
                            formElement, "Parent Child Relation Source", null,
124
                            EntitySelectionElement.DELETABLE, style, 100);
125

    
126
            microReference = formFactory.createTextWithLabelElement(formElement,"Detail", "", style);
127
            microReference.setText(parentNode.getMicroReference());
128

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

    
135
    		selection_reuseExistingName = formFactory
136
    				.createSelectionElement(TaxonName.class,//getConversationHolder(),
137
    				        formElement,
138
    						"Reuse existing name", taxon != null? taxon.getName(): null,
139
    						EntitySelectionElement.DELETABLE, style);
140
    		selection_SecRef = formFactory
141
                    .createSelectionElement(Reference.class,//getConversationHolder(),
142
                            formElement, "Secundum Reference", taxon != null? taxon.getSec(): null,
143
                            EntitySelectionElement.DELETABLE, style, 100);
144

    
145
    		if (isCreateNew()){
146
    		    textNewTaxonName = formFactory.createTextWithLabelElement(formElement,"New Taxon", "", style);
147
    		    textNewTaxonName.setFocus();
148
    		} else{
149
    		    textNewTaxonName = formFactory.createTextWithLabelElement(formElement,"Taxon", "", style);
150
    		}
151
	    }else{
152
	        taxon = getEntity().getTaxon();
153
	        if (entity.getParent().getTaxon() == null){
154
	            TextWithLabelElement textParent = formFactory.createTextWithLabelElement(formElement,"Classification", entity.getClassification().getTitleCache(), style);
155
                textParent.setEnabled(false);
156
	        }else{
157
	            TextWithLabelElement textParent = formFactory.createTextWithLabelElement(formElement,"Parent", entity.getParent().getTaxon().getTitleCache(), style);
158
	            textParent.setEnabled(false);
159
	        }
160

    
161
	        selection_Ref = formFactory
162
                    .createSelectionElement(Reference.class,
163
                            formElement, "Parent Child Relation Source", null,
164
                            EntitySelectionElement.DELETABLE, style);
165

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

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

    
179
        if (!isCreateNew()){
180
            if (entity.getReference() != null){
181
                selection_Ref.setEntity(entity.getReference());
182
            }
183
        }
184

    
185
        Taxon tax = null;
186
        if (entity.getTaxon() != null){
187
            tax = entity.getTaxon();
188
        }
189
        checkbox_publish = formFactory.createCheckbox(formElement,
190
                "Taxon is published", tax != null? tax.isPublish(): true, style);
191

    
192
        checkbox_publish.setEnabled(isCreateNew());
193

    
194
        combo_status = formFactory.createEnumComboElement(TaxonNodeStatus.class, formElement, style, true);
195
        if (!isCreateNew()){
196
            combo_status.setSelection(entity.getStatus());
197
        }
198

    
199
        if(isCreateNew()){
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) && this.getParentTreeNode().getTaxon() != null){
208
	                checkbox_publish.setSelection(this.getParentTreeNode().getTaxon().isPublish());
209
	            }else{
210
	                checkbox_publish.setSelection(true);
211
	            }
212
	        }
213
	    } else{
214
            setTreeNode(entity);
215
            complete = true;
216
        }
217

    
218
		if (isCreateNew()){
219
		    preFillTaxonName();
220
		}
221

    
222
	    multiLanguageTextStatusNotes = formFactory.createMultiLanguageTextElement(formElement, "Status Notes", null, 50, style);
223
	    if (!isCreateNew()){
224
	        multiLanguageTextStatusNotes.setMultilanguageText(entity.getStatusNote());
225
	        multiLanguageTextStatusNotes.setEnabled(entity.getStatus() != null);
226
	    }
227

    
228
		if (!isCreateNew()){
229
		    selectionNodeAgentRelation = formFactory.createTaxonNodeAgentRelationCollectionSection(formElement, getConversationHolder(), StoreUtil.getSectionStyle(TaxonNodeAgentRelationCollectionSection.class, entity.getClass().getCanonicalName()));
230
		    selectionNodeAgentRelation.setEntity(entity);
231
		    selectionNodeAgentRelation.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
232
		}
233
	}
234

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

    
263
	@Override
264
	public void handleEvent(Object eventSource) {
265
		if (eventSource == selection_parentTaxonNode) {
266
		    setParentTreeNode(selection_parentTaxonNode.getEntity());
267
		} else if (eventSource == selection_reuseExistingTaxon) {
268
		    if (selection_reuseExistingTaxon.getEntity() != null){
269
		        setTaxon(selection_reuseExistingTaxon.getEntity());
270
		        if (getTaxon().getSec()!= null){
271
		            if (isCreateNew()){
272
		               selection_SecRef.setEntity(getTaxon().getSec());
273
		            }else{
274
		               textTaxonSec.setText((getTaxon().getSec().getTitleCache()));
275
		            }
276
		        }else{
277
		            if (isCreateNew()){
278
	                       selection_SecRef.setEntity(null);
279
	                    }else{
280
	                       textTaxonSec.setText("");
281
	                    }
282
		        }
283
		        checkbox_publish.setSelected(getTaxon().isPublish());
284
            }
285
		    boolean enabled = selection_reuseExistingTaxon.getEntity() == null;
286
		    if (selection_reuseExistingName != null){
287
		        selection_reuseExistingName.setEnabled(enabled);
288
            }
289
		    if (textNewTaxonName != null){
290
		        textNewTaxonName.setEnabled(enabled);
291
		    }
292
			complete = true;
293
		} else if (eventSource == selection_reuseExistingName) {
294
		    boolean enabled = selection_reuseExistingName.getEntity() == null;
295
			setTaxon(HibernateProxyHelper.deproxy(selection_reuseExistingName.getEntity()));
296
			if (selection_reuseExistingTaxon != null){
297
			    selection_reuseExistingTaxon.setEnabled(enabled);
298
			}
299
			textNewTaxonName.setEnabled(enabled);
300
			complete = !textNewTaxonName.getText().isEmpty();
301
		}
302

    
303
        if (eventSource == selection_Ref) {
304
            if(!isCreateNew()){
305
                getEntity().setReference(selection_Ref.getEntity());
306
            }
307
        }else if (eventSource == selection_SecRef) {
308
            if (taxon != null){
309
                taxon.setSec(selection_SecRef.getEntity());
310
            }
311
        }else if (eventSource == microReference) {
312
            if(!isCreateNew()){
313
                getEntity().setMicroReference(microReference.getText());
314
            }
315
        }  else if (eventSource == checkbox_publish) {
316
            if (taxon != null){
317
                taxon.setPublish(checkbox_publish.getSelection());
318
            }
319
        }
320

    
321
		if (eventSource == combo_status) {
322
            status = combo_status.getSelection();
323
            if (status == null){
324
                if (multiLanguageTextStatusNotes.getMultilanguageText() != null && !multiLanguageTextStatusNotes.getMultilanguageText().isEmpty()){
325
                    multilanguageTextCache = new HashMap<>();
326
                    for (LanguageString langString: multiLanguageTextStatusNotes.getMultilanguageText().values()){
327
                        multilanguageTextCache.put(langString.getLanguage(), langString);
328
                    }
329
//                    multilanguageTextCache = multiLanguageTextStatusNotes.getMultilanguageText();
330
                }
331
                multiLanguageTextStatusNotes.setMultilanguageText(new HashMap<>());
332
//                multiLanguageTextStatusNotes.refresh();
333

    
334
            }else if (multilanguageTextCache != null ){
335

    
336
                multiLanguageTextStatusNotes.setMultilanguageText(multilanguageTextCache);
337
                multilanguageTextCache = null;
338
            }
339
            if (!isCreateNew()){
340
                getEntity().setStatus(status);
341
                if (status == null){
342
                    getEntity().getStatusNote().clear();
343
                    multiLanguageTextStatusNotes.setMultilanguageText(new HashMap<>());
344
                    multiLanguageTextStatusNotes.refresh();
345
                }else{
346
                    if (multiLanguageTextStatusNotes.getMultilanguageText() != null){
347
                        for (LanguageString lang:multiLanguageTextStatusNotes.getMultilanguageText().values()){
348
                            getEntity().putStatusNote(lang);
349
                        }
350
                    }
351
                }
352
            }
353

    
354
            multiLanguageTextStatusNotes.setEnabled(status != null);
355

    
356
            complete = true;
357
        }
358

    
359
		if (eventSource == textNewTaxonName) {
360
            boolean enabled = CdmUtils.isBlank(textNewTaxonName.getText());
361
            if (selection_reuseExistingTaxon != null){
362
                selection_reuseExistingTaxon.setEnabled(enabled);
363
            }
364
            selection_reuseExistingName.setEnabled(enabled);
365

    
366
            complete = CdmUtils.isNotBlank(textNewTaxonName.getText());
367
        }
368

    
369
		if (eventSource == selectionNodeAgentRelation){
370
		   boolean allComplete = true;
371
		   for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
372
		       allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
373
		   }
374
		    complete = !isCreateNew() && allComplete;
375
		}
376
	}
377

    
378
	public Classification getClassification() {
379
		return classification;
380
	}
381

    
382
	public TaxonNode getParentTreeNode() {
383
		return parentNode;
384
	}
385

    
386
	public void setParentTreeNode(TaxonNode parentTreeNode) {
387
		this.parentNode = parentTreeNode;
388
		updateContent();
389
		if (parentTreeNode.getTaxon() == null) {
390
			classification = parentTreeNode.getClassification();
391
			if (selection_parentTaxonNode != null){
392
    			selection_parentTaxonNode.setEntity(classification.getRootNode());
393
    			selection_parentTaxonNode.setClassification(classification);
394
    			selection_SecRef.setEntity(classification.getReference());
395
			}
396
		} else  {
397
			classification = HibernateProxyHelper
398
					.deproxy(parentTreeNode.getClassification());
399
			if (selection_parentTaxonNode != null){
400
			    selection_parentTaxonNode.setEntity(HibernateProxyHelper.deproxy(parentTreeNode));
401
			    selection_parentTaxonNode.setClassification(classification);
402
	            selection_SecRef.setEntity(parentTreeNode.getTaxon().getSec());
403
			}
404
		}
405
	}
406

    
407
	@Override
408
	protected void updateControlStates(){
409
        Collection<Object> except = new ArrayList<Object>();
410
        for(ICdmFormElement formElement:getElements()){
411
            if(formElement instanceof IEnableableFormElement && !((IEnableableFormElement) formElement).isEnabled()){
412
                except.add(formElement);
413
            }
414
        }
415
        if (isCreateNew() ){
416
            enabled = true;
417
        }else{
418
            enabled = getEntity() != null && CdmStore.currentAuthentiationHasPermission(StoreUtil.getCdmEntity(getEntity()), requiredCrud);
419
        }
420
        setEnabled(enabled, except);
421
    }
422

    
423
	private void setTreeNode(TaxonNode treeNode) {
424
        classification = HibernateProxyHelper
425
                    .deproxy(treeNode.getClassification());
426
        if (isCreateNew()){
427
           selection_parentTaxonNode.setEntity(treeNode.getParent());
428
           selection_parentTaxonNode.setClassification(treeNode.getParent().getClassification());
429
           selection_SecRef.setEntity(treeNode.getTaxon().getSec());
430
        }
431
	}
432

    
433
	private void setTaxon(Taxon taxon) {
434
		this.taxon = taxon;
435
		if (!isCreateNew()){
436
		    getEntity().setTaxon(taxon);
437
		}
438
		this.set_publish(taxon.isPublish());
439
		if (isCreateNew()){
440
		    textNewTaxonName.setText(taxon.getName().getTitleCache());
441
		    if (taxon.getSec() != null){
442
		        selection_SecRef.setEntity(taxon.getSec());
443
		    }
444
		}else{
445
		    if (taxon.getSec() != null) {
446
                this.textTaxonSec.setText(taxon.getSec().getTitleCache());
447
            }
448
		}
449
	}
450

    
451
	private void setTaxon(TaxonName taxonName) {
452
		Reference secundum = null;
453
		if (getParentTreeNode() != null) {
454
			if (this.selection_SecRef.getEntity() != null){
455
				secundum = this.selection_SecRef.getEntity();
456
			}
457
		}
458
		if (taxonName != null){
459
		    taxon = Taxon.NewInstance(taxonName, secundum);
460
		    if (textNewTaxonName.getText() == null || !taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
461
		    	 textNewTaxonName.setText(taxonName.getTitleCache());
462
		    }
463
		    if (!isCreateNew()){
464
		        getEntity().setTaxon(taxon);
465
		    }
466

    
467
		} else {
468
		    textNewTaxonName.setText(null);
469
		}
470
	}
471

    
472
	public String getTaxonName() {
473
        return textNewTaxonName.getText();
474
    }
475
	public Taxon getTaxon() {
476
        return taxon;
477
    }
478

    
479
	public Reference getSecReference(){
480
	    return selection_SecRef.getSelection();
481
	}
482

    
483
	public Control getMainControl(){
484
	    return textNewTaxonName.getMainControl();
485
	}
486

    
487
	public boolean isComplete() {
488
	    boolean allComplete = true;
489
	    if (!isCreateNew()){
490
	        for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
491
	            allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
492
	        }
493
	        complete = allComplete;
494
	    }
495
		return complete;
496
	}
497

    
498
    public boolean isCreateNew() {
499
        return createNew;
500
    }
501

    
502
    public boolean is_publish() {
503
        boolean isPublishPreference = true;
504
        CdmPreference defaultPublish = CdmPreferenceCache.instance().get(PreferencePredicate.DefaultBehaviourForPublishFlag.getKey());
505
        if (defaultPublish != null && defaultPublish.getValue()!= null && defaultPublish.getValue().equals(PublishEnum.NotPublish.getKey())){
506
            isPublishPreference = false;
507
        }
508
        return checkbox_publish!=null?checkbox_publish.getSelection():isPublishPreference;
509
    }
510

    
511
    public void set_publish(boolean publish) {
512
        this.checkbox_publish.setSelection(publish);
513
    }
514

    
515
    /**
516
     * Updates all widgets to display the latest data
517
     */
518
    @Override
519
    protected void updateContent() {
520
        removeElements();
521
        if (isCreateNew()){
522
            createControls(this, parentNode, SWT.NONE);
523
        }else{
524
            createControls(this, getEntity(), SWT.NONE);
525
        }
526
        updateControlStates();
527
    }
528
}
(10-10/12)