3bd9d8b9fc9bee2b35400be7530ec476ff62fb51
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / filter / FilterView.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2013 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.linuxtools.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.linuxtools.internal.tmf.ui.Activator;
35 import org.eclipse.linuxtools.internal.tmf.ui.Messages;
36 import org.eclipse.linuxtools.tmf.core.filter.model.ITmfFilterTreeNode;
37 import org.eclipse.linuxtools.tmf.core.filter.model.TmfFilterNode;
38 import org.eclipse.linuxtools.tmf.core.filter.model.TmfFilterRootNode;
39 import org.eclipse.linuxtools.tmf.core.filter.xml.TmfFilterXMLParser;
40 import org.eclipse.linuxtools.tmf.core.filter.xml.TmfFilterXMLWriter;
41 import org.eclipse.linuxtools.tmf.ui.views.TmfView;
42 import org.eclipse.swt.SWT;
43 import org.eclipse.swt.graphics.Image;
44 import org.eclipse.swt.widgets.Composite;
45 import org.eclipse.swt.widgets.FileDialog;
46 import org.eclipse.swt.widgets.Shell;
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. This does not modify the XML, which must
114 * be done manually. If the filter is already in the FilterView, this is a
115 * no-op.
116 *
117 * @param filter
118 * The filter to add.
119 * @since 3.1
120 */
121 public void addFilter(ITmfFilterTreeNode filter) {
122 ITmfFilterTreeNode root = fViewer.getInput();
123 for (ITmfFilterTreeNode node : root.getChildren()) {
124 if (node.equals(filter)) {
125 return;
126 }
127 }
128 root.addChild(filter);
129 fViewer.setInput(root);
130 }
131
132 /**
133 * Refresh the tree widget
134 */
135 public void refresh() {
136 fViewer.refresh();
137 }
138
139 /**
140 * Setter for selection
141 *
142 * @param node
143 * The node to select
144 */
145 public void setSelection(ITmfFilterTreeNode node) {
146 fViewer.setSelection(node, true);
147 }
148
149 // ------------------------------------------------------------------------
150 // ViewPart
151 // ------------------------------------------------------------------------
152
153 @Override
154 public void createPartControl(Composite parent) {
155
156 fViewer = new FilterViewer(parent, SWT.NONE);
157 fViewer.setInput(fRoot);
158
159 contributeToActionBars();
160
161 fViewer.addSelectionChangedListener(new ISelectionChangedListener() {
162 @Override
163 public void selectionChanged(SelectionChangedEvent event) {
164 if (!(event.getSelection().isEmpty()) && event.getSelection() instanceof IStructuredSelection) {
165 fExportAction.setEnabled(true);
166 } else {
167 fExportAction.setEnabled(false);
168 }
169 }
170 });
171 this.getSite().setSelectionProvider(fViewer.getTreeViewer());
172
173 MenuManager menuManager = fViewer.getMenuManager();
174 this.getSite().registerContextMenu(menuManager, fViewer.getTreeViewer());
175 }
176
177 /**
178 * @return the ITmfFilterTreeNode currently selected
179 */
180 ITmfFilterTreeNode getSelection() {
181 return fViewer.getSelection();
182 }
183
184 @Override
185 public void setFocus() {
186 fViewer.setFocus();
187 }
188
189 @Override
190 public String toString() {
191 return "[FilterView]"; //$NON-NLS-1$
192 }
193
194 /**
195 * Builds the menu toolbar
196 */
197 private void contributeToActionBars() {
198 IActionBars bars = getViewSite().getActionBars();
199 // fillLocalPullDown(bars.getMenuManager());
200 fillLocalToolBar(bars.getToolBarManager());
201 }
202
203 /**
204 * Build the popup menu
205 *
206 * @param manager
207 * The manager to build
208 */
209 private void fillLocalToolBar(IToolBarManager manager) {
210
211 fSaveAction = new SaveAction();
212 fSaveAction.setImageDescriptor(ImageDescriptor.createFromImage(SAVE_IMAGE));
213 fSaveAction.setToolTipText(Messages.FilterView_SaveActionToolTipText);
214
215 fAddAction = new AddAction();
216 fAddAction.setImageDescriptor(ImageDescriptor.createFromImage(ADD_IMAGE));
217 fAddAction.setToolTipText(Messages.FilterView_AddActionToolTipText);
218
219 fExportAction = new ExportAction();
220 fExportAction.setImageDescriptor(ImageDescriptor.createFromImage(EXPORT_IMAGE));
221 fExportAction.setToolTipText(Messages.FilterView_ExportActionToolTipText);
222
223 fImportAction = new ImportAction();
224 fImportAction.setImageDescriptor(ImageDescriptor.createFromImage(IMPORT_IMAGE));
225 fImportAction.setToolTipText(Messages.FilterView_ImportActionToolTipText);
226
227 manager.add(fSaveAction);
228 manager.add(new Separator("add_delete")); //$NON-NLS-1$
229 manager.add(fAddAction);
230 manager.add(new Separator("edit")); //$NON-NLS-1$
231 manager.add(new Separator());
232 manager.add(fExportAction);
233 manager.add(fImportAction);
234 }
235
236 private class SaveAction extends Action {
237 @Override
238 public void run() {
239 FilterManager.setSavedFilters(fRoot.getChildren());
240 }
241 }
242
243 private class AddAction extends Action {
244 @Override
245 public void run() {
246
247 TmfFilterNode newNode = new TmfFilterNode(fRoot, ""); //$NON-NLS-1$
248 refresh();
249 setSelection(newNode);
250 }
251 }
252
253 private class ExportAction extends Action {
254 @Override
255 public void run() {
256 try {
257 FileDialog dlg = new FileDialog(new Shell(), SWT.SAVE);
258 dlg.setFilterNames(new String[] { Messages.FilterView_FileDialogFilterName + " (*.xml)" }); //$NON-NLS-1$
259 dlg.setFilterExtensions(new String[] { "*.xml" }); //$NON-NLS-1$
260
261 String fn = dlg.open();
262 if (fn != null) {
263 TmfFilterXMLWriter writerXML = new TmfFilterXMLWriter(fRoot);
264 writerXML.saveTree(fn);
265 }
266
267 } catch (ParserConfigurationException e) {
268 Activator.getDefault().logError("Error parsing filter xml file", e); //$NON-NLS-1$
269 }
270 }
271 }
272
273 private class ImportAction extends Action {
274 @Override
275 public void run() {
276 if (fViewer != null) {
277 ITmfFilterTreeNode root = null;
278 try {
279 FileDialog dlg = new FileDialog(new Shell(), SWT.OPEN);
280 dlg.setFilterNames(new String[] { Messages.FilterView_FileDialogFilterName + " (*.xml)" }); //$NON-NLS-1$
281 dlg.setFilterExtensions(new String[] { "*.xml" }); //$NON-NLS-1$
282
283 TmfFilterXMLParser parserXML = null;
284 String fn = dlg.open();
285 if (fn != null) {
286 parserXML = new TmfFilterXMLParser(fn);
287 root = parserXML.getTree();
288 }
289
290 } catch (SAXException e) {
291 Activator.getDefault().logError("Error importing filter xml file", e); //$NON-NLS-1$
292 } catch (IOException e) {
293 Activator.getDefault().logError("Error importing filter xml file", e); //$NON-NLS-1$
294 }
295
296 if (root != null) {
297 for (ITmfFilterTreeNode node : root.getChildren()) {
298 if (node instanceof TmfFilterNode) {
299 fRoot.addChild(node);
300 refresh();
301 fViewer.setSelection(node);
302 }
303 }
304 }
305 }
306 }
307 }
308
309 }
This page took 0.053893 seconds and 4 git commands to generate.