Fix saving routine for descriptions
[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(10, false));
66
67 wsLabel = new Label(this, SWT.NONE);
68
69 Button btnToggleTooltips = new Button(this, SWT.TOGGLE);
70 Button btnToggleTree = new Button(this, SWT.PUSH);
71 Button btnToggleFlat = new Button(this, SWT.PUSH);
72 Button btnCollapseAll = new Button(this, SWT.PUSH);
73 Button btnExpandAll = new Button(this, SWT.PUSH);
74 Button btnFreezeSuppInfo = new Button(this, SWT.TOGGLE);
75 ComboViewer comboStates = new ComboViewer(this, SWT.DROP_DOWN);
76 Button btnManageState = new Button(this, SWT.PUSH);
77 Button btnExcelExport = new Button(this, SWT.PUSH);
78
79 /**
80 * Toggle tooltips button
81 */
82 initButton(
83 btnToggleTooltips,
84 ImageResources.getImage(ImageResources.LIGHT_BULB),
85 "Show tooltips",
86 null,
87 true,
88 true,
89 (e)->matrix.toogleIsShowTooltips()
90 );
91 /**
92 * Toogle tree button
93 */
94 initButton(
95 btnToggleTree,
96 ImageResources.getImage(ImageResources.HIERARCHICAL),
97 Messages.CharacterMatrix_SHOW_HIERARCHY,
98 null,
99 false,
100 true,
101 (e)->matrix.toggleTreeFlat(true, btnToggleFlat, btnToggleTree, btnCollapseAll, btnExpandAll, btnFreezeSuppInfo)
102 );
103
104 /**
105 * Toogle flat button
106 */
107 initButton(
108 btnToggleFlat,
109 ImageResources.getImage(ImageResources.FLAT),
110 Messages.CharacterMatrix_SHOW_FLAT_LIST,
111 null,
112 true,
113 false,
114 (e)->matrix.toggleTreeFlat(false, btnToggleFlat, btnToggleTree, btnCollapseAll, btnExpandAll, btnFreezeSuppInfo)
115 );
116
117 /**
118 *
119 * Collapse button
120 */
121 initButton(
122 btnCollapseAll,
123 ImageResources.getImage(ImageResources.COLLAPSE_ALL),
124 Messages.CharacterMatrix_COLLAPSE,
125 null,
126 true,
127 false,
128 (e)->matrix.getNatTable().doCommand(new TreeCollapseAllCommand())
129 );
130
131 /**
132 * Expand button
133 */
134 initButton(
135 btnExpandAll,
136 ImageResources.getImage(ImageResources.EXPAND_ALL),
137 Messages.CharacterMatrix_EXPAND,
138 null,
139 true,
140 false,
141 (e)->matrix.getNatTable().doCommand(new TreeExpandAllCommand())
142 );
143
144 /**
145 * Freeze supplemental info button
146 */
147 initButton(
148 btnFreezeSuppInfo,
149 ImageResources.getImage(ImageResources.LOCK_ICON),
150 Messages.CharacterMatrix_LOCK_COLUMNS,
151 null,
152 true,
153 true,
154 (e)->{
155 boolean isSelected = btnFreezeSuppInfo.getSelection();
156 matrix.freezeSupplementalColumns(isSelected);
157 btnFreezeSuppInfo.setImage(isSelected?
158 ImageResources.getImage(ImageResources.LOCK_ICON):
159 ImageResources.getImage(ImageResources.LOCK_OPEN_ICON));
160 }
161 );
162
163 /**
164 * Table state persistence
165 */
166 natTableState = new Properties();
167 //load persisted state
168 File statePropertiesFile = matrix.getStatePropertiesFile();
169 FileInputStream inputStream;
170 try {
171 inputStream = new FileInputStream(statePropertiesFile);
172 natTableState.load(inputStream);
173 } catch (IOException e) {
174 MessagingUtils.info("No initial state properties file found for character matrix"); //$NON-NLS-1$
175 }
176
177 // create a combobox for showing the available view states
178 Collection<String> availableStates = PersistenceHelper.getAvailableStates(natTableState);
179 comboStates.setLabelProvider(new LabelProvider(){
180 @Override
181 public String getText(Object element) {
182 if(element instanceof String && ((String) element).isEmpty()){
183 return Messages.CharacterMatrix_DEFAULT;
184 }
185 return super.getText(element);
186 }
187 });
188 comboStates.setContentProvider(new ArrayContentProvider());
189 comboStates.addSelectionChangedListener(e->
190 {
191 int index = comboStates.getCombo().getSelectionIndex();
192 if (index >= 0) {
193 String selected = comboStates.getCombo().getItem(index);
194 // load the state
195 matrix.getNatTable().loadState(selected, natTableState);
196 natTableState.setProperty(PersistenceDialog.ACTIVE_VIEW_CONFIGURATION_KEY, selected);
197 }
198 });
199 comboStates.setInput(availableStates);
200 if(comboStates.getCombo().getItemCount()>0){
201 comboStates.getCombo().select(0);
202 }
203
204 displayPersistenceDialogCommandHandler = new DisplayPersistenceDialogCommandHandler(natTableState, matrix.getNatTable());
205 // add listener to update the combo on view state management changes
206 displayPersistenceDialogCommandHandler.addStateChangeListener(new IStateChangedListener() {
207 @Override
208 public void handleStateChange(StateChangeEvent event) {
209 comboStates.setInput(PersistenceHelper.getAvailableStates(natTableState));
210 matrix.selectStateItem(comboStates, event.getViewConfigName());
211 }
212 });
213
214 // add button to show dialog
215 btnManageState.setImage(ImageResources.getImage(ImageResources.SETTINGS));
216 btnManageState.setToolTipText(Messages.CharacterMatrix_VIEW_CONFIG);
217 btnManageState.addSelectionListener(new SelectionAdapter() {
218 @Override
219 public void widgetSelected(SelectionEvent e) {
220 matrix.getNatTable().doCommand(new DisplayPersistenceDialogCommand(matrix.getNatTable()));
221 Object activeConfig = natTableState.get(PersistenceDialog.ACTIVE_VIEW_CONFIGURATION_KEY);
222 if(activeConfig!=null){
223 matrix.selectStateItem(comboStates, activeConfig.toString());
224 }
225 }
226 });
227
228 /**
229 * excel export
230 */
231 btnExcelExport.setToolTipText(Messages.CharacterMatrix_EXPORT);
232 btnExcelExport.setImage(ImageResources.getImage(ImageResources.EXPORT));
233 btnExcelExport.addSelectionListener(new SelectionAdapter() {
234 @Override
235 public void widgetSelected(SelectionEvent e) {
236 matrix.getNatTable().doCommand(
237 new ExportCommand(
238 matrix.getNatTable().getConfigRegistry(),
239 matrix.getNatTable().getShell()));
240 }
241 });
242
243 }
244
245 private void initButton(Button button, Image image, String tooltipText,
246 String label, boolean enabled, boolean selected, Consumer<SelectionEvent> widgetSelected){
247 if(image!=null){
248 button.setImage(image);
249 }
250 if(label!=null){
251 button.setText(label);
252 }
253 if(tooltipText!=null){
254 button.setToolTipText(tooltipText);
255 }
256 button.setSelection(selected);
257 button.setEnabled(enabled);
258 button.addSelectionListener(new SelectionAdapter() {
259 @Override
260 public void widgetSelected(SelectionEvent e) {
261 widgetSelected.accept(e);
262 }
263 });
264 }
265
266 public Label getWsLabel() {
267 return wsLabel;
268 }
269
270 public Properties getNatTableState() {
271 return natTableState;
272 }
273
274 public DisplayPersistenceDialogCommandHandler getDisplayPersistenceDialogCommandHandler() {
275 return displayPersistenceDialogCommandHandler;
276 }
277 }