adapt master to develop
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / occurrence / FieldUnitFacadeGeneralDetailElement.java
1 /**
2 * Copyright (C) 2013 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 package eu.etaxonomy.taxeditor.ui.section.occurrence;
10
11 import java.util.HashSet;
12 import java.util.Set;
13
14 import eu.etaxonomy.cdm.facade.DerivedUnitFacade;
15 import eu.etaxonomy.cdm.model.agent.Person;
16 import eu.etaxonomy.cdm.model.agent.Team;
17 import eu.etaxonomy.cdm.model.common.LanguageString;
18 import eu.etaxonomy.cdm.model.location.NamedArea;
19 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
20 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
21 import eu.etaxonomy.taxeditor.store.CdmStore;
22 import eu.etaxonomy.taxeditor.store.StoreUtil;
23 import eu.etaxonomy.taxeditor.ui.combo.EnumComboElement;
24 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
25 import eu.etaxonomy.taxeditor.ui.element.CheckboxElement;
26 import eu.etaxonomy.taxeditor.ui.element.GatheringEventUnitElement;
27 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
28 import eu.etaxonomy.taxeditor.ui.element.LanguageStringWithLabelElement;
29 import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
30 import eu.etaxonomy.taxeditor.ui.element.MinMaxTextSection;
31 import eu.etaxonomy.taxeditor.ui.element.NumberWithLabelElement;
32 import eu.etaxonomy.taxeditor.ui.element.PointElement;
33 import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
34 import eu.etaxonomy.taxeditor.ui.element.TimePeriodElement;
35 import eu.etaxonomy.taxeditor.ui.element.ToggleableTextElement;
36 import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
37 import eu.etaxonomy.taxeditor.ui.selection.CollectorSelectionElement;
38 import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
39
40 /**
41 * @author pplitzner
42 * @date 05.11.2013
43 */
44 public class FieldUnitFacadeGeneralDetailElement extends AbstractCdmDetailElement<DerivedUnitFacade> {
45
46 private ToggleableTextElement toggleableText_titleCache;
47 private EnumComboElement<SpecimenOrObservationType> combo_specorobstype;
48 private EntitySelectionElement<NamedArea> selection_country;
49 private CollectingAreasDetailSection section_collectingAreas;
50 private LanguageStringWithLabelElement languageText_locality;
51 private PointElement element_point;
52 private NumberWithLabelElement number_elevation;
53 private TimePeriodElement element_date;
54 private CollectorSelectionElement selection_collector;
55 private CollectorSelectionElement selection_primary_collector;
56 private TextWithLabelElement text_collectingNumber;
57 private GatheringEventUnitElement element_elevation;
58 private CheckboxElement checkIsPublish;
59
60 public FieldUnitFacadeGeneralDetailElement(CdmFormFactory formFactory, ICdmFormElement formElement) {
61 super(formFactory, formElement);
62 }
63
64 @Override
65 protected void createControls(ICdmFormElement formElement, DerivedUnitFacade entity, int style) {
66 toggleableText_titleCache = formFactory.createToggleableTextField(formElement, "Title Cache",
67 entity.getTitleCache(), entity.isProtectedTitleCache(), style);
68
69 if (!entity.getType().equals(SpecimenOrObservationType.FieldUnit)){
70 combo_specorobstype = formFactory.createEnumComboElement(SpecimenOrObservationType.class, formElement, style);
71 combo_specorobstype.setSelection(entity.getType());
72 }
73
74 selection_country = formFactory.createSelectionElement(NamedArea.class,
75 formElement, "Country", entity.getCountry(), EntitySelectionElement.DELETABLE, style);
76 if(PreferencesUtil.isCollectingAreaInGeneralSection()){
77 section_collectingAreas = formFactory.createCollectingAreasDetailSection(formElement, StoreUtil.getSectionStyle(CollectingAreasDetailSection.class, entity.getClass().getCanonicalName()));
78 section_collectingAreas.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
79 section_collectingAreas.setEntity(entity);
80 }
81
82 languageText_locality = formFactory.createLanguageStringWithLabelElement(formElement, "Locality",
83 entity.getLocality(), style);
84 element_point = formFactory.createPointElement(formElement, entity.getExactLocation(), style);
85 element_elevation = formFactory.createGatheringEventUnitElement(
86 formElement, "Elevation : ", entity, MinMaxTextSection.UnitType.ELEVATION, style);
87
88 element_date = formFactory.createTimePeriodElement(formElement, "Date", entity.getGatheringPeriod(), style);
89 selection_collector = formFactory.createCollectorSelectionElement(formElement, "Collector", entity.getCollector(), EntitySelectionElement.ALL, style);
90 selection_primary_collector = formFactory
91 .createCollectorSelectionElement(
92 formElement, "Primary collector",
93 entity.getCollector(), entity.getPrimaryCollector(), EntitySelectionElement.ALL,
94 style);
95 if (!(entity.getCollector() instanceof Team)){
96 selection_primary_collector.setEnabled(false);
97 }
98 selection_primary_collector.setIndent(8);
99 text_collectingNumber = formFactory.createTextWithLabelElement(formElement, "Collecting number",
100 entity.getFieldNumber(), style);
101 checkIsPublish = formFactory.createCheckbox(formElement, "Publish", entity.innerFieldUnit().isPublish(), style);
102 }
103
104 @Override
105 public void handleEvent(Object eventSource) {
106 if (eventSource == toggleableText_titleCache) {
107 getEntity().setTitleCache(toggleableText_titleCache.getText(), toggleableText_titleCache.getState());
108
109 } else if (eventSource == selection_country) {
110 getEntity().setCountry(selection_country.getSelection());
111 } else if (eventSource == languageText_locality) {
112 LanguageString locality = languageText_locality.updateLanguageString(getEntity().getLocality());
113 getEntity().setLocality(locality);
114 } else if (eventSource == element_point) {
115 getEntity().setExactLocation(element_point.getPoint());
116 } else if (eventSource == number_elevation) {
117 getEntity().setAbsoluteElevation(number_elevation.getInteger());
118 } else if (eventSource == element_date) {
119 getEntity().setGatheringPeriod(element_date.getTimePeriod());
120 } else if (eventSource == selection_collector) {
121 getEntity().setCollector(selection_collector.getSelection());
122
123 if (getEntity().getCollector() instanceof Team){
124 if (selection_primary_collector.getCollectorTeam() != null && selection_primary_collector.getEntity() != null ){
125 boolean collInNewTeam = false;
126 for (Person person:((Team)getEntity().getCollector()).getTeamMembers()){
127 collInNewTeam = person.getId() == selection_primary_collector.getEntity().getId();
128 if (collInNewTeam){
129 break;
130 }
131 }
132 if (!collInNewTeam){
133 selection_primary_collector.setEntity(null);
134 }
135 }
136 selection_primary_collector.setCollectorTeam((Team)getEntity().getCollector());
137 }else{
138 selection_primary_collector.setEntity(null);
139 }
140 selection_primary_collector.setEnabled((getEntity().getCollector() instanceof Team));
141
142 } else if (eventSource == selection_primary_collector) {
143 getEntity().setPrimaryCollector((Person)selection_primary_collector.getSelection());
144 } else if (eventSource == text_collectingNumber) {
145 getEntity().setFieldNumber(text_collectingNumber.getText());
146 } else if (eventSource == combo_specorobstype) {
147 getEntity().setType(combo_specorobstype.getSelection());
148 } else if (eventSource == checkIsPublish) {
149 getEntity().innerFieldUnit().setPublish(checkIsPublish.getSelection());
150 }
151
152 toggleableText_titleCache.setText(getEntity().getTitleCache());
153
154 }
155 @Override
156 protected void updateControlStates(){
157 if (getEntity() != null ){
158 enabled = enabled && CdmStore.currentAuthentiationHasPermission(StoreUtil.getCdmEntity(getEntity()), requiredCrud);
159 }else{
160 enabled = true;
161 }
162 Set<Object> except = new HashSet<>();
163 if (!selection_primary_collector.isEnabled() && enabled){
164 except.add(selection_primary_collector);
165 }
166 setEnabled(enabled, except);
167
168 }
169
170 @Override
171 public void fillFields() {
172 // TODO Auto-generated method stub
173
174 }
175
176 }