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.eclipse.jface.dialogs.Dialog;
|
17
|
import org.eclipse.jface.layout.GridDataFactory;
|
18
|
import org.eclipse.jface.viewers.ArrayContentProvider;
|
19
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
20
|
import org.eclipse.jface.viewers.ITableLabelProvider;
|
21
|
import org.eclipse.jface.viewers.LabelProvider;
|
22
|
import org.eclipse.jface.viewers.TableViewer;
|
23
|
import org.eclipse.jface.viewers.TableViewerColumn;
|
24
|
import org.eclipse.swt.SWT;
|
25
|
import org.eclipse.swt.events.FocusEvent;
|
26
|
import org.eclipse.swt.events.FocusListener;
|
27
|
import org.eclipse.swt.events.SelectionAdapter;
|
28
|
import org.eclipse.swt.events.SelectionEvent;
|
29
|
import org.eclipse.swt.graphics.Image;
|
30
|
import org.eclipse.swt.layout.GridData;
|
31
|
import org.eclipse.swt.layout.GridLayout;
|
32
|
import org.eclipse.swt.widgets.Button;
|
33
|
import org.eclipse.swt.widgets.Composite;
|
34
|
import org.eclipse.swt.widgets.Control;
|
35
|
import org.eclipse.swt.widgets.Label;
|
36
|
import org.eclipse.swt.widgets.Shell;
|
37
|
import org.eclipse.swt.widgets.Table;
|
38
|
import org.eclipse.swt.widgets.Text;
|
39
|
|
40
|
import eu.etaxonomy.cdm.api.service.IDescriptiveDataSetService;
|
41
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
42
|
import eu.etaxonomy.cdm.persistence.dto.SpecimenNodeWrapper;
|
43
|
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
|
44
|
import eu.etaxonomy.taxeditor.model.AbstractUtility;
|
45
|
import eu.etaxonomy.taxeditor.model.ImageResources;
|
46
|
import eu.etaxonomy.taxeditor.preference.Resources;
|
47
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
48
|
import eu.etaxonomy.taxeditor.ui.combo.taxon.TaxonNodeCombo;
|
49
|
|
50
|
/**
|
51
|
* Dialog to choose specimens for the character matrix.<br>
|
52
|
* Filters specimens that have already been added.
|
53
|
* @author pplitzner
|
54
|
* @since Jan 3, 2018
|
55
|
*
|
56
|
*/
|
57
|
public class SpecimenSelectionDialog extends Dialog {
|
58
|
|
59
|
private static String TEXT_FILTER_DEFAULT = "Enter text filter";
|
60
|
|
61
|
private TableViewer list;
|
62
|
|
63
|
private Collection<SpecimenNodeWrapper> selectedSpecimens = new ArrayList<>();
|
64
|
private CharacterMatrix matrix;
|
65
|
private Text txtTextFilter;
|
66
|
|
67
|
private TaxonNodeCombo comboTaxon;
|
68
|
|
69
|
protected SpecimenSelectionDialog(Shell parentShell, CharacterMatrix matrix) {
|
70
|
super(parentShell);
|
71
|
this.matrix = matrix;
|
72
|
}
|
73
|
|
74
|
@Override
|
75
|
protected Control createDialogArea(Composite parent) {
|
76
|
Composite composite = (Composite) super.createDialogArea(parent);
|
77
|
GridLayout gl_composite = new GridLayout();
|
78
|
composite.setLayout(gl_composite);
|
79
|
|
80
|
Composite composite_1 = new Composite(composite, SWT.NONE);
|
81
|
composite_1.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1));
|
82
|
composite_1.setLayout(new GridLayout(4, false));
|
83
|
|
84
|
Label lblNewLabel = new Label(composite_1, SWT.NONE);
|
85
|
lblNewLabel.setText("Taxon");
|
86
|
|
87
|
comboTaxon = new TaxonNodeCombo(composite_1, SWT.NONE);
|
88
|
comboTaxon.setInput(matrix.getDescriptiveDataSet().getTaxonSubtreeFilter());
|
89
|
comboTaxon.addSelectionChangedListener(e->applyFilter());
|
90
|
|
91
|
txtTextFilter = new Text(composite_1, SWT.BORDER);
|
92
|
txtTextFilter.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
|
93
|
txtTextFilter.setText(TEXT_FILTER_DEFAULT);
|
94
|
txtTextFilter.addFocusListener(new FocusListener() {
|
95
|
|
96
|
@Override
|
97
|
public void focusGained(FocusEvent e) {
|
98
|
txtTextFilter.setForeground(AbstractUtility.getColor(Resources.SEARCH_VIEW_FOCUS));
|
99
|
if (TEXT_FILTER_DEFAULT.equals(txtTextFilter.getText())) {
|
100
|
txtTextFilter.setText("");
|
101
|
}
|
102
|
}
|
103
|
|
104
|
@Override
|
105
|
public void focusLost(FocusEvent e) {
|
106
|
if (CdmUtils.isBlank(txtTextFilter.getText())){
|
107
|
txtTextFilter.setForeground(AbstractUtility.getColor(Resources.SEARCH_VIEW_FOREGROUND));
|
108
|
txtTextFilter.setText(TEXT_FILTER_DEFAULT);
|
109
|
}
|
110
|
}
|
111
|
});
|
112
|
txtTextFilter.addModifyListener(e->applyFilter());
|
113
|
|
114
|
Button btnRefreshButton = new Button(composite_1, SWT.NONE);
|
115
|
btnRefreshButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
|
116
|
btnRefreshButton.setToolTipText(Messages.SpecimenSelectionDialog_REFRESH);
|
117
|
btnRefreshButton.setImage(ImageResources.getImage(ImageResources.REFRESH));
|
118
|
btnRefreshButton.addSelectionListener(new SelectionAdapter() {
|
119
|
@Override
|
120
|
public void widgetSelected(SelectionEvent e) {
|
121
|
loadSpecimens();
|
122
|
list.setInput(matrix.getSpecimenCache());
|
123
|
}
|
124
|
});
|
125
|
//table
|
126
|
list = new TableViewer(composite, SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.MULTI);
|
127
|
Table table = list.getTable();
|
128
|
GridData gd_table = GridDataFactory.fillDefaults().create();
|
129
|
gd_table.grabExcessHorizontalSpace = true;
|
130
|
gd_table.grabExcessVerticalSpace = true;
|
131
|
table.setLayoutData(gd_table);
|
132
|
//table columns
|
133
|
TableViewerColumn columnTaxon = new TableViewerColumn(list, SWT.NONE);
|
134
|
columnTaxon.getColumn().setText("Taxon");
|
135
|
columnTaxon.getColumn().setWidth(200);
|
136
|
columnTaxon.getColumn().setResizable(true);
|
137
|
columnTaxon.getColumn().setMoveable(true);
|
138
|
TableViewerColumn columnSpecimen = new TableViewerColumn(list, SWT.NONE);
|
139
|
columnSpecimen.getColumn().setText("Specimen");
|
140
|
columnSpecimen.getColumn().setWidth(200);
|
141
|
columnSpecimen.getColumn().setResizable(true);
|
142
|
columnSpecimen.getColumn().setMoveable(true);
|
143
|
table.setHeaderVisible(true);
|
144
|
table.setLinesVisible(true);
|
145
|
list.setContentProvider(new ArrayContentProvider());
|
146
|
list.setLabelProvider(new SpecimenListLabelProvider());
|
147
|
|
148
|
//gather specimens
|
149
|
if(matrix.getSpecimenCache()==null){
|
150
|
loadSpecimens();
|
151
|
}
|
152
|
list.setInput(matrix.getSpecimenCache());
|
153
|
return composite;
|
154
|
}
|
155
|
|
156
|
private void applyFilter(){
|
157
|
Collection<SpecimenNodeWrapper> specimenCache = matrix.getSpecimenCache();
|
158
|
List<SpecimenNodeWrapper> result = new ArrayList<>();
|
159
|
if(comboTaxon.getSelection()!=null){
|
160
|
result.addAll(specimenCache.stream()
|
161
|
.filter(wrapper->wrapper.getTaxonNode().equals(comboTaxon.getSelection()))
|
162
|
.collect(Collectors.toList()));
|
163
|
}
|
164
|
if(!txtTextFilter.getForeground().equals(AbstractUtility.getColor(Resources.SEARCH_VIEW_FOREGROUND))
|
165
|
&&!CdmUtils.isBlank(txtTextFilter.getText())){
|
166
|
result.addAll(specimenCache.stream()
|
167
|
.filter(wrapper->wrapper.getUuidAndTitleCache().getTitleCache().toLowerCase().contains(txtTextFilter.getText().toLowerCase()))
|
168
|
.collect(Collectors.toList()));
|
169
|
}
|
170
|
list.setInput(result);
|
171
|
}
|
172
|
|
173
|
private void loadSpecimens(){
|
174
|
Collection<SpecimenNodeWrapper> specimenCache = CdmStore.getService(IDescriptiveDataSetService.class).loadSpecimens(matrix.getDescriptiveDataSet());
|
175
|
matrix.setSpecimenCache(specimenCache);
|
176
|
}
|
177
|
|
178
|
@Override
|
179
|
protected void configureShell(Shell newShell) {
|
180
|
super.configureShell(newShell);
|
181
|
newShell.setText(Messages.SpecimenSelectionDialog_SELECT_SPECIMENS);
|
182
|
newShell.setMinimumSize(500, 400);
|
183
|
}
|
184
|
|
185
|
@Override
|
186
|
protected void okPressed() {
|
187
|
selectedSpecimens.clear();
|
188
|
selectedSpecimens = ((IStructuredSelection)list.getSelection()).toList();
|
189
|
super.okPressed();
|
190
|
}
|
191
|
|
192
|
@Override
|
193
|
protected boolean isResizable() {
|
194
|
return true;
|
195
|
}
|
196
|
|
197
|
public Collection<SpecimenNodeWrapper> getSpecimen(){
|
198
|
return selectedSpecimens;
|
199
|
}
|
200
|
|
201
|
private class SpecimenListLabelProvider extends LabelProvider implements ITableLabelProvider{
|
202
|
|
203
|
@Override
|
204
|
public Image getColumnImage(Object element, int columnIndex) {
|
205
|
return null;
|
206
|
}
|
207
|
|
208
|
@Override
|
209
|
public String getColumnText(Object element, int columnIndex) {
|
210
|
if(element instanceof SpecimenNodeWrapper){
|
211
|
SpecimenNodeWrapper wrapper = (SpecimenNodeWrapper)element;
|
212
|
switch (columnIndex) {
|
213
|
case 0:
|
214
|
return wrapper.getTaxonNode().getTaxon().getName().getTitleCache();
|
215
|
case 1:
|
216
|
return wrapper.getUuidAndTitleCache().getTitleCache();
|
217
|
default:
|
218
|
break;
|
219
|
}
|
220
|
}
|
221
|
return "";
|
222
|
}
|
223
|
}
|
224
|
}
|