Commit | Line | Data |
---|---|---|
be222f56 | 1 | /******************************************************************************* |
2aebf5f3 | 2 | * Copyright (c) 2010, 2015 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 | |
2aebf5f3 | 11 | * Bernd Hufmann - Ensure backwards compatibility to Linux Tools |
be222f56 PT |
12 | *******************************************************************************/ |
13 | ||
2bdf0193 | 14 | package org.eclipse.tracecompass.tmf.ui.views.filter; |
be222f56 | 15 | |
2aebf5f3 | 16 | import java.io.File; |
be222f56 PT |
17 | import java.io.FileNotFoundException; |
18 | import java.io.IOException; | |
19 | ||
20 | import javax.xml.parsers.ParserConfigurationException; | |
21 | ||
2aebf5f3 | 22 | import org.eclipse.core.runtime.IPath; |
aa353506 | 23 | import org.eclipse.jdt.annotation.NonNull; |
2bdf0193 AM |
24 | import org.eclipse.tracecompass.internal.tmf.ui.Activator; |
25 | import org.eclipse.tracecompass.tmf.core.filter.model.ITmfFilterTreeNode; | |
26 | import org.eclipse.tracecompass.tmf.core.filter.model.TmfFilterRootNode; | |
27 | import org.eclipse.tracecompass.tmf.core.filter.xml.TmfFilterXMLParser; | |
28 | import org.eclipse.tracecompass.tmf.core.filter.xml.TmfFilterXMLWriter; | |
be222f56 PT |
29 | import org.xml.sax.SAXException; |
30 | ||
31 | /** | |
32 | * Central filter manager | |
33 | * | |
34 | * @version 1.0 | |
35 | * @author Patrick Tasse | |
36 | */ | |
37 | public class FilterManager { | |
38 | ||
d5efe032 AF |
39 | private static final String SAVED_FILTERS_FILE_NAME = "saved_filters.xml"; //$NON-NLS-1$ |
40 | private static final String SAVED_FILTERS_PATH_NAME = | |
be222f56 PT |
41 | Activator.getDefault().getStateLocation().addTrailingSeparator().append(SAVED_FILTERS_FILE_NAME).toString(); |
42 | ||
2aebf5f3 BH |
43 | /* |
44 | * Legacy path to the XML definitions file (in Linux Tools) | |
45 | * TODO Remove once we feel the transition phase is over. | |
46 | */ | |
47 | private static final IPath SAVED_FILTERS_FILE_NAME_LEGACY = | |
48 | Activator.getDefault().getStateLocation().removeLastSegments(1) | |
49 | .append("org.eclipse.linuxtools.tmf.ui") //$NON-NLS-1$ | |
50 | .append(SAVED_FILTERS_FILE_NAME); | |
51 | ||
52 | ||
be222f56 PT |
53 | private static ITmfFilterTreeNode fRoot = new TmfFilterRootNode(); |
54 | static { | |
2aebf5f3 BH |
55 | |
56 | File defaultFile = new File(SAVED_FILTERS_PATH_NAME); | |
57 | ||
58 | try { | |
59 | /* | |
60 | * If there is no file at the expected location, check the legacy | |
61 | * location instead. | |
62 | */ | |
63 | if (!defaultFile.exists()) { | |
64 | File legacyFileCore = SAVED_FILTERS_FILE_NAME_LEGACY.toFile(); | |
65 | if (legacyFileCore.exists()) { | |
66 | ITmfFilterTreeNode root = new TmfFilterXMLParser(SAVED_FILTERS_FILE_NAME_LEGACY.toString()).getTree(); | |
67 | setSavedFilters(root.getChildren()); | |
68 | } | |
69 | } | |
70 | } catch (FileNotFoundException e) { | |
71 | } catch (SAXException e) { | |
72 | Activator.getDefault().logError("Error parsing saved filter xml file: " + SAVED_FILTERS_FILE_NAME_LEGACY, e); //$NON-NLS-1$ | |
73 | } catch (IOException e) { | |
74 | Activator.getDefault().logError("Error parsing saved filter xml file: " + SAVED_FILTERS_FILE_NAME_LEGACY, e); //$NON-NLS-1$ | |
75 | } | |
76 | ||
d5efe032 | 77 | try { |
2aebf5f3 | 78 | // Now load the filters from the current location |
d5efe032 | 79 | fRoot = new TmfFilterXMLParser(SAVED_FILTERS_PATH_NAME).getTree(); |
be222f56 PT |
80 | } catch (FileNotFoundException e) { |
81 | } catch (SAXException e) { | |
82 | Activator.getDefault().logError("Error parsing saved filter xml file: " + SAVED_FILTERS_PATH_NAME, e); //$NON-NLS-1$ | |
83 | } catch (IOException e) { | |
84 | Activator.getDefault().logError("Error parsing saved filter xml file: " + SAVED_FILTERS_PATH_NAME, e); //$NON-NLS-1$ | |
85 | } | |
86 | } | |
87 | ||
88 | /** | |
89 | * Retrieve the currently saved filters | |
90 | * | |
91 | * @return The array of filters | |
92 | */ | |
aa353506 | 93 | public static @NonNull ITmfFilterTreeNode[] getSavedFilters() { |
d5efe032 | 94 | return fRoot.clone().getChildren(); |
be222f56 PT |
95 | } |
96 | ||
97 | /** | |
98 | * Set the passed filters as the currently saved ones. | |
99 | * | |
100 | * @param filters | |
101 | * The filters to save | |
102 | */ | |
103 | public static void setSavedFilters(ITmfFilterTreeNode[] filters) { | |
d5efe032 AF |
104 | fRoot = new TmfFilterRootNode(); |
105 | for (ITmfFilterTreeNode filter : filters) { | |
106 | fRoot.addChild(filter.clone()); | |
107 | } | |
108 | try { | |
109 | TmfFilterXMLWriter writerXML = new TmfFilterXMLWriter(fRoot); | |
110 | writerXML.saveTree(SAVED_FILTERS_PATH_NAME); | |
be222f56 PT |
111 | } catch (ParserConfigurationException e) { |
112 | Activator.getDefault().logError("Error saving filter xml file: " + SAVED_FILTERS_PATH_NAME, e); //$NON-NLS-1$ | |
113 | } | |
114 | } | |
115 | } |