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 | |
ef906471 | 11 | * Xavier Raynaud - add cut/copy/paste/dnd support |
be222f56 PT |
12 | * based on Francois Chouinard ProjectView code. |
13 | */ | |
14 | ||
15 | package org.eclipse.linuxtools.tmf.ui.views.filter; | |
16 | ||
17 | import java.io.IOException; | |
18 | ||
19 | import javax.xml.parsers.ParserConfigurationException; | |
20 | ||
21 | import org.eclipse.core.resources.IResource; | |
22 | import org.eclipse.core.resources.IWorkspace; | |
23 | import org.eclipse.core.resources.ResourcesPlugin; | |
24 | import org.eclipse.core.runtime.CoreException; | |
dab5f596 | 25 | import org.eclipse.jdt.annotation.NonNull; |
be222f56 PT |
26 | import org.eclipse.jface.action.Action; |
27 | import org.eclipse.jface.action.IToolBarManager; | |
ef906471 | 28 | import org.eclipse.jface.action.MenuManager; |
be222f56 PT |
29 | import org.eclipse.jface.action.Separator; |
30 | import org.eclipse.jface.resource.ImageDescriptor; | |
31 | import org.eclipse.jface.viewers.ISelectionChangedListener; | |
32 | import org.eclipse.jface.viewers.IStructuredSelection; | |
33 | import org.eclipse.jface.viewers.SelectionChangedEvent; | |
34 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; | |
35 | import org.eclipse.linuxtools.internal.tmf.ui.Messages; | |
36 | import org.eclipse.linuxtools.tmf.core.filter.model.ITmfFilterTreeNode; | |
37 | import org.eclipse.linuxtools.tmf.core.filter.model.TmfFilterNode; | |
38 | import org.eclipse.linuxtools.tmf.core.filter.model.TmfFilterRootNode; | |
39 | import org.eclipse.linuxtools.tmf.core.filter.xml.TmfFilterXMLParser; | |
40 | import org.eclipse.linuxtools.tmf.core.filter.xml.TmfFilterXMLWriter; | |
41 | import org.eclipse.linuxtools.tmf.ui.views.TmfView; | |
42 | import org.eclipse.swt.SWT; | |
43 | import org.eclipse.swt.graphics.Image; | |
44 | import org.eclipse.swt.widgets.Composite; | |
45 | import org.eclipse.swt.widgets.FileDialog; | |
46 | import org.eclipse.swt.widgets.Shell; | |
47 | import org.eclipse.ui.IActionBars; | |
48 | import org.xml.sax.SAXException; | |
49 | ||
50 | /** | |
51 | * View that contain UI to the TMF filter. | |
52 | * | |
53 | * @version 1.0 | |
54 | * @author Yuriy Vashchuk | |
55 | */ | |
56 | public class FilterView extends TmfView { | |
57 | ||
58 | /** ID for the Filter view */ | |
dab5f596 | 59 | public static final @NonNull String ID = "org.eclipse.linuxtools.tmf.ui.views.filter"; //$NON-NLS-1$ |
be222f56 PT |
60 | |
61 | private static final Image SAVE_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/save_button.gif"); //$NON-NLS-1$ | |
62 | private static final Image ADD_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/add_button.gif"); //$NON-NLS-1$ | |
be222f56 PT |
63 | private static final Image IMPORT_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/import_button.gif"); //$NON-NLS-1$ |
64 | private static final Image EXPORT_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/export_button.gif"); //$NON-NLS-1$ | |
65 | ||
66 | // ------------------------------------------------------------------------ | |
67 | // Main data structures | |
68 | // ------------------------------------------------------------------------ | |
69 | ||
70 | private FilterViewer fViewer; | |
71 | private final ITmfFilterTreeNode fRoot; | |
72 | ||
73 | private final IWorkspace fWorkspace; | |
74 | ||
75 | private SaveAction fSaveAction; | |
76 | private AddAction fAddAction; | |
be222f56 PT |
77 | private ExportAction fExportAction; |
78 | private ImportAction fImportAction; | |
79 | ||
80 | /** | |
81 | * Getter for the Filter Tree Root | |
82 | * | |
83 | * @return The root of builded tree | |
84 | */ | |
85 | public ITmfFilterTreeNode getFilterRoot() { | |
11252342 | 86 | return fRoot; |
be222f56 PT |
87 | } |
88 | ||
be222f56 PT |
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()) { | |
11252342 | 108 | fRoot.addChild(node); |
be222f56 | 109 | } |
11252342 AM |
110 | } |
111 | ||
112 | /** | |
113 | * Refresh the tree widget | |
be222f56 | 114 | */ |
11252342 AM |
115 | public void refresh() { |
116 | fViewer.refresh(); | |
117 | } | |
118 | ||
119 | /** | |
120 | * Setter for selection | |
121 | * | |
122 | * @param node | |
123 | * The node to select | |
be222f56 | 124 | */ |
11252342 AM |
125 | public void setSelection(ITmfFilterTreeNode node) { |
126 | fViewer.setSelection(node, true); | |
127 | } | |
128 | ||
129 | // ------------------------------------------------------------------------ | |
130 | // ViewPart | |
131 | // ------------------------------------------------------------------------ | |
132 | ||
133 | @Override | |
134 | public void createPartControl(Composite parent) { | |
135 | ||
136 | fViewer = new FilterViewer(parent, SWT.NONE); | |
137 | fViewer.setInput(fRoot); | |
138 | ||
139 | contributeToActionBars(); | |
140 | ||
141 | fViewer.addSelectionChangedListener(new ISelectionChangedListener() { | |
142 | @Override | |
143 | public void selectionChanged(SelectionChangedEvent event) { | |
144 | if (!(event.getSelection().isEmpty()) && event.getSelection() instanceof IStructuredSelection) { | |
11252342 AM |
145 | fExportAction.setEnabled(true); |
146 | } else { | |
11252342 AM |
147 | fExportAction.setEnabled(false); |
148 | } | |
149 | } | |
150 | }); | |
ef906471 XR |
151 | this.getSite().setSelectionProvider(fViewer.getTreeViewer()); |
152 | ||
780fc05e | 153 | MenuManager menuManager = fViewer.getMenuManager(); |
ef906471 XR |
154 | this.getSite().registerContextMenu(menuManager, fViewer.getTreeViewer()); |
155 | } | |
156 | ||
157 | /** | |
158 | * @return the ITmfFilterTreeNode currently selected | |
159 | */ | |
160 | ITmfFilterTreeNode getSelection() { | |
161 | return fViewer.getSelection(); | |
11252342 AM |
162 | } |
163 | ||
be222f56 PT |
164 | @Override |
165 | public void setFocus() { | |
166 | fViewer.setFocus(); | |
167 | } | |
168 | ||
11252342 AM |
169 | @Override |
170 | public String toString() { | |
171 | return "[FilterView]"; //$NON-NLS-1$ | |
172 | } | |
be222f56 PT |
173 | |
174 | /** | |
175 | * Builds the menu toolbar | |
176 | */ | |
11252342 AM |
177 | private void contributeToActionBars() { |
178 | IActionBars bars = getViewSite().getActionBars(); | |
179 | // fillLocalPullDown(bars.getMenuManager()); | |
180 | fillLocalToolBar(bars.getToolBarManager()); | |
181 | } | |
182 | ||
183 | /** | |
184 | * Build the popup menu | |
185 | * | |
186 | * @param manager | |
187 | * The manager to build | |
188 | */ | |
189 | private void fillLocalToolBar(IToolBarManager manager) { | |
190 | ||
191 | fSaveAction = new SaveAction(); | |
192 | fSaveAction.setImageDescriptor(ImageDescriptor.createFromImage(SAVE_IMAGE)); | |
193 | fSaveAction.setToolTipText(Messages.FilterView_SaveActionToolTipText); | |
194 | ||
195 | fAddAction = new AddAction(); | |
196 | fAddAction.setImageDescriptor(ImageDescriptor.createFromImage(ADD_IMAGE)); | |
197 | fAddAction.setToolTipText(Messages.FilterView_AddActionToolTipText); | |
198 | ||
11252342 AM |
199 | fExportAction = new ExportAction(); |
200 | fExportAction.setImageDescriptor(ImageDescriptor.createFromImage(EXPORT_IMAGE)); | |
201 | fExportAction.setToolTipText(Messages.FilterView_ExportActionToolTipText); | |
202 | ||
203 | fImportAction = new ImportAction(); | |
204 | fImportAction.setImageDescriptor(ImageDescriptor.createFromImage(IMPORT_IMAGE)); | |
205 | fImportAction.setToolTipText(Messages.FilterView_ImportActionToolTipText); | |
206 | ||
207 | manager.add(fSaveAction); | |
ef906471 | 208 | manager.add(new Separator("add_delete")); //$NON-NLS-1$ |
11252342 | 209 | manager.add(fAddAction); |
ef906471 | 210 | manager.add(new Separator("edit")); //$NON-NLS-1$ |
11252342 AM |
211 | manager.add(new Separator()); |
212 | manager.add(fExportAction); | |
213 | manager.add(fImportAction); | |
214 | } | |
215 | ||
216 | private class SaveAction extends Action { | |
217 | @Override | |
218 | public void run() { | |
219 | FilterManager.setSavedFilters(fRoot.getChildren()); | |
220 | } | |
221 | } | |
222 | ||
223 | private class AddAction extends Action { | |
224 | @Override | |
225 | public void run() { | |
226 | ||
227 | TmfFilterNode newNode = new TmfFilterNode(fRoot, ""); //$NON-NLS-1$ | |
228 | refresh(); | |
229 | setSelection(newNode); | |
230 | } | |
231 | } | |
232 | ||
11252342 AM |
233 | private class ExportAction extends Action { |
234 | @Override | |
235 | public void run() { | |
236 | try { | |
237 | FileDialog dlg = new FileDialog(new Shell(), SWT.SAVE); | |
0d3171c0 VP |
238 | dlg.setFilterNames(new String[] { Messages.FilterView_FileDialogFilterName + " (*.xml)" }); //$NON-NLS-1$ |
239 | dlg.setFilterExtensions(new String[] { "*.xml" }); //$NON-NLS-1$ | |
11252342 AM |
240 | |
241 | String fn = dlg.open(); | |
242 | if (fn != null) { | |
243 | TmfFilterXMLWriter writerXML = new TmfFilterXMLWriter(fRoot); | |
244 | writerXML.saveTree(fn); | |
245 | } | |
246 | ||
247 | } catch (ParserConfigurationException e) { | |
248 | Activator.getDefault().logError("Error parsing filter xml file", e); //$NON-NLS-1$ | |
249 | } | |
250 | } | |
251 | } | |
252 | ||
253 | private class ImportAction extends Action { | |
254 | @Override | |
255 | public void run() { | |
256 | if (fViewer != null) { | |
257 | ITmfFilterTreeNode root = null; | |
258 | try { | |
259 | FileDialog dlg = new FileDialog(new Shell(), SWT.OPEN); | |
0d3171c0 VP |
260 | dlg.setFilterNames(new String[] { Messages.FilterView_FileDialogFilterName + " (*.xml)" }); //$NON-NLS-1$ |
261 | dlg.setFilterExtensions(new String[] { "*.xml" }); //$NON-NLS-1$ | |
11252342 AM |
262 | |
263 | TmfFilterXMLParser parserXML = null; | |
264 | String fn = dlg.open(); | |
265 | if (fn != null) { | |
266 | parserXML = new TmfFilterXMLParser(fn); | |
267 | root = parserXML.getTree(); | |
268 | } | |
269 | ||
270 | } catch (SAXException e) { | |
271 | Activator.getDefault().logError("Error importing filter xml file", e); //$NON-NLS-1$ | |
272 | } catch (IOException e) { | |
273 | Activator.getDefault().logError("Error importing filter xml file", e); //$NON-NLS-1$ | |
274 | } | |
275 | ||
276 | if (root != null) { | |
277 | for (ITmfFilterTreeNode node : root.getChildren()) { | |
278 | if (node instanceof TmfFilterNode) { | |
279 | fRoot.addChild(node); | |
280 | refresh(); | |
281 | fViewer.setSelection(node); | |
282 | } | |
283 | } | |
284 | } | |
285 | } | |
286 | } | |
287 | } | |
be222f56 | 288 | |
90e2b925 | 289 | } |