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