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.IPreferenceKeys;
|
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<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
|
CdmPreference defaultPublish = PreferencesUtil.getPreferenceFromDB(PreferencePredicate.DefaultBehaviourForPublishFlag);
|
171
|
if (defaultPublish != null && defaultPublish.getValue().equals(IPreferenceKeys.PUBLISH)){
|
172
|
checkbox_publish.setSelection(true);
|
173
|
} else if (defaultPublish != null && defaultPublish.getValue().equals(IPreferenceKeys.DO_NOT_PUBLISH)){
|
174
|
checkbox_publish.setSelection(false);
|
175
|
}else{
|
176
|
if(this.getParentTreeNode() != null && this.getParentTreeNode().isInstanceOf(TaxonNode.class)){
|
177
|
checkbox_publish.setSelection(((TaxonNode)this.getParentTreeNode()).getTaxon().isPublish());
|
178
|
}else{
|
179
|
checkbox_publish.setSelection(true);
|
180
|
}
|
181
|
}
|
182
|
setParentTreeNode(entity);
|
183
|
} else{
|
184
|
setTreeNode(entity);
|
185
|
complete = true;
|
186
|
}
|
187
|
if (entity instanceof TaxonNode) {
|
188
|
checkbox_unplaced = formFactory.createCheckbox(formElement,
|
189
|
"Taxon is unplaced", ((TaxonNode)entity).isUnplaced(), style);
|
190
|
}else{
|
191
|
checkbox_unplaced = formFactory.createCheckbox(formElement,
|
192
|
"Taxon is unplaced", false, style);
|
193
|
}
|
194
|
if (isCreateNew()){
|
195
|
preFillTaxonName();
|
196
|
}
|
197
|
|
198
|
if (entity instanceof TaxonNode) {
|
199
|
checkbox_excluded = formFactory.createCheckbox(formElement,
|
200
|
"Taxon is excluded", ((TaxonNode)entity).isExcluded(), style);
|
201
|
multiLanguageTextExcludedNotes = formFactory.createMultiLanguageTextElement(formElement, "Excluded Notes", ((TaxonNode)entity).getExcludedNote(), 50, style);
|
202
|
multiLanguageTextExcludedNotes.setEnabled(((TaxonNode) entity).isExcluded());
|
203
|
}else{
|
204
|
checkbox_excluded = formFactory.createCheckbox(formElement,
|
205
|
"Taxon is excluded", false, style);
|
206
|
multiLanguageTextExcludedNotes = formFactory.createMultiLanguageTextElement(formElement, "Excluded Notes", new HashMap<Language, LanguageString>(), 50, style);
|
207
|
multiLanguageTextExcludedNotes.setEnabled(false);
|
208
|
}
|
209
|
if (!isCreateNew()){
|
210
|
selectionNodeAgentRelation = formFactory.createTaxonNodeAgentRelationCollectionSection(formElement, getConversationHolder(), style);
|
211
|
selectionNodeAgentRelation.setEntity((TaxonNode)entity);
|
212
|
selectionNodeAgentRelation.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
|
213
|
}
|
214
|
checkbox_openInEditor = formFactory.createCheckbox(formElement,
|
215
|
"Open in Editor", true, style);
|
216
|
setOpenInEditor(true);
|
217
|
}
|
218
|
|
219
|
private void preFillTaxonName() {
|
220
|
if(getEntity() instanceof TaxonNode ){
|
221
|
TaxonNode node = (TaxonNode)getEntity();
|
222
|
if(node.getTaxon()!=null){
|
223
|
Taxon taxon = HibernateProxyHelper.deproxy(node.getTaxon(), Taxon.class);
|
224
|
if(taxon.getName()!=null){
|
225
|
TaxonName name = HibernateProxyHelper.deproxy(node.getTaxon().getName());
|
226
|
if (!isCreateNew()){
|
227
|
textNewTaxonName.setText(name.getNameCache());
|
228
|
} else if( ! name.isSupraGeneric() && name.getRank() != null){
|
229
|
String taxonName = "";
|
230
|
if(name.isGenus() || name.isInfraGeneric()|| name.isSpeciesAggregate() ){
|
231
|
taxonName = name.getGenusOrUninomial();
|
232
|
}
|
233
|
else if(name.isSpecies() || name.isInfraSpecific() ){
|
234
|
taxonName = CdmUtils.concat(" ", name.getGenusOrUninomial(),name.getSpecificEpithet());
|
235
|
}
|
236
|
if (StringUtils.isNotBlank(taxonName)){
|
237
|
textNewTaxonName.setText(taxonName + " ");
|
238
|
if(textNewTaxonName.getMainControl() instanceof Text){
|
239
|
Text text = (Text)textNewTaxonName.getMainControl();
|
240
|
text.setSelection(textNewTaxonName.getText().length());
|
241
|
}
|
242
|
}
|
243
|
}
|
244
|
}
|
245
|
}
|
246
|
}
|
247
|
}
|
248
|
|
249
|
/** {@inheritDoc} */
|
250
|
@Override
|
251
|
public void handleEvent(Object eventSource) {
|
252
|
if (eventSource == selection_classification) {
|
253
|
setClassification(selection_classification.getEntity());
|
254
|
} else if (eventSource == selection_parentTaxonNode) {
|
255
|
setParentTreeNode(selection_parentTaxonNode.getEntity());
|
256
|
} else if (eventSource == selection_reuseExistingTaxon) {
|
257
|
boolean enabled = selection_reuseExistingTaxon.getEntity() == null;
|
258
|
selection_reuseExistingName.setEnabled(enabled);
|
259
|
if (!enabled){
|
260
|
setTaxon(selection_reuseExistingTaxon.getEntity());
|
261
|
}else{
|
262
|
textNewTaxonName.setText(null);
|
263
|
}
|
264
|
complete = !textNewTaxonName.getText().isEmpty();
|
265
|
|
266
|
|
267
|
} else if (eventSource == selection_reuseExistingName) {
|
268
|
boolean enabled = selection_reuseExistingName.getEntity() == null;
|
269
|
setTaxon(selection_reuseExistingName.getEntity());
|
270
|
if (selection_reuseExistingTaxon != null){
|
271
|
selection_reuseExistingTaxon.setEnabled(enabled);
|
272
|
}
|
273
|
textNewTaxonName.setEnabled(enabled);
|
274
|
|
275
|
complete = !textNewTaxonName.getText().isEmpty();
|
276
|
}
|
277
|
if (!isCreateNew()){
|
278
|
if (eventSource == selection_Ref) {
|
279
|
((TaxonNode) getEntity()).setReference(selection_Ref.getEntity());
|
280
|
}else if (eventSource == selection_SecRef) {
|
281
|
((TaxonNode) getEntity()).getTaxon().setSec(selection_SecRef.getEntity());
|
282
|
}else if (eventSource == microReference) {
|
283
|
((TaxonNode) getEntity()).setMicroReference(microReference.getText());
|
284
|
} else if (eventSource == checkbox_publish) {
|
285
|
((TaxonNode)getEntity()).getTaxon().setPublish(checkbox_publish.getSelection());
|
286
|
}
|
287
|
}
|
288
|
if (eventSource == checkbox_excluded) {
|
289
|
excluded = checkbox_excluded.getSelection();
|
290
|
if (!isCreateNew()){
|
291
|
((TaxonNode)getEntity()).setExcluded(excluded);
|
292
|
}
|
293
|
multiLanguageTextExcludedNotes.setEnabled(excluded);
|
294
|
}
|
295
|
if (eventSource == checkbox_unplaced) {
|
296
|
unplaced = checkbox_unplaced.getSelection();
|
297
|
if (!isCreateNew()){
|
298
|
((TaxonNode)getEntity()).setUnplaced(unplaced);
|
299
|
}
|
300
|
}
|
301
|
if (eventSource == textNewTaxonName) {
|
302
|
boolean enabled = CdmUtils.isBlank(textNewTaxonName.getText());
|
303
|
if (selection_reuseExistingTaxon != null){
|
304
|
selection_reuseExistingTaxon.setEnabled(enabled);
|
305
|
}
|
306
|
selection_reuseExistingName.setEnabled(enabled);
|
307
|
|
308
|
complete = CdmUtils.isNotBlank(textNewTaxonName.getText());
|
309
|
}
|
310
|
if (eventSource == checkbox_openInEditor) {
|
311
|
setOpenInEditor(checkbox_openInEditor.getSelection());
|
312
|
}
|
313
|
if (eventSource == selectionNodeAgentRelation){
|
314
|
boolean allComplete = true;
|
315
|
for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
|
316
|
allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
|
317
|
}
|
318
|
complete = !isCreateNew() && allComplete;
|
319
|
}
|
320
|
}
|
321
|
|
322
|
public Classification getClassification() {
|
323
|
return classification;
|
324
|
}
|
325
|
|
326
|
public boolean isOpenInEditor() {
|
327
|
return openInEditor;
|
328
|
}
|
329
|
|
330
|
public ITaxonTreeNode getParentTreeNode() {
|
331
|
return parentTreeNode;
|
332
|
}
|
333
|
|
334
|
private void setClassification(Classification classification) {
|
335
|
this.classification = classification;
|
336
|
setParentTreeNode(classification);
|
337
|
}
|
338
|
|
339
|
private void setParentTreeNode(ITaxonTreeNode parentTreeNode) {
|
340
|
this.parentTreeNode = parentTreeNode;
|
341
|
|
342
|
if (parentTreeNode instanceof Classification) {
|
343
|
classification = (Classification) parentTreeNode;
|
344
|
if (selection_classification != null){
|
345
|
selection_classification.setEntity(classification);
|
346
|
}
|
347
|
selection_parentTaxonNode.setEntity(null);
|
348
|
selection_parentTaxonNode.setClassification(classification);
|
349
|
selection_SecRef.setEntity(classification.getReference());
|
350
|
} else if (parentTreeNode instanceof TaxonNode) {
|
351
|
classification = HibernateProxyHelper
|
352
|
.deproxy(((TaxonNode) parentTreeNode).getClassification());
|
353
|
if (selection_classification != null){
|
354
|
selection_classification.setEntity(classification);
|
355
|
}
|
356
|
selection_parentTaxonNode.setEntity(HibernateProxyHelper
|
357
|
.deproxy((TaxonNode) parentTreeNode));
|
358
|
selection_parentTaxonNode.setClassification(classification);
|
359
|
selection_SecRef.setEntity(((TaxonNode)parentTreeNode).getTaxon().getSec());
|
360
|
} else if(parentTreeNode == null){
|
361
|
this.parentTreeNode = classification;
|
362
|
}
|
363
|
}
|
364
|
|
365
|
private void setTreeNode(ITaxonTreeNode treeNode) {
|
366
|
classification = HibernateProxyHelper
|
367
|
.deproxy(((TaxonNode) treeNode).getClassification());
|
368
|
if (isCreateNew()){
|
369
|
//selection_classification.setEntity(classification);
|
370
|
selection_parentTaxonNode.setEntity(((TaxonNode) treeNode).getParent());
|
371
|
selection_parentTaxonNode.setClassification(((TaxonNode) treeNode).getParent().getClassification());
|
372
|
selection_SecRef.setEntity(((TaxonNode)treeNode).getTaxon().getSec());
|
373
|
}
|
374
|
}
|
375
|
|
376
|
private void setTaxon(Taxon taxon) {
|
377
|
this.taxon = taxon;
|
378
|
if (isCreateNew()){
|
379
|
textNewTaxonName.setText(taxon.getName().getTitleCache());
|
380
|
}
|
381
|
selection_SecRef.setEntity(taxon.getSec());
|
382
|
}
|
383
|
|
384
|
private void setTaxon(TaxonName taxonName) {
|
385
|
Reference secundum = null;
|
386
|
if (getParentTreeNode() != null) {
|
387
|
if (this.selection_SecRef.getEntity() != null){
|
388
|
secundum = this.selection_SecRef.getEntity();
|
389
|
}
|
390
|
}
|
391
|
if (taxonName != null){
|
392
|
taxon = Taxon.NewInstance(taxonName, secundum);
|
393
|
if (textNewTaxonName.getText() == null || !taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
|
394
|
textNewTaxonName.setText(taxonName.getTitleCache());
|
395
|
}
|
396
|
// if (!taxonName.getTitleCache().trim().equals(textNewTaxonName.getText().trim())){
|
397
|
// textNewTaxonName.setText(taxonName.getTitleCache());
|
398
|
// }
|
399
|
} else {
|
400
|
textNewTaxonName.setText(null);
|
401
|
}
|
402
|
}
|
403
|
|
404
|
private void setOpenInEditor(boolean openInEditor) {
|
405
|
this.openInEditor = openInEditor;
|
406
|
}
|
407
|
|
408
|
public String getTaxonName() {
|
409
|
return textNewTaxonName.getText();
|
410
|
}
|
411
|
public Taxon getTaxon() {
|
412
|
return taxon;
|
413
|
}
|
414
|
|
415
|
public Reference getReference(){
|
416
|
return selection_SecRef.getSelection();
|
417
|
}
|
418
|
|
419
|
public Control getMainControl(){
|
420
|
return textNewTaxonName.getMainControl();
|
421
|
}
|
422
|
|
423
|
public boolean isComplete() {
|
424
|
boolean allComplete = true;
|
425
|
if (!isCreateNew()){
|
426
|
for (ICdmFormElement element : selectionNodeAgentRelation.getElements()){
|
427
|
allComplete &=((TaxonNodeAgentRelationCollectionElement)element).isComplete();
|
428
|
}
|
429
|
complete = allComplete;
|
430
|
}
|
431
|
return complete;
|
432
|
}
|
433
|
|
434
|
public boolean isCreateNew() {
|
435
|
return createNew;
|
436
|
}
|
437
|
|
438
|
public boolean isExcluded() {
|
439
|
return excluded;
|
440
|
}
|
441
|
|
442
|
public boolean isUnplaced() {
|
443
|
return unplaced;
|
444
|
}
|
445
|
|
446
|
public boolean is_publish() {
|
447
|
boolean isPublishPreference = true;
|
448
|
CdmPreference defaultPublish = PreferencesUtil.getPreferenceFromDB(PreferencePredicate.DefaultBehaviourForPublishFlag);
|
449
|
if (defaultPublish != null && defaultPublish.getValue().equals(IPreferenceKeys.DO_NOT_PUBLISH)){
|
450
|
isPublishPreference = false;
|
451
|
}
|
452
|
return checkbox_publish!=null?checkbox_publish.getSelection():isPublishPreference;
|
453
|
}
|
454
|
|
455
|
public void set_publish(boolean publish) {
|
456
|
this.checkbox_publish.setSelection(publish);
|
457
|
}
|
458
|
|
459
|
|
460
|
}
|