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