Project

General

Profile

Download (12 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.taxeditor.editor.view.checklist.e4;
2

    
3
import java.util.ArrayList;
4
import java.util.Arrays;
5
import java.util.Collection;
6
import java.util.Collections;
7
import java.util.Comparator;
8
import java.util.List;
9
import java.util.Map;
10
import java.util.Set;
11
import java.util.SortedSet;
12
import java.util.UUID;
13

    
14
import javax.inject.Inject;
15

    
16
import org.apache.commons.lang.StringUtils;
17
import org.apache.log4j.Logger;
18
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
19
import org.eclipse.jface.viewers.ArrayContentProvider;
20
import org.eclipse.jface.viewers.CellEditor;
21
import org.eclipse.jface.viewers.ComboBoxViewerCellEditor;
22
import org.eclipse.jface.viewers.EditingSupport;
23
import org.eclipse.jface.viewers.ISelection;
24
import org.eclipse.jface.viewers.ISelectionChangedListener;
25
import org.eclipse.jface.viewers.LabelProvider;
26
import org.eclipse.jface.viewers.StructuredSelection;
27
import org.eclipse.jface.viewers.TableViewer;
28
import org.eclipse.swt.SWT;
29
import org.eclipse.swt.widgets.Composite;
30
import org.eclipse.swt.widgets.TableColumn;
31

    
32
import eu.etaxonomy.cdm.model.common.Annotation;
33
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
34
import eu.etaxonomy.cdm.model.common.OrderedTermComparator;
35
import eu.etaxonomy.cdm.model.common.Representation;
36
import eu.etaxonomy.cdm.model.common.TermType;
37
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
38
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
39
import eu.etaxonomy.cdm.model.description.Distribution;
40
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
41
import eu.etaxonomy.cdm.model.description.TaxonDescription;
42
import eu.etaxonomy.cdm.model.location.NamedArea;
43
import eu.etaxonomy.cdm.model.metadata.TermDisplayEnum;
44
import eu.etaxonomy.cdm.model.taxon.Taxon;
45
import eu.etaxonomy.taxeditor.event.EventUtility;
46
import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
47
import eu.etaxonomy.taxeditor.preference.EditorPreferencePredicate;
48
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
49
import eu.etaxonomy.taxeditor.store.CdmStore;
50

    
51
public final class DistributionEditingSupportE4 extends EditingSupport {
52

    
53
    /**
54
     *
55
     */
56
    private static final String DISTRIBUTION_EDITOR_DESCRIPTIONS = "Distribution Editor Descriptions";
57
    private ComboBoxViewerCellEditor cellEditor = null;
58
    //private final TableViewer viewer;
59
    private final ChecklistEditorE4 editor;
60
//    private final IDescriptionService descriptionService;
61
    private final int columnIndex;
62

    
63
    private Taxon taxon;
64

    
65
    @Inject
66
    private ESelectionService selService;
67
    private ISelectionChangedListener selectionChangedListener;
68

    
69
    private static final Logger logger = Logger.getLogger(DistributionEditingSupportE4.class);
70

    
71
    public DistributionEditingSupportE4(TableViewer viewer, ChecklistEditorE4 checklistEditor, int columnIndex) {
72
        super(viewer);
73

    
74
        this.columnIndex = columnIndex;
75

    
76
        editor = checklistEditor;
77
        cellEditor = new ComboBoxViewerCellEditor((Composite) this.getViewer().getControl(), SWT.READ_ONLY);
78
        cellEditor.setLabelProvider(new LabelProvider(){
79
        	 @Override
80
        	   public String getText(Object element) {
81
        	     if (element instanceof PresenceAbsenceTerm) {
82
        	    	 PresenceAbsenceTerm  status = (PresenceAbsenceTerm)element;
83
        	    	 Representation rep = status.getPreferredRepresentation(CdmStore.getDefaultLanguage());
84
        	    	 String label = null;
85
        	    	 if (rep != null){
86
        	    	     label = rep.getLabel();
87

    
88
        	    	 }else{
89
        	    	     label = status.getTitleCache();
90
        	    	 }
91
        	    	 String result = null;
92
        	    	 String statusDisplay = PreferencesUtil.displayStatusInChecklistEditor();
93
        	    	 if (statusDisplay != null){
94
        	    	     if (statusDisplay.equals(TermDisplayEnum.Symbol1.getKey()) && !StringUtils.isBlank(status.getSymbol())){
95
        	    	         result = label + "("+ status.getSymbol()+")";
96
        	    	     }else if (statusDisplay.equals(TermDisplayEnum.IdInVocabulary.getKey()) && !StringUtils.isBlank(status.getIdInVocabulary())){
97
        	    	         result = label + "("+ status.getIdInVocabulary()+")";
98
        	    	     }else if (statusDisplay.equals(TermDisplayEnum.Symbol2.getKey()) && !StringUtils.isBlank(status.getSymbol2())){
99
                             result = label + "("+ status.getSymbol2()+")";
100
                         }else{
101
        	    	         result = label;
102
        	    	     }
103

    
104
        	    	 }else{
105
        	    	     result = label;
106
        	    	 }
107
//
108
//        	    	 if (PreferencesUtil.displayStatusInChecklistEditor() != null && !StringUtils.isBlank(status.getSymbol())){
109
//        	    		 result = label + "("+ status.getSymbol()+")";
110
//        	    	 }else{
111
//        	    		 result = label;
112
//        	    	 }
113
        	    	 return result;
114
        	     }
115
        	     return null;
116
        	   }
117
        });
118

    
119
        cellEditor.setContentProvider(new ArrayContentProvider());
120

    
121

    
122
        List<DefinedTermBase> inputAll = new ArrayList<>();
123
        PresenceAbsenceTerm noneTerm = PresenceAbsenceTerm.NewInstance();
124
        noneTerm.setTitleCache(" ", true);
125
        inputAll.add(noneTerm);
126
        Collection<DefinedTermBase> input = CdmStore.getTermManager().getPreferredTerms(TermType.PresenceAbsenceTerm);
127
        List<DefinedTermBase> inputList = new ArrayList(input);
128
        Comparator<DefinedTermBase> comp = new OrderedTermComparator<>();
129
        Collections.sort(inputList, comp);
130
        Collections.sort(inputList, Collections.reverseOrder());
131

    
132
        inputAll.addAll(1,inputList);
133

    
134

    
135
        cellEditor.setInput(inputAll);
136

    
137

    
138

    
139
    }
140

    
141
    @Override
142
    protected CellEditor getCellEditor(Object element) {
143
        return cellEditor;
144
    }
145

    
146
    @Override
147
    protected boolean canEdit(Object element) {
148
        return true;
149
    }
150

    
151
    @Override
152
    protected PresenceAbsenceTerm getValue(Object element) {
153

    
154
        if (element instanceof Taxon) {
155
            taxon = (Taxon) element;
156
            String result = null;
157
            Distribution distributionForColumn = getDistributionForColumn(taxon);
158

    
159
            if (distributionForColumn != null) {
160
                PresenceAbsenceTerm status = distributionForColumn.getStatus();
161
                return status;
162

    
163
            }
164

    
165
           //fireStateChanged();
166
            return null;
167
        }
168
        return null;
169
    }
170

    
171

    
172

    
173
    protected void fireStateChanged() {
174
    	ISelection selection = new StructuredSelection(getDistributionForColumn(taxon));
175
	   getViewer().setSelection(selection);
176

    
177
    }
178

    
179
    @Override
180
    protected void setValue(Object element, Object value) {
181
    	if (element instanceof Taxon && value instanceof PresenceAbsenceTerm) {
182

    
183
            Taxon taxon = (Taxon) element;
184
            PresenceAbsenceTerm occurenceStatus = (PresenceAbsenceTerm) value;
185
            Distribution distribution = getDistributionForColumn(taxon);
186
            boolean changed = false;
187
            if (distribution != null) {
188
                // change status for already exsiting distribution
189
                if (occurenceStatus.getTitleCache().equals(" ")){
190
                    TaxonDescription descr = (TaxonDescription)distribution.getInDescription();
191
                    descr.removeElement(distribution);
192
                    changed = true;
193
                }else{
194
                    if (distribution.getStatus() == null || !distribution.getStatus().equals(occurenceStatus)){
195
                        changed = true;
196
                        distribution.setStatus(occurenceStatus);
197
                    }
198

    
199
                }
200
            } else {
201
                if (!occurenceStatus.getTitleCache().equals(" ")){
202
                    createDistributionForColumn(taxon, occurenceStatus);
203
                    changed = true;
204
                }
205
            }
206

    
207
            if (changed){
208
                editor.changed(element);
209
            }
210
            getViewer().refresh();
211

    
212
            EventUtility.postEvent(WorkbenchEventConstants.REFRESH_TAXON_DETAILS, true);
213
        }
214
    }
215

    
216
    private static final List<String> DESC_INIT_STRATEGY = Arrays.asList(new String[] { "descriptions", //$NON-NLS-1$
217
            "descriptions.*", "description.state" }); //$NON-NLS-1$ //$NON-NLS-2$
218

    
219
    /**
220
     *
221
     * @param taxon
222
     * @return
223
     */
224
    private Distribution getDistributionForColumn(Taxon taxon) {
225

    
226
        Set<TaxonDescription> listTaxonDescriptions = taxon.getDescriptions();
227
        TableColumn column = ((TableViewer)getViewer()).getTable().getColumn(columnIndex);
228
        for (TaxonDescription td : listTaxonDescriptions) {
229
            for (DescriptionElementBase deb : td.getElements()) {
230
                if (deb instanceof Distribution) {
231
                    Distribution distribution = (Distribution) deb;
232
                    UUID areaUuid = null;
233
                    for (Map.Entry<UUID,Integer> entry: editor.getAreaPosition().entrySet()){
234
                        if (entry.getValue().intValue()== columnIndex){
235
                            areaUuid = entry.getKey();
236
                            break;
237
                        }
238
                    }
239
                    if (distribution.getArea() != null){
240
                        if (distribution.getArea().getUuid().equals(areaUuid)){
241
                            return distribution;
242

    
243
                        }
244
                    }
245
                }
246
            }
247
        }
248
        return null;
249
    }
250

    
251
    private void createDistributionForColumn(Taxon taxon, PresenceAbsenceTerm occurenceStatus) {
252
        TableColumn column = ((TableViewer)this.getViewer()).getTable().getColumn(columnIndex);
253

    
254
        SortedSet<DefinedTermBase> namedAreas = this.editor.getLabelProvider().getNamedAreas(false);
255

    
256

    
257
        if (namedAreas != null) {
258
            for (DefinedTermBase term : namedAreas) {
259
                Integer areaIndex = editor.getAreaPosition().get(term.getUuid());
260
                if (areaIndex == columnIndex){
261
                    NamedArea area = (NamedArea) term;
262
                    Distribution distribution = Distribution.NewInstance(area, occurenceStatus);
263
                    if (editor.getDefaultSource() != null){
264
                        DescriptionElementSource source = DescriptionElementSource.NewPrimarySourceInstance(editor.getDefaultSource(), "");
265
                        distribution.addSource(source);
266
                    }
267
                    boolean added = false;
268
                    Set<TaxonDescription> descriptions = taxon.getDescriptions();
269

    
270
                    if (!descriptions.isEmpty()) {
271
                        //TODO: use the default description
272
                        for (TaxonDescription desc : descriptions) {
273
//                            if (PreferencesUtil.getBooleanValue(EditorPreferencePredicate.OwnDescriptionForDistributionEditor.getKey())){
274
//                                for (Marker marker: desc.getMarkers()){
275
//                                    if(marker.getMarkerType().equals(MarkerType.DISTRIBUTION_EDITOR_FACT())){
276
//                                        desc.addElement(distribution);
277
//                                        added = true;
278
//                                        break;
279
//                                    }
280
//                                }
281
//                            }else {
282
                                desc.addElement(distribution);
283
                                added = true;
284
                                break;
285
//                            }
286

    
287
                        }
288
                    }
289
                    if (!added){// there are no TaxonDescription yet.
290
                        TaxonDescription td = TaxonDescription.NewInstance(taxon);
291

    
292
                        if (PreferencesUtil.getBooleanValue(EditorPreferencePredicate.OwnDescriptionForDistributionEditor.getKey())){
293
                            td.addAnnotation(Annotation.NewDefaultLanguageInstance(DISTRIBUTION_EDITOR_DESCRIPTIONS));
294
                            td.setTitleCache(td.getTitleCache() + ": Distribution Editor Facts", true);
295
                        }
296
                        td.addElement(distribution);
297
                        break;
298
                    }
299

    
300
                }
301

    
302
            }
303
        }
304

    
305
        return;
306
    }
307

    
308

    
309
}
(7-7/20)