1 /*******************************************************************************
2 * Copyright (c) 2010, 2014 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
.linuxtools
.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
.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
;
32 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.filter
.model
.ITmfFilterTreeNode
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.filter
.xml
.TmfFilterContentHandler
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.filter
.xml
.TmfFilterXMLWriter
;
36 import org
.eclipse
.swt
.graphics
.RGB
;
37 import org
.w3c
.dom
.Document
;
38 import org
.w3c
.dom
.Element
;
39 import org
.xml
.sax
.Attributes
;
40 import org
.xml
.sax
.SAXException
;
41 import org
.xml
.sax
.XMLReader
;
42 import org
.xml
.sax
.helpers
.DefaultHandler
;
45 * Class for saving and loading of color settings to/from file.
48 * @author Patrick Tasse
51 public class ColorSettingsXML
{
53 // XML Tags and attributes
54 private static final String COLOR_SETTINGS_TAG
= "COLOR_SETTINGS"; //$NON-NLS-1$
55 private static final String COLOR_SETTING_TAG
= "COLOR_SETTING"; //$NON-NLS-1$
56 private static final String FG_TAG
= "FG"; //$NON-NLS-1$
57 private static final String BG_TAG
= "BG"; //$NON-NLS-1$
58 private static final String R_ATTR
= "R"; //$NON-NLS-1$
59 private static final String G_ATTR
= "G"; //$NON-NLS-1$
60 private static final String B_ATTR
= "B"; //$NON-NLS-1$
61 private static final String TICK_TAG
= "TICK"; //$NON-NLS-1$
62 private static final String FILTER_TAG
= "FILTER"; //$NON-NLS-1$
65 * Saves the given color settings to file.
68 * A file name with path
69 * @param colorSettings
70 * -An array of color settings to save.
72 public static void save(String pathName
, ColorSetting
[] colorSettings
) {
74 DocumentBuilderFactory documentBuilderFactory
= DocumentBuilderFactory
.newInstance();
75 DocumentBuilder documentBuilder
= documentBuilderFactory
.newDocumentBuilder();
76 Document document
= documentBuilder
.newDocument();
78 Element rootElement
= document
.createElement(COLOR_SETTINGS_TAG
);
79 document
.appendChild(rootElement
);
81 for (ColorSetting colorSetting
: colorSettings
) {
82 Element colorSettingElement
= document
.createElement(COLOR_SETTING_TAG
);
83 rootElement
.appendChild(colorSettingElement
);
85 Element fgElement
= document
.createElement(FG_TAG
);
86 colorSettingElement
.appendChild(fgElement
);
87 RGB foreground
= colorSetting
.getForegroundRGB();
88 fgElement
.setAttribute(R_ATTR
, Integer
.toString(foreground
.red
));
89 fgElement
.setAttribute(G_ATTR
, Integer
.toString(foreground
.green
));
90 fgElement
.setAttribute(B_ATTR
, Integer
.toString(foreground
.blue
));
92 Element bgElement
= document
.createElement(BG_TAG
);
93 colorSettingElement
.appendChild(bgElement
);
94 RGB background
= colorSetting
.getBackgroundRGB();
95 bgElement
.setAttribute(R_ATTR
, Integer
.toString(background
.red
));
96 bgElement
.setAttribute(G_ATTR
, Integer
.toString(background
.green
));
97 bgElement
.setAttribute(B_ATTR
, Integer
.toString(background
.blue
));
99 Element tickColorElement
= document
.createElement(TICK_TAG
);
100 colorSettingElement
.appendChild(tickColorElement
);
101 RGB tickColor
= colorSetting
.getTickColorRGB();
102 tickColorElement
.setAttribute(R_ATTR
, Integer
.toString(tickColor
.red
));
103 tickColorElement
.setAttribute(G_ATTR
, Integer
.toString(tickColor
.green
));
104 tickColorElement
.setAttribute(B_ATTR
, Integer
.toString(tickColor
.blue
));
106 if (colorSetting
.getFilter() != null) {
107 Element filterElement
= document
.createElement(FILTER_TAG
);
108 colorSettingElement
.appendChild(filterElement
);
109 TmfFilterXMLWriter
.buildXMLTree(document
, colorSetting
.getFilter(), filterElement
);
113 TransformerFactory transformerFactory
= TransformerFactory
.newInstance();
115 Transformer transformer
= transformerFactory
.newTransformer();
116 DOMSource source
= new DOMSource(document
);
117 StreamResult result
= new StreamResult(new File(pathName
));
118 transformer
.transform(source
, result
);
119 } catch (ParserConfigurationException e
) {
120 Activator
.getDefault().logError("Error saving color xml file: " + pathName
, e
); //$NON-NLS-1$
121 } catch (TransformerConfigurationException e
) {
122 Activator
.getDefault().logError("Error saving color xml file: " + pathName
, e
); //$NON-NLS-1$
123 } catch (TransformerException e
) {
124 Activator
.getDefault().logError("Error saving color xml file: " + pathName
, e
); //$NON-NLS-1$
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 e
) {
150 Activator
.getDefault().logError("Error loading color xml file: " + pathName
, e
); //$NON-NLS-1$
151 } catch (SAXException e
) {
152 Activator
.getDefault().logError("Error loading color xml file: " + pathName
, e
); //$NON-NLS-1$
153 } catch (IOException e
) {
154 Activator
.getDefault().logError("Error loading color xml file: " + pathName
, e
); //$NON-NLS-1$
156 // In case of error, dispose the partial list of color settings
157 for (ColorSetting colorSetting
: handler
.colorSettings
) {
158 colorSetting
.dispose();
160 return new ColorSetting
[0];
164 private static class ColorSettingsContentHandler
extends DefaultHandler
{
166 private List
<ColorSetting
> colorSettings
= new ArrayList
<>(0);
167 private RGB fg
= new RGB(0, 0, 0);
168 private RGB bg
= new RGB(255, 255, 255);
169 private RGB tickColor
= new RGB(0, 0, 0);
170 private ITmfFilterTreeNode filter
;
171 private TmfFilterContentHandler filterContentHandler
;
174 public void startElement(String uri
, String localName
, String qName
, Attributes attributes
)
175 throws SAXException
{
176 if (localName
.equals(COLOR_SETTINGS_TAG
)) {
177 colorSettings
= new ArrayList
<>();
178 } else if (localName
.equals(COLOR_SETTING_TAG
)) {
182 } else if (localName
.equals(FG_TAG
)) {
183 int r
= Integer
.parseInt(attributes
.getValue(R_ATTR
));
184 int g
= Integer
.parseInt(attributes
.getValue(G_ATTR
));
185 int b
= Integer
.parseInt(attributes
.getValue(B_ATTR
));
186 fg
= new RGB(r
, g
, b
);
187 } else if (localName
.equals(BG_TAG
)) {
188 int r
= Integer
.parseInt(attributes
.getValue(R_ATTR
));
189 int g
= Integer
.parseInt(attributes
.getValue(G_ATTR
));
190 int b
= Integer
.parseInt(attributes
.getValue(B_ATTR
));
191 bg
= new RGB(r
, g
, b
);
192 } else if (localName
.equals(TICK_TAG
)) {
193 int r
= Integer
.parseInt(attributes
.getValue(R_ATTR
));
194 int g
= Integer
.parseInt(attributes
.getValue(G_ATTR
));
195 int b
= Integer
.parseInt(attributes
.getValue(B_ATTR
));
196 tickColor
= new RGB(r
, g
, b
);
197 } else if (localName
.equals(FILTER_TAG
)) {
198 filterContentHandler
= new TmfFilterContentHandler();
199 } else if (filterContentHandler
!= null) {
200 filterContentHandler
.startElement(uri
, localName
, qName
, attributes
);
205 public void endElement(String uri
, String localName
, String qName
)
206 throws SAXException
{
207 if (localName
.equals(COLOR_SETTINGS_TAG
)) {
209 } else if (localName
.equals(COLOR_SETTING_TAG
)) {
210 ColorSetting colorSetting
= new ColorSetting(fg
, bg
, tickColor
, filter
);
211 colorSettings
.add(colorSetting
);
212 } else if (localName
.equals(FILTER_TAG
)) {
213 filter
= filterContentHandler
.getTree();
214 filterContentHandler
= null;
215 } else if (filterContentHandler
!= null) {
216 filterContentHandler
.endElement(uri
, localName
, qName
);