tmf: Fix the import/export of the filter view
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / filter / FilterView.java
CommitLineData
be222f56 1/*******************************************************************************
11252342 2 * Copyright (c) 2010, 2013 Ericsson
be222f56
PT
3 *
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
8 *
9 * Contributors:
10 * Yuriy Vashchuk - Initial API and implementation
ef906471 11 * Xavier Raynaud - add cut/copy/paste/dnd support
be222f56
PT
12 * based on Francois Chouinard ProjectView code.
13 */
14
15package org.eclipse.linuxtools.tmf.ui.views.filter;
16
17import java.io.IOException;
18
19import javax.xml.parsers.ParserConfigurationException;
20
21import org.eclipse.core.resources.IResource;
22import org.eclipse.core.resources.IWorkspace;
23import org.eclipse.core.resources.ResourcesPlugin;
24import org.eclipse.core.runtime.CoreException;
25import org.eclipse.jface.action.Action;
26import org.eclipse.jface.action.IToolBarManager;
ef906471 27import org.eclipse.jface.action.MenuManager;
be222f56
PT
28import org.eclipse.jface.action.Separator;
29import org.eclipse.jface.resource.ImageDescriptor;
30import org.eclipse.jface.viewers.ISelectionChangedListener;
31import org.eclipse.jface.viewers.IStructuredSelection;
32import org.eclipse.jface.viewers.SelectionChangedEvent;
33import org.eclipse.linuxtools.internal.tmf.ui.Activator;
34import org.eclipse.linuxtools.internal.tmf.ui.Messages;
35import org.eclipse.linuxtools.tmf.core.filter.model.ITmfFilterTreeNode;
36import org.eclipse.linuxtools.tmf.core.filter.model.TmfFilterNode;
37import org.eclipse.linuxtools.tmf.core.filter.model.TmfFilterRootNode;
38import org.eclipse.linuxtools.tmf.core.filter.xml.TmfFilterXMLParser;
39import org.eclipse.linuxtools.tmf.core.filter.xml.TmfFilterXMLWriter;
40import org.eclipse.linuxtools.tmf.ui.views.TmfView;
41import org.eclipse.swt.SWT;
42import org.eclipse.swt.graphics.Image;
43import org.eclipse.swt.widgets.Composite;
44import org.eclipse.swt.widgets.FileDialog;
45import org.eclipse.swt.widgets.Shell;
46import org.eclipse.ui.IActionBars;
47import org.xml.sax.SAXException;
48
49/**
50 * View that contain UI to the TMF filter.
51 *
52 * @version 1.0
53 * @author Yuriy Vashchuk
54 */
55public class FilterView extends TmfView {
56
57 /** ID for the Filter view */
58 public static final String ID = "org.eclipse.linuxtools.tmf.ui.views.filter"; //$NON-NLS-1$
59
60 private static final Image SAVE_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/save_button.gif"); //$NON-NLS-1$
61 private static final Image ADD_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/add_button.gif"); //$NON-NLS-1$
be222f56
PT
62 private static final Image IMPORT_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/import_button.gif"); //$NON-NLS-1$
63 private static final Image EXPORT_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/export_button.gif"); //$NON-NLS-1$
64
65 // ------------------------------------------------------------------------
66 // Main data structures
67 // ------------------------------------------------------------------------
68
69 private FilterViewer fViewer;
70 private final ITmfFilterTreeNode fRoot;
71
72 private final IWorkspace fWorkspace;
73
74 private SaveAction fSaveAction;
75 private AddAction fAddAction;
be222f56
PT
76 private ExportAction fExportAction;
77 private ImportAction fImportAction;
78
79 /**
80 * Getter for the Filter Tree Root
81 *
82 * @return The root of builded tree
83 */
84 public ITmfFilterTreeNode getFilterRoot() {
11252342 85 return fRoot;
be222f56
PT
86 }
87
be222f56
PT
88 // ------------------------------------------------------------------------
89 // Constructor
90 // ------------------------------------------------------------------------
91
92 /**
93 * Default Constructor
94 */
95 public FilterView() {
96 super("Filter"); //$NON-NLS-1$
97
98 fWorkspace = ResourcesPlugin.getWorkspace();
99 try {
100 fWorkspace.getRoot().refreshLocal(IResource.DEPTH_INFINITE, null);
101 } catch (CoreException e) {
102 Activator.getDefault().logError("Error refreshing workspace", e); //$NON-NLS-1$
103 }
104
105 fRoot = new TmfFilterRootNode();
106 for (ITmfFilterTreeNode node : FilterManager.getSavedFilters()) {
11252342 107 fRoot.addChild(node);
be222f56 108 }
11252342
AM
109 }
110
111 /**
112 * Refresh the tree widget
be222f56 113 */
11252342
AM
114 public void refresh() {
115 fViewer.refresh();
116 }
117
118 /**
119 * Setter for selection
120 *
121 * @param node
122 * The node to select
be222f56 123 */
11252342
AM
124 public void setSelection(ITmfFilterTreeNode node) {
125 fViewer.setSelection(node, true);
126 }
127
128 // ------------------------------------------------------------------------
129 // ViewPart
130 // ------------------------------------------------------------------------
131
132 @Override
133 public void createPartControl(Composite parent) {
134
135 fViewer = new FilterViewer(parent, SWT.NONE);
136 fViewer.setInput(fRoot);
137
138 contributeToActionBars();
139
140 fViewer.addSelectionChangedListener(new ISelectionChangedListener() {
141 @Override
142 public void selectionChanged(SelectionChangedEvent event) {
143 if (!(event.getSelection().isEmpty()) && event.getSelection() instanceof IStructuredSelection) {
11252342
AM
144 fExportAction.setEnabled(true);
145 } else {
11252342
AM
146 fExportAction.setEnabled(false);
147 }
148 }
149 });
ef906471
XR
150 this.getSite().setSelectionProvider(fViewer.getTreeViewer());
151
780fc05e 152 MenuManager menuManager = fViewer.getMenuManager();
ef906471
XR
153 this.getSite().registerContextMenu(menuManager, fViewer.getTreeViewer());
154 }
155
156 /**
157 * @return the ITmfFilterTreeNode currently selected
158 */
159 ITmfFilterTreeNode getSelection() {
160 return fViewer.getSelection();
11252342
AM
161 }
162
be222f56
PT
163 @Override
164 public void setFocus() {
165 fViewer.setFocus();
166 }
167
11252342
AM
168 @Override
169 public String toString() {
170 return "[FilterView]"; //$NON-NLS-1$
171 }
be222f56
PT
172
173 /**
174 * Builds the menu toolbar
175 */
11252342
AM
176 private void contributeToActionBars() {
177 IActionBars bars = getViewSite().getActionBars();
178 // fillLocalPullDown(bars.getMenuManager());
179 fillLocalToolBar(bars.getToolBarManager());
180 }
181
182 /**
183 * Build the popup menu
184 *
185 * @param manager
186 * The manager to build
187 */
188 private void fillLocalToolBar(IToolBarManager manager) {
189
190 fSaveAction = new SaveAction();
191 fSaveAction.setImageDescriptor(ImageDescriptor.createFromImage(SAVE_IMAGE));
192 fSaveAction.setToolTipText(Messages.FilterView_SaveActionToolTipText);
193
194 fAddAction = new AddAction();
195 fAddAction.setImageDescriptor(ImageDescriptor.createFromImage(ADD_IMAGE));
196 fAddAction.setToolTipText(Messages.FilterView_AddActionToolTipText);
197
11252342
AM
198 fExportAction = new ExportAction();
199 fExportAction.setImageDescriptor(ImageDescriptor.createFromImage(EXPORT_IMAGE));
200 fExportAction.setToolTipText(Messages.FilterView_ExportActionToolTipText);
201
202 fImportAction = new ImportAction();
203 fImportAction.setImageDescriptor(ImageDescriptor.createFromImage(IMPORT_IMAGE));
204 fImportAction.setToolTipText(Messages.FilterView_ImportActionToolTipText);
205
206 manager.add(fSaveAction);
ef906471 207 manager.add(new Separator("add_delete")); //$NON-NLS-1$
11252342 208 manager.add(fAddAction);
ef906471 209 manager.add(new Separator("edit")); //$NON-NLS-1$
11252342
AM
210 manager.add(new Separator());
211 manager.add(fExportAction);
212 manager.add(fImportAction);
213 }
214
215 private class SaveAction extends Action {
216 @Override
217 public void run() {
218 FilterManager.setSavedFilters(fRoot.getChildren());
219 }
220 }
221
222 private class AddAction extends Action {
223 @Override
224 public void run() {
225
226 TmfFilterNode newNode = new TmfFilterNode(fRoot, ""); //$NON-NLS-1$
227 refresh();
228 setSelection(newNode);
229 }
230 }
231
11252342
AM
232 private class ExportAction extends Action {
233 @Override
234 public void run() {
235 try {
236 FileDialog dlg = new FileDialog(new Shell(), SWT.SAVE);
0d3171c0
VP
237 dlg.setFilterNames(new String[] { Messages.FilterView_FileDialogFilterName + " (*.xml)" }); //$NON-NLS-1$
238 dlg.setFilterExtensions(new String[] { "*.xml" }); //$NON-NLS-1$
11252342
AM
239
240 String fn = dlg.open();
241 if (fn != null) {
242 TmfFilterXMLWriter writerXML = new TmfFilterXMLWriter(fRoot);
243 writerXML.saveTree(fn);
244 }
245
246 } catch (ParserConfigurationException e) {
247 Activator.getDefault().logError("Error parsing filter xml file", e); //$NON-NLS-1$
248 }
249 }
250 }
251
252 private class ImportAction extends Action {
253 @Override
254 public void run() {
255 if (fViewer != null) {
256 ITmfFilterTreeNode root = null;
257 try {
258 FileDialog dlg = new FileDialog(new Shell(), SWT.OPEN);
0d3171c0
VP
259 dlg.setFilterNames(new String[] { Messages.FilterView_FileDialogFilterName + " (*.xml)" }); //$NON-NLS-1$
260 dlg.setFilterExtensions(new String[] { "*.xml" }); //$NON-NLS-1$
11252342
AM
261
262 TmfFilterXMLParser parserXML = null;
263 String fn = dlg.open();
264 if (fn != null) {
265 parserXML = new TmfFilterXMLParser(fn);
266 root = parserXML.getTree();
267 }
268
269 } catch (SAXException e) {
270 Activator.getDefault().logError("Error importing filter xml file", e); //$NON-NLS-1$
271 } catch (IOException e) {
272 Activator.getDefault().logError("Error importing filter xml file", e); //$NON-NLS-1$
273 }
274
275 if (root != null) {
276 for (ITmfFilterTreeNode node : root.getChildren()) {
277 if (node instanceof TmfFilterNode) {
278 fRoot.addChild(node);
279 refresh();
280 fViewer.setSelection(node);
281 }
282 }
283 }
284 }
285 }
286 }
be222f56 287
90e2b925 288}
This page took 0.062969 seconds and 5 git commands to generate.