1 /*******************************************************************************
2 * Copyright (c) 2010, 2012 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() {
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
);
114 * Refresh the tree widget
116 public void refresh() {
121 * Setter for selection
123 * @param node The node to select
125 public void setSelection(ITmfFilterTreeNode node
) {
126 fViewer
.setSelection(node
, true);
129 // ------------------------------------------------------------------------
131 // ------------------------------------------------------------------------
134 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(Composite)
137 public void createPartControl(Composite parent
) {
139 fViewer
= new FilterViewer(parent
, SWT
.NONE
);
140 fViewer
.setInput(fRoot
);
142 contributeToActionBars();
144 fViewer
.addSelectionChangedListener(new ISelectionChangedListener() {
146 public void selectionChanged(SelectionChangedEvent event
) {
147 if (!(event
.getSelection().isEmpty()) && event
.getSelection() instanceof IStructuredSelection
) {
148 fDeleteAction
.setEnabled(true);
149 fExportAction
.setEnabled(true);
151 fDeleteAction
.setEnabled(false);
152 fExportAction
.setEnabled(false);
159 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
162 public void setFocus() {
168 * @see java.lang.Object#toString()
171 public String
toString() {
172 return "[FilterView]"; //$NON-NLS-1$
177 * Builds the menu toolbar
179 private void contributeToActionBars() {
180 IActionBars bars
= getViewSite().getActionBars();
181 //fillLocalPullDown(bars.getMenuManager());
182 fillLocalToolBar(bars
.getToolBarManager());
187 * Build the popup menu
189 * @param manager The manager to build
191 private void fillLocalToolBar(IToolBarManager manager
) {
193 fSaveAction
= new SaveAction();
194 fSaveAction
.setImageDescriptor(ImageDescriptor
.createFromImage(SAVE_IMAGE
));
195 fSaveAction
.setToolTipText(Messages
.FilterView_SaveActionToolTipText
);
197 fAddAction
= new AddAction();
198 fAddAction
.setImageDescriptor(ImageDescriptor
.createFromImage(ADD_IMAGE
));
199 fAddAction
.setToolTipText(Messages
.FilterView_AddActionToolTipText
);
201 fDeleteAction
= new DeleteAction();
202 fDeleteAction
.setImageDescriptor(ImageDescriptor
.createFromImage(DELETE_IMAGE
));
203 fDeleteAction
.setToolTipText(Messages
.FilterView_DeleteActionToolTipText
);
204 fDeleteAction
.setEnabled(false);
206 fExportAction
= new ExportAction();
207 fExportAction
.setImageDescriptor(ImageDescriptor
.createFromImage(EXPORT_IMAGE
));
208 fExportAction
.setToolTipText(Messages
.FilterView_ExportActionToolTipText
);
210 fImportAction
= new ImportAction();
211 fImportAction
.setImageDescriptor(ImageDescriptor
.createFromImage(IMPORT_IMAGE
));
212 fImportAction
.setToolTipText(Messages
.FilterView_ImportActionToolTipText
);
214 manager
.add(fSaveAction
);
215 manager
.add(new Separator());
216 manager
.add(fAddAction
);
217 manager
.add(fDeleteAction
);
218 manager
.add(new Separator());
219 manager
.add(fExportAction
);
220 manager
.add(fImportAction
);
223 private class SaveAction
extends Action
{
226 FilterManager
.setSavedFilters(fRoot
.getChildren());
230 private class AddAction
extends Action
{
234 TmfFilterNode newNode
= new TmfFilterNode(fRoot
, ""); //$NON-NLS-1$
236 setSelection(newNode
);
240 private class DeleteAction
extends Action
{
243 ITmfFilterTreeNode node
= fViewer
.getSelection();
251 private class ExportAction
extends Action
{
255 FileDialog dlg
= new FileDialog(new Shell(), SWT
.SAVE
);
256 dlg
.setFilterNames(new String
[] {Messages
.FilterView_FileDialogFilterName
+ " (*.filter.xml)"}); //$NON-NLS-1$
257 dlg
.setFilterExtensions(new String
[] {"*.filter.xml"}); //$NON-NLS-1$
259 String fn
= dlg
.open();
261 TmfFilterXMLWriter writerXML
= new TmfFilterXMLWriter(fRoot
);
262 writerXML
.saveTree(fn
);
265 } catch (ParserConfigurationException e
) {
266 Activator
.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 Activator
.getDefault().logError("Error importing filter xml file", e
); //$NON-NLS-1$
290 } catch (IOException e
) {
291 Activator
.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
);