1 /*******************************************************************************
2 * Copyright (c) 2010, 2014 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Yuriy Vashchuk - Initial API and implementation
11 * Xavier Raynaud - add cut/copy/paste/dnd support
12 * based on Francois Chouinard ProjectView code.
13 *******************************************************************************/
15 package org
.eclipse
.tracecompass
.tmf
.ui
.views
.filter
;
17 import java
.io
.IOException
;
19 import javax
.xml
.parsers
.ParserConfigurationException
;
21 import org
.eclipse
.core
.resources
.IResource
;
22 import org
.eclipse
.core
.resources
.IWorkspace
;
23 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
24 import org
.eclipse
.core
.runtime
.CoreException
;
25 import org
.eclipse
.jdt
.annotation
.NonNull
;
26 import org
.eclipse
.jface
.action
.Action
;
27 import org
.eclipse
.jface
.action
.IToolBarManager
;
28 import org
.eclipse
.jface
.action
.MenuManager
;
29 import org
.eclipse
.jface
.action
.Separator
;
30 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
31 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
32 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
33 import org
.eclipse
.jface
.viewers
.SelectionChangedEvent
;
34 import org
.eclipse
.swt
.SWT
;
35 import org
.eclipse
.swt
.graphics
.Image
;
36 import org
.eclipse
.swt
.widgets
.Composite
;
37 import org
.eclipse
.swt
.widgets
.FileDialog
;
38 import org
.eclipse
.swt
.widgets
.Shell
;
39 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Activator
;
40 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Messages
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.filter
.model
.ITmfFilterTreeNode
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.filter
.model
.TmfFilterNode
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.filter
.model
.TmfFilterRootNode
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.filter
.xml
.TmfFilterXMLParser
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.filter
.xml
.TmfFilterXMLWriter
;
46 import org
.eclipse
.tracecompass
.tmf
.ui
.dialog
.TmfFileDialogFactory
;
47 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.TmfView
;
48 import org
.eclipse
.ui
.IActionBars
;
49 import org
.xml
.sax
.SAXException
;
52 * View that contain UI to the TMF filter.
55 * @author Yuriy Vashchuk
57 public class FilterView
extends TmfView
{
59 /** ID for the Filter view */
60 public static final @NonNull String ID
= "org.eclipse.linuxtools.tmf.ui.views.filter"; //$NON-NLS-1$
62 private static final Image SAVE_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/save_button.gif"); //$NON-NLS-1$
63 private static final Image ADD_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/add_button.gif"); //$NON-NLS-1$
64 private static final Image IMPORT_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/import_button.gif"); //$NON-NLS-1$
65 private static final Image EXPORT_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/export_button.gif"); //$NON-NLS-1$
67 // ------------------------------------------------------------------------
68 // Main data structures
69 // ------------------------------------------------------------------------
71 private FilterViewer fViewer
;
72 private final ITmfFilterTreeNode fRoot
;
74 private final IWorkspace fWorkspace
;
76 private SaveAction fSaveAction
;
77 private AddAction fAddAction
;
78 private ExportAction fExportAction
;
79 private ImportAction fImportAction
;
82 * Getter for the Filter Tree Root
84 * @return The root of builded tree
86 public ITmfFilterTreeNode
getFilterRoot() {
90 // ------------------------------------------------------------------------
92 // ------------------------------------------------------------------------
98 super("Filter"); //$NON-NLS-1$
100 fWorkspace
= ResourcesPlugin
.getWorkspace();
102 fWorkspace
.getRoot().refreshLocal(IResource
.DEPTH_INFINITE
, null);
103 } catch (CoreException e
) {
104 Activator
.getDefault().logError("Error refreshing workspace", e
); //$NON-NLS-1$
107 fRoot
= new TmfFilterRootNode();
108 for (ITmfFilterTreeNode node
: FilterManager
.getSavedFilters()) {
109 fRoot
.addChild(node
);
114 * Add a filter to the FilterView and select it. This does not modify the
115 * XML, which must be done manually. If an equivalent filter is already in
116 * the FilterView, it is not added.
121 public void addFilter(ITmfFilterTreeNode filter
) {
122 if (filter
== null) {
125 ITmfFilterTreeNode root
= fViewer
.getInput();
126 for (ITmfFilterTreeNode node
: root
.getChildren()) {
127 // Use toString(explicit) equality because equals() is not implemented
128 if (node
.toString(true).equals(filter
.toString(true))) {
129 fViewer
.setSelection(node
);
133 root
.addChild(filter
);
134 fViewer
.setInput(root
);
135 fViewer
.setSelection(filter
);
139 * Refresh the tree widget
141 public void refresh() {
146 * Setter for selection
151 public void setSelection(ITmfFilterTreeNode node
) {
152 fViewer
.setSelection(node
, true);
155 // ------------------------------------------------------------------------
157 // ------------------------------------------------------------------------
160 public void createPartControl(Composite parent
) {
162 fViewer
= new FilterViewer(parent
, SWT
.NONE
);
163 fViewer
.setInput(fRoot
);
165 contributeToActionBars();
167 fViewer
.addSelectionChangedListener(new ISelectionChangedListener() {
169 public void selectionChanged(SelectionChangedEvent event
) {
170 if (!(event
.getSelection().isEmpty()) && event
.getSelection() instanceof IStructuredSelection
) {
171 fExportAction
.setEnabled(true);
173 fExportAction
.setEnabled(false);
177 this.getSite().setSelectionProvider(fViewer
.getTreeViewer());
179 MenuManager menuManager
= fViewer
.getMenuManager();
180 this.getSite().registerContextMenu(menuManager
, fViewer
.getTreeViewer());
184 * @return the ITmfFilterTreeNode currently selected
186 ITmfFilterTreeNode
getSelection() {
187 return fViewer
.getSelection();
191 public void setFocus() {
196 * @return whether the tree is in focus or not
198 public boolean isTreeInFocus() {
199 return fViewer
.isTreeInFocus();
203 public String
toString() {
204 return "[FilterView]"; //$NON-NLS-1$
208 * Builds the menu toolbar
210 private void contributeToActionBars() {
211 IActionBars bars
= getViewSite().getActionBars();
212 // fillLocalPullDown(bars.getMenuManager());
213 fillLocalToolBar(bars
.getToolBarManager());
217 * Build the popup menu
220 * The manager to build
222 private void fillLocalToolBar(IToolBarManager manager
) {
224 fSaveAction
= new SaveAction();
225 fSaveAction
.setImageDescriptor(ImageDescriptor
.createFromImage(SAVE_IMAGE
));
226 fSaveAction
.setToolTipText(Messages
.FilterView_SaveActionToolTipText
);
228 fAddAction
= new AddAction();
229 fAddAction
.setImageDescriptor(ImageDescriptor
.createFromImage(ADD_IMAGE
));
230 fAddAction
.setToolTipText(Messages
.FilterView_AddActionToolTipText
);
232 fExportAction
= new ExportAction();
233 fExportAction
.setImageDescriptor(ImageDescriptor
.createFromImage(EXPORT_IMAGE
));
234 fExportAction
.setToolTipText(Messages
.FilterView_ExportActionToolTipText
);
236 fImportAction
= new ImportAction();
237 fImportAction
.setImageDescriptor(ImageDescriptor
.createFromImage(IMPORT_IMAGE
));
238 fImportAction
.setToolTipText(Messages
.FilterView_ImportActionToolTipText
);
240 manager
.add(fSaveAction
);
241 manager
.add(new Separator("add_delete")); //$NON-NLS-1$
242 manager
.add(fAddAction
);
243 manager
.add(new Separator("edit")); //$NON-NLS-1$
244 manager
.add(new Separator());
245 manager
.add(fExportAction
);
246 manager
.add(fImportAction
);
249 private class SaveAction
extends Action
{
252 FilterManager
.setSavedFilters(fRoot
.getChildren());
256 private class AddAction
extends Action
{
260 TmfFilterNode newNode
= new TmfFilterNode(fRoot
, ""); //$NON-NLS-1$
262 setSelection(newNode
);
266 private class ExportAction
extends Action
{
270 FileDialog dlg
= TmfFileDialogFactory
.create(new Shell(), SWT
.SAVE
);
271 dlg
.setFilterNames(new String
[] { Messages
.FilterView_FileDialogFilterName
+ " (*.xml)" }); //$NON-NLS-1$
272 dlg
.setFilterExtensions(new String
[] { "*.xml" }); //$NON-NLS-1$
274 String fn
= dlg
.open();
276 TmfFilterXMLWriter writerXML
= new TmfFilterXMLWriter(fRoot
);
277 writerXML
.saveTree(fn
);
280 } catch (ParserConfigurationException e
) {
281 Activator
.getDefault().logError("Error parsing filter xml file", e
); //$NON-NLS-1$
286 private class ImportAction
extends Action
{
289 if (fViewer
!= null) {
290 ITmfFilterTreeNode root
= null;
292 FileDialog dlg
= TmfFileDialogFactory
.create(new Shell(), SWT
.OPEN
);
293 dlg
.setFilterNames(new String
[] { Messages
.FilterView_FileDialogFilterName
+ " (*.xml)" }); //$NON-NLS-1$
294 dlg
.setFilterExtensions(new String
[] { "*.xml" }); //$NON-NLS-1$
296 TmfFilterXMLParser parserXML
= null;
297 String fn
= dlg
.open();
299 parserXML
= new TmfFilterXMLParser(fn
);
300 root
= parserXML
.getTree();
303 } catch (SAXException e
) {
304 Activator
.getDefault().logError("Error importing filter xml file", e
); //$NON-NLS-1$
305 } catch (IOException e
) {
306 Activator
.getDefault().logError("Error importing filter xml file", e
); //$NON-NLS-1$
310 for (ITmfFilterTreeNode node
: root
.getChildren()) {
311 if (node
instanceof TmfFilterNode
) {
312 fRoot
.addChild(node
);
314 fViewer
.setSelection(node
);