1 package eu
.etaxonomy
.taxeditor
.editor
.view
.checklist
.edit
;
3 import java
.util
.Arrays
;
6 import java
.util
.SortedSet
;
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
;
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
;
32 public final class DistributionEditingSupport
extends EditingSupport
{
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
;
40 private static final Logger logger
= Logger
.getLogger(DistributionEditingSupport
.class);
42 public DistributionEditingSupport(TableViewer viewer
, ChecklistEditor checklistEditor
, int columnIndex
) {
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());
52 // List<PresenceAbsenceTermBase> input = CdmStore.getTermManager()
53 // .getPreferredTerms(PresenceAbsenceTermBase.class);
54 List
<DefinedTermBase
<?
>> input
= CdmStore
.getTermManager().getPreferredTerms(TermType
.PresenceAbsenceTerm
);
55 cellEditor
.setInput(input
);
59 protected CellEditor
getCellEditor(Object element
) {
64 protected boolean canEdit(Object element
) {
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();
78 distributionForColumn
.getStatus().getTitleCache();
81 return distributionForColumn
.getStatus().getTitleCache();
84 // return PresenceAbsenceTerms
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
);
99 createDistributionForColumn(taxon
, occurenceStatus
);
101 // viewer.update(distribution, null);
102 // viewer.update(element, null);
104 // editor.setDirty(true);
105 editor
.changed(element
);
110 private static final List
<String
> DESC_INIT_STRATEGY
= Arrays
.asList(new String
[] { "descriptions",
111 "descriptions.*", "description.state" });
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())) {
133 if (column
.getText().equalsIgnoreCase(distribution
.getArea().getTitleCache())) {
144 private void createDistributionForColumn(Taxon taxon
, PresenceAbsenceTerm occurenceStatus
) {
145 TableColumn column
= viewer
.getTable().getColumn(columnIndex
);
147 SortedSet
<DefinedTermBase
> namedAreas
= this.editor
.getLabelProvider().getNamedAreas();
150 if (namedAreas
!= null) {
151 for (DefinedTermBase term
: namedAreas
) {
152 boolean sameArea
= true;
153 if (!PreferencesUtil
.isShowIdInVocabularyInChecklistEditor()){
154 sameArea
= column
.getText().equalsIgnoreCase(term
.getTitleCache());
156 sameArea
= column
.getText().equalsIgnoreCase(term
.getIdInVocabulary());
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
);
168 } else {// there are no TaxonDescription yet.
169 TaxonDescription td
= TaxonDescription
.NewInstance(taxon
);
170 td
.addElement(distribution
);