lttng: Help stabilize some TimeGraphs tests
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / views / colors / ColorSettingsXML.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 - Updated to use RGB for the tick color
12 *******************************************************************************/
13
14 package org.eclipse.tracecompass.tmf.ui.views.colors;
15
16 import java.io.File;
17 import java.io.IOException;
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import javax.xml.parsers.DocumentBuilder;
22 import javax.xml.parsers.DocumentBuilderFactory;
23 import javax.xml.parsers.ParserConfigurationException;
24 import javax.xml.parsers.SAXParserFactory;
25 import javax.xml.transform.Transformer;
26 import javax.xml.transform.TransformerException;
27 import javax.xml.transform.TransformerFactory;
28 import javax.xml.transform.dom.DOMSource;
29 import javax.xml.transform.stream.StreamResult;
30
31 import org.eclipse.swt.graphics.RGB;
32 import org.eclipse.tracecompass.internal.tmf.ui.Activator;
33 import org.eclipse.tracecompass.tmf.core.filter.model.ITmfFilterTreeNode;
34 import org.eclipse.tracecompass.tmf.core.filter.xml.TmfFilterContentHandler;
35 import org.eclipse.tracecompass.tmf.core.filter.xml.TmfFilterXMLWriter;
36 import org.w3c.dom.Document;
37 import org.w3c.dom.Element;
38 import org.xml.sax.Attributes;
39 import org.xml.sax.SAXException;
40 import org.xml.sax.XMLReader;
41 import org.xml.sax.helpers.DefaultHandler;
42
43 /**
44 * Class for saving and loading of color settings to/from file.
45 *
46 * @version 1.0
47 * @author Patrick Tasse
48 *
49 */
50 public class ColorSettingsXML {
51
52 // XML Tags and attributes
53 private static final String COLOR_SETTINGS_TAG = "COLOR_SETTINGS"; //$NON-NLS-1$
54 private static final String COLOR_SETTING_TAG = "COLOR_SETTING"; //$NON-NLS-1$
55 private static final String FG_TAG = "FG"; //$NON-NLS-1$
56 private static final String BG_TAG = "BG"; //$NON-NLS-1$
57 private static final String R_ATTR = "R"; //$NON-NLS-1$
58 private static final String G_ATTR = "G"; //$NON-NLS-1$
59 private static final String B_ATTR = "B"; //$NON-NLS-1$
60 private static final String TICK_TAG = "TICK"; //$NON-NLS-1$
61 private static final String FILTER_TAG = "FILTER"; //$NON-NLS-1$
62
63 /**
64 * Saves the given color settings to file.
65 *
66 * @param pathName
67 * A file name with path
68 * @param colorSettings
69 * An array of color settings to save.
70 */
71 public static void save(String pathName, ColorSetting[] colorSettings) {
72 try {
73 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
74 DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
75 Document document = documentBuilder.newDocument();
76
77 Element rootElement = document.createElement(COLOR_SETTINGS_TAG);
78 document.appendChild(rootElement);
79
80 for (ColorSetting colorSetting : colorSettings) {
81 Element colorSettingElement = document.createElement(COLOR_SETTING_TAG);
82 rootElement.appendChild(colorSettingElement);
83
84 RGB foreground = colorSetting.getForegroundRGB();
85 if (foreground != null) {
86 Element fgElement = document.createElement(FG_TAG);
87 colorSettingElement.appendChild(fgElement);
88 setElementColor(fgElement, foreground);
89 }
90
91 RGB background = colorSetting.getBackgroundRGB();
92 if (background != null) {
93 Element bgElement = document.createElement(BG_TAG);
94 colorSettingElement.appendChild(bgElement);
95 setElementColor(bgElement, background);
96 }
97
98 Element tickColorElement = document.createElement(TICK_TAG);
99 colorSettingElement.appendChild(tickColorElement);
100 RGB tickColor = colorSetting.getTickColorRGB();
101 setElementColor(tickColorElement, tickColor);
102
103 ITmfFilterTreeNode filter = colorSetting.getFilter();
104 if (filter != null) {
105 Element filterElement = document.createElement(FILTER_TAG);
106 colorSettingElement.appendChild(filterElement);
107 TmfFilterXMLWriter.buildXMLTree(document, filter, filterElement);
108 }
109 }
110
111 TransformerFactory transformerFactory = TransformerFactory.newInstance();
112
113 Transformer transformer = transformerFactory.newTransformer();
114 DOMSource source = new DOMSource(document);
115 StreamResult result = new StreamResult(new File(pathName));
116 transformer.transform(source, result);
117 } catch (ParserConfigurationException | TransformerException e) {
118 Activator.getDefault().logError("Error saving color xml file: " + pathName, e); //$NON-NLS-1$
119 }
120 }
121
122 private static void setElementColor(Element element, RGB rgb) {
123 element.setAttribute(R_ATTR, Integer.toString(rgb.red));
124 element.setAttribute(G_ATTR, Integer.toString(rgb.green));
125 element.setAttribute(B_ATTR, Integer.toString(rgb.blue));
126 }
127
128 /**
129 * Loads color settings from file and returns it in an array.
130 *
131 * @param pathName
132 * A file name with path
133 *
134 * @return the color settings array loaded from file
135 */
136 public static ColorSetting[] load(String pathName) {
137 if (!new File(pathName).canRead()) {
138 return new ColorSetting[0];
139 }
140 SAXParserFactory parserFactory = SAXParserFactory.newInstance();
141 parserFactory.setNamespaceAware(true);
142
143 ColorSettingsContentHandler handler = new ColorSettingsContentHandler();
144 try {
145 XMLReader saxReader = parserFactory.newSAXParser().getXMLReader();
146 saxReader.setContentHandler(handler);
147 saxReader.parse(pathName);
148 return handler.colorSettings.toArray(new ColorSetting[0]);
149 } catch (ParserConfigurationException | SAXException | IOException e) {
150 Activator.getDefault().logError("Error loading color xml file: " + pathName, e); //$NON-NLS-1$
151 }
152 // In case of error, dispose the partial list of color settings
153 for (ColorSetting colorSetting : handler.colorSettings) {
154 colorSetting.dispose();
155 }
156 return new ColorSetting[0];
157 }
158
159 // Helper class
160 private static class ColorSettingsContentHandler extends DefaultHandler {
161
162 private List<ColorSetting> colorSettings = new ArrayList<>(0);
163 private RGB fg;
164 private RGB bg;
165 private RGB tickColor;
166 private ITmfFilterTreeNode filter;
167 private TmfFilterContentHandler filterContentHandler;
168
169 @Override
170 public void startElement(String uri, String localName, String qName, Attributes attributes)
171 throws SAXException {
172 switch (localName) {
173 case COLOR_SETTINGS_TAG:
174 colorSettings = new ArrayList<>();
175 break;
176 case COLOR_SETTING_TAG:
177 fg = null;
178 bg = null;
179 tickColor = null;
180 filter = null;
181 break;
182 case FG_TAG:
183 fg = getRGBfromAttributes(attributes);
184 break;
185 case BG_TAG:
186 bg = getRGBfromAttributes(attributes);
187 break;
188 case TICK_TAG:
189 tickColor = getRGBfromAttributes(attributes);
190 break;
191 default:
192 if (filterContentHandler != null) {
193 filterContentHandler.startElement(uri, localName, qName, attributes);
194 }
195 break;
196 case FILTER_TAG:
197 filterContentHandler = new TmfFilterContentHandler();
198 break;
199 }
200
201 }
202
203 private static RGB getRGBfromAttributes(Attributes attributes) {
204 int r = Integer.parseInt(attributes.getValue(R_ATTR));
205 int g = Integer.parseInt(attributes.getValue(G_ATTR));
206 int b = Integer.parseInt(attributes.getValue(B_ATTR));
207 return new RGB(r, g, b);
208 }
209
210 @Override
211 public void endElement(String uri, String localName, String qName)
212 throws SAXException {
213 if (localName.equals(COLOR_SETTINGS_TAG)) {
214 // Nothing to do
215 } else if (localName.equals(COLOR_SETTING_TAG)) {
216 ColorSetting colorSetting = new ColorSetting(fg, bg, tickColor, filter);
217 colorSettings.add(colorSetting);
218 } else if (localName.equals(FILTER_TAG)) {
219 filter = filterContentHandler.getTree();
220 filterContentHandler = null;
221 } else if (filterContentHandler != null) {
222 filterContentHandler.endElement(uri, localName, qName);
223 }
224 }
225
226 }
227 }
This page took 0.081825 seconds and 5 git commands to generate.