Fix for Bug338155
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / colors / ColorSettingsXML.java
CommitLineData
ca13a91c
FC
1/*******************************************************************************\r
2 * Copyright (c) 2010 Ericsson\r
3 * \r
4 * All rights reserved. This program and the accompanying materials are\r
5 * made available under the terms of the Eclipse Public License v1.0 which\r
6 * accompanies this distribution, and is available at\r
7 * http://www.eclipse.org/legal/epl-v10.html\r
8 * \r
9 * Contributors:\r
10 * Patrick Tasse - Initial API and implementation\r
11 *******************************************************************************/\r
12\r
13package org.eclipse.linuxtools.tmf.ui.views.colors;\r
14\r
15import java.io.File;\r
16import java.io.IOException;\r
17import java.util.ArrayList;\r
18\r
19import javax.xml.parsers.DocumentBuilder;\r
20import javax.xml.parsers.DocumentBuilderFactory;\r
21import javax.xml.parsers.ParserConfigurationException;\r
22import javax.xml.parsers.SAXParserFactory;\r
23import javax.xml.transform.Transformer;\r
24import javax.xml.transform.TransformerConfigurationException;\r
25import javax.xml.transform.TransformerException;\r
26import javax.xml.transform.TransformerFactory;\r
27import javax.xml.transform.dom.DOMSource;\r
28import javax.xml.transform.stream.StreamResult;\r
29\r
30import org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode;\r
31import org.eclipse.linuxtools.tmf.filter.xml.TmfFilterContentHandler;\r
32import org.eclipse.linuxtools.tmf.filter.xml.TmfFilterXMLWriter;\r
33import org.eclipse.swt.graphics.RGB;\r
34import org.w3c.dom.Document;\r
35import org.w3c.dom.Element;\r
36import org.xml.sax.Attributes;\r
37import org.xml.sax.SAXException;\r
38import org.xml.sax.XMLReader;\r
39import org.xml.sax.helpers.DefaultHandler;\r
40\r
41public class ColorSettingsXML {\r
42\r
43 private static final String COLOR_SETTINGS_TAG = "COLOR_SETTINGS"; //$NON-NLS-1$\r
44 private static final String COLOR_SETTING_TAG = "COLOR_SETTING"; //$NON-NLS-1$\r
45 private static final String FG_TAG = "FG"; //$NON-NLS-1$\r
46 private static final String BG_TAG = "BG"; //$NON-NLS-1$\r
47 private static final String R_ATTR = "R"; //$NON-NLS-1$\r
48 private static final String G_ATTR = "G"; //$NON-NLS-1$\r
49 private static final String B_ATTR = "B"; //$NON-NLS-1$\r
50 private static final String TICK_COLOR_TAG = "TICK_COLOR"; //$NON-NLS-1$\r
51 private static final String INDEX_ATTR = "INDEX"; //$NON-NLS-1$\r
52 private static final String FILTER_TAG = "FILTER"; //$NON-NLS-1$\r
53\r
54 public static void save(String pathName, ColorSetting[] colorSettings) {\r
55 try {\r
56 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();\r
57 DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();\r
58 Document document = documentBuilder.newDocument();\r
59 \r
60 Element rootElement = document.createElement(COLOR_SETTINGS_TAG);\r
61 document.appendChild(rootElement);\r
62 \r
63 for (ColorSetting colorSetting : colorSettings) {\r
64 Element colorSettingElement = document.createElement(COLOR_SETTING_TAG);\r
65 rootElement.appendChild(colorSettingElement);\r
66 \r
67 Element fgElement = document.createElement(FG_TAG);\r
68 colorSettingElement.appendChild(fgElement);\r
69 RGB foreground = colorSetting.getForegroundRGB();\r
70 fgElement.setAttribute(R_ATTR, Integer.toString(foreground.red));\r
71 fgElement.setAttribute(G_ATTR, Integer.toString(foreground.green));\r
72 fgElement.setAttribute(B_ATTR, Integer.toString(foreground.blue));\r
73 \r
74 Element bgElement = document.createElement(BG_TAG);\r
75 colorSettingElement.appendChild(bgElement);\r
76 RGB background = colorSetting.getBackgroundRGB();\r
77 bgElement.setAttribute(R_ATTR, Integer.toString(background.red));\r
78 bgElement.setAttribute(G_ATTR, Integer.toString(background.green));\r
79 bgElement.setAttribute(B_ATTR, Integer.toString(background.blue));\r
80 \r
81 Element tickColorElement = document.createElement(TICK_COLOR_TAG);\r
82 colorSettingElement.appendChild(tickColorElement);\r
83 int index = colorSetting.getTickColorIndex();\r
84 tickColorElement.setAttribute(INDEX_ATTR, Integer.toString(index));\r
85 \r
86 if (colorSetting.getFilter() != null) {\r
87 Element filterElement = document.createElement(FILTER_TAG);\r
88 colorSettingElement.appendChild(filterElement);\r
89 TmfFilterXMLWriter.buildXMLTree(document, colorSetting.getFilter(), filterElement);\r
90 }\r
91 }\r
92 \r
93 TransformerFactory transformerFactory = TransformerFactory.newInstance();\r
94 \r
95 Transformer transformer = transformerFactory.newTransformer();\r
96 DOMSource source = new DOMSource(document);\r
97 StreamResult result = new StreamResult(new File(pathName));\r
98 transformer.transform(source, result);\r
99 } catch (ParserConfigurationException e) {\r
100 e.printStackTrace();\r
101 } catch (TransformerConfigurationException e) {\r
102 e.printStackTrace();\r
103 } catch (TransformerException e) {\r
104 e.printStackTrace();\r
105 }\r
106 }\r
107 \r
108 public static ColorSetting[] load(String pathName) {\r
109 if (! new File(pathName).canRead()) {\r
110 return new ColorSetting[0];\r
111 }\r
112 SAXParserFactory parserFactory = SAXParserFactory.newInstance(); \r
113 parserFactory.setNamespaceAware(true); \r
114\r
115 try {\r
116 XMLReader saxReader = parserFactory.newSAXParser().getXMLReader();\r
117 ColorSettingsContentHandler handler = new ColorSettingsContentHandler();\r
118 saxReader.setContentHandler(handler);\r
119 saxReader.parse(pathName);\r
120 return handler.colorSettings.toArray(new ColorSetting[0]);\r
121 } catch (ParserConfigurationException e) {\r
122 e.printStackTrace();\r
123 } catch (SAXException e) {\r
124 e.printStackTrace();\r
125 } catch (IOException e) {\r
126 e.printStackTrace();\r
127 }\r
128 return new ColorSetting[0];\r
129 }\r
130 \r
131 private static class ColorSettingsContentHandler extends DefaultHandler {\r
132\r
133 private ArrayList<ColorSetting> colorSettings = new ArrayList<ColorSetting>(0);\r
134 private RGB fg;\r
135 private RGB bg;\r
136 private int tickColorIndex;\r
137 private ITmfFilterTreeNode filter;\r
138 private TmfFilterContentHandler filterContentHandler;\r
139 \r
140 /* (non-Javadoc)\r
141 * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)\r
142 */\r
143 @Override\r
144 public void startElement(String uri, String localName, String qName, Attributes attributes)\r
145 throws SAXException {\r
146 if (localName.equals(COLOR_SETTINGS_TAG)) {\r
147 colorSettings = new ArrayList<ColorSetting>();\r
148 } else if (localName.equals(COLOR_SETTING_TAG)) {\r
149 fg = null;\r
150 bg = null;\r
151 filter = null;\r
152 } else if (localName.equals(FG_TAG)) {\r
153 int r = Integer.valueOf(attributes.getValue(R_ATTR));\r
154 int g = Integer.valueOf(attributes.getValue(G_ATTR));\r
155 int b = Integer.valueOf(attributes.getValue(B_ATTR));\r
156 fg = new RGB(r, g, b);\r
157 } else if (localName.equals(BG_TAG)) {\r
158 int r = Integer.valueOf(attributes.getValue(R_ATTR));\r
159 int g = Integer.valueOf(attributes.getValue(G_ATTR));\r
160 int b = Integer.valueOf(attributes.getValue(B_ATTR));\r
161 bg = new RGB(r, g, b);\r
162 } else if (localName.equals(TICK_COLOR_TAG)) {\r
163 int index = Integer.valueOf(attributes.getValue(INDEX_ATTR));\r
164 tickColorIndex = index;\r
165 } else if (localName.equals(FILTER_TAG)) {\r
166 filterContentHandler = new TmfFilterContentHandler();\r
167 } else if (filterContentHandler != null) {\r
168 filterContentHandler.startElement(uri, localName, qName, attributes);\r
169 }\r
170 }\r
171 \r
172 /* (non-Javadoc)\r
173 * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)\r
174 */\r
175 @Override\r
176 public void endElement(String uri, String localName, String qName)\r
177 throws SAXException {\r
178 if (localName.equals(COLOR_SETTINGS_TAG)) {\r
179 } else if (localName.equals(COLOR_SETTING_TAG)) {\r
180 ColorSetting colorSetting = new ColorSetting(fg, bg, tickColorIndex, filter);\r
181 colorSettings.add(colorSetting);\r
182 } else if (localName.equals(FILTER_TAG)) {\r
183 filter = filterContentHandler.getTree();\r
184 filterContentHandler = null;\r
185 } else if (filterContentHandler != null) {\r
186 filterContentHandler.endElement(uri, localName, qName);\r
187 }\r
188 }\r
189\r
190 }\r
191}\r
This page took 0.031454 seconds and 5 git commands to generate.