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
;
11 import java
.io
.IOException
;
12 import java
.io
.StringReader
;
13 import java
.io
.StringWriter
;
14 import java
.util
.Collection
;
15 import java
.util
.Properties
;
16 import java
.util
.function
.Consumer
;
18 import org
.apache
.commons
.lang
.StringUtils
;
19 import org
.eclipse
.jface
.viewers
.ArrayContentProvider
;
20 import org
.eclipse
.jface
.viewers
.ComboViewer
;
21 import org
.eclipse
.jface
.viewers
.LabelProvider
;
22 import org
.eclipse
.nebula
.widgets
.nattable
.NatTable
;
23 import org
.eclipse
.nebula
.widgets
.nattable
.command
.VisualRefreshCommand
;
24 import org
.eclipse
.nebula
.widgets
.nattable
.export
.command
.ExportCommand
;
25 import org
.eclipse
.nebula
.widgets
.nattable
.persistence
.PersistenceHelper
;
26 import org
.eclipse
.nebula
.widgets
.nattable
.persistence
.command
.DisplayPersistenceDialogCommand
;
27 import org
.eclipse
.nebula
.widgets
.nattable
.persistence
.command
.DisplayPersistenceDialogCommandHandler
;
28 import org
.eclipse
.nebula
.widgets
.nattable
.persistence
.command
.IStateChangedListener
;
29 import org
.eclipse
.nebula
.widgets
.nattable
.persistence
.command
.StateChangeEvent
;
30 import org
.eclipse
.nebula
.widgets
.nattable
.persistence
.gui
.PersistenceDialog
;
31 import org
.eclipse
.nebula
.widgets
.nattable
.tree
.command
.TreeCollapseAllCommand
;
32 import org
.eclipse
.nebula
.widgets
.nattable
.tree
.command
.TreeExpandAllCommand
;
33 import org
.eclipse
.nebula
.widgets
.nattable
.viewport
.ViewportLayer
;
34 import org
.eclipse
.swt
.SWT
;
35 import org
.eclipse
.swt
.events
.SelectionAdapter
;
36 import org
.eclipse
.swt
.events
.SelectionEvent
;
37 import org
.eclipse
.swt
.graphics
.Image
;
38 import org
.eclipse
.swt
.layout
.GridData
;
39 import org
.eclipse
.swt
.layout
.GridLayout
;
40 import org
.eclipse
.swt
.widgets
.Button
;
41 import org
.eclipse
.swt
.widgets
.Composite
;
42 import org
.eclipse
.swt
.widgets
.Label
;
44 import eu
.etaxonomy
.cdm
.api
.application
.ICdmRepository
;
45 import eu
.etaxonomy
.cdm
.api
.service
.IPreferenceService
;
46 import eu
.etaxonomy
.cdm
.model
.metadata
.CdmPreference
;
47 import eu
.etaxonomy
.cdm
.model
.metadata
.PreferencePredicate
;
48 import eu
.etaxonomy
.taxeditor
.editor
.l10n
.Messages
;
49 import eu
.etaxonomy
.taxeditor
.model
.ImageResources
;
50 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
51 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
52 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
59 public class CharacterMatrixToolbar
extends Composite
{
61 private CharacterMatrix matrix
;
62 private Label wsLabel
;
63 private DisplayPersistenceDialogCommandHandler displayPersistenceDialogCommandHandler
;
64 private Properties natTableState
;
66 public CharacterMatrixToolbar(CharacterMatrix matrix
, int style
) {
74 setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
75 setLayout(new GridLayout(10, false));
77 wsLabel
= new Label(this, SWT
.NONE
);
79 Button btnToggleTooltips
= new Button(this, SWT
.TOGGLE
);
80 Button btnToggleTree
= new Button(this, SWT
.PUSH
);
81 Button btnToggleFlat
= new Button(this, SWT
.PUSH
);
82 Button btnCollapseAll
= new Button(this, SWT
.PUSH
);
83 Button btnExpandAll
= new Button(this, SWT
.PUSH
);
84 Button btnFreezeSuppInfo
= new Button(this, SWT
.TOGGLE
);
85 ComboViewer comboStates
= new ComboViewer(this, SWT
.DROP_DOWN
);
86 Button btnManageState
= new Button(this, SWT
.PUSH
);
87 Button btnExcelExport
= new Button(this, SWT
.PUSH
);
90 * Toggle tooltips button
94 ImageResources
.getImage(ImageResources
.LIGHT_BULB
),
99 (e
)->matrix
.toogleIsShowTooltips()
106 ImageResources
.getImage(ImageResources
.HIERARCHICAL
),
107 Messages
.CharacterMatrix_SHOW_HIERARCHY
,
111 (e
)->matrix
.toggleTreeFlat(true, btnToggleFlat
, btnToggleTree
, btnCollapseAll
, btnExpandAll
, btnFreezeSuppInfo
)
119 ImageResources
.getImage(ImageResources
.FLAT
),
120 Messages
.CharacterMatrix_SHOW_FLAT_LIST
,
124 (e
)->matrix
.toggleTreeFlat(false, btnToggleFlat
, btnToggleTree
, btnCollapseAll
, btnExpandAll
, btnFreezeSuppInfo
)
133 ImageResources
.getImage(ImageResources
.COLLAPSE_ALL
),
134 Messages
.CharacterMatrix_COLLAPSE
,
138 (e
)->matrix
.getNatTable().doCommand(new TreeCollapseAllCommand())
146 ImageResources
.getImage(ImageResources
.EXPAND_ALL
),
147 Messages
.CharacterMatrix_EXPAND
,
151 (e
)->matrix
.getNatTable().doCommand(new TreeExpandAllCommand())
155 * Freeze supplemental info button
159 ImageResources
.getImage(ImageResources
.LOCK_ICON
),
160 Messages
.CharacterMatrix_LOCK_COLUMNS
,
165 boolean isSelected
= btnFreezeSuppInfo
.getSelection();
166 matrix
.freezeSupplementalColumns(isSelected
);
167 btnFreezeSuppInfo
.setImage(isSelected?
168 ImageResources
.getImage(ImageResources
.LOCK_ICON
):
169 ImageResources
.getImage(ImageResources
.LOCK_OPEN_ICON
));
174 * Table state persistence
176 natTableState
= new Properties();
177 //load persisted state
180 CdmPreference preference
= PreferencesUtil
.getPreferenceFromDB(PreferencePredicate
.CharacterMatrixTableState
);
181 if(preference
!=null){
182 natTableState
.load(new StringReader(preference
.getValue()));
184 } catch (IOException e1
) {
185 MessagingUtils
.error(getClass(), e1
);
188 // create a combobox for showing the available view states
189 Collection
<String
> availableStates
= PersistenceHelper
.getAvailableStates(natTableState
);
190 if(availableStates
.isEmpty()){
191 natTableState
.put(StringUtils
.EMPTY
, StringUtils
.EMPTY
); // add default config if no states are persisted
192 natTableState
.setProperty(PersistenceDialog
.ACTIVE_VIEW_CONFIGURATION_KEY
, StringUtils
.EMPTY
);
193 availableStates
.add(StringUtils
.EMPTY
);
195 comboStates
.setLabelProvider(new LabelProvider(){
197 public String
getText(Object element
) {
198 if(element
instanceof String
&& ((String
) element
).isEmpty()){
199 return Messages
.CharacterMatrix_DEFAULT
;
201 return super.getText(element
);
204 comboStates
.setContentProvider(new ArrayContentProvider());
205 comboStates
.addSelectionChangedListener(e
->
207 int index
= comboStates
.getCombo().getSelectionIndex();
209 String selected
= comboStates
.getCombo().getItem(index
);
211 matrix
.getNatTable().loadState(selected
, natTableState
);
212 natTableState
.setProperty(PersistenceDialog
.ACTIVE_VIEW_CONFIGURATION_KEY
, selected
);
215 comboStates
.setInput(availableStates
);
216 if(comboStates
.getCombo().getItemCount()>0){
217 selectStateItem(comboStates
, Messages
.CharacterMatrix_DEFAULT
);
220 displayPersistenceDialogCommandHandler
= new DisplayPersistenceDialogCommandHandler(natTableState
, matrix
.getNatTable());
221 // add listener to update the combo on view state management changes
222 displayPersistenceDialogCommandHandler
.addStateChangeListener(new IStateChangedListener() {
224 public void handleStateChange(StateChangeEvent event
) {
225 comboStates
.setInput(PersistenceHelper
.getAvailableStates(natTableState
));
226 selectStateItem(comboStates
, event
.getViewConfigName());
230 // add button to show dialog
231 btnManageState
.setImage(ImageResources
.getImage(ImageResources
.SETTINGS
));
232 btnManageState
.setToolTipText(Messages
.CharacterMatrix_VIEW_CONFIG
);
233 btnManageState
.addSelectionListener(new SelectionAdapter() {
235 public void widgetSelected(SelectionEvent e
) {
236 getNatTableState().remove(NatTable
.INITIAL_PAINT_COMPLETE_FLAG
);
237 matrix
.getNatTable().doCommand(new DisplayPersistenceDialogCommand(matrix
.getNatTable()));
238 Object activeConfig
= natTableState
.get(PersistenceDialog
.ACTIVE_VIEW_CONFIGURATION_KEY
);
239 if(activeConfig
!=null){
240 selectStateItem(comboStates
, activeConfig
.toString());
249 btnExcelExport
.setToolTipText(Messages
.CharacterMatrix_EXPORT
);
250 btnExcelExport
.setImage(ImageResources
.getImage(ImageResources
.EXPORT
));
251 btnExcelExport
.addSelectionListener(new SelectionAdapter() {
253 public void widgetSelected(SelectionEvent e
) {
254 // hack for fixing #8332
255 // By scrolling for only 1 pixel the export then exports all rows
256 ViewportLayer viewportLayer
= matrix
.getBodyLayer().getViewportLayer();
257 if(viewportLayer
.getOrigin().getY()==0){
258 viewportLayer
.setOriginY(1);
260 matrix
.getNatTable().doCommand(
262 matrix
.getNatTable().getConfigRegistry(),
263 matrix
.getNatTable().getShell()));
264 matrix
.getNatTable().doCommand(new VisualRefreshCommand());
270 private void persistTableState() {
271 if(matrix
.getNatTableState()!=null){
272 StringWriter writer
= new StringWriter();
274 matrix
.getNatTableState().store(writer
, null);
275 ICdmRepository controller
= CdmStore
.getCurrentApplicationConfiguration();
276 if (controller
!= null){
277 IPreferenceService service
= controller
.getPreferenceService();
278 CdmPreference pref
= CdmPreference
.NewTaxEditorInstance(PreferencePredicate
.CharacterMatrixTableState
, writer
.toString());
280 PreferencesUtil
.updateDBPreferences();
282 } catch (IOException e
) {
283 MessagingUtils
.error(getClass(), e
);
288 private void selectStateItem(ComboViewer comboStates
, String stateName
){
289 String
[] items
= comboStates
.getCombo().getItems();
290 for(int i
=0;i
<items
.length
;i
++){
291 if(items
[i
].equals(stateName
)){
292 comboStates
.getCombo().select(i
);
298 private void initButton(Button button
, Image image
, String tooltipText
,
299 String label
, boolean enabled
, boolean selected
, Consumer
<SelectionEvent
> widgetSelected
){
301 button
.setImage(image
);
304 button
.setText(label
);
306 if(tooltipText
!=null){
307 button
.setToolTipText(tooltipText
);
309 button
.setSelection(selected
);
310 button
.setEnabled(enabled
);
311 button
.addSelectionListener(new SelectionAdapter() {
313 public void widgetSelected(SelectionEvent e
) {
314 widgetSelected
.accept(e
);
319 public Label
getWsLabel() {
323 public Properties
getNatTableState() {
324 return natTableState
;
327 public DisplayPersistenceDialogCommandHandler
getDisplayPersistenceDialogCommandHandler() {
328 return displayPersistenceDialogCommandHandler
;