Commit | Line | Data |
---|---|---|
90e2b925 FC |
1 | /*******************************************************************************\r |
2 | * Copyright (c) 2010 Ericsson\r | |
3 | * \r | |
4 | * All rights reserved. This program and the accompanying materials are\r | |
5 | * made available under the terms of the Eclipse Public License v1.0 which\r | |
6 | * accompanies this distribution, and is available at\r | |
7 | * http://www.eclipse.org/legal/epl-v10.html\r | |
8 | * \r | |
9 | * Contributors:\r | |
10 | * Yuriy Vashchuk - Initial API and implementation\r | |
11 | * based on Francois Chouinard ProjectView code.\r | |
12 | */\r | |
13 | \r | |
14 | package org.eclipse.linuxtools.tmf.ui.views.filter;\r | |
15 | \r | |
16 | import java.io.IOException;\r | |
17 | \r | |
18 | import javax.xml.parsers.ParserConfigurationException;\r | |
19 | \r | |
20 | import org.eclipse.core.resources.IResource;\r | |
21 | import org.eclipse.core.resources.IWorkspace;\r | |
22 | import org.eclipse.core.resources.ResourcesPlugin;\r | |
23 | import org.eclipse.core.runtime.CoreException;\r | |
24 | import org.eclipse.jface.action.Action;\r | |
25 | import org.eclipse.jface.action.IToolBarManager;\r | |
26 | import org.eclipse.jface.action.Separator;\r | |
27 | import org.eclipse.jface.resource.ImageDescriptor;\r | |
28 | import org.eclipse.jface.viewers.ISelectionChangedListener;\r | |
29 | import org.eclipse.jface.viewers.IStructuredSelection;\r | |
30 | import org.eclipse.jface.viewers.SelectionChangedEvent;\r | |
31 | import org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode;\r | |
32 | import org.eclipse.linuxtools.tmf.filter.model.TmfFilterNode;\r | |
33 | import org.eclipse.linuxtools.tmf.filter.model.TmfFilterRootNode;\r | |
34 | import org.eclipse.linuxtools.tmf.filter.xml.TmfFilterXMLParser;\r | |
35 | import org.eclipse.linuxtools.tmf.filter.xml.TmfFilterXMLWriter;\r | |
36 | import org.eclipse.linuxtools.tmf.ui.TmfUiPlugin;\r | |
37 | import org.eclipse.linuxtools.tmf.ui.internal.Messages;\r | |
38 | import org.eclipse.linuxtools.tmf.ui.views.TmfView;\r | |
39 | import org.eclipse.swt.SWT;\r | |
40 | import org.eclipse.swt.graphics.Image;\r | |
41 | import org.eclipse.swt.widgets.Composite;\r | |
42 | import org.eclipse.swt.widgets.FileDialog;\r | |
43 | import org.eclipse.swt.widgets.Shell;\r | |
44 | import org.eclipse.ui.IActionBars;\r | |
45 | import org.xml.sax.SAXException;\r | |
46 | \r | |
47 | /**\r | |
48 | * <b><u>FilterView</u></b>\r | |
49 | * <p>\r | |
50 | * View that contain UI to the TMF filter.\r | |
51 | */\r | |
52 | public class FilterView extends TmfView {\r | |
53 | \r | |
54 | public static final String ID = "org.eclipse.linuxtools.tmf.ui.views.filter"; //$NON-NLS-1$\r | |
55 | \r | |
56 | private static final Image SAVE_IMAGE = TmfUiPlugin.getDefault().getImageFromPath("/icons/elcl16/save_button.gif"); //$NON-NLS-1$\r | |
57 | private static final Image ADD_IMAGE = TmfUiPlugin.getDefault().getImageFromPath("/icons/elcl16/add_button.gif"); //$NON-NLS-1$\r | |
58 | private static final Image DELETE_IMAGE = TmfUiPlugin.getDefault().getImageFromPath("/icons/elcl16/delete_button.gif"); //$NON-NLS-1$\r | |
59 | private static final Image IMPORT_IMAGE = TmfUiPlugin.getDefault().getImageFromPath("/icons/elcl16/import_button.gif"); //$NON-NLS-1$\r | |
60 | private static final Image EXPORT_IMAGE = TmfUiPlugin.getDefault().getImageFromPath("/icons/elcl16/export_button.gif"); //$NON-NLS-1$\r | |
61 | \r | |
62 | // ------------------------------------------------------------------------\r | |
63 | // Main data structures\r | |
64 | // ------------------------------------------------------------------------\r | |
65 | \r | |
66 | private FilterViewer fViewer;\r | |
67 | private ITmfFilterTreeNode fRoot;\r | |
68 | \r | |
69 | private IWorkspace fWorkspace;\r | |
70 | \r | |
71 | private SaveAction fSaveAction;\r | |
72 | private AddAction fAddAction;\r | |
73 | private DeleteAction fDeleteAction;\r | |
74 | private ExportAction fExportAction;\r | |
75 | private ImportAction fImportAction;\r | |
76 | \r | |
77 | /**\r | |
78 | * Getter for the Filter Tree Root\r | |
79 | * \r | |
80 | * @return The root of builded tree\r | |
81 | */\r | |
82 | public ITmfFilterTreeNode getFilterRoot() {\r | |
83 | return fRoot;\r | |
84 | }\r | |
85 | \r | |
86 | \r | |
87 | // ------------------------------------------------------------------------\r | |
88 | // Constructor\r | |
89 | // ------------------------------------------------------------------------\r | |
90 | \r | |
91 | /**\r | |
92 | * Default Constructor\r | |
93 | */\r | |
94 | public FilterView() {\r | |
95 | super("Filter"); //$NON-NLS-1$\r | |
96 | \r | |
97 | fWorkspace = ResourcesPlugin.getWorkspace();\r | |
98 | try {\r | |
99 | fWorkspace.getRoot().refreshLocal(IResource.DEPTH_INFINITE, null);\r | |
100 | } catch (CoreException e) {\r | |
101 | e.printStackTrace();\r | |
102 | }\r | |
103 | \r | |
104 | fRoot = new TmfFilterRootNode();\r | |
105 | for (ITmfFilterTreeNode node : FilterManager.getSavedFilters()) {\r | |
106 | fRoot.addChild(node);\r | |
107 | }\r | |
108 | }\r | |
109 | \r | |
110 | \r | |
111 | /**\r | |
112 | * Refresh the tree widget\r | |
113 | */\r | |
114 | public void refresh() {\r | |
115 | fViewer.refresh();\r | |
116 | }\r | |
117 | \r | |
118 | /**\r | |
119 | * Setter for selection\r | |
120 | * \r | |
121 | * @param node The node to select\r | |
122 | */\r | |
123 | public void setSelection(ITmfFilterTreeNode node) {\r | |
124 | fViewer.setSelection(node, true);\r | |
125 | }\r | |
126 | \r | |
127 | /*\r | |
128 | * (non-Javadoc)\r | |
129 | * @see org.eclipse.linuxtools.tmf.ui.views.TmfView#createPartControl(org.eclipse.swt.widgets.Composite)\r | |
130 | */\r | |
131 | @Override\r | |
132 | public void createPartControl(Composite parent) {\r | |
133 | \r | |
134 | fViewer = new FilterViewer(parent, SWT.NONE);\r | |
135 | fViewer.setInput(fRoot);\r | |
136 | \r | |
137 | contributeToActionBars();\r | |
138 | \r | |
139 | fViewer.addSelectionChangedListener(new ISelectionChangedListener() {\r | |
140 | @Override\r | |
141 | public void selectionChanged(SelectionChangedEvent event) {\r | |
142 | if (!(event.getSelection().isEmpty()) && event.getSelection() instanceof IStructuredSelection) {\r | |
143 | fDeleteAction.setEnabled(true);\r | |
144 | fExportAction.setEnabled(true);\r | |
145 | } else {\r | |
146 | fDeleteAction.setEnabled(false);\r | |
147 | fExportAction.setEnabled(false);\r | |
148 | }\r | |
149 | }\r | |
150 | });\r | |
151 | }\r | |
152 | \r | |
153 | \r | |
154 | // ------------------------------------------------------------------------\r | |
155 | // ViewPart\r | |
156 | // ------------------------------------------------------------------------\r | |
157 | \r | |
158 | /*\r | |
159 | * (non-Javadoc)\r | |
160 | * @see java.lang.Object#toString()\r | |
161 | */\r | |
162 | @Override\r | |
163 | public String toString() {\r | |
164 | return "[FilterView]"; //$NON-NLS-1$\r | |
165 | } \r | |
166 | \r | |
167 | \r | |
168 | /**\r | |
169 | * Builds the menu toolbar\r | |
170 | */\r | |
171 | private void contributeToActionBars() {\r | |
172 | IActionBars bars = getViewSite().getActionBars();\r | |
173 | //fillLocalPullDown(bars.getMenuManager());\r | |
174 | fillLocalToolBar(bars.getToolBarManager());\r | |
175 | }\r | |
176 | \r | |
177 | \r | |
178 | /**\r | |
179 | * Build the popup menu\r | |
180 | * \r | |
181 | * @param manager The manager to build\r | |
182 | */\r | |
183 | private void fillLocalToolBar(IToolBarManager manager) {\r | |
184 | \r | |
185 | fSaveAction = new SaveAction();\r | |
186 | fSaveAction.setImageDescriptor(ImageDescriptor.createFromImage(SAVE_IMAGE));\r | |
187 | fSaveAction.setToolTipText(Messages.FilterView_SaveActionToolTipText);\r | |
188 | \r | |
189 | fAddAction = new AddAction();\r | |
190 | fAddAction.setImageDescriptor(ImageDescriptor.createFromImage(ADD_IMAGE));\r | |
191 | fAddAction.setToolTipText(Messages.FilterView_AddActionToolTipText); \r | |
192 | \r | |
193 | fDeleteAction = new DeleteAction();\r | |
194 | fDeleteAction.setImageDescriptor(ImageDescriptor.createFromImage(DELETE_IMAGE));\r | |
195 | fDeleteAction.setToolTipText(Messages.FilterView_DeleteActionToolTipText);\r | |
196 | fDeleteAction.setEnabled(false);\r | |
197 | \r | |
198 | fExportAction = new ExportAction();\r | |
199 | fExportAction.setImageDescriptor(ImageDescriptor.createFromImage(EXPORT_IMAGE));\r | |
200 | fExportAction.setToolTipText(Messages.FilterView_ExportActionToolTipText);\r | |
201 | \r | |
202 | fImportAction = new ImportAction();\r | |
203 | fImportAction.setImageDescriptor(ImageDescriptor.createFromImage(IMPORT_IMAGE));\r | |
204 | fImportAction.setToolTipText(Messages.FilterView_ImportActionToolTipText);\r | |
205 | \r | |
206 | manager.add(fSaveAction);\r | |
207 | manager.add(new Separator());\r | |
208 | manager.add(fAddAction);\r | |
209 | manager.add(fDeleteAction);\r | |
210 | manager.add(new Separator());\r | |
211 | manager.add(fExportAction);\r | |
212 | manager.add(fImportAction);\r | |
213 | }\r | |
214 | \r | |
215 | private class SaveAction extends Action {\r | |
216 | @Override\r | |
217 | public void run() {\r | |
218 | FilterManager.setSavedFilters(fRoot.getChildren());\r | |
219 | }\r | |
220 | }\r | |
221 | \r | |
222 | private class AddAction extends Action {\r | |
223 | @Override\r | |
224 | public void run() {\r | |
225 | \r | |
226 | TmfFilterNode newNode = new TmfFilterNode(fRoot, ""); //$NON-NLS-1$\r | |
227 | refresh();\r | |
228 | setSelection(newNode);\r | |
229 | }\r | |
230 | }\r | |
231 | \r | |
232 | private class DeleteAction extends Action {\r | |
233 | @Override\r | |
234 | public void run() {\r | |
235 | ITmfFilterTreeNode node = fViewer.getSelection();\r | |
236 | if (node != null) {\r | |
237 | node.remove();\r | |
238 | }\r | |
239 | refresh();\r | |
240 | }\r | |
241 | }\r | |
242 | \r | |
243 | private class ExportAction extends Action {\r | |
244 | @Override\r | |
245 | public void run() {\r | |
246 | try {\r | |
247 | FileDialog dlg = new FileDialog(new Shell(), SWT.SAVE);\r | |
248 | dlg.setFilterNames(new String[] {Messages.FilterView_FileDialogFilterName + " (*.filter.xml)"}); //$NON-NLS-1$\r | |
249 | dlg.setFilterExtensions(new String[] {"*.filter.xml"}); //$NON-NLS-1$\r | |
250 | \r | |
251 | String fn = dlg.open();\r | |
252 | if (fn != null) {\r | |
253 | TmfFilterXMLWriter writerXML = new TmfFilterXMLWriter(fRoot);\r | |
254 | writerXML.saveTree(fn);\r | |
255 | }\r | |
256 | \r | |
257 | } catch (ParserConfigurationException e) {\r | |
258 | e.printStackTrace();\r | |
259 | } catch (IOException e) {\r | |
260 | e.printStackTrace();\r | |
261 | }\r | |
262 | }\r | |
263 | }\r | |
264 | \r | |
265 | private class ImportAction extends Action {\r | |
266 | @Override\r | |
267 | public void run() {\r | |
268 | if (fViewer != null) {\r | |
269 | ITmfFilterTreeNode root = null;\r | |
270 | try {\r | |
271 | FileDialog dlg = new FileDialog(new Shell(), SWT.OPEN);\r | |
272 | dlg.setFilterNames(new String[] {Messages.FilterView_FileDialogFilterName + " (*.filter.xml)"}); //$NON-NLS-1$\r | |
273 | dlg.setFilterExtensions(new String[] {"*.filter.xml"}); //$NON-NLS-1$\r | |
274 | \r | |
275 | TmfFilterXMLParser parserXML = null;\r | |
276 | String fn = dlg.open();\r | |
277 | if (fn != null) {\r | |
278 | parserXML = new TmfFilterXMLParser(fn);\r | |
279 | root = parserXML.getTree();\r | |
280 | }\r | |
281 | \r | |
282 | } catch (SAXException e) {\r | |
283 | e.printStackTrace();\r | |
284 | } catch (IOException e) {\r | |
285 | e.printStackTrace();\r | |
286 | }\r | |
287 | \r | |
288 | if (root != null) {\r | |
289 | for (ITmfFilterTreeNode node : root.getChildren()) {\r | |
290 | if (node instanceof TmfFilterNode) {\r | |
291 | fRoot.addChild(node);\r | |
292 | refresh();\r | |
293 | fViewer.setSelection(node);\r | |
294 | }\r | |
295 | }\r | |
296 | }\r | |
297 | }\r | |
298 | }\r | |
299 | }\r | |
300 | \r | |
301 | } |