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(10, false));
67 wsLabel
= new Label(this, SWT
.NONE
);
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
);
80 * Toggle tooltips button
84 ImageResources
.getImage(ImageResources
.LIGHT_BULB
),
89 (e
)->matrix
.toogleIsShowTooltips()
96 ImageResources
.getImage(ImageResources
.HIERARCHICAL
),
97 Messages
.CharacterMatrix_SHOW_HIERARCHY
,
101 (e
)->matrix
.toggleTreeFlat(true, btnToggleFlat
, btnToggleTree
, btnCollapseAll
, btnExpandAll
, btnFreezeSuppInfo
)
109 ImageResources
.getImage(ImageResources
.FLAT
),
110 Messages
.CharacterMatrix_SHOW_FLAT_LIST
,
114 (e
)->matrix
.toggleTreeFlat(false, btnToggleFlat
, btnToggleTree
, btnCollapseAll
, btnExpandAll
, btnFreezeSuppInfo
)
123 ImageResources
.getImage(ImageResources
.COLLAPSE_ALL
),
124 Messages
.CharacterMatrix_COLLAPSE
,
128 (e
)->matrix
.getNatTable().doCommand(new TreeCollapseAllCommand())
136 ImageResources
.getImage(ImageResources
.EXPAND_ALL
),
137 Messages
.CharacterMatrix_EXPAND
,
141 (e
)->matrix
.getNatTable().doCommand(new TreeExpandAllCommand())
145 * Freeze supplemental info button
149 ImageResources
.getImage(ImageResources
.LOCK_ICON
),
150 Messages
.CharacterMatrix_LOCK_COLUMNS
,
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
));
164 * Table state persistence
166 natTableState
= new Properties();
167 //load persisted state
168 File statePropertiesFile
= matrix
.getStatePropertiesFile();
169 FileInputStream inputStream
;
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$
177 // create a combobox for showing the available view states
178 Collection
<String
> availableStates
= PersistenceHelper
.getAvailableStates(natTableState
);
179 comboStates
.setLabelProvider(new LabelProvider(){
181 public String
getText(Object element
) {
182 if(element
instanceof String
&& ((String
) element
).isEmpty()){
183 return Messages
.CharacterMatrix_DEFAULT
;
185 return super.getText(element
);
188 comboStates
.setContentProvider(new ArrayContentProvider());
189 comboStates
.addSelectionChangedListener(e
->
191 int index
= comboStates
.getCombo().getSelectionIndex();
193 String selected
= comboStates
.getCombo().getItem(index
);
195 matrix
.getNatTable().loadState(selected
, natTableState
);
196 natTableState
.setProperty(PersistenceDialog
.ACTIVE_VIEW_CONFIGURATION_KEY
, selected
);
199 comboStates
.setInput(availableStates
);
200 if(comboStates
.getCombo().getItemCount()>0){
201 comboStates
.getCombo().select(0);
204 displayPersistenceDialogCommandHandler
= new DisplayPersistenceDialogCommandHandler(natTableState
, matrix
.getNatTable());
205 // add listener to update the combo on view state management changes
206 displayPersistenceDialogCommandHandler
.addStateChangeListener(new IStateChangedListener() {
208 public void handleStateChange(StateChangeEvent event
) {
209 comboStates
.setInput(PersistenceHelper
.getAvailableStates(natTableState
));
210 matrix
.selectStateItem(comboStates
, event
.getViewConfigName());
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() {
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());
231 btnExcelExport
.setToolTipText(Messages
.CharacterMatrix_EXPORT
);
232 btnExcelExport
.setImage(ImageResources
.getImage(ImageResources
.EXPORT
));
233 btnExcelExport
.addSelectionListener(new SelectionAdapter() {
235 public void widgetSelected(SelectionEvent e
) {
236 matrix
.getNatTable().doCommand(
238 matrix
.getNatTable().getConfigRegistry(),
239 matrix
.getNatTable().getShell()));
245 private void initButton(Button button
, Image image
, String tooltipText
,
246 String label
, boolean enabled
, boolean selected
, Consumer
<SelectionEvent
> widgetSelected
){
248 button
.setImage(image
);
251 button
.setText(label
);
253 if(tooltipText
!=null){
254 button
.setToolTipText(tooltipText
);
256 button
.setSelection(selected
);
257 button
.setEnabled(enabled
);
258 button
.addSelectionListener(new SelectionAdapter() {
260 public void widgetSelected(SelectionEvent e
) {
261 widgetSelected
.accept(e
);
266 public Label
getWsLabel() {
270 public Properties
getNatTableState() {
271 return natTableState
;
274 public DisplayPersistenceDialogCommandHandler
getDisplayPersistenceDialogCommandHandler() {
275 return displayPersistenceDialogCommandHandler
;