Commit | Line | Data |
---|---|---|
be222f56 | 1 | /******************************************************************************* |
11252342 | 2 | * Copyright (c) 2010, 2013 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 | |
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.Activator; | |
32 | import org.eclipse.linuxtools.internal.tmf.ui.Messages; | |
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 | * View that contain UI to the TMF filter. | |
49 | * | |
50 | * @version 1.0 | |
51 | * @author Yuriy Vashchuk | |
52 | */ | |
53 | public class FilterView extends TmfView { | |
54 | ||
55 | /** ID for the Filter view */ | |
56 | public static final String ID = "org.eclipse.linuxtools.tmf.ui.views.filter"; //$NON-NLS-1$ | |
57 | ||
58 | private static final Image SAVE_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/save_button.gif"); //$NON-NLS-1$ | |
59 | private static final Image ADD_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/add_button.gif"); //$NON-NLS-1$ | |
60 | private static final Image DELETE_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/delete_button.gif"); //$NON-NLS-1$ | |
61 | private static final Image IMPORT_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/import_button.gif"); //$NON-NLS-1$ | |
62 | private static final Image EXPORT_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/export_button.gif"); //$NON-NLS-1$ | |
63 | ||
64 | // ------------------------------------------------------------------------ | |
65 | // Main data structures | |
66 | // ------------------------------------------------------------------------ | |
67 | ||
68 | private FilterViewer fViewer; | |
69 | private final ITmfFilterTreeNode fRoot; | |
70 | ||
71 | private final IWorkspace fWorkspace; | |
72 | ||
73 | private SaveAction fSaveAction; | |
74 | private AddAction fAddAction; | |
75 | private DeleteAction fDeleteAction; | |
76 | private ExportAction fExportAction; | |
77 | private ImportAction fImportAction; | |
78 | ||
79 | /** | |
80 | * Getter for the Filter Tree Root | |
81 | * | |
82 | * @return The root of builded tree | |
83 | */ | |
84 | public ITmfFilterTreeNode getFilterRoot() { | |
11252342 | 85 | return fRoot; |
be222f56 PT |
86 | } |
87 | ||
be222f56 PT |
88 | // ------------------------------------------------------------------------ |
89 | // Constructor | |
90 | // ------------------------------------------------------------------------ | |
91 | ||
92 | /** | |
93 | * Default Constructor | |
94 | */ | |
95 | public FilterView() { | |
96 | super("Filter"); //$NON-NLS-1$ | |
97 | ||
98 | fWorkspace = ResourcesPlugin.getWorkspace(); | |
99 | try { | |
100 | fWorkspace.getRoot().refreshLocal(IResource.DEPTH_INFINITE, null); | |
101 | } catch (CoreException e) { | |
102 | Activator.getDefault().logError("Error refreshing workspace", e); //$NON-NLS-1$ | |
103 | } | |
104 | ||
105 | fRoot = new TmfFilterRootNode(); | |
106 | for (ITmfFilterTreeNode node : FilterManager.getSavedFilters()) { | |
11252342 | 107 | fRoot.addChild(node); |
be222f56 | 108 | } |
11252342 AM |
109 | } |
110 | ||
111 | /** | |
112 | * Refresh the tree widget | |
be222f56 | 113 | */ |
11252342 AM |
114 | public void refresh() { |
115 | fViewer.refresh(); | |
116 | } | |
117 | ||
118 | /** | |
119 | * Setter for selection | |
120 | * | |
121 | * @param node | |
122 | * The node to select | |
be222f56 | 123 | */ |
11252342 AM |
124 | public void setSelection(ITmfFilterTreeNode node) { |
125 | fViewer.setSelection(node, true); | |
126 | } | |
127 | ||
128 | // ------------------------------------------------------------------------ | |
129 | // ViewPart | |
130 | // ------------------------------------------------------------------------ | |
131 | ||
132 | @Override | |
133 | public void createPartControl(Composite parent) { | |
134 | ||
135 | fViewer = new FilterViewer(parent, SWT.NONE); | |
136 | fViewer.setInput(fRoot); | |
137 | ||
138 | contributeToActionBars(); | |
139 | ||
140 | fViewer.addSelectionChangedListener(new ISelectionChangedListener() { | |
141 | @Override | |
142 | public void selectionChanged(SelectionChangedEvent event) { | |
143 | if (!(event.getSelection().isEmpty()) && event.getSelection() instanceof IStructuredSelection) { | |
144 | fDeleteAction.setEnabled(true); | |
145 | fExportAction.setEnabled(true); | |
146 | } else { | |
147 | fDeleteAction.setEnabled(false); | |
148 | fExportAction.setEnabled(false); | |
149 | } | |
150 | } | |
151 | }); | |
152 | } | |
153 | ||
be222f56 PT |
154 | @Override |
155 | public void setFocus() { | |
156 | fViewer.setFocus(); | |
157 | } | |
158 | ||
11252342 AM |
159 | @Override |
160 | public String toString() { | |
161 | return "[FilterView]"; //$NON-NLS-1$ | |
162 | } | |
be222f56 PT |
163 | |
164 | /** | |
165 | * Builds the menu toolbar | |
166 | */ | |
11252342 AM |
167 | private void contributeToActionBars() { |
168 | IActionBars bars = getViewSite().getActionBars(); | |
169 | // fillLocalPullDown(bars.getMenuManager()); | |
170 | fillLocalToolBar(bars.getToolBarManager()); | |
171 | } | |
172 | ||
173 | /** | |
174 | * Build the popup menu | |
175 | * | |
176 | * @param manager | |
177 | * The manager to build | |
178 | */ | |
179 | private void fillLocalToolBar(IToolBarManager manager) { | |
180 | ||
181 | fSaveAction = new SaveAction(); | |
182 | fSaveAction.setImageDescriptor(ImageDescriptor.createFromImage(SAVE_IMAGE)); | |
183 | fSaveAction.setToolTipText(Messages.FilterView_SaveActionToolTipText); | |
184 | ||
185 | fAddAction = new AddAction(); | |
186 | fAddAction.setImageDescriptor(ImageDescriptor.createFromImage(ADD_IMAGE)); | |
187 | fAddAction.setToolTipText(Messages.FilterView_AddActionToolTipText); | |
188 | ||
189 | fDeleteAction = new DeleteAction(); | |
190 | fDeleteAction.setImageDescriptor(ImageDescriptor.createFromImage(DELETE_IMAGE)); | |
191 | fDeleteAction.setToolTipText(Messages.FilterView_DeleteActionToolTipText); | |
192 | fDeleteAction.setEnabled(false); | |
193 | ||
194 | fExportAction = new ExportAction(); | |
195 | fExportAction.setImageDescriptor(ImageDescriptor.createFromImage(EXPORT_IMAGE)); | |
196 | fExportAction.setToolTipText(Messages.FilterView_ExportActionToolTipText); | |
197 | ||
198 | fImportAction = new ImportAction(); | |
199 | fImportAction.setImageDescriptor(ImageDescriptor.createFromImage(IMPORT_IMAGE)); | |
200 | fImportAction.setToolTipText(Messages.FilterView_ImportActionToolTipText); | |
201 | ||
202 | manager.add(fSaveAction); | |
203 | manager.add(new Separator()); | |
204 | manager.add(fAddAction); | |
205 | manager.add(fDeleteAction); | |
206 | manager.add(new Separator()); | |
207 | manager.add(fExportAction); | |
208 | manager.add(fImportAction); | |
209 | } | |
210 | ||
211 | private class SaveAction extends Action { | |
212 | @Override | |
213 | public void run() { | |
214 | FilterManager.setSavedFilters(fRoot.getChildren()); | |
215 | } | |
216 | } | |
217 | ||
218 | private class AddAction extends Action { | |
219 | @Override | |
220 | public void run() { | |
221 | ||
222 | TmfFilterNode newNode = new TmfFilterNode(fRoot, ""); //$NON-NLS-1$ | |
223 | refresh(); | |
224 | setSelection(newNode); | |
225 | } | |
226 | } | |
227 | ||
228 | private class DeleteAction extends Action { | |
229 | @Override | |
230 | public void run() { | |
231 | ITmfFilterTreeNode node = fViewer.getSelection(); | |
232 | if (node != null) { | |
233 | node.remove(); | |
234 | } | |
235 | refresh(); | |
236 | } | |
237 | } | |
238 | ||
239 | private class ExportAction extends Action { | |
240 | @Override | |
241 | public void run() { | |
242 | try { | |
243 | FileDialog dlg = new FileDialog(new Shell(), SWT.SAVE); | |
244 | dlg.setFilterNames(new String[] { Messages.FilterView_FileDialogFilterName + " (*.filter.xml)" }); //$NON-NLS-1$ | |
245 | dlg.setFilterExtensions(new String[] { "*.filter.xml" }); //$NON-NLS-1$ | |
246 | ||
247 | String fn = dlg.open(); | |
248 | if (fn != null) { | |
249 | TmfFilterXMLWriter writerXML = new TmfFilterXMLWriter(fRoot); | |
250 | writerXML.saveTree(fn); | |
251 | } | |
252 | ||
253 | } catch (ParserConfigurationException e) { | |
254 | Activator.getDefault().logError("Error parsing filter xml file", e); //$NON-NLS-1$ | |
255 | } | |
256 | } | |
257 | } | |
258 | ||
259 | private class ImportAction extends Action { | |
260 | @Override | |
261 | public void run() { | |
262 | if (fViewer != null) { | |
263 | ITmfFilterTreeNode root = null; | |
264 | try { | |
265 | FileDialog dlg = new FileDialog(new Shell(), SWT.OPEN); | |
266 | dlg.setFilterNames(new String[] { Messages.FilterView_FileDialogFilterName + " (*.filter.xml)" }); //$NON-NLS-1$ | |
267 | dlg.setFilterExtensions(new String[] { "*.filter.xml" }); //$NON-NLS-1$ | |
268 | ||
269 | TmfFilterXMLParser parserXML = null; | |
270 | String fn = dlg.open(); | |
271 | if (fn != null) { | |
272 | parserXML = new TmfFilterXMLParser(fn); | |
273 | root = parserXML.getTree(); | |
274 | } | |
275 | ||
276 | } catch (SAXException e) { | |
277 | Activator.getDefault().logError("Error importing filter xml file", e); //$NON-NLS-1$ | |
278 | } catch (IOException e) { | |
279 | Activator.getDefault().logError("Error importing filter xml file", e); //$NON-NLS-1$ | |
280 | } | |
281 | ||
282 | if (root != null) { | |
283 | for (ITmfFilterTreeNode node : root.getChildren()) { | |
284 | if (node instanceof TmfFilterNode) { | |
285 | fRoot.addChild(node); | |
286 | refresh(); | |
287 | fViewer.setSelection(node); | |
288 | } | |
289 | } | |
290 | } | |
291 | } | |
292 | } | |
293 | } | |
be222f56 | 294 | |
90e2b925 | 295 | } |