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