Re-structure LTTng sub-project as per the Linux Tools guidelines
[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.tmf.core.filter.model.ITmfFilterTreeNode;
32 import org.eclipse.linuxtools.tmf.core.filter.model.TmfFilterNode;
33 import org.eclipse.linuxtools.tmf.core.filter.model.TmfFilterRootNode;
34 import org.eclipse.linuxtools.tmf.core.filter.xml.TmfFilterXMLParser;
35 import org.eclipse.linuxtools.tmf.core.filter.xml.TmfFilterXMLWriter;
36 import org.eclipse.linuxtools.tmf.ui.TmfUiPlugin;
37 import org.eclipse.linuxtools.tmf.ui.internal.Messages;
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 e.printStackTrace();
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 * (non-Javadoc)
129 * @see org.eclipse.linuxtools.tmf.ui.views.TmfView#createPartControl(org.eclipse.swt.widgets.Composite)
130 */
131 @Override
132 public void createPartControl(Composite parent) {
133
134 fViewer = new FilterViewer(parent, SWT.NONE);
135 fViewer.setInput(fRoot);
136
137 contributeToActionBars();
138
139 fViewer.addSelectionChangedListener(new ISelectionChangedListener() {
140 @Override
141 public void selectionChanged(SelectionChangedEvent event) {
142 if (!(event.getSelection().isEmpty()) && event.getSelection() instanceof IStructuredSelection) {
143 fDeleteAction.setEnabled(true);
144 fExportAction.setEnabled(true);
145 } else {
146 fDeleteAction.setEnabled(false);
147 fExportAction.setEnabled(false);
148 }
149 }
150 });
151 }
152
153
154 // ------------------------------------------------------------------------
155 // ViewPart
156 // ------------------------------------------------------------------------
157
158 /*
159 * (non-Javadoc)
160 * @see java.lang.Object#toString()
161 */
162 @Override
163 public String toString() {
164 return "[FilterView]"; //$NON-NLS-1$
165 }
166
167
168 /**
169 * Builds the menu toolbar
170 */
171 private void contributeToActionBars() {
172 IActionBars bars = getViewSite().getActionBars();
173 //fillLocalPullDown(bars.getMenuManager());
174 fillLocalToolBar(bars.getToolBarManager());
175 }
176
177
178 /**
179 * Build the popup menu
180 *
181 * @param manager The manager to build
182 */
183 private void fillLocalToolBar(IToolBarManager manager) {
184
185 fSaveAction = new SaveAction();
186 fSaveAction.setImageDescriptor(ImageDescriptor.createFromImage(SAVE_IMAGE));
187 fSaveAction.setToolTipText(Messages.FilterView_SaveActionToolTipText);
188
189 fAddAction = new AddAction();
190 fAddAction.setImageDescriptor(ImageDescriptor.createFromImage(ADD_IMAGE));
191 fAddAction.setToolTipText(Messages.FilterView_AddActionToolTipText);
192
193 fDeleteAction = new DeleteAction();
194 fDeleteAction.setImageDescriptor(ImageDescriptor.createFromImage(DELETE_IMAGE));
195 fDeleteAction.setToolTipText(Messages.FilterView_DeleteActionToolTipText);
196 fDeleteAction.setEnabled(false);
197
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);
207 manager.add(new Separator());
208 manager.add(fAddAction);
209 manager.add(fDeleteAction);
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
232 private class DeleteAction extends Action {
233 @Override
234 public void run() {
235 ITmfFilterTreeNode node = fViewer.getSelection();
236 if (node != null) {
237 node.remove();
238 }
239 refresh();
240 }
241 }
242
243 private class ExportAction extends Action {
244 @Override
245 public void run() {
246 try {
247 FileDialog dlg = new FileDialog(new Shell(), SWT.SAVE);
248 dlg.setFilterNames(new String[] {Messages.FilterView_FileDialogFilterName + " (*.filter.xml)"}); //$NON-NLS-1$
249 dlg.setFilterExtensions(new String[] {"*.filter.xml"}); //$NON-NLS-1$
250
251 String fn = dlg.open();
252 if (fn != null) {
253 TmfFilterXMLWriter writerXML = new TmfFilterXMLWriter(fRoot);
254 writerXML.saveTree(fn);
255 }
256
257 } catch (ParserConfigurationException e) {
258 e.printStackTrace();
259 } catch (IOException e) {
260 e.printStackTrace();
261 }
262 }
263 }
264
265 private class ImportAction extends Action {
266 @Override
267 public void run() {
268 if (fViewer != null) {
269 ITmfFilterTreeNode root = null;
270 try {
271 FileDialog dlg = new FileDialog(new Shell(), SWT.OPEN);
272 dlg.setFilterNames(new String[] {Messages.FilterView_FileDialogFilterName + " (*.filter.xml)"}); //$NON-NLS-1$
273 dlg.setFilterExtensions(new String[] {"*.filter.xml"}); //$NON-NLS-1$
274
275 TmfFilterXMLParser parserXML = null;
276 String fn = dlg.open();
277 if (fn != null) {
278 parserXML = new TmfFilterXMLParser(fn);
279 root = parserXML.getTree();
280 }
281
282 } catch (SAXException e) {
283 e.printStackTrace();
284 } catch (IOException e) {
285 e.printStackTrace();
286 }
287
288 if (root != null) {
289 for (ITmfFilterTreeNode node : root.getChildren()) {
290 if (node instanceof TmfFilterNode) {
291 fRoot.addChild(node);
292 refresh();
293 fViewer.setSelection(node);
294 }
295 }
296 }
297 }
298 }
299 }
300
301 }
This page took 0.037121 seconds and 5 git commands to generate.