Rename xxx.lttng to xxx.lttng.core
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / colors / ColorSettingsXML.java
1 /*******************************************************************************
2 * Copyright (c) 2010 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 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.ui.views.colors;
14
15 import java.io.File;
16 import java.io.IOException;
17 import java.util.ArrayList;
18
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;
29
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;
40
41 public class ColorSettingsXML {
42
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$
53
54 public static void save(String pathName, ColorSetting[] colorSettings) {
55 try {
56 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
57 DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
58 Document document = documentBuilder.newDocument();
59
60 Element rootElement = document.createElement(COLOR_SETTINGS_TAG);
61 document.appendChild(rootElement);
62
63 for (ColorSetting colorSetting : colorSettings) {
64 Element colorSettingElement = document.createElement(COLOR_SETTING_TAG);
65 rootElement.appendChild(colorSettingElement);
66
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));
73
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));
80
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));
85
86 if (colorSetting.getFilter() != null) {
87 Element filterElement = document.createElement(FILTER_TAG);
88 colorSettingElement.appendChild(filterElement);
89 TmfFilterXMLWriter.buildXMLTree(document, colorSetting.getFilter(), filterElement);
90 }
91 }
92
93 TransformerFactory transformerFactory = TransformerFactory.newInstance();
94
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) {
100 e.printStackTrace();
101 } catch (TransformerConfigurationException e) {
102 e.printStackTrace();
103 } catch (TransformerException e) {
104 e.printStackTrace();
105 }
106 }
107
108 public static ColorSetting[] load(String pathName) {
109 if (! new File(pathName).canRead()) {
110 return new ColorSetting[0];
111 }
112 SAXParserFactory parserFactory = SAXParserFactory.newInstance();
113 parserFactory.setNamespaceAware(true);
114
115 ColorSettingsContentHandler handler = new ColorSettingsContentHandler();
116 try {
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) {
122 e.printStackTrace();
123 } catch (SAXException e) {
124 e.printStackTrace();
125 } catch (IOException e) {
126 e.printStackTrace();
127 }
128 // In case of error, dispose the partial list of color settings
129 for (ColorSetting colorSetting : handler.colorSettings) {
130 colorSetting.dispose();
131 }
132 return new ColorSetting[0];
133 }
134
135 private static class ColorSettingsContentHandler extends DefaultHandler {
136
137 private ArrayList<ColorSetting> colorSettings = new ArrayList<ColorSetting>(0);
138 private RGB fg;
139 private RGB bg;
140 private int tickColorIndex;
141 private ITmfFilterTreeNode filter;
142 private TmfFilterContentHandler filterContentHandler;
143
144 /* (non-Javadoc)
145 * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
146 */
147 @Override
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)) {
153 fg = null;
154 bg = null;
155 filter = null;
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);
173 }
174 }
175
176 /* (non-Javadoc)
177 * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
178 */
179 @Override
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);
191 }
192 }
193
194 }
195 }
This page took 0.034244 seconds and 5 git commands to generate.