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