2 * Copyright (C) 2011 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.ui
.section
.userecords
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Iterator
;
13 import java
.util
.List
;
14 import java
.util
.ListIterator
;
17 import org
.apache
.log4j
.Logger
;
18 import org
.eclipse
.swt
.events
.SelectionEvent
;
19 import org
.eclipse
.swt
.events
.SelectionListener
;
20 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
21 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
22 import eu
.etaxonomy
.cdm
.model
.common
.Marker
;
23 import eu
.etaxonomy
.cdm
.model
.description
.CategoricalData
;
24 import eu
.etaxonomy
.cdm
.model
.description
.Modifier
;
25 import eu
.etaxonomy
.cdm
.model
.description
.State
;
26 import eu
.etaxonomy
.cdm
.model
.description
.StateData
;
27 import eu
.etaxonomy
.taxeditor
.editor
.UsageTermCollection
;
28 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
29 import eu
.etaxonomy
.taxeditor
.store
.TermStore
;
30 import eu
.etaxonomy
.taxeditor
.ui
.combo
.TermComboElement
;
31 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
32 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
33 import eu
.etaxonomy
.taxeditor
.ui
.section
.AbstractCdmDetailElement
;
37 * The context manager mediates context start/stop and workbench shutdowns to all registered listeners.
40 * @created mar 13, 2012
43 public class UseRecordDetailElement
extends
44 AbstractCdmDetailElement
<CategoricalData
> implements SelectionListener
{
46 private static final Logger logger
= Logger
47 .getLogger(UseRecordDetailElement
.class);
49 private TermComboElement
<State
> combo_UseCategory
;
50 private TermComboElement
<State
> combo_UseSubCategory
;
51 private TermComboElement
<Modifier
> combo_PlantPart
;
52 private TermComboElement
<Modifier
> combo_Country
;
53 private TermComboElement
<Modifier
> combo_HumanGroup
;
54 private TermComboElement
<Modifier
> combo_EthnicGroup
;
56 public UseRecordDetailElement(CdmFormFactory formFactory
,
57 ICdmFormElement formElement
) {
58 super(formFactory
, formElement
);
63 protected void createControls(ICdmFormElement formElement
,
64 CategoricalData entity
, int style
) {
66 combo_UseCategory
= formFactory
.createTermComboElement(State
.class, this, UsageTermCollection
.useCategoryVocabularyLabel
, null, style
);
67 combo_UseCategory
.addSelectionListener(this);
68 combo_UseSubCategory
= formFactory
.createTermComboElement(State
.class, this, UsageTermCollection
.useSubCategoryVocabularyLabel
, null, style
);
69 combo_PlantPart
= formFactory
.createTermComboElement(Modifier
.class, this, UsageTermCollection
.plantPartLabel
, null, style
);
70 combo_HumanGroup
= formFactory
.createTermComboElement(Modifier
.class, this, UsageTermCollection
.humanGroupLabel
, null, style
);
71 combo_HumanGroup
.addSelectionListener(this);
72 combo_EthnicGroup
= formFactory
.createTermComboElement(Modifier
.class, this, UsageTermCollection
.ethnicGroupLabel
, null, style
);
73 combo_Country
= formFactory
.createTermComboElement(Modifier
.class, this, UsageTermCollection
.countryLabel
, null, style
);
76 List
<State
> termsUseCategory
= setUseCategoryComboTerms(TermStore
.getTerms(State
.class, null, false));
77 combo_UseCategory
.setTerms(termsUseCategory
);
78 combo_UseCategory
.setSelection(stateSelection(UsageTermCollection
.useCategoryVocabularyLabel
));
80 List
<State
> termsSubCategory
= setUseCategoryComboTerms(TermStore
.getTerms(State
.class, null, false), combo_UseCategory
.getSelection());
82 if (termsSubCategory
!= null) {
83 combo_UseSubCategory
.setTerms(termsSubCategory
);
84 combo_UseSubCategory
.setSelection(stateSelection(UsageTermCollection
.useSubCategoryVocabularyLabel
));
85 combo_UseSubCategory
.setEnabled(true);
88 combo_UseSubCategory
.setEnabled(false);
91 List
<Modifier
> plantPartsToAdd
= setModifierComboTerms(TermStore
.getTerms(Modifier
.class, null, false), UsageTermCollection
.plantPartLabel
);
92 combo_PlantPart
.setTerms(plantPartsToAdd
);
93 combo_PlantPart
.setSelection(modifierSelection(UsageTermCollection
.plantPartLabel
));
95 List
<Modifier
> countriesToAdd
= setModifierComboTerms(TermStore
.getTerms(Modifier
.class, null, false), UsageTermCollection
.countryLabel
);
96 combo_Country
.setTerms(countriesToAdd
);
97 combo_Country
.setSelection(modifierSelection(UsageTermCollection
.countryLabel
));
99 List
<Modifier
> humanGroupToAdd
= setModifierComboTerms(TermStore
.getTerms(Modifier
.class, null, false), UsageTermCollection
.humanGroupLabel
);
100 combo_HumanGroup
.setTerms(humanGroupToAdd
);
101 combo_HumanGroup
.setSelection(modifierSelection(UsageTermCollection
.humanGroupLabel
));
103 List
<Modifier
> ethnicGroupToAdd
= setEthnicGroupComboTerms(TermStore
.getTerms(Modifier
.class, null, false), combo_HumanGroup
.getSelection());
104 if (ethnicGroupToAdd
!= null) {
105 combo_EthnicGroup
.setTerms(ethnicGroupToAdd
);
106 combo_EthnicGroup
.setSelection(modifierSelection(UsageTermCollection
.ethnicGroupLabel
));
107 combo_EthnicGroup
.setEnabled(true);
109 combo_EthnicGroup
.setEnabled(false);
115 * Returns the select modifier
116 * @param comboCategory
119 private Modifier
modifierSelection(String comboCategory
) {
120 if(!getEntity().getModifiers().isEmpty()) {
121 for (Modifier modifier
: getEntity().getModifiers()) {
122 if(GetVocabularyType(modifier
, comboCategory
)) {
134 * Returns the selected state
135 * @param comboCategory
138 private State
stateSelection(String comboCategory
) {
139 if (!getEntity().getStates().isEmpty()) {
140 for (StateData statedata
: getEntity().getStates()) {
141 if (statedata
.getModifyingText().get(CdmStore
.getDefaultLanguage()) != null) {
142 String testString
= statedata
.getModifyingText().get(CdmStore
.getDefaultLanguage()).getText();
143 if(testString
.equals(comboCategory
)) {
144 if(statedata
.getState() !=null) {
145 return statedata
.getState();
159 * This function sets the combo terms (Besides EthnicGroup
164 private List
<Modifier
> setModifierComboTerms(List
<Modifier
> listOfTerms
, String modType
) {
165 List
<Modifier
> termsToAdd
= new ArrayList
<Modifier
>();
166 for (Modifier term
: listOfTerms
) {
168 if ((term
.getPartOf() != null) && (term
.getPartOf().getTitleCache().equals(modType
))) {
169 termsToAdd
.add(term
);
171 else if ((term
.getVocabulary() != null) && (term
.getPartOf() == null) && (term
.getVocabulary().getTitleCache().equals(modType
))) {
172 termsToAdd
.add(term
);
181 * This function sets the Use Category combo term
185 private List
<State
> setUseCategoryComboTerms(List
<State
> listOfTerms
) {
186 List
<State
> termsToAdd
= new ArrayList
<State
>();
187 for (State term
: listOfTerms
) {
189 if ((term
.getPartOf() != null) && (term
.getPartOf().getTitleCache().equals(UsageTermCollection
.useCategoryVocabularyLabel
))) {
190 termsToAdd
.add(term
);
192 else if ((term
.getVocabulary() !=null) && (term
.getPartOf() == null) && (term
.getVocabulary().getTitleCache().equals(UsageTermCollection
.useCategoryVocabularyLabel
))) {
193 termsToAdd
.add(term
);
201 * This function sets the Use SubCategory combo term
203 * @param selectedUseCategory
206 private List
<State
> setUseCategoryComboTerms(List
<State
> listOfTerms
,
207 State selectedUseCategory
) {
208 List
<State
> termsToAdd
= new ArrayList
<State
>();
209 if (combo_UseCategory
.getSelection() != null) {
211 for (State term
: listOfTerms
) {
212 if ((term
.getPartOf() != null) && (term
.getPartOf().getTitleCache().equals(selectedUseCategory
.getTitleCache()))) {
213 termsToAdd
.add(term
);
215 else if ((term
.getVocabulary() !=null) && (term
.getVocabulary().getTitleCache().equals(selectedUseCategory
.getTitleCache()))) {
216 termsToAdd
.add(term
);
221 else if (!getEntity().getStates().isEmpty()) {
222 for (StateData stateData
: getEntity().getStates()) {
223 if (stateData
.getModifyingText().get(CdmStore
.getDefaultLanguage()) != null) {
224 if ((stateData
.getState() != null) && (stateData
.getModifyingText().get(CdmStore
.getDefaultLanguage()).getText() == UsageTermCollection
.useCategoryVocabularyLabel
)) {
225 for (State term
: listOfTerms
) {
226 if ((term
.getPartOf() != null) && (term
.getPartOf().getTitleCache().equals(stateData
.getState().getTitleCache()))) {
227 termsToAdd
.add(term
);
228 } else if ((term
.getVocabulary() != null) && (term
.getVocabulary().getTitleCache().equals(stateData
.getState().getTitleCache()))) {
229 termsToAdd
.add(term
);
248 * This function sets the EthnicGroup combo term
250 * @param selectedHumangroup
253 private List
<Modifier
> setEthnicGroupComboTerms(List
<Modifier
> listOfTerms
,
254 Modifier selectedHumangroup
) {
255 List
<Modifier
> termsToAdd
= new ArrayList
<Modifier
>();
256 if (combo_HumanGroup
.getSelection() != null) {
257 for (Modifier term
: listOfTerms
) {
259 if ((term
.getPartOf() != null) && (term
.getPartOf().getTitleCache().equals(selectedHumangroup
.getTitleCache()))) {
260 termsToAdd
.add(term
);
262 else if ((term
.getVocabulary() != null) && (term
.getVocabulary().getTitleCache().equals(selectedHumangroup
.getTitleCache()))) {
263 termsToAdd
.add(term
);
267 else if (!getEntity().getModifiers().isEmpty()) {
268 for (Modifier modifier
: getEntity().getModifiers()) {
269 if (modifier
.getMarkers() != null) {
270 for (Marker marker
: modifier
.getMarkers()) {
271 if(marker
.getMarkerType().getTitleCache().equals(UsageTermCollection
.humanGroupLabel
)) {
272 for (Modifier term
: listOfTerms
) {
273 if ((term
.getPartOf() != null) && (term
.getPartOf().getTitleCache().equals(modifier
.getTitleCache()))) {
274 termsToAdd
.add(modifier
);
275 } else if ((term
.getVocabulary() != null) && (term
.getVocabulary().getTitleCache().equals(modifier
.getTitleCache()))) {
276 termsToAdd
.add(modifier
);
294 public void handleEvent(Object eventSource
) {
295 if (eventSource
== combo_UseCategory
) {
296 if (combo_UseCategory
.getSelection() != null) {
297 boolean isChanged
= false;
298 boolean isUseCategoryChanged
= false;
299 if (!getEntity().getStates().isEmpty()) {
300 for (StateData useCategoryStateDataCheck
: getEntity().getStates()) {
301 Map
<Language
, LanguageString
> modText
= useCategoryStateDataCheck
.getModifyingText();
302 if(modText
.get(CdmStore
.getDefaultLanguage()) != null && modText
.get(CdmStore
.getDefaultLanguage()).getText().equals(UsageTermCollection
.useCategoryVocabularyLabel
)) {
303 if(!useCategoryStateDataCheck
.getState().getTitleCache().equals(combo_UseCategory
.getSelection().getTitleCache())) {
304 isUseCategoryChanged
= true;
309 ListIterator
<StateData
> itrExistingStates
= getEntity().getStates().listIterator();
310 while(itrExistingStates
.hasNext()) {
311 StateData existingStateData
= itrExistingStates
.next();
312 Map
<Language
, LanguageString
> modifyingText
= existingStateData
314 if (modifyingText
.get(CdmStore
.getDefaultLanguage()) != null) {
315 if (modifyingText
.get(CdmStore
.getDefaultLanguage()).getText().equals(UsageTermCollection
.useCategoryVocabularyLabel
)) {
317 if(!existingStateData
.getState().getTitleCache().equals(combo_UseCategory
.getSelection().getTitleCache())) {
318 itrExistingStates
.remove();
319 StateData stateData
= StateData
.NewInstance(combo_UseCategory
.getSelection());
320 stateData
.setState(combo_UseCategory
.getSelection());
321 stateData
.putModifyingText(CdmStore
.getDefaultLanguage(),UsageTermCollection
.useCategoryVocabularyLabel
);
322 itrExistingStates
.add(stateData
);
325 } else if (modifyingText
.get(CdmStore
.getDefaultLanguage()).getText().equals(UsageTermCollection
.useSubCategoryVocabularyLabel
) && isUseCategoryChanged
== true) {
326 itrExistingStates
.remove();
333 if (isChanged
== false) {
334 StateData stateData
= StateData
335 .NewInstance(combo_UseCategory
.getSelection());
336 stateData
.setState(combo_UseCategory
.getSelection());
337 stateData
.putModifyingText(CdmStore
.getDefaultLanguage(), UsageTermCollection
.useCategoryVocabularyLabel
);
338 getEntity().addState(stateData
);
341 combo_UseSubCategory
.setEnabled(true);
345 if (!getEntity().getStates().isEmpty()) {
346 Iterator
<StateData
> itr
= getEntity().getStates().iterator();
347 while(itr
.hasNext()) {
348 StateData stateToRemove
= itr
.next();
351 List
<State
> termsSubCategory
= new ArrayList
<State
>();
352 State emptyState
= State
.NewInstance();
353 termsSubCategory
.add(emptyState
);
354 combo_UseSubCategory
.setTerms(termsSubCategory
);
355 combo_UseSubCategory
.setEnabled(false);
360 if (eventSource
== combo_UseSubCategory
) {
361 if (combo_UseSubCategory
.getSelection() != null) {
362 boolean isChanged
= false;
363 if (!getEntity().getStates().isEmpty()) {
364 ListIterator
<StateData
> itrExistingStates
= getEntity().getStates().listIterator();
365 while(itrExistingStates
.hasNext()) {
366 StateData existingStateData
= itrExistingStates
.next();
367 Map
<Language
, LanguageString
> modifyingText
= existingStateData
369 if (modifyingText
.get(CdmStore
.getDefaultLanguage()) != null) {
370 if (modifyingText
.get(CdmStore
.getDefaultLanguage()).getText().equals(UsageTermCollection
.useSubCategoryVocabularyLabel
)) {
372 if(!existingStateData
.getState().getTitleCache().equals(combo_UseSubCategory
.getSelection().getTitleCache())) {
373 itrExistingStates
.remove();
374 StateData stateData
= StateData
.NewInstance(combo_UseSubCategory
.getSelection());
375 stateData
.setState(combo_UseSubCategory
.getSelection());
376 stateData
.putModifyingText(CdmStore
.getDefaultLanguage(),UsageTermCollection
.useSubCategoryVocabularyLabel
);
377 itrExistingStates
.add(stateData
);
384 if (isChanged
== false){
385 StateData stateData
= StateData
386 .NewInstance(combo_UseSubCategory
.getSelection());
387 stateData
.setState(combo_UseSubCategory
.getSelection());
388 stateData
.putModifyingText(CdmStore
.getDefaultLanguage(), UsageTermCollection
.useSubCategoryVocabularyLabel
);
389 getEntity().addState(stateData
);
393 if (!getEntity().getStates().isEmpty()) {
394 Iterator
<StateData
> itrStateData
= getEntity().getStates().iterator();
395 while(itrStateData
.hasNext()) {
396 StateData existingStateData
= itrStateData
.next();
397 Map
<Language
, LanguageString
> modifyingText
= existingStateData
400 if (modifyingText
.get(CdmStore
.getDefaultLanguage()) != null) {
401 if (modifyingText
.get(CdmStore
.getDefaultLanguage()).getText() == UsageTermCollection
.useSubCategoryVocabularyLabel
) {
402 itrStateData
.remove();
409 if (eventSource
== combo_PlantPart
) {
410 if (combo_PlantPart
.getSelection() != null) {
411 Iterator
<Modifier
> itrExistingModifiers
= getEntity().getModifiers().iterator();
412 while (itrExistingModifiers
.hasNext()) {
413 Modifier modifier
= itrExistingModifiers
.next();
414 if(GetVocabularyType(modifier
, UsageTermCollection
.plantPartLabel
)) {
415 itrExistingModifiers
.remove();
419 Modifier plantPart
= combo_PlantPart
.getSelection();
420 getEntity().addModifier(plantPart
);
422 Set
<Modifier
> modifiers
= getEntity().getModifiers();
423 if(!modifiers
.isEmpty()) {
424 Iterator
<Modifier
> itr
= modifiers
.iterator();
425 while (itr
.hasNext()) {
426 Modifier currentMod
= itr
.next();
427 if(GetVocabularyType(currentMod
, UsageTermCollection
.plantPartLabel
)) {
435 if (eventSource
== combo_Country
) {
436 if (combo_Country
.getSelection() != null) {
437 Iterator
<Modifier
> itrExistingModifiers
= getEntity().getModifiers().iterator();
438 while (itrExistingModifiers
.hasNext()) {
439 Modifier modifier
= itrExistingModifiers
.next();
440 if(GetVocabularyType(modifier
, UsageTermCollection
.countryLabel
)) {
441 itrExistingModifiers
.remove();
445 Modifier country
= combo_Country
.getSelection();
446 getEntity().addModifier(country
);
448 Set
<Modifier
> modifiers
= getEntity().getModifiers();
449 if(!modifiers
.isEmpty()) {
450 Iterator
<Modifier
> itr
= modifiers
.iterator();
451 while (itr
.hasNext()) {
452 Modifier currentMod
= itr
.next();
453 if(GetVocabularyType(currentMod
, UsageTermCollection
.countryLabel
)) {
460 if (eventSource
== combo_HumanGroup
) {
461 if (combo_HumanGroup
.getSelection() != null) {
462 boolean isHumanGroupChanged
= true;
463 for (Modifier modToCheck
: getEntity().getModifiers()) {
464 if(modToCheck
.equals(combo_HumanGroup
.getSelection()) && (GetVocabularyType(modToCheck
, UsageTermCollection
.humanGroupLabel
))) {
465 isHumanGroupChanged
= false;
470 Iterator
<Modifier
> itrExistingModifiers
= getEntity().getModifiers().iterator();
471 while (itrExistingModifiers
.hasNext()) {
472 Modifier modifier
= itrExistingModifiers
.next();
473 if((GetVocabularyType(modifier
, UsageTermCollection
.humanGroupLabel
) || GetVocabularyType(modifier
, UsageTermCollection
.ethnicGroupLabel
)) && isHumanGroupChanged
) {
474 itrExistingModifiers
.remove();
478 if(isHumanGroupChanged
){
479 Modifier humanGroup
= combo_HumanGroup
.getSelection();
480 getEntity().addModifier(humanGroup
);
482 combo_EthnicGroup
.setEnabled(true);
484 Set
<Modifier
> modifiers
= getEntity().getModifiers();
485 if(!modifiers
.isEmpty()) {
486 Iterator
<Modifier
> itr
= modifiers
.iterator();
487 while (itr
.hasNext()) {
488 Modifier currentMod
= itr
.next();
489 if(GetVocabularyType(currentMod
, UsageTermCollection
.humanGroupLabel
) || GetVocabularyType(currentMod
, UsageTermCollection
.ethnicGroupLabel
)) {
494 List
<Modifier
> emptyListToResetComboBox
= new ArrayList
<Modifier
>();
495 Modifier emptyMod
= Modifier
.NewInstance();
496 emptyListToResetComboBox
.add(emptyMod
);
497 combo_EthnicGroup
.setTerms(emptyListToResetComboBox
);
498 combo_EthnicGroup
.setEnabled(false);
502 if (eventSource
== combo_EthnicGroup
) {
503 if (combo_EthnicGroup
.getSelection() != null) {
504 Iterator
<Modifier
> itrExistingModifiers
= getEntity().getModifiers().iterator();
505 while (itrExistingModifiers
.hasNext()) {
506 Modifier modifier
= itrExistingModifiers
.next();
507 if(GetVocabularyType(modifier
, combo_HumanGroup
.getSelection().getTitleCache())) {
508 itrExistingModifiers
.remove();
512 Modifier ethnicGroup
= combo_EthnicGroup
.getSelection();
513 getEntity().addModifier(ethnicGroup
);
515 Set
<Modifier
> modifiers
= getEntity().getModifiers();
516 if(!modifiers
.isEmpty()) {
518 Iterator
<Modifier
> itr
= modifiers
.iterator();
519 while (itr
.hasNext()) {
520 Modifier currentMod
= itr
.next();
521 if(GetVocabularyType(currentMod
, UsageTermCollection
.ethnicGroupLabel
)) {
534 * This function Checks the vocabulary type
536 * @param vocabularyExpected
539 private boolean GetVocabularyType(Modifier term
, String vocabularyExpected
) {
540 if ((term
.getPartOf() != null) && (term
.getPartOf().getTitleCache().equals(vocabularyExpected
))) {
543 else if ((term
.getVocabulary() != null) && (term
.getPartOf()==null) && (term
.getVocabulary().getTitleCache().equals(vocabularyExpected
))) {
546 else if ((vocabularyExpected
.equals(UsageTermCollection
.ethnicGroupLabel
)) && ((term
.getPartOf() != null))) {
556 public void setEntity(CategoricalData entity
) {
557 super.setEntity(entity
);
562 public void widgetSelected(SelectionEvent e
) {
563 Object eventSource
= e
.getSource();
564 for (Object object
: combo_UseCategory
.getControls()) {
565 if (object
== eventSource
) {
566 State selectedUseCategory
= combo_UseCategory
.getSelection();
567 if (selectedUseCategory
!= null) {
568 combo_UseSubCategory
.setTerms(setUseCategoryComboTerms(TermStore
.getTerms(State
.class, null, false),selectedUseCategory
));
570 if (!getEntity().getStates().isEmpty()) {
571 Iterator
<StateData
> itrExistingState
= getEntity().getStates().iterator();
572 while(itrExistingState
.hasNext()) {
573 StateData existingStateData
= (StateData
) itrExistingState
.next();
574 Map
<Language
, LanguageString
> modifyingText
= existingStateData
.getModifyingText();
575 if (modifyingText
.get(CdmStore
.getDefaultLanguage()) != null) {
576 if (modifyingText
.get(CdmStore
.getDefaultLanguage())
577 .getText().equals(UsageTermCollection
.useSubCategoryVocabularyLabel
)) {
578 combo_UseSubCategory
.setSelection(stateSelection(UsageTermCollection
.useSubCategoryVocabularyLabel
));
586 for (Object object
: combo_HumanGroup
.getControls()) {
587 if (object
== eventSource
) {
588 Modifier selectedHumanGroup
= combo_HumanGroup
.getSelection();
589 if (selectedHumanGroup
!= null) {
590 combo_EthnicGroup
.setTerms(setEthnicGroupComboTerms(TermStore
.getTerms(Modifier
.class, null, false), selectedHumanGroup
));
591 if (!getEntity().getModifiers().isEmpty()) {
592 Iterator
<Modifier
> iterModifiers
= getEntity().getModifiers().iterator();
593 while (iterModifiers
.hasNext()) {
594 Modifier existingModifier
= iterModifiers
.next();
595 if(GetVocabularyType(existingModifier
, UsageTermCollection
.ethnicGroupLabel
)) {
596 combo_EthnicGroup
.setSelection(modifierSelection(UsageTermCollection
.ethnicGroupLabel
));
608 public void widgetDefaultSelected(SelectionEvent e
) {
609 // TODO Auto-generated method stub