Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 79743330de148b49d94313c6bc4083966e4fca8f (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*******************************************************************************
 * 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.abstractview;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.linuxtools.dataviewers.abstractviewers.AbstractSTTreeViewer;
import org.eclipse.linuxtools.dataviewers.abstractviewers.AbstractSTViewer;
import org.eclipse.linuxtools.dataviewers.actions.STCollapseAllTreeAction;
import org.eclipse.linuxtools.dataviewers.actions.STCollapseSelectionAction;
import org.eclipse.linuxtools.dataviewers.actions.STCopyAction;
import org.eclipse.linuxtools.dataviewers.actions.STDataViewersSortAction;
import org.eclipse.linuxtools.dataviewers.actions.STExpandAllTreeAction;
import org.eclipse.linuxtools.dataviewers.actions.STExpandSelectionAction;
import org.eclipse.linuxtools.dataviewers.actions.STExportToCSVAction;
import org.eclipse.linuxtools.dataviewers.actions.STHideShowColAction;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.part.ViewPart;

/**
 * The AbstractSTDataView is a view that generically implements data result views.
 */
public abstract class AbstractSTDataView extends ViewPart {

    private AbstractSTViewer stViewer = null;

    private IAction sortAction = null;

    private IAction preferencesAction = null;

    private IAction hideShowColAction = null;

    private IAction expandAllAction = null;

    private IAction collapseAllAction = null;

    private IAction expandSelectionAction = null;

    private IAction collapseSelectionAction = null;

    private IAction exportToCSVAction = null;

    private IAction copyToAction = null;

    /**
     * Create optional arbitrary Controls, on top of viewer. Does nothing by default. Can be used to display a title, or
     * some other informations.
     *
     * @param parent The parent composite, with a gridlayout (1 column).
     */
    protected abstract void createTitle(Composite parent) ;

    /**
     * Creates a wrapper handling a TreeTable or Table Viewer
     *
     * <p>
     * Subclasses may override it.
     * </p>
     *
     * @param parent The parent composite.
     * @return An AbstractSTViewer
     */
    protected abstract AbstractSTViewer createAbstractSTViewer(Composite parent);

    /**
     * Add actions to the toolbar (top-right toolbar)
     * <p>
     * Subclasses may override it.
     * </p>
     *
     * @param manager The IToolBarManager to contribute to.
     */
    protected abstract void contributeToToolbar(IToolBarManager manager);

    @Override
    public void createPartControl(Composite parent) {
        GridLayout gridLayout = new GridLayout(1, true);
        parent.setLayout(gridLayout);
        createTitle(parent);
        stViewer = createAbstractSTViewer(parent);

        // create the actions before the input is set on the viewer but after
        // the sorter and filter are set so the actions will be enabled correctly.
        createActions();

        final MenuManager mgr = initContextMenu();
        final Menu menu = mgr.createContextMenu(parent);
        stViewer.getViewer().getControl().setMenu(menu);

        getSite().registerContextMenu(mgr, stViewer.getViewer());

        // the selection provider registered
        getSite().setSelectionProvider(stViewer.getViewer());

        IActionBars actionBars = getViewSite().getActionBars();

        initMenu(actionBars.getMenuManager());
        initToolBar(actionBars.getToolBarManager());

    }

    /**
     * Creates the actions for the receiver.
     */
    protected void createActions() {
        // menu bar
        expandAllAction = createExpandAllAction();
        collapseAllAction = createCollapseAllAction();

        sortAction = new STDataViewersSortAction(getSTViewer());

        hideShowColAction = new STHideShowColAction(getSTViewer());

        exportToCSVAction = createExportToCSVAction();

        // context menu (right-click)
        expandSelectionAction = createExpandSelectionAction();
        collapseSelectionAction = createCollapseSelectionAction();
        copyToAction = new STCopyAction(getSTViewer());
    }


    /**
     * Creates the export To CSV actions.
     *
     * @return IAction The newly created action.
     */
    protected IAction createExportToCSVAction() {
        return new STExportToCSVAction(getSTViewer());
    }

    /**
     * Creates the collapse selection actions.
     *
     * @return IAction The newly created action.
     */
    private IAction createCollapseSelectionAction() {
        if (getSTViewer() instanceof AbstractSTTreeViewer) {
            AbstractSTTreeViewer stTreeViewer = (AbstractSTTreeViewer) getSTViewer();
            return new STCollapseSelectionAction(stTreeViewer);
        }
        return null;
    }

    /**
     * Creates the expand selection actions.
     *
     * @return IAction The newly created action.
     */
    private IAction createExpandSelectionAction() {
        if (getSTViewer() instanceof AbstractSTTreeViewer) {
            AbstractSTTreeViewer stTreeViewer = (AbstractSTTreeViewer) getSTViewer();
            return new STExpandSelectionAction(stTreeViewer);
        }
        return null;
    }

    /**
     * Creates the collapse all action.
     *
     * @return IAction The newly created action.
     */
    private IAction createCollapseAllAction() {
        if (getSTViewer() instanceof AbstractSTTreeViewer) {
            AbstractSTTreeViewer stTreeViewer = (AbstractSTTreeViewer) getSTViewer();
            return new STCollapseAllTreeAction(stTreeViewer);
        }
        return null;
    }

    /**
     * Creates the expand all action.
     *
     * @return IAction The newly created action.
     */
    private IAction createExpandAllAction() {
        if (getSTViewer() instanceof AbstractSTTreeViewer) {
            AbstractSTTreeViewer stTreeViewer = (AbstractSTTreeViewer) getSTViewer();
            return new STExpandAllTreeAction(stTreeViewer);
        }
        return null;
    }

    /**
     * Init the context menu
     * <p>
     * If you intend to add an action in the context menu you may override the <code>fillContextMenu</code> method.
     * </p>
     *
     * @see #fillContextMenu(IMenuManager manager)
     */
    private MenuManager initContextMenu() {
        MenuManager mgr = new MenuManager();
        mgr.setRemoveAllWhenShown(true);
        mgr.addMenuListener(mgr1 -> {
		    getSTViewer().getViewer().cancelEditing();
		    fillContextMenu(mgr1);
		});
        return mgr;
    }

    /**
     * Init the toolbar for the receiver
     * <p>
     * You can override this method if you want - but remember to call <code>super.initToolBar()</code>.
     * </p>
     *
     * @param manager The tool bar manager of this view.
     */
    private void initToolBar(IToolBarManager manager) {
        if (expandAllAction != null) {
            manager.add(expandAllAction);
        }
        if (collapseAllAction != null) {
            manager.add(collapseAllAction);
        }
        if (hideShowColAction != null) {
            manager.add(hideShowColAction);
        }
        if (exportToCSVAction != null) {
            manager.add(exportToCSVAction);
        }
        if (sortAction != null) {
            manager.add(sortAction);
        }
        contributeToToolbar(manager);
        manager.update(true);
    }

    /**
     * Init the menu for the receiver.
     *
     * @param menu The IMenuManager to add to.
     */
    private void initMenu(IMenuManager menu) {
        if (preferencesAction != null) {
            menu.add(preferencesAction);
        }
    }

    @Override
    public void setFocus() {
        Viewer viewer = getSTViewer().getViewer();
        if (viewer != null && !viewer.getControl().isDisposed()) {
            viewer.getControl().setFocus();
        }
    }

    /**
     * Return the viewer.
     *
     * @return AbstractSTViewer
     */
    public AbstractSTViewer getSTViewer() {
        return stViewer;
    }

    /**
     * Shortcut for getViewer().setInput(input).
     *
     * @param input The viewer input.
     * @see org.eclipse.jface.viewers.TreeViewer#setInput(Object)
     */
    public void setInput(Object input) {
        stViewer.getViewer().setInput(input);
    }

    /**
     * Fills the context menu (mouse right-click)
     * <p>
     * Subclasses may extend it. don't forget to call <code>super.fillContextMenu(...)</code>
     * </p>
     *
     * @param manager The IMenuManager to fill.
     * @since 6.0
     */
    protected void fillContextMenu(IMenuManager manager) {
        Control control = stViewer.getViewer().getControl();
        if (control instanceof Tree) {
            Tree tree = (Tree) control;
            TreeItem[] selection = tree.getSelection();
            if (selection != null && selection.length > 0) {
                if (collapseSelectionAction != null) {
                    manager.add(collapseSelectionAction);
                }
                if (expandSelectionAction != null) {
                    manager.add(expandSelectionAction);
                }
                manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
                if (copyToAction != null) {
                    manager.add(copyToAction);
                }
            }
        }
        manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
    }
}

Back to the top