2 * Copyright (C) 2018 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.editor
.descriptiveDataSet
.matrix
;
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
;
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
;
40 import eu
.etaxonomy
.taxeditor
.editor
.l10n
.Messages
;
41 import eu
.etaxonomy
.taxeditor
.model
.ImageResources
;
42 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
49 public class CharacterMatrixToolbar
extends Composite
{
51 private CharacterMatrix matrix
;
52 private Label wsLabel
;
53 private DisplayPersistenceDialogCommandHandler displayPersistenceDialogCommandHandler
;
54 private Properties natTableState
;
56 public CharacterMatrixToolbar(CharacterMatrix matrix
, int style
) {
64 setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
65 setLayout(new GridLayout(11, false));
67 wsLabel
= new Label(this, SWT
.NONE
);
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
);
81 * Toggle exact quantitative value
84 btnToggleExactQuantValue
,
90 (e
)->matrix
.toogleExactQuantitativeValue()
93 * Toggle tooltips button
97 ImageResources
.getImage(ImageResources
.LIGHT_BULB
),
102 (e
)->matrix
.toogleIsShowTooltips()
109 ImageResources
.getImage(ImageResources
.HIERARCHICAL
),
110 Messages
.CharacterMatrix_SHOW_HIERARCHY
,
114 (e
)->matrix
.toggleTreeFlat(true, btnToggleFlat
, btnToggleTree
, btnCollapseAll
, btnExpandAll
, btnFreezeSuppInfo
)
122 ImageResources
.getImage(ImageResources
.FLAT
),
123 Messages
.CharacterMatrix_SHOW_FLAT_LIST
,
127 (e
)->matrix
.toggleTreeFlat(false, btnToggleFlat
, btnToggleTree
, btnCollapseAll
, btnExpandAll
, btnFreezeSuppInfo
)
136 ImageResources
.getImage(ImageResources
.COLLAPSE_ALL
),
137 Messages
.CharacterMatrix_COLLAPSE
,
141 (e
)->matrix
.getNatTable().doCommand(new TreeCollapseAllCommand())
149 ImageResources
.getImage(ImageResources
.EXPAND_ALL
),
150 Messages
.CharacterMatrix_EXPAND
,
154 (e
)->matrix
.getNatTable().doCommand(new TreeExpandAllCommand())
158 * Freeze supplemental info button
162 ImageResources
.getImage(ImageResources
.LOCK_ICON
),
163 Messages
.CharacterMatrix_LOCK_COLUMNS
,
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
));
177 * Table state persistence
179 natTableState
= new Properties();
180 //load persisted state
181 File statePropertiesFile
= matrix
.getStatePropertiesFile();
182 FileInputStream inputStream
;
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$
190 // create a combobox for showing the available view states
191 Collection
<String
> availableStates
= PersistenceHelper
.getAvailableStates(natTableState
);
192 comboStates
.setLabelProvider(new LabelProvider(){
194 public String
getText(Object element
) {
195 if(element
instanceof String
&& ((String
) element
).isEmpty()){
196 return Messages
.CharacterMatrix_DEFAULT
;
198 return super.getText(element
);
201 comboStates
.setContentProvider(new ArrayContentProvider());
202 comboStates
.addSelectionChangedListener(e
->
204 int index
= comboStates
.getCombo().getSelectionIndex();
206 String selected
= comboStates
.getCombo().getItem(index
);
208 matrix
.getNatTable().loadState(selected
, natTableState
);
209 natTableState
.setProperty(PersistenceDialog
.ACTIVE_VIEW_CONFIGURATION_KEY
, selected
);
212 comboStates
.setInput(availableStates
);
213 if(comboStates
.getCombo().getItemCount()>0){
214 comboStates
.getCombo().select(0);
217 displayPersistenceDialogCommandHandler
= new DisplayPersistenceDialogCommandHandler(natTableState
, matrix
.getNatTable());
218 // add listener to update the combo on view state management changes
219 displayPersistenceDialogCommandHandler
.addStateChangeListener(new IStateChangedListener() {
221 public void handleStateChange(StateChangeEvent event
) {
222 comboStates
.setInput(PersistenceHelper
.getAvailableStates(natTableState
));
223 matrix
.selectStateItem(comboStates
, event
.getViewConfigName());
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() {
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());
244 btnExcelExport
.setToolTipText(Messages
.CharacterMatrix_EXPORT
);
245 btnExcelExport
.setImage(ImageResources
.getImage(ImageResources
.EXPORT
));
246 btnExcelExport
.addSelectionListener(new SelectionAdapter() {
248 public void widgetSelected(SelectionEvent e
) {
249 matrix
.getNatTable().doCommand(
251 matrix
.getNatTable().getConfigRegistry(),
252 matrix
.getNatTable().getShell()));
258 private void initButton(Button button
, Image image
, String tooltipText
,
259 String label
, boolean enabled
, boolean selected
, Consumer
<SelectionEvent
> widgetSelected
){
261 button
.setImage(image
);
264 button
.setText(label
);
266 if(tooltipText
!=null){
267 button
.setToolTipText(tooltipText
);
269 button
.setSelection(selected
);
270 button
.setEnabled(enabled
);
271 button
.addSelectionListener(new SelectionAdapter() {
273 public void widgetSelected(SelectionEvent e
) {
274 widgetSelected
.accept(e
);
279 public Label
getWsLabel() {
283 public Properties
getNatTableState() {
284 return natTableState
;
287 public DisplayPersistenceDialogCommandHandler
getDisplayPersistenceDialogCommandHandler() {
288 return displayPersistenceDialogCommandHandler
;