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
.internal
.tmf
.ui
.Messages
;
32 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.TmfUiPlugin
;
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 * <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
) {
101 TmfUiPlugin
.getDefault().logError("Error refreshing workspace", e
); //$NON-NLS-1$
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);
127 // ------------------------------------------------------------------------
129 // ------------------------------------------------------------------------
132 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(Composite)
135 public void createPartControl(Composite parent
) {
137 fViewer
= new FilterViewer(parent
, SWT
.NONE
);
138 fViewer
.setInput(fRoot
);
140 contributeToActionBars();
142 fViewer
.addSelectionChangedListener(new ISelectionChangedListener() {
144 public void selectionChanged(SelectionChangedEvent event
) {
145 if (!(event
.getSelection().isEmpty()) && event
.getSelection() instanceof IStructuredSelection
) {
146 fDeleteAction
.setEnabled(true);
147 fExportAction
.setEnabled(true);
149 fDeleteAction
.setEnabled(false);
150 fExportAction
.setEnabled(false);
157 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
160 public void setFocus() {
166 * @see java.lang.Object#toString()
169 public String
toString() {
170 return "[FilterView]"; //$NON-NLS-1$
175 * Builds the menu toolbar
177 private void contributeToActionBars() {
178 IActionBars bars
= getViewSite().getActionBars();
179 //fillLocalPullDown(bars.getMenuManager());
180 fillLocalToolBar(bars
.getToolBarManager());
185 * Build the popup menu
187 * @param manager The manager to build
189 private void fillLocalToolBar(IToolBarManager manager
) {
191 fSaveAction
= new SaveAction();
192 fSaveAction
.setImageDescriptor(ImageDescriptor
.createFromImage(SAVE_IMAGE
));
193 fSaveAction
.setToolTipText(Messages
.FilterView_SaveActionToolTipText
);
195 fAddAction
= new AddAction();
196 fAddAction
.setImageDescriptor(ImageDescriptor
.createFromImage(ADD_IMAGE
));
197 fAddAction
.setToolTipText(Messages
.FilterView_AddActionToolTipText
);
199 fDeleteAction
= new DeleteAction();
200 fDeleteAction
.setImageDescriptor(ImageDescriptor
.createFromImage(DELETE_IMAGE
));
201 fDeleteAction
.setToolTipText(Messages
.FilterView_DeleteActionToolTipText
);
202 fDeleteAction
.setEnabled(false);
204 fExportAction
= new ExportAction();
205 fExportAction
.setImageDescriptor(ImageDescriptor
.createFromImage(EXPORT_IMAGE
));
206 fExportAction
.setToolTipText(Messages
.FilterView_ExportActionToolTipText
);
208 fImportAction
= new ImportAction();
209 fImportAction
.setImageDescriptor(ImageDescriptor
.createFromImage(IMPORT_IMAGE
));
210 fImportAction
.setToolTipText(Messages
.FilterView_ImportActionToolTipText
);
212 manager
.add(fSaveAction
);
213 manager
.add(new Separator());
214 manager
.add(fAddAction
);
215 manager
.add(fDeleteAction
);
216 manager
.add(new Separator());
217 manager
.add(fExportAction
);
218 manager
.add(fImportAction
);
221 private class SaveAction
extends Action
{
224 FilterManager
.setSavedFilters(fRoot
.getChildren());
228 private class AddAction
extends Action
{
232 TmfFilterNode newNode
= new TmfFilterNode(fRoot
, ""); //$NON-NLS-1$
234 setSelection(newNode
);
238 private class DeleteAction
extends Action
{
241 ITmfFilterTreeNode node
= fViewer
.getSelection();
249 private class ExportAction
extends Action
{
253 FileDialog dlg
= new FileDialog(new Shell(), SWT
.SAVE
);
254 dlg
.setFilterNames(new String
[] {Messages
.FilterView_FileDialogFilterName
+ " (*.filter.xml)"}); //$NON-NLS-1$
255 dlg
.setFilterExtensions(new String
[] {"*.filter.xml"}); //$NON-NLS-1$
257 String fn
= dlg
.open();
259 TmfFilterXMLWriter writerXML
= new TmfFilterXMLWriter(fRoot
);
260 writerXML
.saveTree(fn
);
263 } catch (ParserConfigurationException e
) {
264 TmfUiPlugin
.getDefault().logError("Error parsing filter xml file", e
); //$NON-NLS-1$
265 } catch (IOException e
) {
266 TmfUiPlugin
.getDefault().logError("Error parsing filter xml file", e
); //$NON-NLS-1$
271 private class ImportAction
extends Action
{
274 if (fViewer
!= null) {
275 ITmfFilterTreeNode root
= null;
277 FileDialog dlg
= new FileDialog(new Shell(), SWT
.OPEN
);
278 dlg
.setFilterNames(new String
[] {Messages
.FilterView_FileDialogFilterName
+ " (*.filter.xml)"}); //$NON-NLS-1$
279 dlg
.setFilterExtensions(new String
[] {"*.filter.xml"}); //$NON-NLS-1$
281 TmfFilterXMLParser parserXML
= null;
282 String fn
= dlg
.open();
284 parserXML
= new TmfFilterXMLParser(fn
);
285 root
= parserXML
.getTree();
288 } catch (SAXException e
) {
289 TmfUiPlugin
.getDefault().logError("Error importing filter xml file", e
); //$NON-NLS-1$
290 } catch (IOException e
) {
291 TmfUiPlugin
.getDefault().logError("Error importing filter xml file", e
); //$NON-NLS-1$
295 for (ITmfFilterTreeNode node
: root
.getChildren()) {
296 if (node
instanceof TmfFilterNode
) {
297 fRoot
.addChild(node
);
299 fViewer
.setSelection(node
);