Project

General

Profile

Download (11 KB) Statistics
| Branch: | Tag: | Revision:
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.IOException;
12
import java.io.StringReader;
13
import java.util.Collection;
14
import java.util.Properties;
15
import java.util.function.Consumer;
16

    
17
import org.eclipse.jface.viewers.ArrayContentProvider;
18
import org.eclipse.jface.viewers.ComboViewer;
19
import org.eclipse.jface.viewers.LabelProvider;
20
import org.eclipse.nebula.widgets.nattable.command.VisualRefreshCommand;
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.nebula.widgets.nattable.viewport.ViewportLayer;
31
import org.eclipse.swt.SWT;
32
import org.eclipse.swt.events.SelectionAdapter;
33
import org.eclipse.swt.events.SelectionEvent;
34
import org.eclipse.swt.graphics.Image;
35
import org.eclipse.swt.layout.GridData;
36
import org.eclipse.swt.layout.GridLayout;
37
import org.eclipse.swt.widgets.Button;
38
import org.eclipse.swt.widgets.Composite;
39
import org.eclipse.swt.widgets.Label;
40

    
41
import eu.etaxonomy.cdm.model.metadata.CdmPreference;
42
import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
43
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
44
import eu.etaxonomy.taxeditor.model.ImageResources;
45
import eu.etaxonomy.taxeditor.model.MessagingUtils;
46
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
47

    
48
/**
49
 * @author pplitzner
50
 * @since Jul 9, 2018
51
 *
52
 */
53
public class CharacterMatrixToolbar extends Composite {
54

    
55
    private CharacterMatrix matrix;
56
    private Label wsLabel;
57
    private DisplayPersistenceDialogCommandHandler displayPersistenceDialogCommandHandler;
58
    private Properties natTableState;
59

    
60
    public CharacterMatrixToolbar(CharacterMatrix matrix, int style) {
61
        super(matrix, style);
62
        this.matrix = matrix;
63

    
64
        init();
65
    }
66

    
67
    private void init() {
68
        setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
69
        setLayout(new GridLayout(10, false));
70

    
71
        wsLabel = new Label(this, SWT.NONE);
72

    
73
        Button btnToggleTooltips = new Button(this, SWT.TOGGLE);
74
        Button btnToggleTree = new Button(this, SWT.PUSH);
75
        Button btnToggleFlat = new Button(this, SWT.PUSH);
76
        Button btnCollapseAll = new Button(this, SWT.PUSH);
77
        Button btnExpandAll = new Button(this, SWT.PUSH);
78
        Button btnFreezeSuppInfo = new Button(this, SWT.TOGGLE);
79
        ComboViewer comboStates = new ComboViewer(this, SWT.DROP_DOWN);
80
        Button btnManageState = new Button(this, SWT.PUSH);
81
        Button btnExcelExport = new Button(this, SWT.PUSH);
82

    
83
        /**
84
         * Toggle tooltips button
85
         */
86
        initButton(
87
                btnToggleTooltips,
88
                ImageResources.getImage(ImageResources.LIGHT_BULB),
89
                "Show tooltips",
90
                null,
91
                true,
92
                true,
93
                (e)->matrix.toogleIsShowTooltips()
94
                );
95
        /**
96
         * Toogle tree button
97
         */
98
        initButton(
99
                btnToggleTree,
100
                ImageResources.getImage(ImageResources.HIERARCHICAL),
101
                Messages.CharacterMatrix_SHOW_HIERARCHY,
102
                null,
103
                false,
104
                true,
105
                (e)->matrix.toggleTreeFlat(true, btnToggleFlat, btnToggleTree, btnCollapseAll, btnExpandAll, btnFreezeSuppInfo)
106
                );
107

    
108
        /**
109
         * Toogle flat button
110
         */
111
        initButton(
112
                btnToggleFlat,
113
                ImageResources.getImage(ImageResources.FLAT),
114
                Messages.CharacterMatrix_SHOW_FLAT_LIST,
115
                null,
116
                true,
117
                false,
118
                (e)->matrix.toggleTreeFlat(false, btnToggleFlat, btnToggleTree, btnCollapseAll, btnExpandAll, btnFreezeSuppInfo)
119
                );
120

    
121
        /**
122
         *
123
         * Collapse button
124
         */
125
        initButton(
126
                btnCollapseAll,
127
                ImageResources.getImage(ImageResources.COLLAPSE_ALL),
128
                Messages.CharacterMatrix_COLLAPSE,
129
                null,
130
                true,
131
                false,
132
                (e)->matrix.getNatTable().doCommand(new TreeCollapseAllCommand())
133
                );
134

    
135
        /**
136
         * Expand button
137
         */
138
        initButton(
139
                btnExpandAll,
140
                ImageResources.getImage(ImageResources.EXPAND_ALL),
141
                Messages.CharacterMatrix_EXPAND,
142
                null,
143
                true,
144
                false,
145
                (e)->matrix.getNatTable().doCommand(new TreeExpandAllCommand())
146
                );
147

    
148
        /**
149
         * Freeze supplemental info button
150
         */
151
        initButton(
152
                btnFreezeSuppInfo,
153
                ImageResources.getImage(ImageResources.LOCK_ICON),
154
                Messages.CharacterMatrix_LOCK_COLUMNS,
155
                null,
156
                true,
157
                true,
158
                (e)->{
159
                    boolean isSelected = btnFreezeSuppInfo.getSelection();
160
                    matrix.freezeSupplementalColumns(isSelected);
161
                    btnFreezeSuppInfo.setImage(isSelected?
162
                            ImageResources.getImage(ImageResources.LOCK_ICON):
163
                                ImageResources.getImage(ImageResources.LOCK_OPEN_ICON));
164
                }
165
                );
166

    
167
        /**
168
         * Table state persistence
169
         */
170
        natTableState = new Properties();
171
        //load persisted state
172
        try {
173

    
174
            CdmPreference preference = PreferencesUtil.getPreferenceFromDB(PreferencePredicate.CharacterMatrixTableState);
175
            if(preference!=null){
176
                natTableState.load(new StringReader(preference.getValue()));
177
            }
178
        } catch (IOException e1) {
179
            MessagingUtils.error(getClass(), e1);
180
        }
181

    
182
        // create a combobox for showing the available view states
183
        Collection<String> availableStates = PersistenceHelper.getAvailableStates(natTableState);
184
        comboStates.setLabelProvider(new LabelProvider(){
185
            @Override
186
            public String getText(Object element) {
187
                if(element instanceof String && ((String) element).isEmpty()){
188
                    return Messages.CharacterMatrix_DEFAULT;
189
                }
190
                return super.getText(element);
191
            }
192
        });
193
        comboStates.setContentProvider(new ArrayContentProvider());
194
        comboStates.addSelectionChangedListener(e->
195
        {
196
            int index = comboStates.getCombo().getSelectionIndex();
197
            if (index >= 0) {
198
                String selected = comboStates.getCombo().getItem(index);
199
                // load the state
200
                matrix.getNatTable().loadState(selected, natTableState);
201
                natTableState.setProperty(PersistenceDialog.ACTIVE_VIEW_CONFIGURATION_KEY, selected);
202
            }
203
        });
204
        comboStates.setInput(availableStates);
205
        if(comboStates.getCombo().getItemCount()>0){
206
            comboStates.getCombo().select(0);
207
        }
208

    
209
        displayPersistenceDialogCommandHandler = new DisplayPersistenceDialogCommandHandler(natTableState, matrix.getNatTable());
210
        // add listener to update the combo on view state management changes
211
        displayPersistenceDialogCommandHandler.addStateChangeListener(new IStateChangedListener() {
212
            @Override
213
            public void handleStateChange(StateChangeEvent event) {
214
                comboStates.setInput(PersistenceHelper.getAvailableStates(natTableState));
215
                matrix.selectStateItem(comboStates, event.getViewConfigName());
216
            }
217
        });
218

    
219
        // add button to show dialog
220
        btnManageState.setImage(ImageResources.getImage(ImageResources.SETTINGS));
221
        btnManageState.setToolTipText(Messages.CharacterMatrix_VIEW_CONFIG);
222
        btnManageState.addSelectionListener(new SelectionAdapter() {
223
            @Override
224
            public void widgetSelected(SelectionEvent e) {
225
                matrix.getNatTable().doCommand(new DisplayPersistenceDialogCommand(matrix.getNatTable()));
226
                Object activeConfig = natTableState.get(PersistenceDialog.ACTIVE_VIEW_CONFIGURATION_KEY);
227
                if(activeConfig!=null){
228
                    matrix.selectStateItem(comboStates, activeConfig.toString());
229
                }
230
            }
231
        });
232

    
233
        /**
234
         * excel export
235
         */
236
        btnExcelExport.setToolTipText(Messages.CharacterMatrix_EXPORT);
237
        btnExcelExport.setImage(ImageResources.getImage(ImageResources.EXPORT));
238
        btnExcelExport.addSelectionListener(new SelectionAdapter() {
239
            @Override
240
            public void widgetSelected(SelectionEvent e) {
241
                // hack for fixing #8332
242
                // By scrolling for only 1 pixel the export then exports all rows
243
                ViewportLayer viewportLayer = matrix.getBodyLayer().getViewportLayer();
244
                if(viewportLayer.getOrigin().getY()==0){
245
                    viewportLayer.setOriginY(1);
246
                }
247
                matrix.getNatTable().doCommand(
248
                        new ExportCommand(
249
                                matrix.getNatTable().getConfigRegistry(),
250
                                matrix.getNatTable().getShell()));
251
                matrix.getNatTable().doCommand(new VisualRefreshCommand());
252
            }
253
        });
254

    
255
    }
256

    
257
    private void initButton(Button button, Image image, String tooltipText,
258
            String label, boolean enabled, boolean selected, Consumer<SelectionEvent> widgetSelected){
259
        if(image!=null){
260
            button.setImage(image);
261
        }
262
        if(label!=null){
263
            button.setText(label);
264
        }
265
        if(tooltipText!=null){
266
            button.setToolTipText(tooltipText);
267
        }
268
        button.setSelection(selected);
269
        button.setEnabled(enabled);
270
        button.addSelectionListener(new SelectionAdapter() {
271
            @Override
272
            public void widgetSelected(SelectionEvent e) {
273
                widgetSelected.accept(e);
274
            }
275
        });
276
    }
277

    
278
    public Label getWsLabel() {
279
        return wsLabel;
280
    }
281

    
282
    public Properties getNatTableState() {
283
        return natTableState;
284
    }
285

    
286
    public DisplayPersistenceDialogCommandHandler getDisplayPersistenceDialogCommandHandler() {
287
        return displayPersistenceDialogCommandHandler;
288
    }
289
}
(12-12/21)