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