1 /*******************************************************************************
2 * Copyright (c) 2010 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 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.ui
.views
.colors
;
16 import java
.io
.IOException
;
17 import java
.util
.ArrayList
;
19 import javax
.xml
.parsers
.DocumentBuilder
;
20 import javax
.xml
.parsers
.DocumentBuilderFactory
;
21 import javax
.xml
.parsers
.ParserConfigurationException
;
22 import javax
.xml
.parsers
.SAXParserFactory
;
23 import javax
.xml
.transform
.Transformer
;
24 import javax
.xml
.transform
.TransformerConfigurationException
;
25 import javax
.xml
.transform
.TransformerException
;
26 import javax
.xml
.transform
.TransformerFactory
;
27 import javax
.xml
.transform
.dom
.DOMSource
;
28 import javax
.xml
.transform
.stream
.StreamResult
;
30 import org
.eclipse
.linuxtools
.tmf
.filter
.model
.ITmfFilterTreeNode
;
31 import org
.eclipse
.linuxtools
.tmf
.filter
.xml
.TmfFilterContentHandler
;
32 import org
.eclipse
.linuxtools
.tmf
.filter
.xml
.TmfFilterXMLWriter
;
33 import org
.eclipse
.swt
.graphics
.RGB
;
34 import org
.w3c
.dom
.Document
;
35 import org
.w3c
.dom
.Element
;
36 import org
.xml
.sax
.Attributes
;
37 import org
.xml
.sax
.SAXException
;
38 import org
.xml
.sax
.XMLReader
;
39 import org
.xml
.sax
.helpers
.DefaultHandler
;
41 public class ColorSettingsXML
{
43 private static final String COLOR_SETTINGS_TAG
= "COLOR_SETTINGS"; //$NON-NLS-1$
44 private static final String COLOR_SETTING_TAG
= "COLOR_SETTING"; //$NON-NLS-1$
45 private static final String FG_TAG
= "FG"; //$NON-NLS-1$
46 private static final String BG_TAG
= "BG"; //$NON-NLS-1$
47 private static final String R_ATTR
= "R"; //$NON-NLS-1$
48 private static final String G_ATTR
= "G"; //$NON-NLS-1$
49 private static final String B_ATTR
= "B"; //$NON-NLS-1$
50 private static final String TICK_COLOR_TAG
= "TICK_COLOR"; //$NON-NLS-1$
51 private static final String INDEX_ATTR
= "INDEX"; //$NON-NLS-1$
52 private static final String FILTER_TAG
= "FILTER"; //$NON-NLS-1$
54 public static void save(String pathName
, ColorSetting
[] colorSettings
) {
56 DocumentBuilderFactory documentBuilderFactory
= DocumentBuilderFactory
.newInstance();
57 DocumentBuilder documentBuilder
= documentBuilderFactory
.newDocumentBuilder();
58 Document document
= documentBuilder
.newDocument();
60 Element rootElement
= document
.createElement(COLOR_SETTINGS_TAG
);
61 document
.appendChild(rootElement
);
63 for (ColorSetting colorSetting
: colorSettings
) {
64 Element colorSettingElement
= document
.createElement(COLOR_SETTING_TAG
);
65 rootElement
.appendChild(colorSettingElement
);
67 Element fgElement
= document
.createElement(FG_TAG
);
68 colorSettingElement
.appendChild(fgElement
);
69 RGB foreground
= colorSetting
.getForegroundRGB();
70 fgElement
.setAttribute(R_ATTR
, Integer
.toString(foreground
.red
));
71 fgElement
.setAttribute(G_ATTR
, Integer
.toString(foreground
.green
));
72 fgElement
.setAttribute(B_ATTR
, Integer
.toString(foreground
.blue
));
74 Element bgElement
= document
.createElement(BG_TAG
);
75 colorSettingElement
.appendChild(bgElement
);
76 RGB background
= colorSetting
.getBackgroundRGB();
77 bgElement
.setAttribute(R_ATTR
, Integer
.toString(background
.red
));
78 bgElement
.setAttribute(G_ATTR
, Integer
.toString(background
.green
));
79 bgElement
.setAttribute(B_ATTR
, Integer
.toString(background
.blue
));
81 Element tickColorElement
= document
.createElement(TICK_COLOR_TAG
);
82 colorSettingElement
.appendChild(tickColorElement
);
83 int index
= colorSetting
.getTickColorIndex();
84 tickColorElement
.setAttribute(INDEX_ATTR
, Integer
.toString(index
));
86 if (colorSetting
.getFilter() != null) {
87 Element filterElement
= document
.createElement(FILTER_TAG
);
88 colorSettingElement
.appendChild(filterElement
);
89 TmfFilterXMLWriter
.buildXMLTree(document
, colorSetting
.getFilter(), filterElement
);
93 TransformerFactory transformerFactory
= TransformerFactory
.newInstance();
95 Transformer transformer
= transformerFactory
.newTransformer();
96 DOMSource source
= new DOMSource(document
);
97 StreamResult result
= new StreamResult(new File(pathName
));
98 transformer
.transform(source
, result
);
99 } catch (ParserConfigurationException e
) {
101 } catch (TransformerConfigurationException e
) {
103 } catch (TransformerException e
) {
108 public static ColorSetting
[] load(String pathName
) {
109 if (! new File(pathName
).canRead()) {
110 return new ColorSetting
[0];
112 SAXParserFactory parserFactory
= SAXParserFactory
.newInstance();
113 parserFactory
.setNamespaceAware(true);
115 ColorSettingsContentHandler handler
= new ColorSettingsContentHandler();
117 XMLReader saxReader
= parserFactory
.newSAXParser().getXMLReader();
118 saxReader
.setContentHandler(handler
);
119 saxReader
.parse(pathName
);
120 return handler
.colorSettings
.toArray(new ColorSetting
[0]);
121 } catch (ParserConfigurationException e
) {
123 } catch (SAXException e
) {
125 } catch (IOException e
) {
128 // In case of error, dispose the partial list of color settings
129 for (ColorSetting colorSetting
: handler
.colorSettings
) {
130 colorSetting
.dispose();
132 return new ColorSetting
[0];
135 private static class ColorSettingsContentHandler
extends DefaultHandler
{
137 private ArrayList
<ColorSetting
> colorSettings
= new ArrayList
<ColorSetting
>(0);
140 private int tickColorIndex
;
141 private ITmfFilterTreeNode filter
;
142 private TmfFilterContentHandler filterContentHandler
;
145 * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
148 public void startElement(String uri
, String localName
, String qName
, Attributes attributes
)
149 throws SAXException
{
150 if (localName
.equals(COLOR_SETTINGS_TAG
)) {
151 colorSettings
= new ArrayList
<ColorSetting
>();
152 } else if (localName
.equals(COLOR_SETTING_TAG
)) {
156 } else if (localName
.equals(FG_TAG
)) {
157 int r
= Integer
.valueOf(attributes
.getValue(R_ATTR
));
158 int g
= Integer
.valueOf(attributes
.getValue(G_ATTR
));
159 int b
= Integer
.valueOf(attributes
.getValue(B_ATTR
));
160 fg
= new RGB(r
, g
, b
);
161 } else if (localName
.equals(BG_TAG
)) {
162 int r
= Integer
.valueOf(attributes
.getValue(R_ATTR
));
163 int g
= Integer
.valueOf(attributes
.getValue(G_ATTR
));
164 int b
= Integer
.valueOf(attributes
.getValue(B_ATTR
));
165 bg
= new RGB(r
, g
, b
);
166 } else if (localName
.equals(TICK_COLOR_TAG
)) {
167 int index
= Integer
.valueOf(attributes
.getValue(INDEX_ATTR
));
168 tickColorIndex
= index
;
169 } else if (localName
.equals(FILTER_TAG
)) {
170 filterContentHandler
= new TmfFilterContentHandler();
171 } else if (filterContentHandler
!= null) {
172 filterContentHandler
.startElement(uri
, localName
, qName
, attributes
);
177 * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
180 public void endElement(String uri
, String localName
, String qName
)
181 throws SAXException
{
182 if (localName
.equals(COLOR_SETTINGS_TAG
)) {
183 } else if (localName
.equals(COLOR_SETTING_TAG
)) {
184 ColorSetting colorSetting
= new ColorSetting(fg
, bg
, tickColorIndex
, filter
);
185 colorSettings
.add(colorSetting
);
186 } else if (localName
.equals(FILTER_TAG
)) {
187 filter
= filterContentHandler
.getTree();
188 filterContentHandler
= null;
189 } else if (filterContentHandler
!= null) {
190 filterContentHandler
.endElement(uri
, localName
, qName
);