Project

General

Profile

Download (21.1 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.l10n.Messages;
36
import eu.etaxonomy.taxeditor.preference.CdmPreferenceCache;
37
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
38
import eu.etaxonomy.taxeditor.store.CdmStore;
39
import eu.etaxonomy.taxeditor.store.StoreUtil;
40
import eu.etaxonomy.taxeditor.ui.combo.EnumComboElement;
41
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
42
import eu.etaxonomy.taxeditor.ui.element.CheckboxElement;
43
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
44
import eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement;
45
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
46
import eu.etaxonomy.taxeditor.ui.element.MultilanguageTextElement;
47
import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
48
import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
49
import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
50
import eu.etaxonomy.taxeditor.ui.selection.TaxonNodeSelectionElement;
51

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

    
58
    private static final String STATUS_NOTES = Messages.TaxonNodeWizardPage_STATUS_NOTES;
59
    private static final String CLASSIFICATION_STR = Messages.TaxonNodeWizardPage_CLASSIFICATION;
60
    private static final String REUSE_EXISTING_TAXON = Messages.TaxonNodeWizardPage_REUSE_EXISTING_TAXON;
61
    private static final String REUSE_EXISTING_NAME = Messages.TaxonNodeWizardPage_REUSE_EXISTING_NAME;
62
    private static final String SECUNDUM_REFERENCE = Messages.TaxonNodeWizardPage_SECUNDUM_REFERENCE;
63

    
64
    private boolean createNew = false;
65
	private Classification classification;
66
	private TaxonNode parentNode;
67
	private Taxon taxon;
68
    private boolean complete;
69
    private TaxonNodeStatus status = null;
70
    private Map<Language, LanguageString> multilanguageTextCache;
71

    
72
	private TaxonNodeSelectionElement selection_parentTaxonNode;
73
	private EntitySelectionElement<Taxon> selection_reuseExistingTaxon;
74
	private EntitySelectionElement<TaxonName> selection_reuseExistingName;
75
    private EntitySelectionElement<Reference> selection_SecRef;
76
	private EntitySelectionElement<Reference> selection_Ref;
77
    private TextWithLabelElement textTaxonSec;
78
	private TextWithLabelElement microReference;
79
    private TextWithLabelElement textNewTaxonName;
80
	private TaxonNodeAgentRelationCollectionSection selectionNodeAgentRelation;
81
	private EnumComboElement<TaxonNodeStatus> combo_status;
82
	private CheckboxElement checkbox_publish;
83
    private MultilanguageTextElement multiLanguageTextStatusNotes;
84

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

    
91
	public Map<Language, LanguageString> getMultiLanguageTextExcludedNotes() {
92
        return multiLanguageTextStatusNotes.getMultilanguageText();
93
    }
94

    
95
	public TaxonNodeStatus getTaxonNodeStatus(){
96
	    return status;
97
	}
98

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

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

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

    
120
    		selection_Ref = formFactory
121
                    .createSelectionElement(Reference.class,
122
                            formElement, Messages.TaxonNodeWizardPage_PLACEMENT_SOURCE, null,
123
                            EntitySelectionElement.DELETABLE, style, 100);
124

    
125
            microReference = formFactory.createTextWithLabelElement(formElement,
126
                    Messages.TaxonNodeWizardPage_PLACEMENT_SOURCE_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,
147
    		            Messages.TaxonNodeWizardPage_NEW_TAXON, "", style);
148
    		    textNewTaxonName.setFocus();
149
    		} else{
150
    		    textNewTaxonName = formFactory.createTextWithLabelElement(formElement,
151
    		            Messages.TaxonNodeWizardPage_TAXON, "", style);
152
    		}
153
	    }else{
154
	        taxon = getEntity().getTaxon();
155
	        if (entity.getParent().getTaxon() == null){
156
	            TextWithLabelElement textParent = formFactory.createTextWithLabelElement(
157
	                    formElement,CLASSIFICATION_STR, entity.getClassification().getTitleCache(), style);
158
                textParent.setEnabled(false);
159
	        }else{
160
	            TextWithLabelElement textParent = formFactory.createTextWithLabelElement(
161
	                    formElement, Messages.TaxonNodeWizardPage_PARENT, entity.getParent().getTaxon().getTitleCache(), style);
162
	            textParent.setEnabled(false);
163
	        }
164

    
165
	        selection_Ref = formFactory
166
                    .createSelectionElement(Reference.class,
167
                            formElement, Messages.TaxonNodeWizardPage_PLACEMENT_SOURCE, null,
168
                            EntitySelectionElement.DELETABLE, style);
169

    
170
	        selection_reuseExistingTaxon = formFactory
171
                  .createSelectionElement(Taxon.class,//getConversationHolder(),
172
                          formElement,
173
                          Messages.TaxonNodeWizardPage_TAXON, taxon,
174
                          EntitySelectionElement.DELETABLE, style);
175

    
176
	        textTaxonSec = formFactory.createTextWithLabelElement(formElement,SECUNDUM_REFERENCE, "", style);
177
            if (entity.getTaxon().getSec() != null){
178
                textTaxonSec.setText(taxon.getSec().getTitleCache());
179
            }
180
            textTaxonSec.setEnabled(false);
181
	    }
182

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

    
189
        Taxon tax = null;
190
        if (entity.getTaxon() != null){
191
            tax = entity.getTaxon();
192
        }
193
        checkbox_publish = formFactory.createCheckbox(formElement,
194
                "Taxon is published", tax != null? tax.isPublish(): true, style);
195

    
196
        checkbox_publish.setEnabled(isCreateNew());
197

    
198
        combo_status = formFactory.createEnumComboElement(TaxonNodeStatus.class, formElement, style, true);
199
        if (!isCreateNew()){
200
            combo_status.setSelection(entity.getStatus());
201
        }
202

    
203
        if(isCreateNew()){
204

    
205
		   String defaultPublish = PreferencesUtil.getStringValue(PreferencePredicate.DefaultBehaviourForPublishFlag.getKey());
206
	        if (defaultPublish != null && defaultPublish.equals(PublishEnum.Publish.getKey())){
207
	            checkbox_publish.setSelection(true);
208
	        } else if (defaultPublish != null && defaultPublish.equals(PublishEnum.NotPublish.getKey())){
209
	            checkbox_publish.setSelection(false);
210
	        }else{
211
	            if(this.getParentTreeNode() != null &&  this.getParentTreeNode().isInstanceOf(TaxonNode.class) && this.getParentTreeNode().getTaxon() != null){
212
	                checkbox_publish.setSelection(this.getParentTreeNode().getTaxon().isPublish());
213
	            }else{
214
	                checkbox_publish.setSelection(true);
215
	            }
216
	        }
217
	    } else{
218
            setTreeNode(entity);
219
            complete = true;
220
        }
221

    
222
		if (isCreateNew()){
223
		    preFillTaxonName();
224
		}
225

    
226
	    multiLanguageTextStatusNotes = formFactory.createMultiLanguageTextElement(formElement, STATUS_NOTES, null, 50, style);
227
	    if (!isCreateNew()){
228
	        multiLanguageTextStatusNotes.setMultilanguageText(entity.getStatusNote());
229
	        multiLanguageTextStatusNotes.setEnabled(entity.getStatus() != null);
230
	    }
231

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

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

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

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

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

    
338
            }else if (multilanguageTextCache != null ){
339

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

    
358
            multiLanguageTextStatusNotes.setEnabled(status != null);
359

    
360
            complete = true;
361
        }
362

    
363
		if (eventSource == textNewTaxonName) {
364
            boolean enabled = CdmUtils.isBlank(textNewTaxonName.getText());
365
            if (selection_reuseExistingTaxon != null){
366
                selection_reuseExistingTaxon.setEnabled(enabled);
367
            }
368
            selection_reuseExistingName.setEnabled(enabled);
369

    
370
            complete = CdmUtils.isNotBlank(textNewTaxonName.getText());
371
        }
372

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

    
382
	public Classification getClassification() {
383
		return classification;
384
	}
385

    
386
	public TaxonNode getParentTreeNode() {
387
		return parentNode;
388
	}
389

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

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

    
427
	private void setTreeNode(TaxonNode treeNode) {
428
        classification = HibernateProxyHelper
429
                    .deproxy(treeNode.getClassification());
430
        if (isCreateNew()){
431
           selection_parentTaxonNode.setEntity(treeNode.getParent());
432
           selection_parentTaxonNode.setClassification(treeNode.getParent().getClassification());
433
           selection_SecRef.setEntity(treeNode.getTaxon().getSec());
434
        }
435
	}
436

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

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

    
471
		} else {
472
		    textNewTaxonName.setText(null);
473
		}
474
	}
475

    
476
	public String getTaxonName() {
477
        return textNewTaxonName.getText();
478
    }
479
	public Taxon getTaxon() {
480
        return taxon;
481
    }
482

    
483
	public Reference getSecReference(){
484
	    return selection_SecRef.getSelection();
485
	}
486

    
487
	public Control getMainControl(){
488
	    return textNewTaxonName.getMainControl();
489
	}
490

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

    
502
    public boolean isCreateNew() {
503
        return createNew;
504
    }
505

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

    
515
    public void set_publish(boolean publish) {
516
        this.checkbox_publish.setSelection(publish);
517
    }
518

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