Merge branch 'master' into TmfTrace-new
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / filter / FilterView.java
1 /*******************************************************************************
2 * Copyright (c) 2010 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 * based on Francois Chouinard ProjectView code.
12 */
13
14 package org.eclipse.linuxtools.tmf.ui.views.filter;
15
16 import java.io.IOException;
17
18 import javax.xml.parsers.ParserConfigurationException;
19
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;
46
47 /**
48 * <b><u>FilterView</u></b>
49 * <p>
50 * View that contain UI to the TMF filter.
51 */
52 public class FilterView extends TmfView {
53
54 public static final String ID = "org.eclipse.linuxtools.tmf.ui.views.filter"; //$NON-NLS-1$
55
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$
61
62 // ------------------------------------------------------------------------
63 // Main data structures
64 // ------------------------------------------------------------------------
65
66 private FilterViewer fViewer;
67 private ITmfFilterTreeNode fRoot;
68
69 private IWorkspace fWorkspace;
70
71 private SaveAction fSaveAction;
72 private AddAction fAddAction;
73 private DeleteAction fDeleteAction;
74 private ExportAction fExportAction;
75 private ImportAction fImportAction;
76
77 /**
78 * Getter for the Filter Tree Root
79 *
80 * @return The root of builded tree
81 */
82 public ITmfFilterTreeNode getFilterRoot() {
83 return fRoot;
84 }
85
86
87 // ------------------------------------------------------------------------
88 // Constructor
89 // ------------------------------------------------------------------------
90
91 /**
92 * Default Constructor
93 */
94 public FilterView() {
95 super("Filter"); //$NON-NLS-1$
96
97 fWorkspace = ResourcesPlugin.getWorkspace();
98 try {
99 fWorkspace.getRoot().refreshLocal(IResource.DEPTH_INFINITE, null);
100 } catch (CoreException e) {
101 TmfUiPlugin.getDefault().logError("Error refreshing workspace", e); //$NON-NLS-1$
102 }
103
104 fRoot = new TmfFilterRootNode();
105 for (ITmfFilterTreeNode node : FilterManager.getSavedFilters()) {
106 fRoot.addChild(node);
107 }
108 }
109
110
111 /**
112 * Refresh the tree widget
113 */
114 public void refresh() {
115 fViewer.refresh();
116 }
117
118 /**
119 * Setter for selection
120 *
121 * @param node The node to select
122 */
123 public void setSelection(ITmfFilterTreeNode node) {
124 fViewer.setSelection(node, true);
125 }
126
127 // ------------------------------------------------------------------------
128 // ViewPart
129 // ------------------------------------------------------------------------
130
131 /* (non-Javadoc)
132 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(Composite)
133 */
134 @Override
135 public void createPartControl(Composite parent) {
136
137 fViewer = new FilterViewer(parent, SWT.NONE);
138 fViewer.setInput(fRoot);
139
140 contributeToActionBars();
141
142 fViewer.addSelectionChangedListener(new ISelectionChangedListener() {
143 @Override
144 public void selectionChanged(SelectionChangedEvent event) {
145 if (!(event.getSelection().isEmpty()) && event.getSelection() instanceof IStructuredSelection) {
146 fDeleteAction.setEnabled(true);
147 fExportAction.setEnabled(true);
148 } else {
149 fDeleteAction.setEnabled(false);
150 fExportAction.setEnabled(false);
151 }
152 }
153 });
154 }
155
156 /* (non-Javadoc)
157 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
158 */
159 @Override
160 public void setFocus() {
161 fViewer.setFocus();
162 }
163
164 /*
165 * (non-Javadoc)
166 * @see java.lang.Object#toString()
167 */
168 @Override
169 public String toString() {
170 return "[FilterView]"; //$NON-NLS-1$
171 }
172
173
174 /**
175 * Builds the menu toolbar
176 */
177 private void contributeToActionBars() {
178 IActionBars bars = getViewSite().getActionBars();
179 //fillLocalPullDown(bars.getMenuManager());
180 fillLocalToolBar(bars.getToolBarManager());
181 }
182
183
184 /**
185 * Build the popup menu
186 *
187 * @param manager The manager to build
188 */
189 private void fillLocalToolBar(IToolBarManager manager) {
190
191 fSaveAction = new SaveAction();
192 fSaveAction.setImageDescriptor(ImageDescriptor.createFromImage(SAVE_IMAGE));
193 fSaveAction.setToolTipText(Messages.FilterView_SaveActionToolTipText);
194
195 fAddAction = new AddAction();
196 fAddAction.setImageDescriptor(ImageDescriptor.createFromImage(ADD_IMAGE));
197 fAddAction.setToolTipText(Messages.FilterView_AddActionToolTipText);
198
199 fDeleteAction = new DeleteAction();
200 fDeleteAction.setImageDescriptor(ImageDescriptor.createFromImage(DELETE_IMAGE));
201 fDeleteAction.setToolTipText(Messages.FilterView_DeleteActionToolTipText);
202 fDeleteAction.setEnabled(false);
203
204 fExportAction = new ExportAction();
205 fExportAction.setImageDescriptor(ImageDescriptor.createFromImage(EXPORT_IMAGE));
206 fExportAction.setToolTipText(Messages.FilterView_ExportActionToolTipText);
207
208 fImportAction = new ImportAction();
209 fImportAction.setImageDescriptor(ImageDescriptor.createFromImage(IMPORT_IMAGE));
210 fImportAction.setToolTipText(Messages.FilterView_ImportActionToolTipText);
211
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);
219 }
220
221 private class SaveAction extends Action {
222 @Override
223 public void run() {
224 FilterManager.setSavedFilters(fRoot.getChildren());
225 }
226 }
227
228 private class AddAction extends Action {
229 @Override
230 public void run() {
231
232 TmfFilterNode newNode = new TmfFilterNode(fRoot, ""); //$NON-NLS-1$
233 refresh();
234 setSelection(newNode);
235 }
236 }
237
238 private class DeleteAction extends Action {
239 @Override
240 public void run() {
241 ITmfFilterTreeNode node = fViewer.getSelection();
242 if (node != null) {
243 node.remove();
244 }
245 refresh();
246 }
247 }
248
249 private class ExportAction extends Action {
250 @Override
251 public void run() {
252 try {
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$
256
257 String fn = dlg.open();
258 if (fn != null) {
259 TmfFilterXMLWriter writerXML = new TmfFilterXMLWriter(fRoot);
260 writerXML.saveTree(fn);
261 }
262
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$
267 }
268 }
269 }
270
271 private class ImportAction extends Action {
272 @Override
273 public void run() {
274 if (fViewer != null) {
275 ITmfFilterTreeNode root = null;
276 try {
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$
280
281 TmfFilterXMLParser parserXML = null;
282 String fn = dlg.open();
283 if (fn != null) {
284 parserXML = new TmfFilterXMLParser(fn);
285 root = parserXML.getTree();
286 }
287
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$
292 }
293
294 if (root != null) {
295 for (ITmfFilterTreeNode node : root.getChildren()) {
296 if (node instanceof TmfFilterNode) {
297 fRoot.addChild(node);
298 refresh();
299 fViewer.setSelection(node);
300 }
301 }
302 }
303 }
304 }
305 }
306
307 }
This page took 0.037917 seconds and 6 git commands to generate.