Internalize some classes and fix a pile of warnings
[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
9fa32496 18import java.util.List;\r
ca13a91c
FC
19\r
20import javax.xml.parsers.DocumentBuilder;\r
21import javax.xml.parsers.DocumentBuilderFactory;\r
22import javax.xml.parsers.ParserConfigurationException;\r
23import javax.xml.parsers.SAXParserFactory;\r
24import javax.xml.transform.Transformer;\r
25import javax.xml.transform.TransformerConfigurationException;\r
26import javax.xml.transform.TransformerException;\r
27import javax.xml.transform.TransformerFactory;\r
28import javax.xml.transform.dom.DOMSource;\r
29import javax.xml.transform.stream.StreamResult;\r
30\r
8fd82db5 31import org.eclipse.linuxtools.internal.tmf.ui.Activator;\r
6c13869b
FC
32import org.eclipse.linuxtools.tmf.core.filter.model.ITmfFilterTreeNode;\r
33import org.eclipse.linuxtools.tmf.core.filter.xml.TmfFilterContentHandler;\r
34import org.eclipse.linuxtools.tmf.core.filter.xml.TmfFilterXMLWriter;\r
ca13a91c
FC
35import org.eclipse.swt.graphics.RGB;\r
36import org.w3c.dom.Document;\r
37import org.w3c.dom.Element;\r
38import org.xml.sax.Attributes;\r
39import org.xml.sax.SAXException;\r
40import org.xml.sax.XMLReader;\r
41import org.xml.sax.helpers.DefaultHandler;\r
42\r
43public class ColorSettingsXML {\r
44\r
45 private static final String COLOR_SETTINGS_TAG = "COLOR_SETTINGS"; //$NON-NLS-1$\r
46 private static final String COLOR_SETTING_TAG = "COLOR_SETTING"; //$NON-NLS-1$\r
47 private static final String FG_TAG = "FG"; //$NON-NLS-1$\r
48 private static final String BG_TAG = "BG"; //$NON-NLS-1$\r
49 private static final String R_ATTR = "R"; //$NON-NLS-1$\r
50 private static final String G_ATTR = "G"; //$NON-NLS-1$\r
51 private static final String B_ATTR = "B"; //$NON-NLS-1$\r
52 private static final String TICK_COLOR_TAG = "TICK_COLOR"; //$NON-NLS-1$\r
53 private static final String INDEX_ATTR = "INDEX"; //$NON-NLS-1$\r
54 private static final String FILTER_TAG = "FILTER"; //$NON-NLS-1$\r
55\r
56 public static void save(String pathName, ColorSetting[] colorSettings) {\r
57 try {\r
58 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();\r
59 DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();\r
60 Document document = documentBuilder.newDocument();\r
61 \r
62 Element rootElement = document.createElement(COLOR_SETTINGS_TAG);\r
63 document.appendChild(rootElement);\r
64 \r
65 for (ColorSetting colorSetting : colorSettings) {\r
66 Element colorSettingElement = document.createElement(COLOR_SETTING_TAG);\r
67 rootElement.appendChild(colorSettingElement);\r
68 \r
69 Element fgElement = document.createElement(FG_TAG);\r
70 colorSettingElement.appendChild(fgElement);\r
71 RGB foreground = colorSetting.getForegroundRGB();\r
72 fgElement.setAttribute(R_ATTR, Integer.toString(foreground.red));\r
73 fgElement.setAttribute(G_ATTR, Integer.toString(foreground.green));\r
74 fgElement.setAttribute(B_ATTR, Integer.toString(foreground.blue));\r
75 \r
76 Element bgElement = document.createElement(BG_TAG);\r
77 colorSettingElement.appendChild(bgElement);\r
78 RGB background = colorSetting.getBackgroundRGB();\r
79 bgElement.setAttribute(R_ATTR, Integer.toString(background.red));\r
80 bgElement.setAttribute(G_ATTR, Integer.toString(background.green));\r
81 bgElement.setAttribute(B_ATTR, Integer.toString(background.blue));\r
82 \r
83 Element tickColorElement = document.createElement(TICK_COLOR_TAG);\r
84 colorSettingElement.appendChild(tickColorElement);\r
85 int index = colorSetting.getTickColorIndex();\r
86 tickColorElement.setAttribute(INDEX_ATTR, Integer.toString(index));\r
87 \r
88 if (colorSetting.getFilter() != null) {\r
89 Element filterElement = document.createElement(FILTER_TAG);\r
90 colorSettingElement.appendChild(filterElement);\r
91 TmfFilterXMLWriter.buildXMLTree(document, colorSetting.getFilter(), filterElement);\r
92 }\r
93 }\r
94 \r
95 TransformerFactory transformerFactory = TransformerFactory.newInstance();\r
96 \r
97 Transformer transformer = transformerFactory.newTransformer();\r
98 DOMSource source = new DOMSource(document);\r
99 StreamResult result = new StreamResult(new File(pathName));\r
100 transformer.transform(source, result);\r
101 } catch (ParserConfigurationException e) {\r
8fd82db5 102 Activator.getDefault().logError("Error saving color xml file: " + pathName, e); //$NON-NLS-1$\r
ca13a91c 103 } catch (TransformerConfigurationException e) {\r
8fd82db5 104 Activator.getDefault().logError("Error saving color xml file: " + pathName, e); //$NON-NLS-1$\r
ca13a91c 105 } catch (TransformerException e) {\r
8fd82db5 106 Activator.getDefault().logError("Error saving color xml file: " + pathName, e); //$NON-NLS-1$\r
ca13a91c
FC
107 }\r
108 }\r
109 \r
110 public static ColorSetting[] load(String pathName) {\r
111 if (! new File(pathName).canRead()) {\r
112 return new ColorSetting[0];\r
113 }\r
114 SAXParserFactory parserFactory = SAXParserFactory.newInstance(); \r
115 parserFactory.setNamespaceAware(true); \r
116\r
5d862cfc 117 ColorSettingsContentHandler handler = new ColorSettingsContentHandler();\r
ca13a91c
FC
118 try {\r
119 XMLReader saxReader = parserFactory.newSAXParser().getXMLReader();\r
ca13a91c
FC
120 saxReader.setContentHandler(handler);\r
121 saxReader.parse(pathName);\r
122 return handler.colorSettings.toArray(new ColorSetting[0]);\r
123 } catch (ParserConfigurationException e) {\r
8fd82db5 124 Activator.getDefault().logError("Error loading color xml file: " + pathName, e); //$NON-NLS-1$\r
ca13a91c 125 } catch (SAXException e) {\r
8fd82db5 126 Activator.getDefault().logError("Error loading color xml file: " + pathName, e); //$NON-NLS-1$\r
ca13a91c 127 } catch (IOException e) {\r
8fd82db5 128 Activator.getDefault().logError("Error loading color xml file: " + pathName, e); //$NON-NLS-1$\r
ca13a91c 129 }\r
5d862cfc
FC
130 // In case of error, dispose the partial list of color settings\r
131 for (ColorSetting colorSetting : handler.colorSettings) {\r
132 colorSetting.dispose();\r
133 }\r
ca13a91c
FC
134 return new ColorSetting[0];\r
135 }\r
136 \r
137 private static class ColorSettingsContentHandler extends DefaultHandler {\r
138\r
9fa32496 139 private List<ColorSetting> colorSettings = new ArrayList<ColorSetting>(0);\r
ca13a91c
FC
140 private RGB fg;\r
141 private RGB bg;\r
142 private int tickColorIndex;\r
143 private ITmfFilterTreeNode filter;\r
144 private TmfFilterContentHandler filterContentHandler;\r
145 \r
146 /* (non-Javadoc)\r
147 * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)\r
148 */\r
149 @Override\r
150 public void startElement(String uri, String localName, String qName, Attributes attributes)\r
151 throws SAXException {\r
152 if (localName.equals(COLOR_SETTINGS_TAG)) {\r
153 colorSettings = new ArrayList<ColorSetting>();\r
154 } else if (localName.equals(COLOR_SETTING_TAG)) {\r
155 fg = null;\r
156 bg = null;\r
157 filter = null;\r
158 } else if (localName.equals(FG_TAG)) {\r
159 int r = Integer.valueOf(attributes.getValue(R_ATTR));\r
160 int g = Integer.valueOf(attributes.getValue(G_ATTR));\r
161 int b = Integer.valueOf(attributes.getValue(B_ATTR));\r
162 fg = new RGB(r, g, b);\r
163 } else if (localName.equals(BG_TAG)) {\r
164 int r = Integer.valueOf(attributes.getValue(R_ATTR));\r
165 int g = Integer.valueOf(attributes.getValue(G_ATTR));\r
166 int b = Integer.valueOf(attributes.getValue(B_ATTR));\r
167 bg = new RGB(r, g, b);\r
168 } else if (localName.equals(TICK_COLOR_TAG)) {\r
169 int index = Integer.valueOf(attributes.getValue(INDEX_ATTR));\r
170 tickColorIndex = index;\r
171 } else if (localName.equals(FILTER_TAG)) {\r
172 filterContentHandler = new TmfFilterContentHandler();\r
173 } else if (filterContentHandler != null) {\r
174 filterContentHandler.startElement(uri, localName, qName, attributes);\r
175 }\r
176 }\r
177 \r
178 /* (non-Javadoc)\r
179 * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)\r
180 */\r
181 @Override\r
182 public void endElement(String uri, String localName, String qName)\r
183 throws SAXException {\r
184 if (localName.equals(COLOR_SETTINGS_TAG)) {\r
185 } else if (localName.equals(COLOR_SETTING_TAG)) {\r
186 ColorSetting colorSetting = new ColorSetting(fg, bg, tickColorIndex, filter);\r
187 colorSettings.add(colorSetting);\r
188 } else if (localName.equals(FILTER_TAG)) {\r
189 filter = filterContentHandler.getTree();\r
190 filterContentHandler = null;\r
191 } else if (filterContentHandler != null) {\r
192 filterContentHandler.endElement(uri, localName, qName);\r
193 }\r
194 }\r
195\r
196 }\r
197}\r
This page took 0.035534 seconds and 5 git commands to generate.