1 /*******************************************************************************
2 * Copyright (c) 2010, 2013 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
.linuxtools
.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
.linuxtools
.internal
.tmf
.ui
.Activator
;
35 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Messages
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.filter
.model
.ITmfFilterTreeNode
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.filter
.model
.TmfFilterNode
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.filter
.model
.TmfFilterRootNode
;
39 import org
.eclipse
.linuxtools
.tmf
.core
.filter
.xml
.TmfFilterXMLParser
;
40 import org
.eclipse
.linuxtools
.tmf
.core
.filter
.xml
.TmfFilterXMLWriter
;
41 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.TmfView
;
42 import org
.eclipse
.swt
.SWT
;
43 import org
.eclipse
.swt
.graphics
.Image
;
44 import org
.eclipse
.swt
.widgets
.Composite
;
45 import org
.eclipse
.swt
.widgets
.FileDialog
;
46 import org
.eclipse
.swt
.widgets
.Shell
;
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. This does not modify the XML, which must
114 * be done manually. If the filter is already in the FilterView, this is a
121 public void addFilter(ITmfFilterTreeNode filter
) {
122 ITmfFilterTreeNode root
= fViewer
.getInput();
123 for (ITmfFilterTreeNode node
: root
.getChildren()) {
124 if (node
.equals(filter
)) {
128 root
.addChild(filter
);
129 fViewer
.setInput(root
);
133 * Refresh the tree widget
135 public void refresh() {
140 * Setter for selection
145 public void setSelection(ITmfFilterTreeNode node
) {
146 fViewer
.setSelection(node
, true);
149 // ------------------------------------------------------------------------
151 // ------------------------------------------------------------------------
154 public void createPartControl(Composite parent
) {
156 fViewer
= new FilterViewer(parent
, SWT
.NONE
);
157 fViewer
.setInput(fRoot
);
159 contributeToActionBars();
161 fViewer
.addSelectionChangedListener(new ISelectionChangedListener() {
163 public void selectionChanged(SelectionChangedEvent event
) {
164 if (!(event
.getSelection().isEmpty()) && event
.getSelection() instanceof IStructuredSelection
) {
165 fExportAction
.setEnabled(true);
167 fExportAction
.setEnabled(false);
171 this.getSite().setSelectionProvider(fViewer
.getTreeViewer());
173 MenuManager menuManager
= fViewer
.getMenuManager();
174 this.getSite().registerContextMenu(menuManager
, fViewer
.getTreeViewer());
178 * @return the ITmfFilterTreeNode currently selected
180 ITmfFilterTreeNode
getSelection() {
181 return fViewer
.getSelection();
185 public void setFocus() {
190 public String
toString() {
191 return "[FilterView]"; //$NON-NLS-1$
195 * Builds the menu toolbar
197 private void contributeToActionBars() {
198 IActionBars bars
= getViewSite().getActionBars();
199 // fillLocalPullDown(bars.getMenuManager());
200 fillLocalToolBar(bars
.getToolBarManager());
204 * Build the popup menu
207 * The manager to build
209 private void fillLocalToolBar(IToolBarManager manager
) {
211 fSaveAction
= new SaveAction();
212 fSaveAction
.setImageDescriptor(ImageDescriptor
.createFromImage(SAVE_IMAGE
));
213 fSaveAction
.setToolTipText(Messages
.FilterView_SaveActionToolTipText
);
215 fAddAction
= new AddAction();
216 fAddAction
.setImageDescriptor(ImageDescriptor
.createFromImage(ADD_IMAGE
));
217 fAddAction
.setToolTipText(Messages
.FilterView_AddActionToolTipText
);
219 fExportAction
= new ExportAction();
220 fExportAction
.setImageDescriptor(ImageDescriptor
.createFromImage(EXPORT_IMAGE
));
221 fExportAction
.setToolTipText(Messages
.FilterView_ExportActionToolTipText
);
223 fImportAction
= new ImportAction();
224 fImportAction
.setImageDescriptor(ImageDescriptor
.createFromImage(IMPORT_IMAGE
));
225 fImportAction
.setToolTipText(Messages
.FilterView_ImportActionToolTipText
);
227 manager
.add(fSaveAction
);
228 manager
.add(new Separator("add_delete")); //$NON-NLS-1$
229 manager
.add(fAddAction
);
230 manager
.add(new Separator("edit")); //$NON-NLS-1$
231 manager
.add(new Separator());
232 manager
.add(fExportAction
);
233 manager
.add(fImportAction
);
236 private class SaveAction
extends Action
{
239 FilterManager
.setSavedFilters(fRoot
.getChildren());
243 private class AddAction
extends Action
{
247 TmfFilterNode newNode
= new TmfFilterNode(fRoot
, ""); //$NON-NLS-1$
249 setSelection(newNode
);
253 private class ExportAction
extends Action
{
257 FileDialog dlg
= new FileDialog(new Shell(), SWT
.SAVE
);
258 dlg
.setFilterNames(new String
[] { Messages
.FilterView_FileDialogFilterName
+ " (*.xml)" }); //$NON-NLS-1$
259 dlg
.setFilterExtensions(new String
[] { "*.xml" }); //$NON-NLS-1$
261 String fn
= dlg
.open();
263 TmfFilterXMLWriter writerXML
= new TmfFilterXMLWriter(fRoot
);
264 writerXML
.saveTree(fn
);
267 } catch (ParserConfigurationException e
) {
268 Activator
.getDefault().logError("Error parsing filter xml file", e
); //$NON-NLS-1$
273 private class ImportAction
extends Action
{
276 if (fViewer
!= null) {
277 ITmfFilterTreeNode root
= null;
279 FileDialog dlg
= new FileDialog(new Shell(), SWT
.OPEN
);
280 dlg
.setFilterNames(new String
[] { Messages
.FilterView_FileDialogFilterName
+ " (*.xml)" }); //$NON-NLS-1$
281 dlg
.setFilterExtensions(new String
[] { "*.xml" }); //$NON-NLS-1$
283 TmfFilterXMLParser parserXML
= null;
284 String fn
= dlg
.open();
286 parserXML
= new TmfFilterXMLParser(fn
);
287 root
= parserXML
.getTree();
290 } catch (SAXException e
) {
291 Activator
.getDefault().logError("Error importing filter xml file", e
); //$NON-NLS-1$
292 } catch (IOException e
) {
293 Activator
.getDefault().logError("Error importing filter xml file", e
); //$NON-NLS-1$
297 for (ITmfFilterTreeNode node
: root
.getChildren()) {
298 if (node
instanceof TmfFilterNode
) {
299 fRoot
.addChild(node
);
301 fViewer
.setSelection(node
);