Commit | Line | Data |
---|---|---|
be222f56 PT |
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.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() { | |
85 | return fRoot; | |
86 | } | |
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 | /** | |
114 | * Refresh the tree widget | |
115 | */ | |
116 | public void refresh() { | |
117 | fViewer.refresh(); | |
118 | } | |
119 | ||
120 | /** | |
121 | * Setter for selection | |
122 | * | |
123 | * @param node The node to select | |
124 | */ | |
125 | public void setSelection(ITmfFilterTreeNode node) { | |
126 | fViewer.setSelection(node, true); | |
127 | } | |
128 | ||
129 | // ------------------------------------------------------------------------ | |
130 | // ViewPart | |
131 | // ------------------------------------------------------------------------ | |
132 | ||
133 | /* (non-Javadoc) | |
134 | * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(Composite) | |
135 | */ | |
136 | @Override | |
137 | public void createPartControl(Composite parent) { | |
138 | ||
139 | fViewer = new FilterViewer(parent, SWT.NONE); | |
140 | fViewer.setInput(fRoot); | |
141 | ||
142 | contributeToActionBars(); | |
143 | ||
144 | fViewer.addSelectionChangedListener(new ISelectionChangedListener() { | |
145 | @Override | |
146 | public void selectionChanged(SelectionChangedEvent event) { | |
147 | if (!(event.getSelection().isEmpty()) && event.getSelection() instanceof IStructuredSelection) { | |
148 | fDeleteAction.setEnabled(true); | |
149 | fExportAction.setEnabled(true); | |
150 | } else { | |
151 | fDeleteAction.setEnabled(false); | |
152 | fExportAction.setEnabled(false); | |
153 | } | |
154 | } | |
155 | }); | |
156 | } | |
157 | ||
158 | /* (non-Javadoc) | |
159 | * @see org.eclipse.ui.part.WorkbenchPart#setFocus() | |
160 | */ | |
161 | @Override | |
162 | public void setFocus() { | |
163 | fViewer.setFocus(); | |
164 | } | |
165 | ||
166 | /* | |
167 | * (non-Javadoc) | |
168 | * @see java.lang.Object#toString() | |
169 | */ | |
170 | @Override | |
171 | public String toString() { | |
172 | return "[FilterView]"; //$NON-NLS-1$ | |
173 | } | |
174 | ||
175 | ||
176 | /** | |
177 | * Builds the menu toolbar | |
178 | */ | |
179 | private void contributeToActionBars() { | |
180 | IActionBars bars = getViewSite().getActionBars(); | |
181 | //fillLocalPullDown(bars.getMenuManager()); | |
182 | fillLocalToolBar(bars.getToolBarManager()); | |
183 | } | |
184 | ||
185 | ||
186 | /** | |
187 | * Build the popup menu | |
188 | * | |
189 | * @param manager The manager to build | |
190 | */ | |
191 | private void fillLocalToolBar(IToolBarManager manager) { | |
192 | ||
193 | fSaveAction = new SaveAction(); | |
194 | fSaveAction.setImageDescriptor(ImageDescriptor.createFromImage(SAVE_IMAGE)); | |
195 | fSaveAction.setToolTipText(Messages.FilterView_SaveActionToolTipText); | |
196 | ||
197 | fAddAction = new AddAction(); | |
198 | fAddAction.setImageDescriptor(ImageDescriptor.createFromImage(ADD_IMAGE)); | |
199 | fAddAction.setToolTipText(Messages.FilterView_AddActionToolTipText); | |
200 | ||
201 | fDeleteAction = new DeleteAction(); | |
202 | fDeleteAction.setImageDescriptor(ImageDescriptor.createFromImage(DELETE_IMAGE)); | |
203 | fDeleteAction.setToolTipText(Messages.FilterView_DeleteActionToolTipText); | |
204 | fDeleteAction.setEnabled(false); | |
205 | ||
206 | fExportAction = new ExportAction(); | |
207 | fExportAction.setImageDescriptor(ImageDescriptor.createFromImage(EXPORT_IMAGE)); | |
208 | fExportAction.setToolTipText(Messages.FilterView_ExportActionToolTipText); | |
209 | ||
210 | fImportAction = new ImportAction(); | |
211 | fImportAction.setImageDescriptor(ImageDescriptor.createFromImage(IMPORT_IMAGE)); | |
212 | fImportAction.setToolTipText(Messages.FilterView_ImportActionToolTipText); | |
213 | ||
214 | manager.add(fSaveAction); | |
215 | manager.add(new Separator()); | |
216 | manager.add(fAddAction); | |
217 | manager.add(fDeleteAction); | |
218 | manager.add(new Separator()); | |
219 | manager.add(fExportAction); | |
220 | manager.add(fImportAction); | |
221 | } | |
222 | ||
223 | private class SaveAction extends Action { | |
224 | @Override | |
225 | public void run() { | |
226 | FilterManager.setSavedFilters(fRoot.getChildren()); | |
227 | } | |
228 | } | |
229 | ||
230 | private class AddAction extends Action { | |
231 | @Override | |
232 | public void run() { | |
233 | ||
234 | TmfFilterNode newNode = new TmfFilterNode(fRoot, ""); //$NON-NLS-1$ | |
235 | refresh(); | |
236 | setSelection(newNode); | |
237 | } | |
238 | } | |
239 | ||
240 | private class DeleteAction extends Action { | |
241 | @Override | |
242 | public void run() { | |
243 | ITmfFilterTreeNode node = fViewer.getSelection(); | |
244 | if (node != null) { | |
245 | node.remove(); | |
246 | } | |
247 | refresh(); | |
248 | } | |
249 | } | |
250 | ||
251 | private class ExportAction extends Action { | |
252 | @Override | |
253 | public void run() { | |
254 | try { | |
255 | FileDialog dlg = new FileDialog(new Shell(), SWT.SAVE); | |
256 | dlg.setFilterNames(new String[] {Messages.FilterView_FileDialogFilterName + " (*.filter.xml)"}); //$NON-NLS-1$ | |
257 | dlg.setFilterExtensions(new String[] {"*.filter.xml"}); //$NON-NLS-1$ | |
258 | ||
259 | String fn = dlg.open(); | |
260 | if (fn != null) { | |
261 | TmfFilterXMLWriter writerXML = new TmfFilterXMLWriter(fRoot); | |
262 | writerXML.saveTree(fn); | |
263 | } | |
264 | ||
265 | } catch (ParserConfigurationException e) { | |
266 | Activator.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 | Activator.getDefault().logError("Error importing filter xml file", e); //$NON-NLS-1$ | |
290 | } catch (IOException e) { | |
291 | Activator.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 | ||
90e2b925 | 307 | } |