Commit | Line | Data |
---|---|---|
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 | * Patrick Tasse - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
2bdf0193 | 13 | package org.eclipse.tracecompass.tmf.ui.views.filter; |
be222f56 PT |
14 | |
15 | import java.io.FileNotFoundException; | |
16 | import java.io.IOException; | |
17 | ||
18 | import javax.xml.parsers.ParserConfigurationException; | |
19 | ||
2bdf0193 AM |
20 | import org.eclipse.tracecompass.internal.tmf.ui.Activator; |
21 | import org.eclipse.tracecompass.tmf.core.filter.model.ITmfFilterTreeNode; | |
22 | import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterRootNode; | |
23 | import org.eclipse.tracecompass.tmf.core.filter.xml.TmfFilterXMLParser; | |
24 | import org.eclipse.tracecompass.tmf.core.filter.xml.TmfFilterXMLWriter; | |
be222f56 PT |
25 | import org.xml.sax.SAXException; |
26 | ||
27 | /** | |
28 | * Central filter manager | |
29 | * | |
30 | * @version 1.0 | |
31 | * @author Patrick Tasse | |
32 | */ | |
33 | public class FilterManager { | |
34 | ||
d5efe032 AF |
35 | private static final String SAVED_FILTERS_FILE_NAME = "saved_filters.xml"; //$NON-NLS-1$ |
36 | private static final String SAVED_FILTERS_PATH_NAME = | |
be222f56 PT |
37 | Activator.getDefault().getStateLocation().addTrailingSeparator().append(SAVED_FILTERS_FILE_NAME).toString(); |
38 | ||
39 | private static ITmfFilterTreeNode fRoot = new TmfFilterRootNode(); | |
40 | static { | |
d5efe032 AF |
41 | try { |
42 | fRoot = new TmfFilterXMLParser(SAVED_FILTERS_PATH_NAME).getTree(); | |
be222f56 PT |
43 | } catch (FileNotFoundException e) { |
44 | } catch (SAXException e) { | |
45 | Activator.getDefault().logError("Error parsing saved filter xml file: " + SAVED_FILTERS_PATH_NAME, e); //$NON-NLS-1$ | |
46 | } catch (IOException e) { | |
47 | Activator.getDefault().logError("Error parsing saved filter xml file: " + SAVED_FILTERS_PATH_NAME, e); //$NON-NLS-1$ | |
48 | } | |
49 | } | |
50 | ||
51 | /** | |
52 | * Retrieve the currently saved filters | |
53 | * | |
54 | * @return The array of filters | |
55 | */ | |
56 | public static ITmfFilterTreeNode[] getSavedFilters() { | |
d5efe032 | 57 | return fRoot.clone().getChildren(); |
be222f56 PT |
58 | } |
59 | ||
60 | /** | |
61 | * Set the passed filters as the currently saved ones. | |
62 | * | |
63 | * @param filters | |
64 | * The filters to save | |
65 | */ | |
66 | public static void setSavedFilters(ITmfFilterTreeNode[] filters) { | |
d5efe032 AF |
67 | fRoot = new TmfFilterRootNode(); |
68 | for (ITmfFilterTreeNode filter : filters) { | |
69 | fRoot.addChild(filter.clone()); | |
70 | } | |
71 | try { | |
72 | TmfFilterXMLWriter writerXML = new TmfFilterXMLWriter(fRoot); | |
73 | writerXML.saveTree(SAVED_FILTERS_PATH_NAME); | |
be222f56 PT |
74 | } catch (ParserConfigurationException e) { |
75 | Activator.getDefault().logError("Error saving filter xml file: " + SAVED_FILTERS_PATH_NAME, e); //$NON-NLS-1$ | |
76 | } | |
77 | } | |
78 | } |