1 package eu
.etaxonomy
.taxeditor
.editor
.view
.checklist
.e4
;
3 import java
.util
.Arrays
;
6 import java
.util
.SortedSet
;
8 import javax
.inject
.Inject
;
10 import org
.apache
.commons
.lang
.StringUtils
;
11 import org
.apache
.log4j
.Logger
;
12 import org
.eclipse
.e4
.ui
.workbench
.modeling
.ESelectionService
;
13 import org
.eclipse
.jface
.viewers
.ArrayContentProvider
;
14 import org
.eclipse
.jface
.viewers
.CellEditor
;
15 import org
.eclipse
.jface
.viewers
.ComboBoxViewerCellEditor
;
16 import org
.eclipse
.jface
.viewers
.EditingSupport
;
17 import org
.eclipse
.jface
.viewers
.ISelection
;
18 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
19 import org
.eclipse
.jface
.viewers
.LabelProvider
;
20 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
21 import org
.eclipse
.jface
.viewers
.TableViewer
;
22 import org
.eclipse
.swt
.SWT
;
23 import org
.eclipse
.swt
.widgets
.Composite
;
24 import org
.eclipse
.swt
.widgets
.TableColumn
;
26 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
27 import eu
.etaxonomy
.cdm
.model
.common
.TermType
;
28 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
29 import eu
.etaxonomy
.cdm
.model
.description
.Distribution
;
30 import eu
.etaxonomy
.cdm
.model
.description
.PresenceAbsenceTerm
;
31 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
32 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
33 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
34 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
35 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
37 public final class DistributionEditingSupportE4
extends EditingSupport
{
39 private ComboBoxViewerCellEditor cellEditor
= null;
40 private final TableViewer viewer
;
41 private final ChecklistEditorE4 editor
;
42 // private final IDescriptionService descriptionService;
43 private final int columnIndex
;
46 private ESelectionService selService
;
47 private ISelectionChangedListener selectionChangedListener
;
49 private static final Logger logger
= Logger
.getLogger(DistributionEditingSupportE4
.class);
51 public DistributionEditingSupportE4(TableViewer viewer
, ChecklistEditorE4 checklistEditor
, int columnIndex
) {
54 this.columnIndex
= columnIndex
;
56 // descriptionService = CdmStore.getService(IDescriptionService.class);
57 editor
= checklistEditor
;
58 cellEditor
= new ComboBoxViewerCellEditor((Composite
) viewer
.getControl(), SWT
.READ_ONLY
);
59 cellEditor
.setLabelProvider(new LabelProvider(){
61 public String
getText(Object element
) {
62 if (element
instanceof PresenceAbsenceTerm
) {
63 PresenceAbsenceTerm status
= (PresenceAbsenceTerm
)element
;
65 if (PreferencesUtil
.isShowSymbolInChecklistEditor() && !StringUtils
.isBlank(status
.getSymbol())){
66 result
= status
.getTitleCache() + "("+ status
.getSymbol()+")";
68 result
= status
.getTitleCache();
75 // selectionChangedListener = (event -> selService.setSelection(event));
76 // cellEditor.addListener(selectionChangedListener);
77 cellEditor
.setContentProvider(new ArrayContentProvider());
79 // List<PresenceAbsenceTermBase> input = CdmStore.getTermManager()
80 // .getPreferredTerms(PresenceAbsenceTermBase.class);
81 // selectionChangedListener = (event -> selService.setSelection(event));
82 // viewer.addSelectionChangedListener(selectionChangedListener);
83 List
<DefinedTermBase
<?
>> input
= CdmStore
.getTermManager().getPreferredTerms(TermType
.PresenceAbsenceTerm
);
84 cellEditor
.setInput(input
);
88 protected CellEditor
getCellEditor(Object element
) {
93 protected boolean canEdit(Object element
) {
98 protected Object
getValue(Object element
) {
99 if (element
instanceof Taxon
) {
100 Taxon taxon
= (Taxon
) element
;
101 String result
= null;
102 Distribution distributionForColumn
= getDistributionForColumn(taxon
);
104 if (distributionForColumn
!= null) {
105 PresenceAbsenceTerm status
= distributionForColumn
.getStatus();
106 if (PreferencesUtil
.isShowIdInVocabularyInChecklistEditor()){
107 if (status
.getIdInVocabulary() != null){
108 result
= status
.getIdInVocabulary();
110 result
= status
.getTitleCache();
113 result
= status
.getTitleCache();
115 if (PreferencesUtil
.isShowSymbolInChecklistEditor() && status
.getSymbol() != null){
116 result
.concat(" ("+ status
.getSymbol() + ")");
120 fireStateChanged(distributionForColumn
);
126 protected void fireStateChanged(Distribution dist
) {
128 ISelection selection
= new StructuredSelection(dist
);
129 // selService.setSelection(selection);
134 protected void setValue(Object element
, Object value
) {
135 if (element
instanceof Taxon
&& value
instanceof PresenceAbsenceTerm
) {
136 Taxon taxon
= (Taxon
) element
;
137 PresenceAbsenceTerm occurenceStatus
= (PresenceAbsenceTerm
) value
;
138 Distribution distribution
= getDistributionForColumn(taxon
);
139 if (distribution
!= null) {
140 // change status for already exsiting distribution
141 distribution
.setStatus(occurenceStatus
);
143 createDistributionForColumn(taxon
, occurenceStatus
);
145 // viewer.update(distribution, null);
146 // viewer.update(element, null);
148 // editor.setDirty(true);
149 editor
.changed(element
);
154 private static final List
<String
> DESC_INIT_STRATEGY
= Arrays
.asList(new String
[] { "descriptions", //$NON-NLS-1$
155 "descriptions.*", "description.state" }); //$NON-NLS-1$ //$NON-NLS-2$
162 private Distribution
getDistributionForColumn(Taxon taxon
) {
163 // List<TaxonDescription> listTaxonDescriptions = descriptionService.listTaxonDescriptions(taxon, null, null,
164 // null, null, null, DESC_INIT_STRATEGY);
165 Set
<TaxonDescription
> listTaxonDescriptions
= taxon
.getDescriptions();
166 TableColumn column
= viewer
.getTable().getColumn(columnIndex
);
167 for (TaxonDescription td
: listTaxonDescriptions
) {
168 for (DescriptionElementBase deb
: td
.getElements()) {
169 if (deb
instanceof Distribution
) {
170 Distribution distribution
= (Distribution
) deb
;
171 if (distribution
.getArea() != null){
172 if (PreferencesUtil
.isShowIdInVocabularyInChecklistEditor()){
173 if (column
.getText().equalsIgnoreCase(distribution
.getArea().getIdInVocabulary())) {
177 if (column
.getText().equalsIgnoreCase(distribution
.getArea().getTitleCache())) {
188 private void createDistributionForColumn(Taxon taxon
, PresenceAbsenceTerm occurenceStatus
) {
189 TableColumn column
= viewer
.getTable().getColumn(columnIndex
);
191 SortedSet
<DefinedTermBase
> namedAreas
= this.editor
.getLabelProvider().getNamedAreas();
194 if (namedAreas
!= null) {
195 for (DefinedTermBase term
: namedAreas
) {
196 boolean sameArea
= true;
197 if (!PreferencesUtil
.isShowIdInVocabularyInChecklistEditor()){
198 sameArea
= column
.getText().equalsIgnoreCase(term
.getTitleCache());
200 if (term
.getIdInVocabulary() != null){
201 sameArea
= column
.getText().equalsIgnoreCase(term
.getIdInVocabulary());
203 sameArea
= column
.getText().equalsIgnoreCase(term
.getTitleCache());
207 NamedArea area
= (NamedArea
) term
;
208 Distribution distribution
= Distribution
.NewInstance(area
, occurenceStatus
);
209 Set
<TaxonDescription
> descriptions
= taxon
.getDescriptions();
210 if (!descriptions
.isEmpty()) {
211 for (TaxonDescription desc
: descriptions
) {
212 // add to first taxon description
213 desc
.addElement(distribution
);
216 } else {// there are no TaxonDescription yet.
217 TaxonDescription td
= TaxonDescription
.NewInstance(taxon
);
218 td
.addElement(distribution
);