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
.views
.TmfView
;
47 import org
.eclipse
.ui
.IActionBars
;
48 import org
.xml
.sax
.SAXException
;
51 * View that contain UI to the TMF filter.
54 * @author Yuriy Vashchuk
56 public class FilterView
extends TmfView
{
58 /** ID for the Filter view */
59 public static final @NonNull String ID
= "org.eclipse.linuxtools.tmf.ui.views.filter"; //$NON-NLS-1$
61 private static final Image SAVE_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/save_button.gif"); //$NON-NLS-1$
62 private static final Image ADD_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/add_button.gif"); //$NON-NLS-1$
63 private static final Image IMPORT_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/import_button.gif"); //$NON-NLS-1$
64 private static final Image EXPORT_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/export_button.gif"); //$NON-NLS-1$
66 // ------------------------------------------------------------------------
67 // Main data structures
68 // ------------------------------------------------------------------------
70 private FilterViewer fViewer
;
71 private final ITmfFilterTreeNode fRoot
;
73 private final IWorkspace fWorkspace
;
75 private SaveAction fSaveAction
;
76 private AddAction fAddAction
;
77 private ExportAction fExportAction
;
78 private ImportAction fImportAction
;
81 * Getter for the Filter Tree Root
83 * @return The root of builded tree
85 public ITmfFilterTreeNode
getFilterRoot() {
89 // ------------------------------------------------------------------------
91 // ------------------------------------------------------------------------
97 super("Filter"); //$NON-NLS-1$
99 fWorkspace
= ResourcesPlugin
.getWorkspace();
101 fWorkspace
.getRoot().refreshLocal(IResource
.DEPTH_INFINITE
, null);
102 } catch (CoreException e
) {
103 Activator
.getDefault().logError("Error refreshing workspace", e
); //$NON-NLS-1$
106 fRoot
= new TmfFilterRootNode();
107 for (ITmfFilterTreeNode node
: FilterManager
.getSavedFilters()) {
108 fRoot
.addChild(node
);
113 * Add a filter to the FilterView and select it. This does not modify the
114 * XML, which must be done manually. If an equivalent filter is already in
115 * the FilterView, it is not added.
120 public void addFilter(ITmfFilterTreeNode filter
) {
121 if (filter
== null) {
124 ITmfFilterTreeNode root
= fViewer
.getInput();
125 for (ITmfFilterTreeNode node
: root
.getChildren()) {
126 // Use toString(explicit) equality because equals() is not implemented
127 if (node
.toString(true).equals(filter
.toString(true))) {
128 fViewer
.setSelection(node
);
132 root
.addChild(filter
);
133 fViewer
.setInput(root
);
134 fViewer
.setSelection(filter
);
138 * Refresh the tree widget
140 public void refresh() {
145 * Setter for selection
150 public void setSelection(ITmfFilterTreeNode node
) {
151 fViewer
.setSelection(node
, true);
154 // ------------------------------------------------------------------------
156 // ------------------------------------------------------------------------
159 public void createPartControl(Composite parent
) {
161 fViewer
= new FilterViewer(parent
, SWT
.NONE
);
162 fViewer
.setInput(fRoot
);
164 contributeToActionBars();
166 fViewer
.addSelectionChangedListener(new ISelectionChangedListener() {
168 public void selectionChanged(SelectionChangedEvent event
) {
169 if (!(event
.getSelection().isEmpty()) && event
.getSelection() instanceof IStructuredSelection
) {
170 fExportAction
.setEnabled(true);
172 fExportAction
.setEnabled(false);
176 this.getSite().setSelectionProvider(fViewer
.getTreeViewer());
178 MenuManager menuManager
= fViewer
.getMenuManager();
179 this.getSite().registerContextMenu(menuManager
, fViewer
.getTreeViewer());
183 * @return the ITmfFilterTreeNode currently selected
185 ITmfFilterTreeNode
getSelection() {
186 return fViewer
.getSelection();
190 public void setFocus() {
195 * @return whether the tree is in focus or not
197 public boolean isTreeInFocus() {
198 return fViewer
.isTreeInFocus();
202 public String
toString() {
203 return "[FilterView]"; //$NON-NLS-1$
207 * Builds the menu toolbar
209 private void contributeToActionBars() {
210 IActionBars bars
= getViewSite().getActionBars();
211 // fillLocalPullDown(bars.getMenuManager());
212 fillLocalToolBar(bars
.getToolBarManager());
216 * Build the popup menu
219 * The manager to build
221 private void fillLocalToolBar(IToolBarManager manager
) {
223 fSaveAction
= new SaveAction();
224 fSaveAction
.setImageDescriptor(ImageDescriptor
.createFromImage(SAVE_IMAGE
));
225 fSaveAction
.setToolTipText(Messages
.FilterView_SaveActionToolTipText
);
227 fAddAction
= new AddAction();
228 fAddAction
.setImageDescriptor(ImageDescriptor
.createFromImage(ADD_IMAGE
));
229 fAddAction
.setToolTipText(Messages
.FilterView_AddActionToolTipText
);
231 fExportAction
= new ExportAction();
232 fExportAction
.setImageDescriptor(ImageDescriptor
.createFromImage(EXPORT_IMAGE
));
233 fExportAction
.setToolTipText(Messages
.FilterView_ExportActionToolTipText
);
235 fImportAction
= new ImportAction();
236 fImportAction
.setImageDescriptor(ImageDescriptor
.createFromImage(IMPORT_IMAGE
));
237 fImportAction
.setToolTipText(Messages
.FilterView_ImportActionToolTipText
);
239 manager
.add(fSaveAction
);
240 manager
.add(new Separator("add_delete")); //$NON-NLS-1$
241 manager
.add(fAddAction
);
242 manager
.add(new Separator("edit")); //$NON-NLS-1$
243 manager
.add(new Separator());
244 manager
.add(fExportAction
);
245 manager
.add(fImportAction
);
248 private class SaveAction
extends Action
{
251 FilterManager
.setSavedFilters(fRoot
.getChildren());
255 private class AddAction
extends Action
{
259 TmfFilterNode newNode
= new TmfFilterNode(fRoot
, ""); //$NON-NLS-1$
261 setSelection(newNode
);
265 private class ExportAction
extends Action
{
269 FileDialog dlg
= new FileDialog(new Shell(), SWT
.SAVE
);
270 dlg
.setFilterNames(new String
[] { Messages
.FilterView_FileDialogFilterName
+ " (*.xml)" }); //$NON-NLS-1$
271 dlg
.setFilterExtensions(new String
[] { "*.xml" }); //$NON-NLS-1$
273 String fn
= dlg
.open();
275 TmfFilterXMLWriter writerXML
= new TmfFilterXMLWriter(fRoot
);
276 writerXML
.saveTree(fn
);
279 } catch (ParserConfigurationException e
) {
280 Activator
.getDefault().logError("Error parsing filter xml file", e
); //$NON-NLS-1$
285 private class ImportAction
extends Action
{
288 if (fViewer
!= null) {
289 ITmfFilterTreeNode root
= null;
291 FileDialog dlg
= new FileDialog(new Shell(), SWT
.OPEN
);
292 dlg
.setFilterNames(new String
[] { Messages
.FilterView_FileDialogFilterName
+ " (*.xml)" }); //$NON-NLS-1$
293 dlg
.setFilterExtensions(new String
[] { "*.xml" }); //$NON-NLS-1$
295 TmfFilterXMLParser parserXML
= null;
296 String fn
= dlg
.open();
298 parserXML
= new TmfFilterXMLParser(fn
);
299 root
= parserXML
.getTree();
302 } catch (SAXException e
) {
303 Activator
.getDefault().logError("Error importing filter xml file", e
); //$NON-NLS-1$
304 } catch (IOException e
) {
305 Activator
.getDefault().logError("Error importing filter xml file", e
); //$NON-NLS-1$
309 for (ITmfFilterTreeNode node
: root
.getChildren()) {
310 if (node
instanceof TmfFilterNode
) {
311 fRoot
.addChild(node
);
313 fViewer
.setSelection(node
);