Commit | Line | Data |
---|---|---|
be222f56 PT |
1 | /******************************************************************************* |
2 | * Copyright (c) 2010, 2012 Ericsson | |
3 | * | |
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 | |
8 | * | |
9 | * Contributors: | |
10 | * Patrick Tasse - Initial API and implementation | |
11 | * Bernd Hufmann - Updated to use RGB for the tick color | |
12 | *******************************************************************************/ | |
13 | ||
14 | package org.eclipse.linuxtools.tmf.ui.views.colors; | |
15 | ||
16 | import java.io.File; | |
17 | import java.io.IOException; | |
18 | import java.util.ArrayList; | |
19 | import java.util.List; | |
20 | ||
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; | |
31 | ||
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; | |
43 | ||
44 | /** | |
45 | * Class for saving and loading of color settings to/from file. | |
46 | * | |
47 | * @version 1.0 | |
48 | * @author Patrick Tasse | |
49 | * | |
50 | */ | |
51 | public class ColorSettingsXML { | |
52 | ||
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$ | |
63 | ||
64 | /** | |
65 | * Saves the given color settings to file. | |
66 | * | |
67 | * @param pathName A file name with path | |
68 | * @param colorSettings -An array of color settings to save. | |
69 | */ | |
70 | public static void save(String pathName, ColorSetting[] colorSettings) { | |
71 | try { | |
72 | DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); | |
73 | DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); | |
74 | Document document = documentBuilder.newDocument(); | |
75 | ||
76 | Element rootElement = document.createElement(COLOR_SETTINGS_TAG); | |
77 | document.appendChild(rootElement); | |
78 | ||
79 | for (ColorSetting colorSetting : colorSettings) { | |
80 | Element colorSettingElement = document.createElement(COLOR_SETTING_TAG); | |
81 | rootElement.appendChild(colorSettingElement); | |
82 | ||
83 | Element fgElement = document.createElement(FG_TAG); | |
84 | colorSettingElement.appendChild(fgElement); | |
85 | RGB foreground = colorSetting.getForegroundRGB(); | |
86 | fgElement.setAttribute(R_ATTR, Integer.toString(foreground.red)); | |
87 | fgElement.setAttribute(G_ATTR, Integer.toString(foreground.green)); | |
88 | fgElement.setAttribute(B_ATTR, Integer.toString(foreground.blue)); | |
89 | ||
90 | Element bgElement = document.createElement(BG_TAG); | |
91 | colorSettingElement.appendChild(bgElement); | |
92 | RGB background = colorSetting.getBackgroundRGB(); | |
93 | bgElement.setAttribute(R_ATTR, Integer.toString(background.red)); | |
94 | bgElement.setAttribute(G_ATTR, Integer.toString(background.green)); | |
95 | bgElement.setAttribute(B_ATTR, Integer.toString(background.blue)); | |
96 | ||
97 | Element tickColorElement = document.createElement(TICK_TAG); | |
98 | colorSettingElement.appendChild(tickColorElement); | |
99 | RGB tickColor = colorSetting.getTickColorRGB(); | |
100 | tickColorElement.setAttribute(R_ATTR, Integer.toString(tickColor.red)); | |
101 | tickColorElement.setAttribute(G_ATTR, Integer.toString(tickColor.green)); | |
102 | tickColorElement.setAttribute(B_ATTR, Integer.toString(tickColor.blue)); | |
103 | ||
104 | if (colorSetting.getFilter() != null) { | |
105 | Element filterElement = document.createElement(FILTER_TAG); | |
106 | colorSettingElement.appendChild(filterElement); | |
107 | TmfFilterXMLWriter.buildXMLTree(document, colorSetting.getFilter(), filterElement); | |
108 | } | |
109 | } | |
110 | ||
111 | TransformerFactory transformerFactory = TransformerFactory.newInstance(); | |
112 | ||
113 | Transformer transformer = transformerFactory.newTransformer(); | |
114 | DOMSource source = new DOMSource(document); | |
115 | StreamResult result = new StreamResult(new File(pathName)); | |
116 | transformer.transform(source, result); | |
117 | } catch (ParserConfigurationException e) { | |
118 | Activator.getDefault().logError("Error saving color xml file: " + pathName, e); //$NON-NLS-1$ | |
119 | } catch (TransformerConfigurationException e) { | |
120 | Activator.getDefault().logError("Error saving color xml file: " + pathName, e); //$NON-NLS-1$ | |
121 | } catch (TransformerException e) { | |
122 | Activator.getDefault().logError("Error saving color xml file: " + pathName, e); //$NON-NLS-1$ | |
123 | } | |
124 | } | |
125 | ||
126 | /** | |
127 | * Loads color settings from file and returns it in an array. | |
128 | * | |
129 | * @param pathName A file name with path | |
130 | * | |
131 | * @return the color settings array loaded from file | |
132 | */ | |
133 | public static ColorSetting[] load(String pathName) { | |
134 | if (! new File(pathName).canRead()) { | |
135 | return new ColorSetting[0]; | |
136 | } | |
137 | SAXParserFactory parserFactory = SAXParserFactory.newInstance(); | |
138 | parserFactory.setNamespaceAware(true); | |
139 | ||
140 | ColorSettingsContentHandler handler = new ColorSettingsContentHandler(); | |
141 | try { | |
142 | XMLReader saxReader = parserFactory.newSAXParser().getXMLReader(); | |
143 | saxReader.setContentHandler(handler); | |
144 | saxReader.parse(pathName); | |
145 | return handler.colorSettings.toArray(new ColorSetting[0]); | |
146 | } catch (ParserConfigurationException e) { | |
147 | Activator.getDefault().logError("Error loading color xml file: " + pathName, e); //$NON-NLS-1$ | |
148 | } catch (SAXException e) { | |
149 | Activator.getDefault().logError("Error loading color xml file: " + pathName, e); //$NON-NLS-1$ | |
150 | } catch (IOException e) { | |
151 | Activator.getDefault().logError("Error loading color xml file: " + pathName, e); //$NON-NLS-1$ | |
152 | } | |
153 | // In case of error, dispose the partial list of color settings | |
154 | for (ColorSetting colorSetting : handler.colorSettings) { | |
155 | colorSetting.dispose(); | |
156 | } | |
157 | return new ColorSetting[0]; | |
158 | } | |
159 | ||
160 | // Helper class | |
161 | private static class ColorSettingsContentHandler extends DefaultHandler { | |
162 | ||
163 | private List<ColorSetting> colorSettings = new ArrayList<ColorSetting>(0); | |
164 | private RGB fg = new RGB(0, 0, 0); | |
165 | private RGB bg = new RGB(255, 255, 255); | |
166 | private RGB tickColor = new RGB(0, 0, 0); | |
167 | private ITmfFilterTreeNode filter; | |
168 | private TmfFilterContentHandler filterContentHandler; | |
169 | ||
170 | /* (non-Javadoc) | |
171 | * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes) | |
172 | */ | |
173 | @Override | |
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<ColorSetting>(); | |
178 | } else if (localName.equals(COLOR_SETTING_TAG)) { | |
179 | fg = null; | |
180 | bg = null; | |
181 | filter = null; | |
182 | } else if (localName.equals(FG_TAG)) { | |
183 | int r = Integer.valueOf(attributes.getValue(R_ATTR)); | |
184 | int g = Integer.valueOf(attributes.getValue(G_ATTR)); | |
185 | int b = Integer.valueOf(attributes.getValue(B_ATTR)); | |
186 | fg = new RGB(r, g, b); | |
187 | } else if (localName.equals(BG_TAG)) { | |
188 | int r = Integer.valueOf(attributes.getValue(R_ATTR)); | |
189 | int g = Integer.valueOf(attributes.getValue(G_ATTR)); | |
190 | int b = Integer.valueOf(attributes.getValue(B_ATTR)); | |
191 | bg = new RGB(r, g, b); | |
192 | } else if (localName.equals(TICK_TAG)) { | |
193 | int r = Integer.valueOf(attributes.getValue(R_ATTR)); | |
194 | int g = Integer.valueOf(attributes.getValue(G_ATTR)); | |
195 | int b = Integer.valueOf(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); | |
201 | } | |
202 | } | |
203 | ||
204 | /* (non-Javadoc) | |
205 | * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String) | |
206 | */ | |
207 | @Override | |
208 | public void endElement(String uri, String localName, String qName) | |
209 | throws SAXException { | |
210 | if (localName.equals(COLOR_SETTINGS_TAG)) { | |
211 | // Nothing to do | |
212 | } else if (localName.equals(COLOR_SETTING_TAG)) { | |
213 | ColorSetting colorSetting = new ColorSetting(fg, bg, tickColor, filter); | |
214 | colorSettings.add(colorSetting); | |
215 | } else if (localName.equals(FILTER_TAG)) { | |
216 | filter = filterContentHandler.getTree(); | |
217 | filterContentHandler = null; | |
218 | } else if (filterContentHandler != null) { | |
219 | filterContentHandler.endElement(uri, localName, qName); | |
220 | } | |
221 | } | |
222 | ||
223 | } | |
224 | } |