2274d9b3fe5af0643968c65b2e28ed9d9a7c8fa2
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / checklist / edit / DistributionEditingSupport.java
1 package eu.etaxonomy.taxeditor.editor.view.checklist.edit;
2
3 import java.util.Arrays;
4 import java.util.List;
5 import java.util.Set;
6 import java.util.SortedSet;
7
8 import org.apache.log4j.Logger;
9 import org.eclipse.jface.viewers.ArrayContentProvider;
10 import org.eclipse.jface.viewers.CellEditor;
11 import org.eclipse.jface.viewers.ComboBoxViewerCellEditor;
12 import org.eclipse.jface.viewers.EditingSupport;
13 import org.eclipse.jface.viewers.LabelProvider;
14 import org.eclipse.jface.viewers.TableViewer;
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.widgets.Composite;
17 import org.eclipse.swt.widgets.TableColumn;
18
19 import eu.etaxonomy.cdm.api.service.IDescriptionService;
20 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
21 import eu.etaxonomy.cdm.model.common.TermType;
22 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
23 import eu.etaxonomy.cdm.model.description.Distribution;
24 import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
25 import eu.etaxonomy.cdm.model.description.TaxonDescription;
26 import eu.etaxonomy.cdm.model.location.NamedArea;
27 import eu.etaxonomy.cdm.model.taxon.Taxon;
28 import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistEditor;
29 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
30 import eu.etaxonomy.taxeditor.store.CdmStore;
31
32 public final class DistributionEditingSupport extends EditingSupport {
33
34 private ComboBoxViewerCellEditor cellEditor = null;
35 private final TableViewer viewer;
36 private final ChecklistEditor editor;
37 private final IDescriptionService descriptionService;
38 private final int columnIndex;
39
40 private static final Logger logger = Logger.getLogger(DistributionEditingSupport.class);
41
42 public DistributionEditingSupport(TableViewer viewer, ChecklistEditor checklistEditor, int columnIndex) {
43 super(viewer);
44 this.viewer = viewer;
45 this.columnIndex = columnIndex;
46 descriptionService = CdmStore.getService(IDescriptionService.class);
47 editor = checklistEditor;
48 cellEditor = new ComboBoxViewerCellEditor((Composite) viewer.getControl(), SWT.READ_ONLY);
49 cellEditor.setLabelProvider(new LabelProvider());
50 cellEditor.setContentProvider(new ArrayContentProvider());
51 // TODO set input
52 // List<PresenceAbsenceTermBase> input = CdmStore.getTermManager()
53 // .getPreferredTerms(PresenceAbsenceTermBase.class);
54 List<DefinedTermBase<?>> input = CdmStore.getTermManager().getPreferredTerms(TermType.PresenceAbsenceTerm);
55 cellEditor.setInput(input);
56 }
57
58 @Override
59 protected CellEditor getCellEditor(Object element) {
60 return cellEditor;
61 }
62
63 @Override
64 protected boolean canEdit(Object element) {
65 return true;
66 }
67
68 @Override
69 protected Object getValue(Object element) {
70 if (element instanceof Taxon) {
71 Taxon taxon = (Taxon) element;
72 Distribution distributionForColumn = getDistributionForColumn(taxon);
73 if (distributionForColumn != null) {
74 if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
75 if (distributionForColumn.getStatus().getIdInVocabulary() != null){
76 return distributionForColumn.getStatus().getIdInVocabulary();
77 } else{
78 distributionForColumn.getStatus().getTitleCache();
79 }
80 } else{
81 return distributionForColumn.getStatus().getTitleCache();
82 }
83 }
84 // return PresenceAbsenceTerms
85 }
86 return null;
87 }
88
89 @Override
90 protected void setValue(Object element, Object value) {
91 if (element instanceof Taxon && value instanceof PresenceAbsenceTerm) {
92 Taxon taxon = (Taxon) element;
93 PresenceAbsenceTerm occurenceStatus = (PresenceAbsenceTerm) value;
94 Distribution distribution = getDistributionForColumn(taxon);
95 if (distribution != null) {
96 // change status for already exsiting distribution
97 distribution.setStatus(occurenceStatus);
98 } else {
99 createDistributionForColumn(taxon, occurenceStatus);
100 }
101 // viewer.update(distribution, null);
102 // viewer.update(element, null);
103 // viewer.refresh();
104 // editor.setDirty(true);
105 editor.changed(element);
106 viewer.refresh();
107 }
108 }
109
110 private static final List<String> DESC_INIT_STRATEGY = Arrays.asList(new String[] { "descriptions",
111 "descriptions.*", "description.state" });
112
113 /**
114 *
115 * @param taxon
116 * @return
117 */
118 private Distribution getDistributionForColumn(Taxon taxon) {
119 // List<TaxonDescription> listTaxonDescriptions = descriptionService.listTaxonDescriptions(taxon, null, null,
120 // null, null, null, DESC_INIT_STRATEGY);
121 Set<TaxonDescription> listTaxonDescriptions = taxon.getDescriptions();
122 TableColumn column = viewer.getTable().getColumn(columnIndex);
123 for (TaxonDescription td : listTaxonDescriptions) {
124 for (DescriptionElementBase deb : td.getElements()) {
125 if (deb instanceof Distribution) {
126 Distribution distribution = (Distribution) deb;
127 if (distribution.getArea() != null){
128 if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
129 if (column.getText().equalsIgnoreCase(distribution.getArea().getIdInVocabulary())) {
130 return distribution;
131 }
132 }else{
133 if (column.getText().equalsIgnoreCase(distribution.getArea().getTitleCache())) {
134 return distribution;
135 }
136 }
137 }
138 }
139 }
140 }
141 return null;
142 }
143
144 private void createDistributionForColumn(Taxon taxon, PresenceAbsenceTerm occurenceStatus) {
145 TableColumn column = viewer.getTable().getColumn(columnIndex);
146
147 SortedSet<DefinedTermBase> namedAreas = this.editor.getLabelProvider().getNamedAreas();
148
149
150 if (namedAreas != null) {
151 for (DefinedTermBase term : namedAreas) {
152 boolean sameArea = true;
153 if (!PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
154 sameArea = column.getText().equalsIgnoreCase(term.getTitleCache());
155 } else{
156 sameArea = column.getText().equalsIgnoreCase(term.getIdInVocabulary());
157 }
158 if (sameArea) {
159 NamedArea area = (NamedArea) term;
160 Distribution distribution = Distribution.NewInstance(area, occurenceStatus);
161 Set<TaxonDescription> descriptions = taxon.getDescriptions();
162 if (!descriptions.isEmpty()) {
163 for (TaxonDescription desc : descriptions) {
164 // add to first taxon description
165 desc.addElement(distribution);
166 break;
167 }
168 } else {// there are no TaxonDescription yet.
169 TaxonDescription td = TaxonDescription.NewInstance(taxon);
170 td.addElement(distribution);
171 break;
172 }
173 }
174 }
175 }
176
177 return;
178 }
179 }