1 /*******************************************************************************
2 * Copyright (c) 2010, 2015 Ericsson
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
10 * Patrick Tasse - Initial API and implementation
11 * Bernd Hufmann - Ensure backwards compatibility to Linux Tools
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.ui
.views
.filter
;
17 import java
.io
.FileNotFoundException
;
18 import java
.io
.IOException
;
20 import javax
.xml
.parsers
.ParserConfigurationException
;
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
;
31 * Central filter manager
34 * @author Patrick Tasse
36 public class FilterManager
{
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();
43 * Legacy path to the XML definitions file (in Linux Tools)
44 * TODO Remove once we feel the transition phase is over.
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
);
52 private static ITmfFilterTreeNode fRoot
= new TmfFilterRootNode();
55 File defaultFile
= new File(SAVED_FILTERS_PATH_NAME
);
59 * If there is no file at the expected location, check the legacy
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());
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$
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$
88 * Retrieve the currently saved filters
90 * @return The array of filters
92 public static ITmfFilterTreeNode
[] getSavedFilters() {
93 return fRoot
.clone().getChildren();
97 * Set the passed filters as the currently saved ones.
100 * The filters to save
102 public static void setSavedFilters(ITmfFilterTreeNode
[] filters
) {
103 fRoot
= new TmfFilterRootNode();
104 for (ITmfFilterTreeNode filter
: filters
) {
105 fRoot
.addChild(filter
.clone());
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$