Re-structure LTTng sub-project as per the Linux Tools guidelines
[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
6c13869b
FC
30import org.eclipse.linuxtools.tmf.core.filter.model.ITmfFilterTreeNode;\r
31import org.eclipse.linuxtools.tmf.core.filter.xml.TmfFilterContentHandler;\r
32import org.eclipse.linuxtools.tmf.core.filter.xml.TmfFilterXMLWriter;\r
ca13a91c
FC
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
5d862cfc 115 ColorSettingsContentHandler handler = new ColorSettingsContentHandler();\r
ca13a91c
FC
116 try {\r
117 XMLReader saxReader = parserFactory.newSAXParser().getXMLReader();\r
ca13a91c
FC
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
5d862cfc
FC
128 // In case of error, dispose the partial list of color settings\r
129 for (ColorSetting colorSetting : handler.colorSettings) {\r
130 colorSetting.dispose();\r
131 }\r
ca13a91c
FC
132 return new ColorSetting[0];\r
133 }\r
134 \r
135 private static class ColorSettingsContentHandler extends DefaultHandler {\r
136\r
137 private ArrayList<ColorSetting> colorSettings = new ArrayList<ColorSetting>(0);\r
138 private RGB fg;\r
139 private RGB bg;\r
140 private int tickColorIndex;\r
141 private ITmfFilterTreeNode filter;\r
142 private TmfFilterContentHandler filterContentHandler;\r
143 \r
144 /* (non-Javadoc)\r
145 * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)\r
146 */\r
147 @Override\r
148 public void startElement(String uri, String localName, String qName, Attributes attributes)\r
149 throws SAXException {\r
150 if (localName.equals(COLOR_SETTINGS_TAG)) {\r
151 colorSettings = new ArrayList<ColorSetting>();\r
152 } else if (localName.equals(COLOR_SETTING_TAG)) {\r
153 fg = null;\r
154 bg = null;\r
155 filter = null;\r
156 } else if (localName.equals(FG_TAG)) {\r
157 int r = Integer.valueOf(attributes.getValue(R_ATTR));\r
158 int g = Integer.valueOf(attributes.getValue(G_ATTR));\r
159 int b = Integer.valueOf(attributes.getValue(B_ATTR));\r
160 fg = new RGB(r, g, b);\r
161 } else if (localName.equals(BG_TAG)) {\r
162 int r = Integer.valueOf(attributes.getValue(R_ATTR));\r
163 int g = Integer.valueOf(attributes.getValue(G_ATTR));\r
164 int b = Integer.valueOf(attributes.getValue(B_ATTR));\r
165 bg = new RGB(r, g, b);\r
166 } else if (localName.equals(TICK_COLOR_TAG)) {\r
167 int index = Integer.valueOf(attributes.getValue(INDEX_ATTR));\r
168 tickColorIndex = index;\r
169 } else if (localName.equals(FILTER_TAG)) {\r
170 filterContentHandler = new TmfFilterContentHandler();\r
171 } else if (filterContentHandler != null) {\r
172 filterContentHandler.startElement(uri, localName, qName, attributes);\r
173 }\r
174 }\r
175 \r
176 /* (non-Javadoc)\r
177 * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)\r
178 */\r
179 @Override\r
180 public void endElement(String uri, String localName, String qName)\r
181 throws SAXException {\r
182 if (localName.equals(COLOR_SETTINGS_TAG)) {\r
183 } else if (localName.equals(COLOR_SETTING_TAG)) {\r
184 ColorSetting colorSetting = new ColorSetting(fg, bg, tickColorIndex, filter);\r
185 colorSettings.add(colorSetting);\r
186 } else if (localName.equals(FILTER_TAG)) {\r
187 filter = filterContentHandler.getTree();\r
188 filterContentHandler = null;\r
189 } else if (filterContentHandler != null) {\r
190 filterContentHandler.endElement(uri, localName, qName);\r
191 }\r
192 }\r
193\r
194 }\r
195}\r
This page took 0.033243 seconds and 5 git commands to generate.