Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4bc767740061a280b171436dc2c6e112a38636b5 (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
/*******************************************************************************
 * Copyright (c) 2010, 2013 Ericsson
 *
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Yuriy Vashchuk - Initial API and implementation
 *   Xavier Raynaud - add cut/copy/paste/dnd support
 *   based on Francois Chouinard ProjectView code.
 */

package org.eclipse.linuxtools.tmf.ui.views.filter;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.linuxtools.internal.tmf.ui.Activator;
import org.eclipse.linuxtools.internal.tmf.ui.Messages;
import org.eclipse.linuxtools.tmf.core.filter.model.ITmfFilterTreeNode;
import org.eclipse.linuxtools.tmf.core.filter.model.TmfFilterNode;
import org.eclipse.linuxtools.tmf.core.filter.model.TmfFilterRootNode;
import org.eclipse.linuxtools.tmf.core.filter.xml.TmfFilterXMLParser;
import org.eclipse.linuxtools.tmf.core.filter.xml.TmfFilterXMLWriter;
import org.eclipse.linuxtools.tmf.ui.views.TmfView;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionBars;
import org.xml.sax.SAXException;

/**
 * View that contain UI to the TMF filter.
 *
 * @version 1.0
 * @author Yuriy Vashchuk
 */
public class FilterView extends TmfView {

    /** ID for the Filter view */
    public static final String ID = "org.eclipse.linuxtools.tmf.ui.views.filter"; //$NON-NLS-1$

    private static final Image SAVE_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/save_button.gif"); //$NON-NLS-1$
    private static final Image ADD_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/add_button.gif"); //$NON-NLS-1$
    private static final Image IMPORT_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/import_button.gif"); //$NON-NLS-1$
    private static final Image EXPORT_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/export_button.gif"); //$NON-NLS-1$

    // ------------------------------------------------------------------------
    // Main data structures
    // ------------------------------------------------------------------------

    private FilterViewer fViewer;
    private final ITmfFilterTreeNode fRoot;

    private final IWorkspace fWorkspace;

    private SaveAction fSaveAction;
    private AddAction fAddAction;
    private ExportAction fExportAction;
    private ImportAction fImportAction;

    /**
     * Getter for the Filter Tree Root
     *
     * @return The root of builded tree
     */
    public ITmfFilterTreeNode getFilterRoot() {
        return fRoot;
    }

    // ------------------------------------------------------------------------
    // Constructor
    // ------------------------------------------------------------------------

    /**
     * Default Constructor
     */
    public FilterView() {
        super("Filter"); //$NON-NLS-1$

        fWorkspace = ResourcesPlugin.getWorkspace();
        try {
            fWorkspace.getRoot().refreshLocal(IResource.DEPTH_INFINITE, null);
        } catch (CoreException e) {
            Activator.getDefault().logError("Error refreshing workspace", e); //$NON-NLS-1$
        }

        fRoot = new TmfFilterRootNode();
        for (ITmfFilterTreeNode node : FilterManager.getSavedFilters()) {
            fRoot.addChild(node);
        }
    }

    /**
     * Refresh the tree widget
     */
    public void refresh() {
        fViewer.refresh();
    }

    /**
     * Setter for selection
     *
     * @param node
     *            The node to select
     */
    public void setSelection(ITmfFilterTreeNode node) {
        fViewer.setSelection(node, true);
    }

    // ------------------------------------------------------------------------
    // ViewPart
    // ------------------------------------------------------------------------

    @Override
    public void createPartControl(Composite parent) {

        fViewer = new FilterViewer(parent, SWT.NONE);
        fViewer.setInput(fRoot);

        contributeToActionBars();

        fViewer.addSelectionChangedListener(new ISelectionChangedListener() {
            @Override
            public void selectionChanged(SelectionChangedEvent event) {
                if (!(event.getSelection().isEmpty()) && event.getSelection() instanceof IStructuredSelection) {
                    fExportAction.setEnabled(true);
                } else {
                    fExportAction.setEnabled(false);
                }
            }
        });
        this.getSite().setSelectionProvider(fViewer.getTreeViewer());

        MenuManager menuManager = fViewer.getMenuManager();
        this.getSite().registerContextMenu(menuManager, fViewer.getTreeViewer());
    }

    /**
     * @return the ITmfFilterTreeNode currently selected
     */
    ITmfFilterTreeNode getSelection() {
        return fViewer.getSelection();
    }

    @Override
    public void setFocus() {
        fViewer.setFocus();
    }

    @Override
    public String toString() {
        return "[FilterView]"; //$NON-NLS-1$
    }

    /**
     * Builds the menu toolbar
     */
    private void contributeToActionBars() {
        IActionBars bars = getViewSite().getActionBars();
        // fillLocalPullDown(bars.getMenuManager());
        fillLocalToolBar(bars.getToolBarManager());
    }

    /**
     * Build the popup menu
     *
     * @param manager
     *            The manager to build
     */
    private void fillLocalToolBar(IToolBarManager manager) {

        fSaveAction = new SaveAction();
        fSaveAction.setImageDescriptor(ImageDescriptor.createFromImage(SAVE_IMAGE));
        fSaveAction.setToolTipText(Messages.FilterView_SaveActionToolTipText);

        fAddAction = new AddAction();
        fAddAction.setImageDescriptor(ImageDescriptor.createFromImage(ADD_IMAGE));
        fAddAction.setToolTipText(Messages.FilterView_AddActionToolTipText);

        fExportAction = new ExportAction();
        fExportAction.setImageDescriptor(ImageDescriptor.createFromImage(EXPORT_IMAGE));
        fExportAction.setToolTipText(Messages.FilterView_ExportActionToolTipText);

        fImportAction = new ImportAction();
        fImportAction.setImageDescriptor(ImageDescriptor.createFromImage(IMPORT_IMAGE));
        fImportAction.setToolTipText(Messages.FilterView_ImportActionToolTipText);

        manager.add(fSaveAction);
        manager.add(new Separator("add_delete")); //$NON-NLS-1$
        manager.add(fAddAction);
        manager.add(new Separator("edit")); //$NON-NLS-1$
        manager.add(new Separator());
        manager.add(fExportAction);
        manager.add(fImportAction);
    }

    private class SaveAction extends Action {
        @Override
        public void run() {
            FilterManager.setSavedFilters(fRoot.getChildren());
        }
    }

    private class AddAction extends Action {
        @Override
        public void run() {

            TmfFilterNode newNode = new TmfFilterNode(fRoot, ""); //$NON-NLS-1$
            refresh();
            setSelection(newNode);
        }
    }

    private class ExportAction extends Action {
        @Override
        public void run() {
            try {
                FileDialog dlg = new FileDialog(new Shell(), SWT.SAVE);
                dlg.setFilterNames(new String[] { Messages.FilterView_FileDialogFilterName + " (*.filter.xml)" }); //$NON-NLS-1$
                dlg.setFilterExtensions(new String[] { "*.filter.xml" }); //$NON-NLS-1$

                String fn = dlg.open();
                if (fn != null) {
                    TmfFilterXMLWriter writerXML = new TmfFilterXMLWriter(fRoot);
                    writerXML.saveTree(fn);
                }

            } catch (ParserConfigurationException e) {
                Activator.getDefault().logError("Error parsing filter xml file", e); //$NON-NLS-1$
            }
        }
    }

    private class ImportAction extends Action {
        @Override
        public void run() {
            if (fViewer != null) {
                ITmfFilterTreeNode root = null;
                try {
                    FileDialog dlg = new FileDialog(new Shell(), SWT.OPEN);
                    dlg.setFilterNames(new String[] { Messages.FilterView_FileDialogFilterName + " (*.filter.xml)" }); //$NON-NLS-1$
                    dlg.setFilterExtensions(new String[] { "*.filter.xml" }); //$NON-NLS-1$

                    TmfFilterXMLParser parserXML = null;
                    String fn = dlg.open();
                    if (fn != null) {
                        parserXML = new TmfFilterXMLParser(fn);
                        root = parserXML.getTree();
                    }

                } catch (SAXException e) {
                    Activator.getDefault().logError("Error importing filter xml file", e); //$NON-NLS-1$
                } catch (IOException e) {
                    Activator.getDefault().logError("Error importing filter xml file", e); //$NON-NLS-1$
                }

                if (root != null) {
                    for (ITmfFilterTreeNode node : root.getChildren()) {
                        if (node instanceof TmfFilterNode) {
                            fRoot.addChild(node);
                            refresh();
                            fViewer.setSelection(node);
                        }
                    }
                }
            }
        }
    }

}

Back to the top