Project

General

Profile

Download (18 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2007 EDIT
3
 * European Distributed Institute of Taxonomy
4
 * http://www.e-taxonomy.eu
5
 *
6
 * The contents of this file are subject to the Mozilla Public License Version 1.1
7
 * See LICENSE.TXT at the top of this package for the full license terms.
8
 */
9

    
10
package eu.etaxonomy.taxeditor.ui.section.classification;
11

    
12
import java.util.HashMap;
13

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

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

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

    
50
    private boolean createNew = false;
51

    
52
//	private EntitySelectionElement<Classification> selection_classification;
53

    
54
	private Classification classification;
55

    
56
	private TaxonNodeSelectionElement selection_parentTaxonNode;
57

    
58
	private TaxonNode parentTreeNode;
59

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

    
66
	private Taxon taxon;
67

    
68
	private TextWithLabelElement textNewTaxonName;
69

    
70
	private TaxonNodeAgentRelationCollectionSection selectionNodeAgentRelation;
71

    
72
//	private CheckboxElement checkbox_openInEditor;
73

    
74
	private CheckboxElement checkbox_unplaced;
75

    
76
	private CheckboxElement checkbox_excluded;
77
	private CheckboxElement checkbox_publish;
78

    
79

    
80

    
81
    private MultilanguageTextElement multiLanguageTextExcludedNotes;
82

    
83
//	private boolean openInEditor;
84

    
85
	private boolean complete;
86

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

    
90
	private EntitySelectionElement<TaxonName> selection_reuseExistingName;
91

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

    
98

    
99

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

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

    
120
    		selection_reuseExistingName = formFactory
121
    				.createSelectionElement(TaxonName.class,//getConversationHolder(),
122
    				        formElement,
123
    						"Reuse existing name", taxon.getName(),
124
    						EntitySelectionElement.DELETABLE, style);
125
    		selection_SecRef = formFactory
126
                    .createSelectionElement(Reference.class,//getConversationHolder(),
127
                            formElement, "Secundum Reference", taxon.getSec(),
128
                            EntitySelectionElement.DELETABLE, style);
129

    
130
    		if (isCreateNew()){
131
    		    textNewTaxonName = formFactory.createTextWithLabelElement(formElement,"New Taxon", "", style);
132
    		    textNewTaxonName.setFocus();
133
    		} else{
134
    		    textNewTaxonName = formFactory.createTextWithLabelElement(formElement,"Taxon", "", style);
135

    
136
    		}
137
	    }else{
138
	        TextWithLabelElement textClassification = formFactory.createTextWithLabelElement(formElement,"Classification", entity.getClassification().getTitleCache(), style);
139
	        textClassification.setEnabled(false);
140

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

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

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

    
156

    
157

    
158
	    }
159

    
160
	    selection_Ref = formFactory
161
                .createSelectionElement(Reference.class,//getConversationHolder(),
162
                        formElement, "Parent Child Relation Source", null,
163
                        EntitySelectionElement.DELETABLE, style);
164
        microReference = formFactory.createTextWithLabelElement(formElement,"Detail", "", style);
165
        microReference.setText(getEntity().getMicroReference());
166
        if (!isCreateNew()){
167
            selection_Ref.setEntity(entity.getReference());
168
        }
169
        parentTreeNode = entity.getParent();
170

    
171
		if (entity instanceof TaxonNode ) {
172
		    checkbox_publish = formFactory.createCheckbox(formElement,
173
		            "Taxon is published", entity.getTaxon().isPublish(), style);
174
		}else{
175
		    checkbox_publish = formFactory.createCheckbox(formElement,
176
                    "Taxon is published", true, style);
177
		}
178
		checkbox_publish.setEnabled(isCreateNew());
179
		if(isCreateNew()){
180
		   setParentTreeNode(entity.getParent());
181

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

    
195
	    } else{
196
            setTreeNode(entity);
197

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

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

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

    
262
	/** {@inheritDoc} */
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
			complete = true;
286
		} else if (eventSource == selection_reuseExistingName) {
287
		    boolean enabled = selection_reuseExistingName.getEntity() == null;
288
			setTaxon(HibernateProxyHelper.deproxy(selection_reuseExistingName.getEntity()));
289
			if (selection_reuseExistingTaxon != null){
290
			    selection_reuseExistingTaxon.setEnabled(enabled);
291
			}
292
			textNewTaxonName.setEnabled(enabled);
293
			complete = !textNewTaxonName.getText().isEmpty();
294
		}
295

    
296
        if (eventSource == selection_Ref) {
297
            getEntity().setReference(selection_Ref.getEntity());
298
        }else if (eventSource == selection_SecRef) {
299
            taxon.setSec(selection_SecRef.getEntity());
300
        }else if (eventSource == microReference) {
301
            getEntity().setMicroReference(microReference.getText());
302
        }  else if (eventSource == checkbox_publish) {
303
            taxon.setPublish(checkbox_publish.getSelection());
304
        }
305

    
306
		if (eventSource == checkbox_excluded) {
307
            excluded = checkbox_excluded.getSelection();
308

    
309
            getEntity().setExcluded(excluded);
310

    
311
            multiLanguageTextExcludedNotes.setEnabled(excluded);
312
        }
313
		if (eventSource == checkbox_unplaced) {
314
            unplaced = checkbox_unplaced.getSelection();
315
            getEntity().setUnplaced(unplaced);
316

    
317
        }
318
		if (eventSource == textNewTaxonName) {
319
            boolean enabled = CdmUtils.isBlank(textNewTaxonName.getText());
320
            if (selection_reuseExistingTaxon != null){
321
                selection_reuseExistingTaxon.setEnabled(enabled);
322
            }
323
            selection_reuseExistingName.setEnabled(enabled);
324

    
325
            complete = CdmUtils.isNotBlank(textNewTaxonName.getText());
326
        }
327

    
328
		if (eventSource == selectionNodeAgentRelation){
329
		   boolean allComplete = true;
330
		   for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
331
		       allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
332
		   }
333
		    complete = !isCreateNew() && allComplete;
334
		}
335
	}
336

    
337
	public Classification getClassification() {
338
		return classification;
339
	}
340

    
341
//	public boolean isOpenInEditor() {
342
//		return openInEditor;
343
//	}
344

    
345
	public ITaxonTreeNode getParentTreeNode() {
346
		return parentTreeNode;
347
	}
348

    
349
	private void setClassification(Classification classification) {
350
		this.classification = classification;
351
		//setParentTreeNode(classification);
352
	}
353

    
354
	private void setParentTreeNode(TaxonNode parentTreeNode) {
355
		this.parentTreeNode = parentTreeNode;
356

    
357
		if (parentTreeNode.getTaxon() == null) {
358
			classification = parentTreeNode.getClassification();
359
//			if (selection_classification != null){
360
//			    selection_classification.setEntity(classification);
361
//			}
362
			selection_parentTaxonNode.setEntity(classification.getRootNode());
363
			selection_parentTaxonNode.setClassification(classification);
364
			selection_SecRef.setEntity(classification.getReference());
365
		} else  {
366
			classification = HibernateProxyHelper
367
					.deproxy(parentTreeNode.getClassification());
368
//			if (selection_classification != null){
369
//			    selection_classification.setEntity(classification);
370
//			}
371
			selection_parentTaxonNode.setEntity(HibernateProxyHelper
372
                    .deproxy(parentTreeNode));
373
			selection_parentTaxonNode.setClassification(classification);
374
			selection_SecRef.setEntity(parentTreeNode.getTaxon().getSec());
375

    
376
		}
377
	}
378

    
379
	private void setTreeNode(TaxonNode treeNode) {
380
        classification = HibernateProxyHelper
381
                    .deproxy(treeNode.getClassification());
382
        if (isCreateNew()){
383
           //selection_classification.setEntity(classification);
384
            selection_parentTaxonNode.setEntity(treeNode.getParent());
385
            selection_parentTaxonNode.setClassification(treeNode.getParent().getClassification());
386
            selection_SecRef.setEntity(treeNode.getTaxon().getSec());
387
        }
388
	}
389

    
390
	private void setTaxon(Taxon taxon) {
391
		this.taxon = taxon;
392
		getEntity().setTaxon(taxon);
393
		this.set_publish(taxon.isPublish());
394
		if (isCreateNew()){
395
		    textNewTaxonName.setText(taxon.getName().getTitleCache());
396
		    if (taxon.getSec() != null){
397
		        selection_SecRef.setEntity(taxon.getSec());
398
		    }
399
		}else{
400
		    if (taxon.getSec() != null) {
401
                this.textTaxonSec.setText(taxon.getSec().getTitleCache());
402
            }
403
		}
404

    
405

    
406
	}
407

    
408
	private void setTaxon(TaxonName taxonName) {
409
		Reference secundum = null;
410
		if (getParentTreeNode() != null) {
411
			if (this.selection_SecRef.getEntity() != null){
412
				secundum = this.selection_SecRef.getEntity();
413
			}
414
		}
415
		if (taxonName != null){
416
		    taxon = Taxon.NewInstance(taxonName, secundum);
417
		    if (textNewTaxonName.getText() == null || !taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
418
		    	 textNewTaxonName.setText(taxonName.getTitleCache());
419
		    }
420
		    getEntity().setTaxon(taxon);
421
//		    if (!taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
422
//		        textNewTaxonName.setText(taxonName.getTitleCache());
423
//		    }
424
		} else {
425
		    textNewTaxonName.setText(null);
426
		}
427
	}
428

    
429
//	private void setOpenInEditor(boolean openInEditor) {
430
//		this.openInEditor = openInEditor;
431
//	}
432

    
433
	public String getTaxonName() {
434
        return textNewTaxonName.getText();
435
    }
436
	public Taxon getTaxon() {
437
        return taxon;
438
    }
439

    
440
	public Reference getReference(){
441
	    return selection_SecRef.getSelection();
442
	}
443

    
444
	public Control getMainControl(){
445
	    return textNewTaxonName.getMainControl();
446
	}
447

    
448
	public boolean isComplete() {
449
	    boolean allComplete = true;
450
	    if (!isCreateNew()){
451
	        for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
452
	            allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
453
	        }
454
	        complete = allComplete;
455
	    }
456
		return complete;
457
	}
458

    
459
    public boolean isCreateNew() {
460
        return createNew;
461
    }
462

    
463
    public boolean isExcluded() {
464
        return excluded;
465
    }
466

    
467
    public boolean isUnplaced() {
468
        return unplaced;
469
    }
470

    
471
    public boolean is_publish() {
472
        boolean isPublishPreference = true;
473
        CdmPreference defaultPublish = PreferencesUtil.getPreferenceFromDB(PreferencePredicate.DefaultBehaviourForPublishFlag);
474
        if (defaultPublish != null && defaultPublish.getValue().equals(PublishEnum.NotPublish.getKey())){
475
            isPublishPreference = false;
476
        }
477
        return checkbox_publish!=null?checkbox_publish.getSelection():isPublishPreference;
478
    }
479

    
480
    public void set_publish(boolean publish) {
481
        this.checkbox_publish.setSelection(publish);
482
    }
483

    
484

    
485
}
(10-10/12)