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