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 * based on Francois Chouinard ProjectView code.
14 package org
.eclipse
.linuxtools
.tmf
.ui
.views
.filter
;
16 import java
.io
.IOException
;
18 import javax
.xml
.parsers
.ParserConfigurationException
;
20 import org
.eclipse
.core
.resources
.IResource
;
21 import org
.eclipse
.core
.resources
.IWorkspace
;
22 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
23 import org
.eclipse
.core
.runtime
.CoreException
;
24 import org
.eclipse
.jface
.action
.Action
;
25 import org
.eclipse
.jface
.action
.IToolBarManager
;
26 import org
.eclipse
.jface
.action
.Separator
;
27 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
28 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
29 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
30 import org
.eclipse
.jface
.viewers
.SelectionChangedEvent
;
31 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
32 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Messages
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.filter
.model
.ITmfFilterTreeNode
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.filter
.model
.TmfFilterNode
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.filter
.model
.TmfFilterRootNode
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.filter
.xml
.TmfFilterXMLParser
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.filter
.xml
.TmfFilterXMLWriter
;
38 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.TmfView
;
39 import org
.eclipse
.swt
.SWT
;
40 import org
.eclipse
.swt
.graphics
.Image
;
41 import org
.eclipse
.swt
.widgets
.Composite
;
42 import org
.eclipse
.swt
.widgets
.FileDialog
;
43 import org
.eclipse
.swt
.widgets
.Shell
;
44 import org
.eclipse
.ui
.IActionBars
;
45 import org
.xml
.sax
.SAXException
;
48 * View that contain UI to the TMF filter.
51 * @author Yuriy Vashchuk
53 public class FilterView
extends TmfView
{
55 /** ID for the Filter view */
56 public static final String ID
= "org.eclipse.linuxtools.tmf.ui.views.filter"; //$NON-NLS-1$
58 private static final Image SAVE_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/save_button.gif"); //$NON-NLS-1$
59 private static final Image ADD_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/add_button.gif"); //$NON-NLS-1$
60 private static final Image DELETE_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/delete_button.gif"); //$NON-NLS-1$
61 private static final Image IMPORT_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/import_button.gif"); //$NON-NLS-1$
62 private static final Image EXPORT_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/export_button.gif"); //$NON-NLS-1$
64 // ------------------------------------------------------------------------
65 // Main data structures
66 // ------------------------------------------------------------------------
68 private FilterViewer fViewer
;
69 private final ITmfFilterTreeNode fRoot
;
71 private final IWorkspace fWorkspace
;
73 private SaveAction fSaveAction
;
74 private AddAction fAddAction
;
75 private DeleteAction fDeleteAction
;
76 private ExportAction fExportAction
;
77 private ImportAction fImportAction
;
80 * Getter for the Filter Tree Root
82 * @return The root of builded tree
84 public ITmfFilterTreeNode
getFilterRoot() {
88 // ------------------------------------------------------------------------
90 // ------------------------------------------------------------------------
96 super("Filter"); //$NON-NLS-1$
98 fWorkspace
= ResourcesPlugin
.getWorkspace();
100 fWorkspace
.getRoot().refreshLocal(IResource
.DEPTH_INFINITE
, null);
101 } catch (CoreException e
) {
102 Activator
.getDefault().logError("Error refreshing workspace", e
); //$NON-NLS-1$
105 fRoot
= new TmfFilterRootNode();
106 for (ITmfFilterTreeNode node
: FilterManager
.getSavedFilters()) {
107 fRoot
.addChild(node
);
112 * Refresh the tree widget
114 public void refresh() {
119 * Setter for selection
124 public void setSelection(ITmfFilterTreeNode node
) {
125 fViewer
.setSelection(node
, true);
128 // ------------------------------------------------------------------------
130 // ------------------------------------------------------------------------
133 public void createPartControl(Composite parent
) {
135 fViewer
= new FilterViewer(parent
, SWT
.NONE
);
136 fViewer
.setInput(fRoot
);
138 contributeToActionBars();
140 fViewer
.addSelectionChangedListener(new ISelectionChangedListener() {
142 public void selectionChanged(SelectionChangedEvent event
) {
143 if (!(event
.getSelection().isEmpty()) && event
.getSelection() instanceof IStructuredSelection
) {
144 fDeleteAction
.setEnabled(true);
145 fExportAction
.setEnabled(true);
147 fDeleteAction
.setEnabled(false);
148 fExportAction
.setEnabled(false);
155 public void setFocus() {
160 public String
toString() {
161 return "[FilterView]"; //$NON-NLS-1$
165 * Builds the menu toolbar
167 private void contributeToActionBars() {
168 IActionBars bars
= getViewSite().getActionBars();
169 // fillLocalPullDown(bars.getMenuManager());
170 fillLocalToolBar(bars
.getToolBarManager());
174 * Build the popup menu
177 * The manager to build
179 private void fillLocalToolBar(IToolBarManager manager
) {
181 fSaveAction
= new SaveAction();
182 fSaveAction
.setImageDescriptor(ImageDescriptor
.createFromImage(SAVE_IMAGE
));
183 fSaveAction
.setToolTipText(Messages
.FilterView_SaveActionToolTipText
);
185 fAddAction
= new AddAction();
186 fAddAction
.setImageDescriptor(ImageDescriptor
.createFromImage(ADD_IMAGE
));
187 fAddAction
.setToolTipText(Messages
.FilterView_AddActionToolTipText
);
189 fDeleteAction
= new DeleteAction();
190 fDeleteAction
.setImageDescriptor(ImageDescriptor
.createFromImage(DELETE_IMAGE
));
191 fDeleteAction
.setToolTipText(Messages
.FilterView_DeleteActionToolTipText
);
192 fDeleteAction
.setEnabled(false);
194 fExportAction
= new ExportAction();
195 fExportAction
.setImageDescriptor(ImageDescriptor
.createFromImage(EXPORT_IMAGE
));
196 fExportAction
.setToolTipText(Messages
.FilterView_ExportActionToolTipText
);
198 fImportAction
= new ImportAction();
199 fImportAction
.setImageDescriptor(ImageDescriptor
.createFromImage(IMPORT_IMAGE
));
200 fImportAction
.setToolTipText(Messages
.FilterView_ImportActionToolTipText
);
202 manager
.add(fSaveAction
);
203 manager
.add(new Separator());
204 manager
.add(fAddAction
);
205 manager
.add(fDeleteAction
);
206 manager
.add(new Separator());
207 manager
.add(fExportAction
);
208 manager
.add(fImportAction
);
211 private class SaveAction
extends Action
{
214 FilterManager
.setSavedFilters(fRoot
.getChildren());
218 private class AddAction
extends Action
{
222 TmfFilterNode newNode
= new TmfFilterNode(fRoot
, ""); //$NON-NLS-1$
224 setSelection(newNode
);
228 private class DeleteAction
extends Action
{
231 ITmfFilterTreeNode node
= fViewer
.getSelection();
239 private class ExportAction
extends Action
{
243 FileDialog dlg
= new FileDialog(new Shell(), SWT
.SAVE
);
244 dlg
.setFilterNames(new String
[] { Messages
.FilterView_FileDialogFilterName
+ " (*.filter.xml)" }); //$NON-NLS-1$
245 dlg
.setFilterExtensions(new String
[] { "*.filter.xml" }); //$NON-NLS-1$
247 String fn
= dlg
.open();
249 TmfFilterXMLWriter writerXML
= new TmfFilterXMLWriter(fRoot
);
250 writerXML
.saveTree(fn
);
253 } catch (ParserConfigurationException e
) {
254 Activator
.getDefault().logError("Error parsing filter xml file", e
); //$NON-NLS-1$
259 private class ImportAction
extends Action
{
262 if (fViewer
!= null) {
263 ITmfFilterTreeNode root
= null;
265 FileDialog dlg
= new FileDialog(new Shell(), SWT
.OPEN
);
266 dlg
.setFilterNames(new String
[] { Messages
.FilterView_FileDialogFilterName
+ " (*.filter.xml)" }); //$NON-NLS-1$
267 dlg
.setFilterExtensions(new String
[] { "*.filter.xml" }); //$NON-NLS-1$
269 TmfFilterXMLParser parserXML
= null;
270 String fn
= dlg
.open();
272 parserXML
= new TmfFilterXMLParser(fn
);
273 root
= parserXML
.getTree();
276 } catch (SAXException e
) {
277 Activator
.getDefault().logError("Error importing filter xml file", e
); //$NON-NLS-1$
278 } catch (IOException e
) {
279 Activator
.getDefault().logError("Error importing filter xml file", e
); //$NON-NLS-1$
283 for (ITmfFilterTreeNode node
: root
.getChildren()) {
284 if (node
instanceof TmfFilterNode
) {
285 fRoot
.addChild(node
);
287 fViewer
.setSelection(node
);