Project

General

Profile

Download (12.9 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2018 EDIT
3
 * European Distributed Institute of Taxonomy
4
 * http://www.e-taxonomy.eu
5
 *
6
 * The contents of this file are subject to the Mozilla Public License Version 1.1
7
 * See LICENSE.TXT at the top of this package for the full license terms.
8
 */
9
package eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix;
10

    
11
import java.util.ArrayList;
12
import java.util.Collection;
13
import java.util.List;
14
import java.util.stream.Collectors;
15

    
16
import org.apache.commons.lang.StringUtils;
17
import org.eclipse.jface.dialogs.Dialog;
18
import org.eclipse.jface.viewers.ArrayContentProvider;
19
import org.eclipse.jface.viewers.ColumnLabelProvider;
20
import org.eclipse.jface.viewers.IStructuredSelection;
21
import org.eclipse.jface.viewers.ITableLabelProvider;
22
import org.eclipse.jface.viewers.TableViewer;
23
import org.eclipse.jface.viewers.TableViewerColumn;
24
import org.eclipse.jface.viewers.Viewer;
25
import org.eclipse.jface.viewers.ViewerComparator;
26
import org.eclipse.swt.SWT;
27
import org.eclipse.swt.custom.ScrolledComposite;
28
import org.eclipse.swt.events.FocusEvent;
29
import org.eclipse.swt.events.FocusListener;
30
import org.eclipse.swt.events.SelectionAdapter;
31
import org.eclipse.swt.events.SelectionEvent;
32
import org.eclipse.swt.graphics.Image;
33
import org.eclipse.swt.layout.GridData;
34
import org.eclipse.swt.layout.GridLayout;
35
import org.eclipse.swt.widgets.Button;
36
import org.eclipse.swt.widgets.Composite;
37
import org.eclipse.swt.widgets.Control;
38
import org.eclipse.swt.widgets.Label;
39
import org.eclipse.swt.widgets.Shell;
40
import org.eclipse.swt.widgets.Table;
41
import org.eclipse.swt.widgets.Text;
42

    
43
import eu.etaxonomy.cdm.api.service.IDescriptiveDataSetService;
44
import eu.etaxonomy.cdm.common.CdmUtils;
45
import eu.etaxonomy.cdm.persistence.dto.SpecimenNodeWrapper;
46
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
47
import eu.etaxonomy.taxeditor.model.AbstractUtility;
48
import eu.etaxonomy.taxeditor.model.ImageResources;
49
import eu.etaxonomy.taxeditor.preference.Resources;
50
import eu.etaxonomy.taxeditor.store.CdmStore;
51
import eu.etaxonomy.taxeditor.store.StoreUtil;
52

    
53
/**
54
 * Dialog to choose specimens for the character matrix.<br>
55
 * Filters specimens that have already been added.
56
 * @author pplitzner
57
 * @since Jan 3, 2018
58
 *
59
 */
60
public class SpecimenSelectionDialog extends Dialog {
61

    
62
    private static String TEXT_FILTER_DEFAULT = "Enter text filter";
63

    
64
    private TableViewer list;
65

    
66
    private Collection<SpecimenNodeWrapper> selectedSpecimens = new ArrayList<>();
67
    private CharacterMatrix matrix;
68
    private Text txtTextFilter;
69
    private List<String> treeIndex;
70

    
71

    
72
    public SpecimenSelectionDialog(Shell parentShell, CharacterMatrix matrix, List<String> treeIndex) {
73
        super(parentShell);
74
        this.matrix = matrix;
75
        this.treeIndex = treeIndex;
76
    }
77

    
78
    @Override
79
    protected Control createDialogArea(Composite parent) {
80
        Composite composite = (Composite) super.createDialogArea(parent);
81
        GridLayout gl_composite = new GridLayout();
82
        gl_composite.numColumns = 2;
83
        composite.setLayout(gl_composite);
84

    
85
        Composite composite_1 = new Composite(composite, SWT.NONE);
86
        composite_1.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 2, 1));
87
        composite_1.setLayout(new GridLayout(4, false));
88

    
89
        Label l_textFilter = new Label(composite_1, SWT.NULL);
90
        GridData data_label = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
91

    
92

    
93
        l_textFilter.setLayoutData(data_label);
94
        l_textFilter.setText("Specimen Filter");
95
        l_textFilter.setAlignment(SWT.RIGHT);
96
        txtTextFilter = new Text(composite_1, SWT.BORDER);
97
        txtTextFilter.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
98
        txtTextFilter.setText(TEXT_FILTER_DEFAULT);
99
        txtTextFilter.addFocusListener(new FocusListener() {
100

    
101
            @Override
102
            public void focusGained(FocusEvent e) {
103
                txtTextFilter.setForeground(AbstractUtility.getColor(Resources.SEARCH_VIEW_FOCUS));
104
                if (TEXT_FILTER_DEFAULT.equals(txtTextFilter.getText())) {
105
                    StoreUtil.setTextWithoutModifyListeners(txtTextFilter, "");
106
                }
107
            }
108

    
109
            @Override
110
            public void focusLost(FocusEvent e) {
111
                if (CdmUtils.isBlank(txtTextFilter.getText())){
112
                    txtTextFilter.setForeground(AbstractUtility.getColor(Resources.SEARCH_VIEW_FOREGROUND));
113
                    StoreUtil.setTextWithoutModifyListeners(txtTextFilter, TEXT_FILTER_DEFAULT);
114
                }
115
            }
116
        });
117
        txtTextFilter.addModifyListener(e->applyFilter());
118

    
119
        Button btnRefreshButton = new Button(composite_1, SWT.NONE);
120
        btnRefreshButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
121
        btnRefreshButton.setToolTipText(Messages.SpecimenSelectionDialog_REFRESH);
122
        btnRefreshButton.setImage(ImageResources.getImage(ImageResources.REFRESH));
123
        btnRefreshButton.addSelectionListener(new SelectionAdapter() {
124
            @Override
125
            public void widgetSelected(SelectionEvent e) {
126
                loadSpecimens();
127
                refreshInput(matrix.getSpecimenCache());
128
            }
129
        });
130

    
131
        Label lblNewLabel = new Label(composite, SWT.NONE);
132
        lblNewLabel.setImage(ImageResources.getImage(ImageResources.WARNING_ICON));
133

    
134
        Label lblNewLabel_1 = new Label(composite, SWT.NONE);
135
        lblNewLabel_1.setText(": Specimen not associated with taxon");
136

    
137
        ScrolledComposite scrolledComposite = new ScrolledComposite(composite, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
138
        scrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
139
        scrolledComposite.setExpandHorizontal(true);
140
        scrolledComposite.setExpandVertical(true);
141
        //table
142
        list = new TableViewer(scrolledComposite, SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.MULTI);
143
        Table table = list.getTable();
144

    
145
        //table columns
146
        TableViewerColumn columnTaxon = new TableViewerColumn(list, SWT.NONE);
147
        columnTaxon.getColumn().setText("Taxon");
148
        columnTaxon.getColumn().setWidth(200);
149
        columnTaxon.getColumn().setResizable(true);
150
        columnTaxon.getColumn().setMoveable(true);
151
        TableViewerColumn columnType = new TableViewerColumn(list, SWT.NONE);
152
        columnType.getColumn().setText("Type");
153
        columnType.getColumn().setWidth(150);
154
        columnType.getColumn().setResizable(true);
155
        columnType.getColumn().setMoveable(true);
156
        TableViewerColumn columnSpecimen = new TableViewerColumn(list, SWT.NONE);
157
        columnSpecimen.getColumn().setText("Specimen");
158
        columnSpecimen.getColumn().setResizable(true);
159
        columnSpecimen.getColumn().setMoveable(true);
160
        columnSpecimen.getColumn().setWidth(400);
161

    
162
        table.setHeaderVisible(true);
163
        table.setLinesVisible(true);
164
        scrolledComposite.setContent(table);
165
        scrolledComposite.setMinSize(table.computeSize(SWT.DEFAULT, SWT.DEFAULT));
166
        list.setContentProvider(new ArrayContentProvider());
167
        list.setLabelProvider(new SpecimenListLabelProvider());
168

    
169
        ViewerComparator comp = new ViewerComparator() {
170
            @Override
171
            public int compare(Viewer viewer, Object o1, Object o2) {
172
                if(o1 == null){
173
                     if(o2 == null){
174
                         return 0;
175
                     }
176
                     return -1;
177
                 }
178
                 if(o2==null){
179
                     return 1;
180
                 }
181
                 try{
182
                    return ((SpecimenNodeWrapper)o1).getTaxonNode().getTitleCache().compareTo(((SpecimenNodeWrapper)o2).getTaxonNode().getTitleCache());
183

    
184
                 }
185
                 catch(NullPointerException e){
186
                     return 0;
187
                 }
188

    
189
             }
190
        };
191

    
192
        list.setComparator(comp);
193

    
194
        //gather specimens
195
        if(matrix.getSpecimenCache()==null){
196
            loadSpecimens();
197
        }
198
        if(treeIndex !=null){
199
            filterByTaxonNode(treeIndex);
200
        }else{
201
            refreshInput(matrix.getSpecimenCache());
202
        }
203

    
204
        columnSpecimen.getColumn().pack();
205

    
206
        return composite;
207
    }
208

    
209
    private void refreshInput(Object input){
210
        list.setInput(input);
211
    }
212

    
213
    private void applyFilter(){
214
        List<SpecimenNodeWrapper> result = new ArrayList<>();
215
        Collection<SpecimenNodeWrapper> specimenCache = matrix.getSpecimenCache();
216
        String text = txtTextFilter.getText();
217
        if(CdmUtils.isBlank(text)){
218
            result = new ArrayList<>(specimenCache);
219
        }
220
        else{
221
            if(!txtTextFilter.getForeground().equals(AbstractUtility.getColor(Resources.SEARCH_VIEW_FOREGROUND))
222
                    &&!CdmUtils.isBlank(text)){
223
                result.addAll(specimenCache.stream()
224
                        .filter(wrapper->wrapper.getUuidAndTitleCache().getTitleCache().toLowerCase().contains(text.toLowerCase()))
225
                        .collect(Collectors.toList()));
226
                result.addAll(specimenCache.stream()
227
                        .filter(wrapper->wrapper.getTaxonNode().getTitleCache().toLowerCase().contains(text.toLowerCase()))
228
                        .collect(Collectors.toList()));
229
            }
230
        }
231
        refreshInput(result);
232
    }
233

    
234
    private void loadSpecimens(){
235
        ArrayList<SpecimenNodeWrapper> specimenList = new ArrayList<>(CdmStore.getService(IDescriptiveDataSetService.class).loadSpecimens(matrix.getDescriptiveDataSet().getUuid()));
236
//        Collections.sort(specimenList, new Comparator<SpecimenNodeWrapper>() {
237
//
238
//            @Override
239
//            public int compare(SpecimenNodeWrapper o1, SpecimenNodeWrapper o2) {
240
//                if(o1 == null){
241
//                    if(o2 == null){
242
//                        return 0;
243
//                    }
244
//                    return -1;
245
//                }
246
//                if(o2==null){
247
//                    return 1;
248
//                }
249
//                try{
250
//                    return o1.getTaxonNode().getTitleCache().compareTo(o2.getTaxonNode().getTitleCache());
251
//                }
252
//                catch(NullPointerException e){
253
//                    return 0;
254
//                }
255
//            }
256
//        });
257
        matrix.setSpecimenCache(specimenList);
258
    }
259

    
260
    @Override
261
    protected void configureShell(Shell newShell) {
262
        super.configureShell(newShell);
263
        newShell.setText(Messages.SpecimenSelectionDialog_SELECT_SPECIMENS);
264
        newShell.setMinimumSize(500, 600);
265
        newShell.setSize(800, 600);
266
    }
267

    
268
    @Override
269
    protected void okPressed() {
270
        selectedSpecimens.clear();
271
        selectedSpecimens = ((IStructuredSelection)list.getSelection()).toList();
272
        super.okPressed();
273
    }
274

    
275
    @Override
276
    protected boolean isResizable() {
277
        return true;
278
    }
279

    
280
    public Collection<SpecimenNodeWrapper> getSpecimen(){
281
        return selectedSpecimens;
282
    }
283

    
284
    private class SpecimenListLabelProvider extends ColumnLabelProvider implements ITableLabelProvider {
285

    
286
        @Override
287
        public String getToolTipText(Object element) {
288
            if(element instanceof SpecimenNodeWrapper
289
                    && ((SpecimenNodeWrapper) element).getTaxonDescriptionUuid()==null){
290
                return "Does not have and IndividualsAssociation with the taxon";
291
            }
292
            return null;
293
        }
294

    
295
        @Override
296
        public Image getColumnImage(Object element, int columnIndex) {
297
            if(element instanceof SpecimenNodeWrapper && columnIndex == 2
298
                    && ((SpecimenNodeWrapper) element).getTaxonDescriptionUuid()==null){
299
                return ImageResources.getImage(ImageResources.WARNING_ICON);
300
            }
301
            return null;
302
        }
303

    
304
        @Override
305
        public String getColumnText(Object element, int columnIndex) {
306
            if(element instanceof SpecimenNodeWrapper){
307
                SpecimenNodeWrapper wrapper = (SpecimenNodeWrapper)element;
308
                switch (columnIndex) {
309
                case 0:
310
                    return wrapper.getTaxonNode().getTitleCache();
311
                case 1:
312
                    return wrapper.getType().getMessage();
313
                case 2:
314
                    return wrapper.getUuidAndTitleCache().getTitleCache();
315
                default:
316
                    break;
317
                }
318
            }
319
            return "";
320
        }
321
    }
322

    
323
    private void filterByTaxonNode(List<String> treeIndexList){
324
        List<SpecimenNodeWrapper> result = new ArrayList<>();
325
        Collection<SpecimenNodeWrapper> specimenCache = matrix.getSpecimenCache();
326
        for (String treeIndex: treeIndexList){
327
            if (StringUtils.isNotBlank(treeIndex)){
328
                result.addAll(specimenCache.stream()
329
                            .filter(wrapper->wrapper.getTaxonNode().getTreeIndex().startsWith(treeIndex))
330
                            .collect(Collectors.toList()));
331
            }
332
        }
333

    
334

    
335
        refreshInput(result);
336
    }
337
}
(20-20/20)