Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a981d1b673ec97e1d5ce9ce8f4406fa02355abbb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*******************************************************************************
 * Copyright (c) 2009, 2018 STMicroelectronics and others.
 * 
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Marzia Maugeri <marzia.maugeri@st.com> - initial API and implementation
 *******************************************************************************/
package org.eclipse.linuxtools.dataviewers.abstractviewers;

import java.util.HashMap;

import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.linuxtools.dataviewers.listeners.STColumnSizeListener;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Item;

/**
 * This class is used to handle the width and visibility of all columns of a {@link AbstractSTViewer}. Width and
 * visibility of these columns are stored in a dialogSetting.
 */
public class STDataViewersHideShowManager {

    public static final int STATE_SHOWN = 1;
    public static final int STATE_HIDDEN = 0;

    private final AbstractSTViewer stViewer;
    private final int[] defaultColumnsWidth;
    private final int[] columnsWidth;
    private final int[] columnsState;

    private final HashMap<Item, STColumnSizeListener> columnsSizeListener = new HashMap<>();

    /**
     * Creates a new instance of STDataViewersHideShowManager.
     *
     * @param stViewer The viewer to manage.
     */
    public STDataViewersHideShowManager(AbstractSTViewer stViewer) {
        this.stViewer = stViewer;
        Item[] columns = stViewer.getColumns();
        this.columnsWidth = new int[columns.length];
        this.columnsState = new int[columns.length];
        this.defaultColumnsWidth = new int[columns.length];
        for (int i = 0; i < columns.length; i++) {
            ISTDataViewersField field = (ISTDataViewersField) columns[i].getData();
            columnsWidth[i] = stViewer.getColumnWidth(columns[i]);
            columnsState[i] = field.isShowingByDefault() ? STATE_SHOWN : STATE_HIDDEN;
            defaultColumnsWidth[i] = field.getPreferredWidth();
            STColumnSizeListener l = new STColumnSizeListener(this);
            columnsSizeListener.put(columns[i], l);
            columns[i].addListener(SWT.Resize, l);
            columns[i].addDisposeListener(e -> {
			    Item column = (Item) e.widget;
			    column.removeListener(SWT.Resize, columnsSizeListener.get(column));
			});
        }
    }

    /**
     * Saves the column width and visibility status in the given dialogSettings.
     *
     * @param dialogSettings The new settings to store.
     */
    public void saveState(IDialogSettings dialogSettings) {
        // delete old settings and save new ones
        IDialogSettings settings = dialogSettings.addNewSection(STDataViewersSettings.TAG_SECTION_HIDESHOW);
        for (int i = 0; i < columnsWidth.length; i++) {
            settings.put(STDataViewersSettings.TAG_HIDE_SHOW_COLUMN_WIDTH_ + i, columnsWidth[i]);
            settings.put(STDataViewersSettings.TAG_HIDE_SHOW_COLUMN_STATE_ + i, columnsState[i]);
        }
    }

    /**
     * Restores the columns width and visibility using the given dialogSettings.
     *
     * @param dialogSettings The settings to restore.
     */
    public void restoreState(IDialogSettings dialogSettings) {
        if (dialogSettings == null) {
            // no settings section
            resetState();
            return;
        }
        IDialogSettings settings = dialogSettings.getSection(STDataViewersSettings.TAG_SECTION_HIDESHOW);
        if (settings == null) {
            // no settings saved
            resetState();
            return;
        }
        try {
            for (int i = 0; i < columnsWidth.length; i++) {
                String width = settings.get(STDataViewersSettings.TAG_HIDE_SHOW_COLUMN_WIDTH_ + i);
                if (width == null) {
                    // no width data
                    resetState();
                    return;
                }
                columnsWidth[i] = Integer.parseInt(width);
                String state = settings.get(STDataViewersSettings.TAG_HIDE_SHOW_COLUMN_STATE_ + i);
                if (state == null) {
                    // no state data
                    resetState();
                    return;
                }
                columnsState[i] = Integer.parseInt(state);
            }
        } catch (NumberFormatException nfe) {
            // invalid entry
            resetState();
        }
    }

    /**
     * It restores the original columns width and visibility
     */
    private void resetState() {
        Item[] columns = stViewer.getColumns();
        for (int i = 0; i < columns.length; i++) {
            ISTDataViewersField field = (ISTDataViewersField) columns[i].getData();
            columnsState[i] = field.isShowingByDefault() ? STATE_SHOWN : STATE_HIDDEN;
            columnsWidth[i] = defaultColumnsWidth[i];
        }
    }

    /**
     * Sets the column width. If the column is hidden width would not be set.
     *
     * @param index Index of column whose width to change.
     * @param width The new width for the column
     */
    public void setWidth(int index, int width) {
        if (columnsState[index] != STATE_HIDDEN) {
            columnsWidth[index] = width;
        }
    }

    /**
     * Sets the state of column
     * @param index
     *            index of the column
     * @param state
     *            one of {@link #STATE_SHOWN} or {@link #STATE_HIDDEN}
     */
    public void setState(int index, int state) {
        columnsState[index] = state;
    }

    /**
     * Gets the column width
     * @param index
     *            index of the column
     * @return a column width
     */
    public int getWidth(int index) {
        return columnsWidth[index];
    }

    /**
     * Gets the column state.
     * @param index
     *            of the column
     * @return one of {@link #STATE_SHOWN} or {@link #STATE_HIDDEN}
     */
    public int getState(int index) {
        return columnsState[index];
    }

    /**
     * Gets the status ({@link #STATE_HIDDEN} or {@link #STATE_SHOWN}) of all columns.
     * @return an array of status
     */
    public int[] getColumnsState() {
        return columnsState;
    }

    /**
     * Updates the columns width and status
     * @since 5.0
     */
    public void updateColumns() {
        Item[] columns = stViewer.getColumns();
        for (int i = columns.length; i-- > 0;) {
            Item column = columns[i];
            if (getState(i) == STDataViewersHideShowManager.STATE_HIDDEN) {
                stViewer.setColumnWidth(column, 0);
                stViewer.setColumnResizable(column, false);
            } else {
                stViewer.setColumnWidth(column, getWidth(i));
                stViewer.setColumnResizable(column, true);
            }
        }
    }

    /**
     * Gets the STViewer hooked to this Hide/Show Manager
     * @return AbstractSTViewer
     */
    public AbstractSTViewer getSTViewer() {
        return stViewer;
    }

}

Back to the top