Fix some null warnings
[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.jdt.annotation.NonNull;
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;
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
39 private static final String SAVED_FILTERS_FILE_NAME = "saved_filters.xml"; //$NON-NLS-1$
40 private static final String SAVED_FILTERS_PATH_NAME =
41 Activator.getDefault().getStateLocation().addTrailingSeparator().append(SAVED_FILTERS_FILE_NAME).toString();
42
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
53 private static ITmfFilterTreeNode fRoot = new TmfFilterRootNode();
54 static {
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
77 try {
78 // Now load the filters from the current location
79 fRoot = new TmfFilterXMLParser(SAVED_FILTERS_PATH_NAME).getTree();
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 */
93 public static @NonNull ITmfFilterTreeNode[] getSavedFilters() {
94 return fRoot.clone().getChildren();
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) {
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);
111 } catch (ParserConfigurationException e) {
112 Activator.getDefault().logError("Error saving filter xml file: " + SAVED_FILTERS_PATH_NAME, e); //$NON-NLS-1$
113 }
114 }
115 }
This page took 0.034271 seconds and 5 git commands to generate.