1 package eu
.etaxonomy
.taxeditor
.ui
.section
.userecords
;
3 import java
.util
.ArrayList
;
4 import java
.util
.Iterator
;
6 import java
.util
.ListIterator
;
9 import org
.apache
.log4j
.Logger
;
10 import org
.eclipse
.swt
.events
.SelectionEvent
;
11 import org
.eclipse
.swt
.events
.SelectionListener
;
12 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
13 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
14 import eu
.etaxonomy
.cdm
.model
.common
.Marker
;
15 import eu
.etaxonomy
.cdm
.model
.description
.CategoricalData
;
16 import eu
.etaxonomy
.cdm
.model
.description
.Modifier
;
17 import eu
.etaxonomy
.cdm
.model
.description
.State
;
18 import eu
.etaxonomy
.cdm
.model
.description
.StateData
;
19 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
20 import eu
.etaxonomy
.taxeditor
.store
.TermStore
;
21 import eu
.etaxonomy
.taxeditor
.ui
.combo
.TermComboElement
;
22 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
23 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
24 import eu
.etaxonomy
.taxeditor
.ui
.section
.AbstractCdmDetailElement
;
25 import eu
.etaxonomy
.taxeditor
.ui
.section
.description
.DescriptionElementDetailElement
;
27 public class UseRecordDetailElement
extends
28 AbstractCdmDetailElement
<CategoricalData
> implements SelectionListener
{
30 private static final Logger logger
= Logger
31 .getLogger(UseRecordDetailElement
.class);
33 private TermComboElement
<State
> combo_UseCategory
;
34 private TermComboElement
<State
> combo_UseSubCategory
;
35 private TermComboElement
<Modifier
> combo_PlantPart
;
36 private TermComboElement
<Modifier
> combo_Country
;
37 private TermComboElement
<Modifier
> combo_HumanGroup
;
38 private TermComboElement
<Modifier
> combo_EthnicGroup
;
40 public UseRecordDetailElement(CdmFormFactory formFactory
,
41 ICdmFormElement formElement
) {
42 super(formFactory
, formElement
);
47 protected void createControls(ICdmFormElement formElement
,
48 CategoricalData entity
, int style
) {
50 combo_UseCategory
= formFactory
.createTermComboElement(State
.class, this, "Use Category", null, style
);
51 combo_UseCategory
.addSelectionListener(this);
52 combo_UseSubCategory
= formFactory
.createTermComboElement(State
.class, this, "Use SubCategory", null, style
);
53 combo_Country
= formFactory
.createTermComboElement(Modifier
.class, this, "Country", null, style
);
54 combo_PlantPart
= formFactory
.createTermComboElement(Modifier
.class, this, "Plant Part", null, style
);
55 combo_HumanGroup
= formFactory
.createTermComboElement(Modifier
.class, this, "Human Group", null, style
);
56 combo_HumanGroup
.addSelectionListener(this);
57 combo_EthnicGroup
= formFactory
.createTermComboElement(Modifier
.class, this, "Ethnic Group", null, style
);
60 List
<State
> termsUseCategory
= setUseCategoryComboTerms(TermStore
.getTerms(State
.class, null, false));
61 combo_UseCategory
.setTerms(termsUseCategory
);
62 combo_UseCategory
.setSelection(stateSelection("Use Category"));
64 List
<State
> termsSubCategory
= setUseCategoryComboTerms(TermStore
.getTerms(State
.class, null, false), combo_UseCategory
.getSelection());
66 if (termsSubCategory
!= null) {
67 combo_UseSubCategory
.setTerms(termsSubCategory
);
68 combo_UseSubCategory
.setSelection(stateSelection("Use SubCategory"));
69 combo_UseSubCategory
.setEnabled(true);
72 combo_UseSubCategory
.setEnabled(false);
75 List
<Modifier
> plantPartsToAdd
= setModifierComboTerms(TermStore
.getTerms(Modifier
.class, null, false), "Plant Part");
76 combo_PlantPart
.setTerms(plantPartsToAdd
);
77 combo_PlantPart
.setSelection(modifierSelection("Plant Part"));
79 List
<Modifier
> countriesToAdd
= setModifierComboTerms(TermStore
.getTerms(Modifier
.class, null, false), "Country");
80 combo_Country
.setTerms(countriesToAdd
);
81 combo_Country
.setSelection(modifierSelection("Country"));
83 List
<Modifier
> humanGroupToAdd
= setModifierComboTerms(TermStore
.getTerms(Modifier
.class, null, false), "Human Group");
84 combo_HumanGroup
.setTerms(humanGroupToAdd
);
85 combo_HumanGroup
.setSelection(modifierSelection("Human Group"));
87 List
<Modifier
> ethnicGroupToAdd
= setEthnicGroupComboTerms(TermStore
.getTerms(Modifier
.class, null, false), combo_HumanGroup
.getSelection());
88 if (ethnicGroupToAdd
!= null) {
89 combo_EthnicGroup
.setTerms(ethnicGroupToAdd
);
90 combo_EthnicGroup
.setSelection(modifierSelection("Ethnic Group"));
91 combo_EthnicGroup
.setEnabled(true);
93 combo_EthnicGroup
.setEnabled(false);
98 private Modifier
modifierSelection(String comboCategory
) {
99 if(!getEntity().getModifiers().isEmpty()) {
100 for (Modifier modifier
: getEntity().getModifiers()) {
101 if(GetVocabularyType(modifier
, comboCategory
)) {
112 private State
stateSelection(String comboCategory
) {
113 if (!getEntity().getStates().isEmpty()) {
114 for (StateData statedata
: getEntity().getStates()) {
115 if (statedata
.getModifyingText().get(CdmStore
.getDefaultLanguage()) != null) {
116 String testString
= statedata
.getModifyingText().get(CdmStore
.getDefaultLanguage()).getText();
117 if(testString
.equals(comboCategory
)) {
118 if(statedata
.getState() !=null) {
119 return statedata
.getState();
132 private List
<Modifier
> setModifierComboTerms(List
<Modifier
> listOfTerms
, String modType
) {
133 List
<Modifier
> termsToAdd
= new ArrayList
<Modifier
>();
134 for (Modifier term
: listOfTerms
) {
136 if ((term
.getPartOf() != null) && (term
.getPartOf().getTitleCache().equals(modType
))) {
137 termsToAdd
.add(term
);
139 else if ((term
.getVocabulary() != null) && (term
.getPartOf() == null) && (term
.getVocabulary().getTitleCache().equals(modType
))) {
140 termsToAdd
.add(term
);
149 private List
<State
> setUseCategoryComboTerms(List
<State
> listOfTerms
) {
150 List
<State
> termsToAdd
= new ArrayList
<State
>();
151 for (State term
: listOfTerms
) {
153 if ((term
.getPartOf() != null) && (term
.getPartOf().getTitleCache().equals("Use Category"))) {
154 termsToAdd
.add(term
);
156 else if ((term
.getVocabulary() !=null) && (term
.getPartOf() == null) && (term
.getVocabulary().getTitleCache().equals("Use Category"))) {
157 termsToAdd
.add(term
);
164 private List
<State
> setUseCategoryComboTerms(List
<State
> listOfTerms
,
165 State selectedUseCategory
) {
166 List
<State
> termsToAdd
= new ArrayList
<State
>();
167 if (combo_UseCategory
.getSelection() != null) {
169 for (State term
: listOfTerms
) {
170 if ((term
.getPartOf() != null) && (term
.getPartOf().getTitleCache().equals(selectedUseCategory
.getTitleCache()))) {
171 termsToAdd
.add(term
);
173 else if ((term
.getVocabulary() !=null) && (term
.getVocabulary().getTitleCache().equals(selectedUseCategory
.getTitleCache()))) {
174 termsToAdd
.add(term
);
179 else if (!getEntity().getStates().isEmpty()) {
180 for (StateData stateData
: getEntity().getStates()) {
181 if (stateData
.getModifyingText().get(CdmStore
.getDefaultLanguage()) != null) {
182 if ((stateData
.getState() != null) && (stateData
.getModifyingText().get(CdmStore
.getDefaultLanguage()).getText() == "Use Category")) {
183 for (State term
: listOfTerms
) {
184 if ((term
.getPartOf() != null) && (term
.getPartOf().getTitleCache().equals(stateData
.getState().getTitleCache()))) {
185 termsToAdd
.add(term
);
186 } else if ((term
.getVocabulary() != null) && (term
.getVocabulary().getTitleCache().equals(stateData
.getState().getTitleCache()))) {
187 termsToAdd
.add(term
);
204 private List
<Modifier
> setEthnicGroupComboTerms(List
<Modifier
> listOfTerms
,
205 Modifier selectedHumangroup
) {
206 List
<Modifier
> termsToAdd
= new ArrayList
<Modifier
>();
207 if (combo_HumanGroup
.getSelection() != null) {
208 for (Modifier term
: listOfTerms
) {
210 if ((term
.getPartOf() != null) && (term
.getPartOf().getTitleCache().equals(selectedHumangroup
.getTitleCache()))) {
211 termsToAdd
.add(term
);
213 else if ((term
.getVocabulary() != null) && (term
.getVocabulary().getTitleCache().equals(selectedHumangroup
.getTitleCache()))) {
214 termsToAdd
.add(term
);
218 else if (!getEntity().getModifiers().isEmpty()) {
219 for (Modifier modifier
: getEntity().getModifiers()) {
220 if (modifier
.getMarkers() != null) {
221 for (Marker marker
: modifier
.getMarkers()) {
222 if(marker
.getMarkerType().getTitleCache().equals("Human Group")) {
223 for (Modifier term
: listOfTerms
) {
224 if ((term
.getPartOf() != null) && (term
.getPartOf().getTitleCache().equals(modifier
.getTitleCache()))) {
225 termsToAdd
.add(modifier
);
226 } else if ((term
.getVocabulary() != null) && (term
.getVocabulary().getTitleCache().equals(modifier
.getTitleCache()))) {
227 termsToAdd
.add(modifier
);
244 public void handleEvent(Object eventSource
) {
246 * if(eventSource == checkbox_orderRelevant){
247 * getEntity().setOrderRelevant(checkbox_orderRelevant.getSelection());
250 if (eventSource
== combo_UseCategory
) {
251 if (combo_UseCategory
.getSelection() != null) {
252 boolean isChanged
= false;
253 boolean isUseCategoryChanged
= false;
254 if (!getEntity().getStates().isEmpty()) {
255 for (StateData useCategoryStateDataCheck
: getEntity().getStates()) {
256 Map
<Language
, LanguageString
> modText
= useCategoryStateDataCheck
.getModifyingText();
257 if(modText
.get(CdmStore
.getDefaultLanguage()) != null && modText
.get(CdmStore
.getDefaultLanguage()).getText().equals("Use Category")) {
258 if(!useCategoryStateDataCheck
.getState().getTitleCache().equals(combo_UseCategory
.getSelection().getTitleCache())) {
259 isUseCategoryChanged
= true;
264 ListIterator
<StateData
> itrExistingStates
= getEntity().getStates().listIterator();
265 while(itrExistingStates
.hasNext()) {
266 StateData existingStateData
= itrExistingStates
.next();
267 Map
<Language
, LanguageString
> modifyingText
= existingStateData
269 if (modifyingText
.get(CdmStore
.getDefaultLanguage()) != null) {
270 if (modifyingText
.get(CdmStore
.getDefaultLanguage()).getText().equals("Use Category")) {
272 if(!existingStateData
.getState().getTitleCache().equals(combo_UseCategory
.getSelection().getTitleCache())) {
273 itrExistingStates
.remove();
274 StateData stateData
= StateData
.NewInstance(combo_UseCategory
.getSelection());
275 stateData
.setState(combo_UseCategory
.getSelection());
276 stateData
.putModifyingText(CdmStore
.getDefaultLanguage(),"Use Category");
277 itrExistingStates
.add(stateData
);
280 } else if (modifyingText
.get(CdmStore
.getDefaultLanguage()).getText().equals("Use SubCategory") && isUseCategoryChanged
== true) {
281 itrExistingStates
.remove();
288 if (isChanged
== false) {
289 StateData stateData
= StateData
290 .NewInstance(combo_UseCategory
.getSelection());
291 stateData
.setState(combo_UseCategory
.getSelection());
292 stateData
.putModifyingText(CdmStore
.getDefaultLanguage(), "Use Category");
293 getEntity().addState(stateData
);
296 combo_UseSubCategory
.setEnabled(true);
300 if (!getEntity().getStates().isEmpty()) {
301 Iterator
<StateData
> itr
= getEntity().getStates().iterator();
302 while(itr
.hasNext()) {
303 StateData stateToRemove
= itr
.next();
306 List
<State
> termsSubCategory
= new ArrayList
<State
>();
307 State emptyState
= State
.NewInstance();
308 termsSubCategory
.add(emptyState
);
309 combo_UseSubCategory
.setTerms(termsSubCategory
);
310 combo_UseSubCategory
.setEnabled(false);
315 if (eventSource
== combo_UseSubCategory
) {
316 if (combo_UseSubCategory
.getSelection() != null) {
317 boolean isChanged
= false;
318 if (!getEntity().getStates().isEmpty()) {
319 ListIterator
<StateData
> itrExistingStates
= getEntity().getStates().listIterator();
320 while(itrExistingStates
.hasNext()) {
321 StateData existingStateData
= itrExistingStates
.next();
322 Map
<Language
, LanguageString
> modifyingText
= existingStateData
324 if (modifyingText
.get(CdmStore
.getDefaultLanguage()) != null) {
325 if (modifyingText
.get(CdmStore
.getDefaultLanguage()).getText().equals("Use SubCategory")) {
327 if(!existingStateData
.getState().getTitleCache().equals(combo_UseSubCategory
.getSelection().getTitleCache())) {
328 itrExistingStates
.remove();
329 StateData stateData
= StateData
.NewInstance(combo_UseSubCategory
.getSelection());
330 stateData
.setState(combo_UseSubCategory
.getSelection());
331 stateData
.putModifyingText(CdmStore
.getDefaultLanguage(),"Use SubCategory");
332 itrExistingStates
.add(stateData
);
339 if (isChanged
== false){
340 StateData stateData
= StateData
341 .NewInstance(combo_UseSubCategory
.getSelection());
342 stateData
.setState(combo_UseSubCategory
.getSelection());
343 stateData
.putModifyingText(CdmStore
.getDefaultLanguage(), "Use SubCategory");
344 getEntity().addState(stateData
);
348 if (!getEntity().getStates().isEmpty()) {
349 Iterator
<StateData
> itrStateData
= getEntity().getStates().iterator();
350 while(itrStateData
.hasNext()) {
351 StateData existingStateData
= itrStateData
.next();
352 Map
<Language
, LanguageString
> modifyingText
= existingStateData
355 if (modifyingText
.get(CdmStore
.getDefaultLanguage()) != null) {
356 if (modifyingText
.get(CdmStore
.getDefaultLanguage()).getText() == "Use SubCategory") {
357 itrStateData
.remove();
364 if (eventSource
== combo_PlantPart
) {
365 if (combo_PlantPart
.getSelection() != null) {
366 Iterator
<Modifier
> itrExistingModifiers
= getEntity().getModifiers().iterator();
367 while (itrExistingModifiers
.hasNext()) {
368 Modifier modifier
= itrExistingModifiers
.next();
369 if(GetVocabularyType(modifier
, "Plant Part")) {
370 itrExistingModifiers
.remove();
374 Modifier plantPart
= combo_PlantPart
.getSelection();
375 getEntity().addModifier(plantPart
);
377 Set
<Modifier
> modifiers
= getEntity().getModifiers();
378 if(!modifiers
.isEmpty()) {
379 Iterator
<Modifier
> itr
= modifiers
.iterator();
380 while (itr
.hasNext()) {
381 Modifier currentMod
= itr
.next();
382 if(GetVocabularyType(currentMod
, "Plant Part")) {
390 if (eventSource
== combo_Country
) {
391 if (combo_Country
.getSelection() != null) {
392 Iterator
<Modifier
> itrExistingModifiers
= getEntity().getModifiers().iterator();
393 while (itrExistingModifiers
.hasNext()) {
394 Modifier modifier
= itrExistingModifiers
.next();
395 if(GetVocabularyType(modifier
, "Country")) {
396 itrExistingModifiers
.remove();
400 Modifier country
= combo_Country
.getSelection();
401 getEntity().addModifier(country
);
403 Set
<Modifier
> modifiers
= getEntity().getModifiers();
404 if(!modifiers
.isEmpty()) {
405 Iterator
<Modifier
> itr
= modifiers
.iterator();
406 while (itr
.hasNext()) {
407 Modifier currentMod
= itr
.next();
408 if(GetVocabularyType(currentMod
, "Country")) {
415 if (eventSource
== combo_HumanGroup
) {
416 if (combo_HumanGroup
.getSelection() != null) {
417 boolean isHumanGroupChanged
= true;
418 for (Modifier modToCheck
: getEntity().getModifiers()) {
419 if(modToCheck
.equals(combo_HumanGroup
.getSelection()) && (GetVocabularyType(modToCheck
, "Human Group"))) {
420 isHumanGroupChanged
= false;
425 Iterator
<Modifier
> itrExistingModifiers
= getEntity().getModifiers().iterator();
426 while (itrExistingModifiers
.hasNext()) {
427 Modifier modifier
= itrExistingModifiers
.next();
428 if((GetVocabularyType(modifier
, "Human Group") || GetVocabularyType(modifier
, "Ethnic Group")) && isHumanGroupChanged
) {
429 itrExistingModifiers
.remove();
433 if(isHumanGroupChanged
){
434 Modifier humanGroup
= combo_HumanGroup
.getSelection();
435 getEntity().addModifier(humanGroup
);
437 combo_EthnicGroup
.setEnabled(true);
439 Set
<Modifier
> modifiers
= getEntity().getModifiers();
440 if(!modifiers
.isEmpty()) {
441 Iterator
<Modifier
> itr
= modifiers
.iterator();
442 while (itr
.hasNext()) {
443 Modifier currentMod
= itr
.next();
444 if(GetVocabularyType(currentMod
, "Human Group") || GetVocabularyType(currentMod
, "Ethnic Group")) {
449 List
<Modifier
> emptyListToResetComboBox
= new ArrayList
<Modifier
>();
450 Modifier emptyMod
= Modifier
.NewInstance();
451 emptyListToResetComboBox
.add(emptyMod
);
452 combo_EthnicGroup
.setTerms(emptyListToResetComboBox
);
453 combo_EthnicGroup
.setEnabled(false);
457 if (eventSource
== combo_EthnicGroup
) {
458 if (combo_EthnicGroup
.getSelection() != null) {
459 Iterator
<Modifier
> itrExistingModifiers
= getEntity().getModifiers().iterator();
460 while (itrExistingModifiers
.hasNext()) {
461 Modifier modifier
= itrExistingModifiers
.next();
462 if(GetVocabularyType(modifier
, combo_HumanGroup
.getSelection().getTitleCache())) {
463 itrExistingModifiers
.remove();
467 Modifier ethnicGroup
= combo_EthnicGroup
.getSelection();
468 getEntity().addModifier(ethnicGroup
);
470 Set
<Modifier
> modifiers
= getEntity().getModifiers();
471 if(!modifiers
.isEmpty()) {
473 Iterator
<Modifier
> itr
= modifiers
.iterator();
474 while (itr
.hasNext()) {
475 Modifier currentMod
= itr
.next();
476 if(GetVocabularyType(currentMod
, "Ethnic Group")) {
488 private boolean GetVocabularyType(Modifier term
, String vocabularyExpected
) {
489 if ((term
.getPartOf() != null) && (term
.getPartOf().getTitleCache().equals(vocabularyExpected
))) {
492 else if ((term
.getVocabulary() != null) && (term
.getPartOf()==null) && (term
.getVocabulary().getTitleCache().equals(vocabularyExpected
))) {
495 else if ((vocabularyExpected
.equals("Ethnic Group")) && (term
.getVocabulary() != null) && ((term
.getPartOf() != null))) {
505 public void setEntity(CategoricalData entity
) {
506 super.setEntity(entity
);
510 public void widgetSelected(SelectionEvent e
) {
511 Object eventSource
= e
.getSource();
512 for (Object object
: combo_UseCategory
.getControls()) {
513 if (object
== eventSource
) {
514 State selectedUseCategory
= combo_UseCategory
.getSelection();
515 //List<State> newSubCategories = setUseCategoryComboTerms(TermStore.getTerms(State.class, null, false),selectedUseCategory);
516 if (selectedUseCategory
!= null) {
517 combo_UseSubCategory
.setTerms(setUseCategoryComboTerms(TermStore
.getTerms(State
.class, null, false),selectedUseCategory
));
519 if (!getEntity().getStates().isEmpty()) {
520 Iterator
<StateData
> itrExistingState
= getEntity().getStates().iterator();
521 while(itrExistingState
.hasNext()) {
522 StateData existingStateData
= (StateData
) itrExistingState
.next();
523 Map
<Language
, LanguageString
> modifyingText
= existingStateData
.getModifyingText();
524 if (modifyingText
.get(CdmStore
.getDefaultLanguage()) != null) {
525 if (modifyingText
.get(CdmStore
.getDefaultLanguage())
526 .getText().equals("Use SubCategory")) {
527 combo_UseSubCategory
.setSelection(stateSelection("Use SubCategory"));
535 for (Object object
: combo_HumanGroup
.getControls()) {
536 if (object
== eventSource
) {
537 Modifier selectedHumanGroup
= combo_HumanGroup
.getSelection();
538 if (selectedHumanGroup
!= null) {
539 combo_EthnicGroup
.setTerms(setEthnicGroupComboTerms(TermStore
.getTerms(Modifier
.class, null, false), selectedHumanGroup
));
540 if (!getEntity().getModifiers().isEmpty()) {
541 Iterator
<Modifier
> iterModifiers
= getEntity().getModifiers().iterator();
542 while (iterModifiers
.hasNext()) {
543 Modifier existingModifier
= iterModifiers
.next();
544 if(GetVocabularyType(existingModifier
, "Ethnic Group")) {
545 combo_EthnicGroup
.setSelection(modifierSelection("Ethnic Group"));
556 public void widgetDefaultSelected(SelectionEvent e
) {
557 // TODO Auto-generated method stub