Project

General

Profile

Download (12.9 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.io.StringWriter;
14
import java.util.Collection;
15
import java.util.Properties;
16
import java.util.function.Consumer;
17

    
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;
43

    
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;
53

    
54
/**
55
 * @author pplitzner
56
 * @since Jul 9, 2018
57
 *
58
 */
59
public class CharacterMatrixToolbar extends Composite {
60

    
61
    private CharacterMatrix matrix;
62
    private Label wsLabel;
63
    private DisplayPersistenceDialogCommandHandler displayPersistenceDialogCommandHandler;
64
    private Properties natTableState;
65

    
66
    public CharacterMatrixToolbar(CharacterMatrix matrix, int style) {
67
        super(matrix, style);
68
        this.matrix = matrix;
69

    
70
        init();
71
    }
72

    
73
    private void init() {
74
        setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
75
        setLayout(new GridLayout(10, false));
76

    
77
        wsLabel = new Label(this, SWT.NONE);
78

    
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);
88

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

    
114
        /**
115
         * Toogle flat button
116
         */
117
        initButton(
118
                btnToggleFlat,
119
                ImageResources.getImage(ImageResources.FLAT),
120
                Messages.CharacterMatrix_SHOW_FLAT_LIST,
121
                null,
122
                true,
123
                false,
124
                (e)->matrix.toggleTreeFlat(false, btnToggleFlat, btnToggleTree, btnCollapseAll, btnExpandAll, btnFreezeSuppInfo)
125
                );
126

    
127
        /**
128
         *
129
         * Collapse button
130
         */
131
        initButton(
132
                btnCollapseAll,
133
                ImageResources.getImage(ImageResources.COLLAPSE_ALL),
134
                Messages.CharacterMatrix_COLLAPSE,
135
                null,
136
                true,
137
                false,
138
                (e)->matrix.getNatTable().doCommand(new TreeCollapseAllCommand())
139
                );
140

    
141
        /**
142
         * Expand button
143
         */
144
        initButton(
145
                btnExpandAll,
146
                ImageResources.getImage(ImageResources.EXPAND_ALL),
147
                Messages.CharacterMatrix_EXPAND,
148
                null,
149
                true,
150
                false,
151
                (e)->matrix.getNatTable().doCommand(new TreeExpandAllCommand())
152
                );
153

    
154
        /**
155
         * Freeze supplemental info button
156
         */
157
        initButton(
158
                btnFreezeSuppInfo,
159
                ImageResources.getImage(ImageResources.LOCK_ICON),
160
                Messages.CharacterMatrix_LOCK_COLUMNS,
161
                null,
162
                true,
163
                true,
164
                (e)->{
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));
170
                }
171
                );
172

    
173
        /**
174
         * Table state persistence
175
         */
176
        natTableState = new Properties();
177
        //load persisted state
178
        try {
179

    
180
            CdmPreference preference = PreferencesUtil.getPreferenceFromDB(PreferencePredicate.CharacterMatrixTableState);
181
            if(preference!=null){
182
                natTableState.load(new StringReader(preference.getValue()));
183
            }
184
        } catch (IOException e1) {
185
            MessagingUtils.error(getClass(), e1);
186
        }
187

    
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);
194
        }
195
        comboStates.setLabelProvider(new LabelProvider(){
196
            @Override
197
            public String getText(Object element) {
198
                if(element instanceof String && ((String) element).isEmpty()){
199
                    return Messages.CharacterMatrix_DEFAULT;
200
                }
201
                return super.getText(element);
202
            }
203
        });
204
        comboStates.setContentProvider(new ArrayContentProvider());
205
        comboStates.addSelectionChangedListener(e->
206
        {
207
            int index = comboStates.getCombo().getSelectionIndex();
208
            if (index >= 0) {
209
                String selected = comboStates.getCombo().getItem(index);
210
                // load the state
211
                matrix.getNatTable().loadState(selected, natTableState);
212
                natTableState.setProperty(PersistenceDialog.ACTIVE_VIEW_CONFIGURATION_KEY, selected);
213
            }
214
        });
215
        comboStates.setInput(availableStates);
216
        if(comboStates.getCombo().getItemCount()>0){
217
           selectStateItem(comboStates, Messages.CharacterMatrix_DEFAULT);
218
        }
219

    
220
        displayPersistenceDialogCommandHandler = new DisplayPersistenceDialogCommandHandler(natTableState, matrix.getNatTable());
221
        // add listener to update the combo on view state management changes
222
        displayPersistenceDialogCommandHandler.addStateChangeListener(new IStateChangedListener() {
223
            @Override
224
            public void handleStateChange(StateChangeEvent event) {
225
                comboStates.setInput(PersistenceHelper.getAvailableStates(natTableState));
226
                selectStateItem(comboStates, event.getViewConfigName());
227
            }
228
        });
229

    
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() {
234
            @Override
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());
241
                }
242
                persistTableState();
243
            }
244
        });
245

    
246
        /**
247
         * excel export
248
         */
249
        btnExcelExport.setToolTipText(Messages.CharacterMatrix_EXPORT);
250
        btnExcelExport.setImage(ImageResources.getImage(ImageResources.EXPORT));
251
        btnExcelExport.addSelectionListener(new SelectionAdapter() {
252
            @Override
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);
259
                }
260
                matrix.getNatTable().doCommand(
261
                        new ExportCommand(
262
                                matrix.getNatTable().getConfigRegistry(),
263
                                matrix.getNatTable().getShell()));
264
                matrix.getNatTable().doCommand(new VisualRefreshCommand());
265
            }
266
        });
267

    
268
    }
269

    
270
    private void persistTableState() {
271
        if(matrix.getNatTableState()!=null){
272
            StringWriter writer = new StringWriter();
273
            try {
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());
279
                    service.set(pref);
280
                    PreferencesUtil.updateDBPreferences();
281
                }
282
            } catch (IOException e) {
283
                MessagingUtils.error(getClass(), e);
284
            }
285
        }
286
    }
287

    
288
    private void selectStateItem(ComboViewer comboStates, String stateName){
289
        if(stateName.equals(StringUtils.EMPTY)){
290
            stateName = Messages.CharacterMatrix_DEFAULT;
291
        }
292
        String[] items = comboStates.getCombo().getItems();
293
        for(int i=0;i<items.length;i++){
294
            if(items[i].equals(stateName)){
295
                comboStates.getCombo().select(i);
296
                break;
297
            }
298
        }
299
    }
300

    
301
    private void initButton(Button button, Image image, String tooltipText,
302
            String label, boolean enabled, boolean selected, Consumer<SelectionEvent> widgetSelected){
303
        if(image!=null){
304
            button.setImage(image);
305
        }
306
        if(label!=null){
307
            button.setText(label);
308
        }
309
        if(tooltipText!=null){
310
            button.setToolTipText(tooltipText);
311
        }
312
        button.setSelection(selected);
313
        button.setEnabled(enabled);
314
        button.addSelectionListener(new SelectionAdapter() {
315
            @Override
316
            public void widgetSelected(SelectionEvent e) {
317
                widgetSelected.accept(e);
318
            }
319
        });
320
    }
321

    
322
    public Label getWsLabel() {
323
        return wsLabel;
324
    }
325

    
326
    public Properties getNatTableState() {
327
        return natTableState;
328
    }
329

    
330
    public DisplayPersistenceDialogCommandHandler getDisplayPersistenceDialogCommandHandler() {
331
        return displayPersistenceDialogCommandHandler;
332
    }
333
}
(12-12/21)