Project

General

Profile

Download (17.7 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.name.TaxonName;
25
import eu.etaxonomy.cdm.model.reference.Reference;
26
import eu.etaxonomy.cdm.model.taxon.Classification;
27
import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
28
import eu.etaxonomy.cdm.model.taxon.Taxon;
29
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
30
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
31
import eu.etaxonomy.taxeditor.preference.PublishEnum;
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<ITaxonTreeNode> {
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 ITaxonTreeNode parentTreeNode;
59

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

    
65
	private Taxon taxon;
66

    
67
	private TextWithLabelElement textNewTaxonName;
68

    
69
	private TaxonNodeAgentRelationCollectionSection selectionNodeAgentRelation;
70

    
71
	private CheckboxElement checkbox_openInEditor;
72

    
73
	private CheckboxElement checkbox_unplaced;
74

    
75
	private CheckboxElement checkbox_excluded;
76
	private CheckboxElement checkbox_publish;
77

    
78

    
79

    
80
    private MultilanguageTextElement multiLanguageTextExcludedNotes;
81

    
82
	private boolean openInEditor;
83

    
84
	private boolean complete;
85

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

    
89
	private EntitySelectionElement<TaxonName> selection_reuseExistingName;
90

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

    
97

    
98

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

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

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

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

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

    
139
	        TextWithLabelElement textTaxon = formFactory.createTextWithLabelElement(formElement,"Taxon", ((TaxonNode)entity).getTaxon().getTitleCache(), style);
140
	        textTaxon.setEnabled(false);
141

    
142
            TextWithLabelElement textTaxonSec = formFactory.createTextWithLabelElement(formElement,"Secundum Reference", "", style);
143
            if (((TaxonNode)entity).getTaxon().getSec() != null){
144
                textTaxonSec.setText(((TaxonNode)entity).getTaxon().getSec().getTitleCache());
145
            }
146
            textTaxonSec.setEnabled(false);
147

    
148

    
149

    
150
	    }
151

    
152
	    selection_Ref = formFactory
153
                .createSelectionElement(Reference.class,//getConversationHolder(),
154
                        formElement, "Parent Child Relation Source", null,
155
                        EntitySelectionElement.DELETABLE, style);
156
        microReference = formFactory.createTextWithLabelElement(formElement,"Detail", "", style);
157

    
158
        if (!isCreateNew()){
159
            selection_Ref.setEntity(entity.getReference());
160
        }
161
		if (entity instanceof TaxonNode) {
162
		    checkbox_publish = formFactory.createCheckbox(formElement,
163
		            "Taxon is public", ((TaxonNode)entity).getTaxon().isPublish(), style);
164
		}else{
165
		    checkbox_publish = formFactory.createCheckbox(formElement,
166
                    "Taxon is public", true, style);
167
		}
168
		checkbox_publish.setEnabled(isCreateNew());
169
		if(isCreateNew()){
170
		    setParentTreeNode(entity);
171
	        CdmPreference defaultPublish = PreferencesUtil.getPreferenceFromDB(PreferencePredicate.DefaultBehaviourForPublishFlag);
172
	        if (defaultPublish != null && defaultPublish.getValue().equals(PublishEnum.Publish.getKey())){
173
	            checkbox_publish.setSelection(true);
174
	        } else if (defaultPublish != null && defaultPublish.getValue().equals(PublishEnum.NotPublish.getKey())){
175
	            checkbox_publish.setSelection(false);
176
	        }else{
177
	            if(this.getParentTreeNode() != null &&  this.getParentTreeNode().isInstanceOf(TaxonNode.class)){
178
	                checkbox_publish.setSelection(((TaxonNode)this.getParentTreeNode()).getTaxon().isPublish());
179
	            }else{
180
	                checkbox_publish.setSelection(true);
181
	            }
182
	        }
183

    
184
	    } else{
185
            setTreeNode(entity);
186
            complete = true;
187
        }
188
		if (entity instanceof TaxonNode) {
189
		    checkbox_unplaced = formFactory.createCheckbox(formElement,
190
		            "Taxon is unplaced", ((TaxonNode)entity).isUnplaced(), style);
191
		}else{
192
		    checkbox_unplaced = formFactory.createCheckbox(formElement,
193
                    "Taxon is unplaced", false, style);
194
		}
195
		if (isCreateNew()){
196
		    preFillTaxonName();
197
		}
198

    
199
		if (entity instanceof TaxonNode) {
200
		    checkbox_excluded = formFactory.createCheckbox(formElement,
201
		            "Taxon is excluded", ((TaxonNode)entity).isExcluded(), style);
202
		    multiLanguageTextExcludedNotes = formFactory.createMultiLanguageTextElement(formElement, "Excluded Notes", ((TaxonNode)entity).getExcludedNote(), 50, style);
203
		    multiLanguageTextExcludedNotes.setEnabled(((TaxonNode) entity).isExcluded());
204
		}else{
205
		    checkbox_excluded = formFactory.createCheckbox(formElement,
206
                    "Taxon is excluded", false, style);
207
		    multiLanguageTextExcludedNotes = formFactory.createMultiLanguageTextElement(formElement, "Excluded Notes", new HashMap<Language, LanguageString>(), 50, style);
208
            multiLanguageTextExcludedNotes.setEnabled(false);
209
		}
210
		if (!isCreateNew()){
211
		    selectionNodeAgentRelation = formFactory.createTaxonNodeAgentRelationCollectionSection(formElement, getConversationHolder(), style);
212
		    selectionNodeAgentRelation.setEntity((TaxonNode)entity);
213
		    selectionNodeAgentRelation.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
214
		}
215
		checkbox_openInEditor = formFactory.createCheckbox(formElement,
216
		        "Open in Editor", true, style);
217
		setOpenInEditor(true);
218
	}
219

    
220
	private void preFillTaxonName() {
221
        if(getEntity() instanceof TaxonNode ){
222
            TaxonNode node = (TaxonNode)getEntity();
223
            if(node.getTaxon()!=null){
224
                Taxon taxon = HibernateProxyHelper.deproxy(node.getTaxon(), Taxon.class);
225
                if(taxon.getName()!=null){
226
                    TaxonName name = HibernateProxyHelper.deproxy(node.getTaxon().getName());
227
                    if (!isCreateNew()){
228
                        textNewTaxonName.setText(name.getNameCache());
229
                    } else if( ! name.isSupraGeneric() && name.getRank() != null){
230
                        String taxonName = "";
231
                        if(name.isGenus() || name.isInfraGeneric()|| name.isSpeciesAggregate() ){
232
                            taxonName = name.getGenusOrUninomial();
233
                        }
234
                        else if(name.isSpecies() || name.isInfraSpecific() ){
235
                            taxonName = CdmUtils.concat(" ", name.getGenusOrUninomial(),name.getSpecificEpithet());
236
                        }
237
                        if (StringUtils.isNotBlank(taxonName)){
238
                        	textNewTaxonName.setText(taxonName + " ");
239
                        	if(textNewTaxonName.getMainControl() instanceof Text){
240
                        		Text text = (Text)textNewTaxonName.getMainControl();
241
                        		text.setSelection(textNewTaxonName.getText().length());
242
                        	}
243
                        }
244
                    }
245
                }
246
            }
247
        }
248
    }
249

    
250
	/** {@inheritDoc} */
251
	@Override
252
	public void handleEvent(Object eventSource) {
253
		if (eventSource == selection_classification) {
254
			setClassification(selection_classification.getEntity());
255
		} else if (eventSource == selection_parentTaxonNode) {
256
			setParentTreeNode(selection_parentTaxonNode.getEntity());
257
		} else if (eventSource == selection_reuseExistingTaxon) {
258
				boolean enabled = selection_reuseExistingTaxon.getEntity() == null;
259
				selection_reuseExistingName.setEnabled(enabled);
260
				if (!enabled){
261
                    setTaxon(selection_reuseExistingTaxon.getEntity());
262
                }else{
263
			        textNewTaxonName.setText(null);
264
			    }
265
				complete = !textNewTaxonName.getText().isEmpty();
266

    
267

    
268
		} else if (eventSource == selection_reuseExistingName) {
269
		    boolean enabled = selection_reuseExistingName.getEntity() == null;
270
			setTaxon(selection_reuseExistingName.getEntity());
271
			if (selection_reuseExistingTaxon != null){
272
			    selection_reuseExistingTaxon.setEnabled(enabled);
273
			}
274
			textNewTaxonName.setEnabled(enabled);
275

    
276
			complete = !textNewTaxonName.getText().isEmpty();
277
		}
278
		if (!isCreateNew()){
279
            if (eventSource == selection_Ref) {
280
                ((TaxonNode) getEntity()).setReference(selection_Ref.getEntity());
281
            }else if (eventSource == selection_SecRef) {
282
                ((TaxonNode) getEntity()).getTaxon().setSec(selection_SecRef.getEntity());
283
            }else if (eventSource == microReference) {
284
                ((TaxonNode) getEntity()).setMicroReference(microReference.getText());
285
            }  else if (eventSource == checkbox_publish) {
286
                ((TaxonNode)getEntity()).getTaxon().setPublish(checkbox_publish.getSelection());
287
            }
288
		}
289
		if (eventSource == checkbox_excluded) {
290
            excluded = checkbox_excluded.getSelection();
291
            if (!isCreateNew()){
292
                ((TaxonNode)getEntity()).setExcluded(excluded);
293
            }
294
            multiLanguageTextExcludedNotes.setEnabled(excluded);
295
        }
296
		if (eventSource == checkbox_unplaced) {
297
            unplaced = checkbox_unplaced.getSelection();
298
            if (!isCreateNew()){
299
                ((TaxonNode)getEntity()).setUnplaced(unplaced);
300
            }
301
        }
302
		if (eventSource == textNewTaxonName) {
303
            boolean enabled = CdmUtils.isBlank(textNewTaxonName.getText());
304
            if (selection_reuseExistingTaxon != null){
305
                selection_reuseExistingTaxon.setEnabled(enabled);
306
            }
307
            selection_reuseExistingName.setEnabled(enabled);
308

    
309
            complete = CdmUtils.isNotBlank(textNewTaxonName.getText());
310
        }
311
		if (eventSource == checkbox_openInEditor) {
312
            setOpenInEditor(checkbox_openInEditor.getSelection());
313
        }
314
		if (eventSource == selectionNodeAgentRelation){
315
		   boolean allComplete = true;
316
		   for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
317
		       allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
318
		   }
319
		    complete = !isCreateNew() && allComplete;
320
		}
321
	}
322

    
323
	public Classification getClassification() {
324
		return classification;
325
	}
326

    
327
	public boolean isOpenInEditor() {
328
		return openInEditor;
329
	}
330

    
331
	public ITaxonTreeNode getParentTreeNode() {
332
		return parentTreeNode;
333
	}
334

    
335
	private void setClassification(Classification classification) {
336
		this.classification = classification;
337
		setParentTreeNode(classification);
338
	}
339

    
340
	private void setParentTreeNode(ITaxonTreeNode parentTreeNode) {
341
		this.parentTreeNode = parentTreeNode;
342

    
343
		if (parentTreeNode instanceof Classification) {
344
			classification = (Classification) parentTreeNode;
345
			if (selection_classification != null){
346
			    selection_classification.setEntity(classification);
347
			}
348
			selection_parentTaxonNode.setEntity(null);
349
			selection_parentTaxonNode.setClassification(classification);
350
			selection_SecRef.setEntity(classification.getReference());
351
		} else if (parentTreeNode instanceof TaxonNode) {
352
			classification = HibernateProxyHelper
353
					.deproxy(((TaxonNode) parentTreeNode).getClassification());
354
			if (selection_classification != null){
355
			    selection_classification.setEntity(classification);
356
			}
357
			selection_parentTaxonNode.setEntity(HibernateProxyHelper
358
                    .deproxy((TaxonNode) parentTreeNode));
359
			selection_parentTaxonNode.setClassification(classification);
360
			selection_SecRef.setEntity(((TaxonNode)parentTreeNode).getTaxon().getSec());
361
		} else if(parentTreeNode == null){
362
			this.parentTreeNode = classification;
363
		}
364
	}
365

    
366
	private void setTreeNode(ITaxonTreeNode treeNode) {
367
        classification = HibernateProxyHelper
368
                    .deproxy(((TaxonNode) treeNode).getClassification());
369
        if (isCreateNew()){
370
           //selection_classification.setEntity(classification);
371
            selection_parentTaxonNode.setEntity(((TaxonNode) treeNode).getParent());
372
            selection_parentTaxonNode.setClassification(((TaxonNode) treeNode).getParent().getClassification());
373
            selection_SecRef.setEntity(((TaxonNode)treeNode).getTaxon().getSec());
374
        }
375
	}
376

    
377
	private void setTaxon(Taxon taxon) {
378
		this.taxon = taxon;
379
		if (isCreateNew()){
380
		    textNewTaxonName.setText(taxon.getName().getTitleCache());
381
		}
382
		selection_SecRef.setEntity(taxon.getSec());
383
	}
384

    
385
	private void setTaxon(TaxonName taxonName) {
386
		Reference secundum = null;
387
		if (getParentTreeNode() != null) {
388
			if (this.selection_SecRef.getEntity() != null){
389
				secundum = this.selection_SecRef.getEntity();
390
			}
391
		}
392
		if (taxonName != null){
393
		    taxon = Taxon.NewInstance(taxonName, secundum);
394
		    if (textNewTaxonName.getText() == null || !taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
395
		    	 textNewTaxonName.setText(taxonName.getTitleCache());
396
		    }
397
//		    if (!taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
398
//		        textNewTaxonName.setText(taxonName.getTitleCache());
399
//		    }
400
		} else {
401
		    textNewTaxonName.setText(null);
402
		}
403
	}
404

    
405
	private void setOpenInEditor(boolean openInEditor) {
406
		this.openInEditor = openInEditor;
407
	}
408

    
409
	public String getTaxonName() {
410
        return textNewTaxonName.getText();
411
    }
412
	public Taxon getTaxon() {
413
        return taxon;
414
    }
415

    
416
	public Reference getReference(){
417
	    return selection_SecRef.getSelection();
418
	}
419

    
420
	public Control getMainControl(){
421
	    return textNewTaxonName.getMainControl();
422
	}
423

    
424
	public boolean isComplete() {
425
	    boolean allComplete = true;
426
	    if (!isCreateNew()){
427
	        for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
428
	            allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
429
	        }
430
	        complete = allComplete;
431
	    }
432
		return complete;
433
	}
434

    
435
    public boolean isCreateNew() {
436
        return createNew;
437
    }
438

    
439
    public boolean isExcluded() {
440
        return excluded;
441
    }
442

    
443
    public boolean isUnplaced() {
444
        return unplaced;
445
    }
446

    
447
    public boolean is_publish() {
448
        boolean isPublishPreference = true;
449
        CdmPreference defaultPublish = PreferencesUtil.getPreferenceFromDB(PreferencePredicate.DefaultBehaviourForPublishFlag);
450
        if (defaultPublish != null && defaultPublish.getValue().equals(PublishEnum.NotPublish.getKey())){
451
            isPublishPreference = false;
452
        }
453
        return checkbox_publish!=null?checkbox_publish.getSelection():isPublishPreference;
454
    }
455

    
456
    public void set_publish(boolean publish) {
457
        this.checkbox_publish.setSelection(publish);
458
    }
459

    
460

    
461
}
(10-10/12)