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 - Updated to use RGB for the tick color
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.ui
.views
.colors
;
17 import java
.io
.IOException
;
18 import java
.util
.ArrayList
;
19 import java
.util
.List
;
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
;
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
;
44 * Class for saving and loading of color settings to/from file.
47 * @author Patrick Tasse
50 public class ColorSettingsXML
{
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$
64 * Saves the given color settings to file.
67 * A file name with path
68 * @param colorSettings
69 * An array of color settings to save.
71 public static void save(String pathName
, ColorSetting
[] colorSettings
) {
73 DocumentBuilderFactory documentBuilderFactory
= DocumentBuilderFactory
.newInstance();
74 DocumentBuilder documentBuilder
= documentBuilderFactory
.newDocumentBuilder();
75 Document document
= documentBuilder
.newDocument();
77 Element rootElement
= document
.createElement(COLOR_SETTINGS_TAG
);
78 document
.appendChild(rootElement
);
80 for (ColorSetting colorSetting
: colorSettings
) {
81 Element colorSettingElement
= document
.createElement(COLOR_SETTING_TAG
);
82 rootElement
.appendChild(colorSettingElement
);
84 RGB foreground
= colorSetting
.getForegroundRGB();
85 if (foreground
!= null) {
86 Element fgElement
= document
.createElement(FG_TAG
);
87 colorSettingElement
.appendChild(fgElement
);
88 setElementColor(fgElement
, foreground
);
91 RGB background
= colorSetting
.getBackgroundRGB();
92 if (background
!= null) {
93 Element bgElement
= document
.createElement(BG_TAG
);
94 colorSettingElement
.appendChild(bgElement
);
95 setElementColor(bgElement
, background
);
98 Element tickColorElement
= document
.createElement(TICK_TAG
);
99 colorSettingElement
.appendChild(tickColorElement
);
100 RGB tickColor
= colorSetting
.getTickColorRGB();
101 setElementColor(tickColorElement
, tickColor
);
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
);
111 TransformerFactory transformerFactory
= TransformerFactory
.newInstance();
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$
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
));
129 * Loads color settings from file and returns it in an array.
132 * A file name with path
134 * @return the color settings array loaded from file
136 public static ColorSetting
[] load(String pathName
) {
137 if (!new File(pathName
).canRead()) {
138 return new ColorSetting
[0];
140 SAXParserFactory parserFactory
= SAXParserFactory
.newInstance();
141 parserFactory
.setNamespaceAware(true);
143 ColorSettingsContentHandler handler
= new ColorSettingsContentHandler();
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$
152 // In case of error, dispose the partial list of color settings
153 for (ColorSetting colorSetting
: handler
.colorSettings
) {
154 colorSetting
.dispose();
156 return new ColorSetting
[0];
160 private static class ColorSettingsContentHandler
extends DefaultHandler
{
162 private List
<ColorSetting
> colorSettings
= new ArrayList
<>(0);
165 private RGB tickColor
;
166 private ITmfFilterTreeNode filter
;
167 private TmfFilterContentHandler filterContentHandler
;
170 public void startElement(String uri
, String localName
, String qName
, Attributes attributes
)
171 throws SAXException
{
173 case COLOR_SETTINGS_TAG
:
174 colorSettings
= new ArrayList
<>();
176 case COLOR_SETTING_TAG
:
183 fg
= getRGBfromAttributes(attributes
);
186 bg
= getRGBfromAttributes(attributes
);
189 tickColor
= getRGBfromAttributes(attributes
);
192 if (filterContentHandler
!= null) {
193 filterContentHandler
.startElement(uri
, localName
, qName
, attributes
);
197 filterContentHandler
= new TmfFilterContentHandler();
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
);
211 public void endElement(String uri
, String localName
, String qName
)
212 throws SAXException
{
213 if (localName
.equals(COLOR_SETTINGS_TAG
)) {
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
);