Project

General

Profile

Download (8.29 KB) Statistics
| Branch: | Tag: | Revision:
1 edb456d4 Katja Luther
package eu.etaxonomy.taxeditor.editor.view.checklist.e4;
2
3 7a105035 Katja Luther
import java.util.ArrayList;
4 edb456d4 Katja Luther
import java.util.Arrays;
5
import java.util.List;
6
import java.util.Set;
7
import java.util.SortedSet;
8
9 d6b3e56b Katja Luther
import javax.inject.Inject;
10
11 edb456d4 Katja Luther
import org.apache.commons.lang.StringUtils;
12
import org.apache.log4j.Logger;
13 d6b3e56b Katja Luther
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
14 edb456d4 Katja Luther
import org.eclipse.jface.viewers.ArrayContentProvider;
15
import org.eclipse.jface.viewers.CellEditor;
16
import org.eclipse.jface.viewers.ComboBoxViewerCellEditor;
17
import org.eclipse.jface.viewers.EditingSupport;
18 63bf9921 Katja Luther
import org.eclipse.jface.viewers.ISelection;
19 d6b3e56b Katja Luther
import org.eclipse.jface.viewers.ISelectionChangedListener;
20 edb456d4 Katja Luther
import org.eclipse.jface.viewers.LabelProvider;
21 63bf9921 Katja Luther
import org.eclipse.jface.viewers.StructuredSelection;
22 edb456d4 Katja Luther
import org.eclipse.jface.viewers.TableViewer;
23
import org.eclipse.swt.SWT;
24
import org.eclipse.swt.widgets.Composite;
25
import org.eclipse.swt.widgets.TableColumn;
26
27
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
28
import eu.etaxonomy.cdm.model.common.TermType;
29
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
30
import eu.etaxonomy.cdm.model.description.Distribution;
31
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
32
import eu.etaxonomy.cdm.model.description.TaxonDescription;
33
import eu.etaxonomy.cdm.model.location.NamedArea;
34
import eu.etaxonomy.cdm.model.taxon.Taxon;
35 04f83f13 Katja Luther
import eu.etaxonomy.taxeditor.event.EventUtility;
36
import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
37 edb456d4 Katja Luther
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
38
import eu.etaxonomy.taxeditor.store.CdmStore;
39
40
public final class DistributionEditingSupportE4 extends EditingSupport {
41
42
    private ComboBoxViewerCellEditor cellEditor = null;
43 04f83f13 Katja Luther
    //private final TableViewer viewer;
44 edb456d4 Katja Luther
    private final ChecklistEditorE4 editor;
45
//    private final IDescriptionService descriptionService;
46
    private final int columnIndex;
47 9a1539bb Katja Luther
48 d6b3e56b Katja Luther
    @Inject
49
    private ESelectionService selService;
50 0cd603f0 Katja Luther
    private ISelectionChangedListener selectionChangedListener;
51 9a1539bb Katja Luther
52 edb456d4 Katja Luther
    private static final Logger logger = Logger.getLogger(DistributionEditingSupportE4.class);
53
54
    public DistributionEditingSupportE4(TableViewer viewer, ChecklistEditorE4 checklistEditor, int columnIndex) {
55
        super(viewer);
56 aff7bab0 Katja Luther
57 edb456d4 Katja Luther
        this.columnIndex = columnIndex;
58 9a1539bb Katja Luther
59 edb456d4 Katja Luther
        editor = checklistEditor;
60 aff7bab0 Katja Luther
        cellEditor = new ComboBoxViewerCellEditor((Composite) this.getViewer().getControl(), SWT.READ_ONLY);
61 edb456d4 Katja Luther
        cellEditor.setLabelProvider(new LabelProvider(){
62
        	 @Override
63
        	   public String getText(Object element) {
64
        	     if (element instanceof PresenceAbsenceTerm) {
65
        	    	 PresenceAbsenceTerm  status = (PresenceAbsenceTerm)element;
66
        	    	 String result = null;
67
        	    	 if (PreferencesUtil.isShowSymbolInChecklistEditor() && !StringUtils.isBlank(status.getSymbol())){
68
        	    		 result = status.getTitleCache() + "("+ status.getSymbol()+")";
69
        	    	 }else{
70
        	    		 result = status.getTitleCache();
71
        	    	 }
72
        	    	 return result;
73
        	     }
74
        	     return null;
75
        	   }
76
        });
77 7a105035 Katja Luther
78 edb456d4 Katja Luther
        cellEditor.setContentProvider(new ArrayContentProvider());
79 7a105035 Katja Luther
80 aff7bab0 Katja Luther
81 7a105035 Katja Luther
        List<DefinedTermBase<?>> inputAll = new ArrayList<>();
82
        PresenceAbsenceTerm noneTerm = PresenceAbsenceTerm.NewInstance();
83
        noneTerm.setTitleCache(" ", true);
84
        inputAll.add(noneTerm);
85 edb456d4 Katja Luther
        List<DefinedTermBase<?>> input = CdmStore.getTermManager().getPreferredTerms(TermType.PresenceAbsenceTerm);
86 7a105035 Katja Luther
        inputAll.addAll(1, input);
87
        cellEditor.setInput(inputAll);
88
89 04f83f13 Katja Luther
90 edb456d4 Katja Luther
    }
91
92
    @Override
93
    protected CellEditor getCellEditor(Object element) {
94
        return cellEditor;
95
    }
96
97
    @Override
98
    protected boolean canEdit(Object element) {
99
        return true;
100
    }
101
102
    @Override
103
    protected Object getValue(Object element) {
104
        if (element instanceof Taxon) {
105
            Taxon taxon = (Taxon) element;
106
            String result = null;
107
            Distribution distributionForColumn = getDistributionForColumn(taxon);
108 9a1539bb Katja Luther
109 edb456d4 Katja Luther
            if (distributionForColumn != null) {
110
                PresenceAbsenceTerm status = distributionForColumn.getStatus();
111 aff7bab0 Katja Luther
                return status;
112 04f83f13 Katja Luther
113 edb456d4 Katja Luther
            }
114 9a1539bb Katja Luther
115 04f83f13 Katja Luther
           //fireStateChanged(distributionForColumn);
116 edb456d4 Katja Luther
            return result;
117
        }
118
        return null;
119
    }
120
121 63bf9921 Katja Luther
    protected void fireStateChanged(Distribution dist) {
122
    	if (dist!= null){
123
	    	ISelection selection = new StructuredSelection(dist);
124 9a1539bb Katja Luther
//	    	selService.setSelection(selection);
125 63bf9921 Katja Luther
    	}
126
    }
127 9a1539bb Katja Luther
128 edb456d4 Katja Luther
    @Override
129
    protected void setValue(Object element, Object value) {
130
    	if (element instanceof Taxon && value instanceof PresenceAbsenceTerm) {
131 04f83f13 Katja Luther
132 edb456d4 Katja Luther
            Taxon taxon = (Taxon) element;
133
            PresenceAbsenceTerm occurenceStatus = (PresenceAbsenceTerm) value;
134
            Distribution distribution = getDistributionForColumn(taxon);
135 7a105035 Katja Luther
136 edb456d4 Katja Luther
            if (distribution != null) {
137
                // change status for already exsiting distribution
138 7a105035 Katja Luther
                if (occurenceStatus.getTitleCache().equals(" ")){
139
                    TaxonDescription descr = (TaxonDescription)distribution.getInDescription();
140
                    descr.removeElement(distribution);
141
                }else{
142
                    distribution.setStatus(occurenceStatus);
143
                }
144 edb456d4 Katja Luther
            } else {
145 8d0745f0 Katja Luther
                if (!occurenceStatus.getTitleCache().equals(" ")){
146
                    createDistributionForColumn(taxon, occurenceStatus);
147
                }
148 edb456d4 Katja Luther
            }
149 aff7bab0 Katja Luther
150 04f83f13 Katja Luther
151 edb456d4 Katja Luther
            editor.changed(element);
152 04f83f13 Katja Luther
            getViewer().refresh();
153
154
            EventUtility.postEvent(WorkbenchEventConstants.REFRESH_TAXON_DETAILS, true);
155 edb456d4 Katja Luther
        }
156
    }
157
158
    private static final List<String> DESC_INIT_STRATEGY = Arrays.asList(new String[] { "descriptions", //$NON-NLS-1$
159
            "descriptions.*", "description.state" }); //$NON-NLS-1$ //$NON-NLS-2$
160
161
    /**
162
     *
163
     * @param taxon
164
     * @return
165
     */
166
    private Distribution getDistributionForColumn(Taxon taxon) {
167 aff7bab0 Katja Luther
168 edb456d4 Katja Luther
        Set<TaxonDescription> listTaxonDescriptions = taxon.getDescriptions();
169 04f83f13 Katja Luther
        TableColumn column = ((TableViewer)getViewer()).getTable().getColumn(columnIndex);
170 edb456d4 Katja Luther
        for (TaxonDescription td : listTaxonDescriptions) {
171
            for (DescriptionElementBase deb : td.getElements()) {
172
                if (deb instanceof Distribution) {
173
                    Distribution distribution = (Distribution) deb;
174
                    if (distribution.getArea() != null){
175
                        if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
176
                            if (column.getText().equalsIgnoreCase(distribution.getArea().getIdInVocabulary())) {
177
                                return distribution;
178
                            }
179
                        }else{
180
                            if (column.getText().equalsIgnoreCase(distribution.getArea().getTitleCache())) {
181
                                return distribution;
182
                            }
183
                        }
184
                    }
185
                }
186
            }
187
        }
188
        return null;
189
    }
190
191
    private void createDistributionForColumn(Taxon taxon, PresenceAbsenceTerm occurenceStatus) {
192 04f83f13 Katja Luther
        TableColumn column = ((TableViewer)this.getViewer()).getTable().getColumn(columnIndex);
193 edb456d4 Katja Luther
194 483d1f1c Katja Luther
        SortedSet<DefinedTermBase> namedAreas = this.editor.getLabelProvider().getNamedAreas(false);
195 edb456d4 Katja Luther
196
197
        if (namedAreas != null) {
198
            for (DefinedTermBase term : namedAreas) {
199 3a77d114 Katja Luther
                Integer areaIndex = editor.getAreaPosition().get(term.getUuid());
200
                if (areaIndex == columnIndex){
201 edb456d4 Katja Luther
                    NamedArea area = (NamedArea) term;
202
                    Distribution distribution = Distribution.NewInstance(area, occurenceStatus);
203
                    Set<TaxonDescription> descriptions = taxon.getDescriptions();
204
                    if (!descriptions.isEmpty()) {
205
                        for (TaxonDescription desc : descriptions) {
206
                            // add to first taxon description
207
                            desc.addElement(distribution);
208
                            break;
209
                        }
210
                    } else {// there are no TaxonDescription yet.
211
                        TaxonDescription td = TaxonDescription.NewInstance(taxon);
212
                        td.addElement(distribution);
213
                        break;
214
                    }
215 3a77d114 Katja Luther
216 edb456d4 Katja Luther
                }
217 3a77d114 Katja Luther
218 edb456d4 Katja Luther
            }
219
        }
220
221
        return;
222
    }
223 d6b3e56b Katja Luther
224 9a1539bb Katja Luther
225 edb456d4 Katja Luther
}