1 /*******************************************************************************
2 * Copyright (c) 2010 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
.tmf
.core
.filter
.model
.ITmfFilterTreeNode
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.filter
.model
.TmfFilterNode
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.filter
.model
.TmfFilterRootNode
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.filter
.xml
.TmfFilterXMLParser
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.filter
.xml
.TmfFilterXMLWriter
;
36 import org
.eclipse
.linuxtools
.tmf
.ui
.TmfUiPlugin
;
37 import org
.eclipse
.linuxtools
.tmf
.ui
.internal
.Messages
;
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 * <b><u>FilterView</u></b>
50 * View that contain UI to the TMF filter.
52 public class FilterView
extends TmfView
{
54 public static final String ID
= "org.eclipse.linuxtools.tmf.ui.views.filter"; //$NON-NLS-1$
56 private static final Image SAVE_IMAGE
= TmfUiPlugin
.getDefault().getImageFromPath("/icons/elcl16/save_button.gif"); //$NON-NLS-1$
57 private static final Image ADD_IMAGE
= TmfUiPlugin
.getDefault().getImageFromPath("/icons/elcl16/add_button.gif"); //$NON-NLS-1$
58 private static final Image DELETE_IMAGE
= TmfUiPlugin
.getDefault().getImageFromPath("/icons/elcl16/delete_button.gif"); //$NON-NLS-1$
59 private static final Image IMPORT_IMAGE
= TmfUiPlugin
.getDefault().getImageFromPath("/icons/elcl16/import_button.gif"); //$NON-NLS-1$
60 private static final Image EXPORT_IMAGE
= TmfUiPlugin
.getDefault().getImageFromPath("/icons/elcl16/export_button.gif"); //$NON-NLS-1$
62 // ------------------------------------------------------------------------
63 // Main data structures
64 // ------------------------------------------------------------------------
66 private FilterViewer fViewer
;
67 private ITmfFilterTreeNode fRoot
;
69 private IWorkspace fWorkspace
;
71 private SaveAction fSaveAction
;
72 private AddAction fAddAction
;
73 private DeleteAction fDeleteAction
;
74 private ExportAction fExportAction
;
75 private ImportAction fImportAction
;
78 * Getter for the Filter Tree Root
80 * @return The root of builded tree
82 public ITmfFilterTreeNode
getFilterRoot() {
87 // ------------------------------------------------------------------------
89 // ------------------------------------------------------------------------
95 super("Filter"); //$NON-NLS-1$
97 fWorkspace
= ResourcesPlugin
.getWorkspace();
99 fWorkspace
.getRoot().refreshLocal(IResource
.DEPTH_INFINITE
, null);
100 } catch (CoreException e
) {
104 fRoot
= new TmfFilterRootNode();
105 for (ITmfFilterTreeNode node
: FilterManager
.getSavedFilters()) {
106 fRoot
.addChild(node
);
112 * Refresh the tree widget
114 public void refresh() {
119 * Setter for selection
121 * @param node The node to select
123 public void setSelection(ITmfFilterTreeNode node
) {
124 fViewer
.setSelection(node
, true);
129 * @see org.eclipse.linuxtools.tmf.ui.views.TmfView#createPartControl(org.eclipse.swt.widgets.Composite)
132 public void createPartControl(Composite parent
) {
134 fViewer
= new FilterViewer(parent
, SWT
.NONE
);
135 fViewer
.setInput(fRoot
);
137 contributeToActionBars();
139 fViewer
.addSelectionChangedListener(new ISelectionChangedListener() {
141 public void selectionChanged(SelectionChangedEvent event
) {
142 if (!(event
.getSelection().isEmpty()) && event
.getSelection() instanceof IStructuredSelection
) {
143 fDeleteAction
.setEnabled(true);
144 fExportAction
.setEnabled(true);
146 fDeleteAction
.setEnabled(false);
147 fExportAction
.setEnabled(false);
154 // ------------------------------------------------------------------------
156 // ------------------------------------------------------------------------
160 * @see java.lang.Object#toString()
163 public String
toString() {
164 return "[FilterView]"; //$NON-NLS-1$
169 * Builds the menu toolbar
171 private void contributeToActionBars() {
172 IActionBars bars
= getViewSite().getActionBars();
173 //fillLocalPullDown(bars.getMenuManager());
174 fillLocalToolBar(bars
.getToolBarManager());
179 * Build the popup menu
181 * @param manager The manager to build
183 private void fillLocalToolBar(IToolBarManager manager
) {
185 fSaveAction
= new SaveAction();
186 fSaveAction
.setImageDescriptor(ImageDescriptor
.createFromImage(SAVE_IMAGE
));
187 fSaveAction
.setToolTipText(Messages
.FilterView_SaveActionToolTipText
);
189 fAddAction
= new AddAction();
190 fAddAction
.setImageDescriptor(ImageDescriptor
.createFromImage(ADD_IMAGE
));
191 fAddAction
.setToolTipText(Messages
.FilterView_AddActionToolTipText
);
193 fDeleteAction
= new DeleteAction();
194 fDeleteAction
.setImageDescriptor(ImageDescriptor
.createFromImage(DELETE_IMAGE
));
195 fDeleteAction
.setToolTipText(Messages
.FilterView_DeleteActionToolTipText
);
196 fDeleteAction
.setEnabled(false);
198 fExportAction
= new ExportAction();
199 fExportAction
.setImageDescriptor(ImageDescriptor
.createFromImage(EXPORT_IMAGE
));
200 fExportAction
.setToolTipText(Messages
.FilterView_ExportActionToolTipText
);
202 fImportAction
= new ImportAction();
203 fImportAction
.setImageDescriptor(ImageDescriptor
.createFromImage(IMPORT_IMAGE
));
204 fImportAction
.setToolTipText(Messages
.FilterView_ImportActionToolTipText
);
206 manager
.add(fSaveAction
);
207 manager
.add(new Separator());
208 manager
.add(fAddAction
);
209 manager
.add(fDeleteAction
);
210 manager
.add(new Separator());
211 manager
.add(fExportAction
);
212 manager
.add(fImportAction
);
215 private class SaveAction
extends Action
{
218 FilterManager
.setSavedFilters(fRoot
.getChildren());
222 private class AddAction
extends Action
{
226 TmfFilterNode newNode
= new TmfFilterNode(fRoot
, ""); //$NON-NLS-1$
228 setSelection(newNode
);
232 private class DeleteAction
extends Action
{
235 ITmfFilterTreeNode node
= fViewer
.getSelection();
243 private class ExportAction
extends Action
{
247 FileDialog dlg
= new FileDialog(new Shell(), SWT
.SAVE
);
248 dlg
.setFilterNames(new String
[] {Messages
.FilterView_FileDialogFilterName
+ " (*.filter.xml)"}); //$NON-NLS-1$
249 dlg
.setFilterExtensions(new String
[] {"*.filter.xml"}); //$NON-NLS-1$
251 String fn
= dlg
.open();
253 TmfFilterXMLWriter writerXML
= new TmfFilterXMLWriter(fRoot
);
254 writerXML
.saveTree(fn
);
257 } catch (ParserConfigurationException e
) {
259 } catch (IOException e
) {
265 private class ImportAction
extends Action
{
268 if (fViewer
!= null) {
269 ITmfFilterTreeNode root
= null;
271 FileDialog dlg
= new FileDialog(new Shell(), SWT
.OPEN
);
272 dlg
.setFilterNames(new String
[] {Messages
.FilterView_FileDialogFilterName
+ " (*.filter.xml)"}); //$NON-NLS-1$
273 dlg
.setFilterExtensions(new String
[] {"*.filter.xml"}); //$NON-NLS-1$
275 TmfFilterXMLParser parserXML
= null;
276 String fn
= dlg
.open();
278 parserXML
= new TmfFilterXMLParser(fn
);
279 root
= parserXML
.getTree();
282 } catch (SAXException e
) {
284 } catch (IOException e
) {
289 for (ITmfFilterTreeNode node
: root
.getChildren()) {
290 if (node
instanceof TmfFilterNode
) {
291 fRoot
.addChild(node
);
293 fViewer
.setSelection(node
);