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.io.File;
|
12
|
import java.io.FileInputStream;
|
13
|
import java.io.IOException;
|
14
|
import java.util.Collection;
|
15
|
import java.util.Properties;
|
16
|
import java.util.function.Consumer;
|
17
|
|
18
|
import org.eclipse.jface.viewers.ArrayContentProvider;
|
19
|
import org.eclipse.jface.viewers.ComboViewer;
|
20
|
import org.eclipse.jface.viewers.LabelProvider;
|
21
|
import org.eclipse.nebula.widgets.nattable.command.VisualRefreshCommand;
|
22
|
import org.eclipse.nebula.widgets.nattable.export.command.ExportCommand;
|
23
|
import org.eclipse.nebula.widgets.nattable.persistence.PersistenceHelper;
|
24
|
import org.eclipse.nebula.widgets.nattable.persistence.command.DisplayPersistenceDialogCommand;
|
25
|
import org.eclipse.nebula.widgets.nattable.persistence.command.DisplayPersistenceDialogCommandHandler;
|
26
|
import org.eclipse.nebula.widgets.nattable.persistence.command.IStateChangedListener;
|
27
|
import org.eclipse.nebula.widgets.nattable.persistence.command.StateChangeEvent;
|
28
|
import org.eclipse.nebula.widgets.nattable.persistence.gui.PersistenceDialog;
|
29
|
import org.eclipse.nebula.widgets.nattable.tree.command.TreeCollapseAllCommand;
|
30
|
import org.eclipse.nebula.widgets.nattable.tree.command.TreeExpandAllCommand;
|
31
|
import org.eclipse.nebula.widgets.nattable.viewport.ViewportLayer;
|
32
|
import org.eclipse.swt.SWT;
|
33
|
import org.eclipse.swt.events.SelectionAdapter;
|
34
|
import org.eclipse.swt.events.SelectionEvent;
|
35
|
import org.eclipse.swt.graphics.Image;
|
36
|
import org.eclipse.swt.layout.GridData;
|
37
|
import org.eclipse.swt.layout.GridLayout;
|
38
|
import org.eclipse.swt.widgets.Button;
|
39
|
import org.eclipse.swt.widgets.Composite;
|
40
|
import org.eclipse.swt.widgets.Label;
|
41
|
|
42
|
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
|
43
|
import eu.etaxonomy.taxeditor.model.ImageResources;
|
44
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
45
|
|
46
|
/**
|
47
|
* @author pplitzner
|
48
|
* @since Jul 9, 2018
|
49
|
*
|
50
|
*/
|
51
|
public class CharacterMatrixToolbar extends Composite {
|
52
|
|
53
|
private CharacterMatrix matrix;
|
54
|
private Label wsLabel;
|
55
|
private DisplayPersistenceDialogCommandHandler displayPersistenceDialogCommandHandler;
|
56
|
private Properties natTableState;
|
57
|
|
58
|
public CharacterMatrixToolbar(CharacterMatrix matrix, int style) {
|
59
|
super(matrix, style);
|
60
|
this.matrix = matrix;
|
61
|
|
62
|
init();
|
63
|
}
|
64
|
|
65
|
private void init() {
|
66
|
setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
|
67
|
setLayout(new GridLayout(10, false));
|
68
|
|
69
|
wsLabel = new Label(this, SWT.NONE);
|
70
|
|
71
|
Button btnToggleTooltips = new Button(this, SWT.TOGGLE);
|
72
|
Button btnToggleTree = new Button(this, SWT.PUSH);
|
73
|
Button btnToggleFlat = new Button(this, SWT.PUSH);
|
74
|
Button btnCollapseAll = new Button(this, SWT.PUSH);
|
75
|
Button btnExpandAll = new Button(this, SWT.PUSH);
|
76
|
Button btnFreezeSuppInfo = new Button(this, SWT.TOGGLE);
|
77
|
ComboViewer comboStates = new ComboViewer(this, SWT.DROP_DOWN);
|
78
|
Button btnManageState = new Button(this, SWT.PUSH);
|
79
|
Button btnExcelExport = new Button(this, SWT.PUSH);
|
80
|
|
81
|
/**
|
82
|
* Toggle tooltips button
|
83
|
*/
|
84
|
initButton(
|
85
|
btnToggleTooltips,
|
86
|
ImageResources.getImage(ImageResources.LIGHT_BULB),
|
87
|
"Show tooltips",
|
88
|
null,
|
89
|
true,
|
90
|
true,
|
91
|
(e)->matrix.toogleIsShowTooltips()
|
92
|
);
|
93
|
/**
|
94
|
* Toogle tree button
|
95
|
*/
|
96
|
initButton(
|
97
|
btnToggleTree,
|
98
|
ImageResources.getImage(ImageResources.HIERARCHICAL),
|
99
|
Messages.CharacterMatrix_SHOW_HIERARCHY,
|
100
|
null,
|
101
|
false,
|
102
|
true,
|
103
|
(e)->matrix.toggleTreeFlat(true, btnToggleFlat, btnToggleTree, btnCollapseAll, btnExpandAll, btnFreezeSuppInfo)
|
104
|
);
|
105
|
|
106
|
/**
|
107
|
* Toogle flat button
|
108
|
*/
|
109
|
initButton(
|
110
|
btnToggleFlat,
|
111
|
ImageResources.getImage(ImageResources.FLAT),
|
112
|
Messages.CharacterMatrix_SHOW_FLAT_LIST,
|
113
|
null,
|
114
|
true,
|
115
|
false,
|
116
|
(e)->matrix.toggleTreeFlat(false, btnToggleFlat, btnToggleTree, btnCollapseAll, btnExpandAll, btnFreezeSuppInfo)
|
117
|
);
|
118
|
|
119
|
/**
|
120
|
*
|
121
|
* Collapse button
|
122
|
*/
|
123
|
initButton(
|
124
|
btnCollapseAll,
|
125
|
ImageResources.getImage(ImageResources.COLLAPSE_ALL),
|
126
|
Messages.CharacterMatrix_COLLAPSE,
|
127
|
null,
|
128
|
true,
|
129
|
false,
|
130
|
(e)->matrix.getNatTable().doCommand(new TreeCollapseAllCommand())
|
131
|
);
|
132
|
|
133
|
/**
|
134
|
* Expand button
|
135
|
*/
|
136
|
initButton(
|
137
|
btnExpandAll,
|
138
|
ImageResources.getImage(ImageResources.EXPAND_ALL),
|
139
|
Messages.CharacterMatrix_EXPAND,
|
140
|
null,
|
141
|
true,
|
142
|
false,
|
143
|
(e)->matrix.getNatTable().doCommand(new TreeExpandAllCommand())
|
144
|
);
|
145
|
|
146
|
/**
|
147
|
* Freeze supplemental info button
|
148
|
*/
|
149
|
initButton(
|
150
|
btnFreezeSuppInfo,
|
151
|
ImageResources.getImage(ImageResources.LOCK_ICON),
|
152
|
Messages.CharacterMatrix_LOCK_COLUMNS,
|
153
|
null,
|
154
|
true,
|
155
|
true,
|
156
|
(e)->{
|
157
|
boolean isSelected = btnFreezeSuppInfo.getSelection();
|
158
|
matrix.freezeSupplementalColumns(isSelected);
|
159
|
btnFreezeSuppInfo.setImage(isSelected?
|
160
|
ImageResources.getImage(ImageResources.LOCK_ICON):
|
161
|
ImageResources.getImage(ImageResources.LOCK_OPEN_ICON));
|
162
|
}
|
163
|
);
|
164
|
|
165
|
/**
|
166
|
* Table state persistence
|
167
|
*/
|
168
|
natTableState = new Properties();
|
169
|
//load persisted state
|
170
|
File statePropertiesFile = matrix.getStatePropertiesFile();
|
171
|
FileInputStream inputStream;
|
172
|
try {
|
173
|
inputStream = new FileInputStream(statePropertiesFile);
|
174
|
natTableState.load(inputStream);
|
175
|
} catch (IOException e) {
|
176
|
MessagingUtils.info("No initial state properties file found for character matrix"); //$NON-NLS-1$
|
177
|
}
|
178
|
|
179
|
// create a combobox for showing the available view states
|
180
|
Collection<String> availableStates = PersistenceHelper.getAvailableStates(natTableState);
|
181
|
comboStates.setLabelProvider(new LabelProvider(){
|
182
|
@Override
|
183
|
public String getText(Object element) {
|
184
|
if(element instanceof String && ((String) element).isEmpty()){
|
185
|
return Messages.CharacterMatrix_DEFAULT;
|
186
|
}
|
187
|
return super.getText(element);
|
188
|
}
|
189
|
});
|
190
|
comboStates.setContentProvider(new ArrayContentProvider());
|
191
|
comboStates.addSelectionChangedListener(e->
|
192
|
{
|
193
|
int index = comboStates.getCombo().getSelectionIndex();
|
194
|
if (index >= 0) {
|
195
|
String selected = comboStates.getCombo().getItem(index);
|
196
|
// load the state
|
197
|
matrix.getNatTable().loadState(selected, natTableState);
|
198
|
natTableState.setProperty(PersistenceDialog.ACTIVE_VIEW_CONFIGURATION_KEY, selected);
|
199
|
}
|
200
|
});
|
201
|
comboStates.setInput(availableStates);
|
202
|
if(comboStates.getCombo().getItemCount()>0){
|
203
|
comboStates.getCombo().select(0);
|
204
|
}
|
205
|
|
206
|
displayPersistenceDialogCommandHandler = new DisplayPersistenceDialogCommandHandler(natTableState, matrix.getNatTable());
|
207
|
// add listener to update the combo on view state management changes
|
208
|
displayPersistenceDialogCommandHandler.addStateChangeListener(new IStateChangedListener() {
|
209
|
@Override
|
210
|
public void handleStateChange(StateChangeEvent event) {
|
211
|
comboStates.setInput(PersistenceHelper.getAvailableStates(natTableState));
|
212
|
matrix.selectStateItem(comboStates, event.getViewConfigName());
|
213
|
}
|
214
|
});
|
215
|
|
216
|
// add button to show dialog
|
217
|
btnManageState.setImage(ImageResources.getImage(ImageResources.SETTINGS));
|
218
|
btnManageState.setToolTipText(Messages.CharacterMatrix_VIEW_CONFIG);
|
219
|
btnManageState.addSelectionListener(new SelectionAdapter() {
|
220
|
@Override
|
221
|
public void widgetSelected(SelectionEvent e) {
|
222
|
matrix.getNatTable().doCommand(new DisplayPersistenceDialogCommand(matrix.getNatTable()));
|
223
|
Object activeConfig = natTableState.get(PersistenceDialog.ACTIVE_VIEW_CONFIGURATION_KEY);
|
224
|
if(activeConfig!=null){
|
225
|
matrix.selectStateItem(comboStates, activeConfig.toString());
|
226
|
}
|
227
|
}
|
228
|
});
|
229
|
|
230
|
/**
|
231
|
* excel export
|
232
|
*/
|
233
|
btnExcelExport.setToolTipText(Messages.CharacterMatrix_EXPORT);
|
234
|
btnExcelExport.setImage(ImageResources.getImage(ImageResources.EXPORT));
|
235
|
btnExcelExport.addSelectionListener(new SelectionAdapter() {
|
236
|
@Override
|
237
|
public void widgetSelected(SelectionEvent e) {
|
238
|
// hack for fixing #8332
|
239
|
// By scrolling for only 1 pixel the export then exports all rows
|
240
|
ViewportLayer viewportLayer = matrix.getBodyLayer().getViewportLayer();
|
241
|
if(viewportLayer.getOrigin().getY()==0){
|
242
|
viewportLayer.setOriginY(1);
|
243
|
}
|
244
|
matrix.getNatTable().doCommand(
|
245
|
new ExportCommand(
|
246
|
matrix.getNatTable().getConfigRegistry(),
|
247
|
matrix.getNatTable().getShell()));
|
248
|
matrix.getNatTable().doCommand(new VisualRefreshCommand());
|
249
|
}
|
250
|
});
|
251
|
|
252
|
}
|
253
|
|
254
|
private void initButton(Button button, Image image, String tooltipText,
|
255
|
String label, boolean enabled, boolean selected, Consumer<SelectionEvent> widgetSelected){
|
256
|
if(image!=null){
|
257
|
button.setImage(image);
|
258
|
}
|
259
|
if(label!=null){
|
260
|
button.setText(label);
|
261
|
}
|
262
|
if(tooltipText!=null){
|
263
|
button.setToolTipText(tooltipText);
|
264
|
}
|
265
|
button.setSelection(selected);
|
266
|
button.setEnabled(enabled);
|
267
|
button.addSelectionListener(new SelectionAdapter() {
|
268
|
@Override
|
269
|
public void widgetSelected(SelectionEvent e) {
|
270
|
widgetSelected.accept(e);
|
271
|
}
|
272
|
});
|
273
|
}
|
274
|
|
275
|
public Label getWsLabel() {
|
276
|
return wsLabel;
|
277
|
}
|
278
|
|
279
|
public Properties getNatTableState() {
|
280
|
return natTableState;
|
281
|
}
|
282
|
|
283
|
public DisplayPersistenceDialogCommandHandler getDisplayPersistenceDialogCommandHandler() {
|
284
|
return displayPersistenceDialogCommandHandler;
|
285
|
}
|
286
|
}
|