| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2010, 2014 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 |
| 68 | * A file name with path |
| 69 | * @param colorSettings |
| 70 | * -An array of color settings to save. |
| 71 | */ |
| 72 | public static void save(String pathName, ColorSetting[] colorSettings) { |
| 73 | try { |
| 74 | DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); |
| 75 | DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); |
| 76 | Document document = documentBuilder.newDocument(); |
| 77 | |
| 78 | Element rootElement = document.createElement(COLOR_SETTINGS_TAG); |
| 79 | document.appendChild(rootElement); |
| 80 | |
| 81 | for (ColorSetting colorSetting : colorSettings) { |
| 82 | Element colorSettingElement = document.createElement(COLOR_SETTING_TAG); |
| 83 | rootElement.appendChild(colorSettingElement); |
| 84 | |
| 85 | Element fgElement = document.createElement(FG_TAG); |
| 86 | colorSettingElement.appendChild(fgElement); |
| 87 | RGB foreground = colorSetting.getForegroundRGB(); |
| 88 | fgElement.setAttribute(R_ATTR, Integer.toString(foreground.red)); |
| 89 | fgElement.setAttribute(G_ATTR, Integer.toString(foreground.green)); |
| 90 | fgElement.setAttribute(B_ATTR, Integer.toString(foreground.blue)); |
| 91 | |
| 92 | Element bgElement = document.createElement(BG_TAG); |
| 93 | colorSettingElement.appendChild(bgElement); |
| 94 | RGB background = colorSetting.getBackgroundRGB(); |
| 95 | bgElement.setAttribute(R_ATTR, Integer.toString(background.red)); |
| 96 | bgElement.setAttribute(G_ATTR, Integer.toString(background.green)); |
| 97 | bgElement.setAttribute(B_ATTR, Integer.toString(background.blue)); |
| 98 | |
| 99 | Element tickColorElement = document.createElement(TICK_TAG); |
| 100 | colorSettingElement.appendChild(tickColorElement); |
| 101 | RGB tickColor = colorSetting.getTickColorRGB(); |
| 102 | tickColorElement.setAttribute(R_ATTR, Integer.toString(tickColor.red)); |
| 103 | tickColorElement.setAttribute(G_ATTR, Integer.toString(tickColor.green)); |
| 104 | tickColorElement.setAttribute(B_ATTR, Integer.toString(tickColor.blue)); |
| 105 | |
| 106 | if (colorSetting.getFilter() != null) { |
| 107 | Element filterElement = document.createElement(FILTER_TAG); |
| 108 | colorSettingElement.appendChild(filterElement); |
| 109 | TmfFilterXMLWriter.buildXMLTree(document, colorSetting.getFilter(), filterElement); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
| 114 | |
| 115 | Transformer transformer = transformerFactory.newTransformer(); |
| 116 | DOMSource source = new DOMSource(document); |
| 117 | StreamResult result = new StreamResult(new File(pathName)); |
| 118 | transformer.transform(source, result); |
| 119 | } catch (ParserConfigurationException e) { |
| 120 | Activator.getDefault().logError("Error saving color xml file: " + pathName, e); //$NON-NLS-1$ |
| 121 | } catch (TransformerConfigurationException e) { |
| 122 | Activator.getDefault().logError("Error saving color xml file: " + pathName, e); //$NON-NLS-1$ |
| 123 | } catch (TransformerException e) { |
| 124 | Activator.getDefault().logError("Error saving color xml file: " + pathName, e); //$NON-NLS-1$ |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Loads color settings from file and returns it in an array. |
| 130 | * |
| 131 | * @param pathName |
| 132 | * A file name with path |
| 133 | * |
| 134 | * @return the color settings array loaded from file |
| 135 | */ |
| 136 | public static ColorSetting[] load(String pathName) { |
| 137 | if (!new File(pathName).canRead()) { |
| 138 | return new ColorSetting[0]; |
| 139 | } |
| 140 | SAXParserFactory parserFactory = SAXParserFactory.newInstance(); |
| 141 | parserFactory.setNamespaceAware(true); |
| 142 | |
| 143 | ColorSettingsContentHandler handler = new ColorSettingsContentHandler(); |
| 144 | try { |
| 145 | XMLReader saxReader = parserFactory.newSAXParser().getXMLReader(); |
| 146 | saxReader.setContentHandler(handler); |
| 147 | saxReader.parse(pathName); |
| 148 | return handler.colorSettings.toArray(new ColorSetting[0]); |
| 149 | } catch (ParserConfigurationException e) { |
| 150 | Activator.getDefault().logError("Error loading color xml file: " + pathName, e); //$NON-NLS-1$ |
| 151 | } catch (SAXException e) { |
| 152 | Activator.getDefault().logError("Error loading color xml file: " + pathName, e); //$NON-NLS-1$ |
| 153 | } catch (IOException e) { |
| 154 | Activator.getDefault().logError("Error loading color xml file: " + pathName, e); //$NON-NLS-1$ |
| 155 | } |
| 156 | // In case of error, dispose the partial list of color settings |
| 157 | for (ColorSetting colorSetting : handler.colorSettings) { |
| 158 | colorSetting.dispose(); |
| 159 | } |
| 160 | return new ColorSetting[0]; |
| 161 | } |
| 162 | |
| 163 | // Helper class |
| 164 | private static class ColorSettingsContentHandler extends DefaultHandler { |
| 165 | |
| 166 | private List<ColorSetting> colorSettings = new ArrayList<>(0); |
| 167 | private RGB fg = new RGB(0, 0, 0); |
| 168 | private RGB bg = new RGB(255, 255, 255); |
| 169 | private RGB tickColor = new RGB(0, 0, 0); |
| 170 | private ITmfFilterTreeNode filter; |
| 171 | private TmfFilterContentHandler filterContentHandler; |
| 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<>(); |
| 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.parseInt(attributes.getValue(R_ATTR)); |
| 184 | int g = Integer.parseInt(attributes.getValue(G_ATTR)); |
| 185 | int b = Integer.parseInt(attributes.getValue(B_ATTR)); |
| 186 | fg = new RGB(r, g, b); |
| 187 | } else if (localName.equals(BG_TAG)) { |
| 188 | int r = Integer.parseInt(attributes.getValue(R_ATTR)); |
| 189 | int g = Integer.parseInt(attributes.getValue(G_ATTR)); |
| 190 | int b = Integer.parseInt(attributes.getValue(B_ATTR)); |
| 191 | bg = new RGB(r, g, b); |
| 192 | } else if (localName.equals(TICK_TAG)) { |
| 193 | int r = Integer.parseInt(attributes.getValue(R_ATTR)); |
| 194 | int g = Integer.parseInt(attributes.getValue(G_ATTR)); |
| 195 | int b = Integer.parseInt(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 | @Override |
| 205 | public void endElement(String uri, String localName, String qName) |
| 206 | throws SAXException { |
| 207 | if (localName.equals(COLOR_SETTINGS_TAG)) { |
| 208 | // Nothing to do |
| 209 | } else if (localName.equals(COLOR_SETTING_TAG)) { |
| 210 | ColorSetting colorSetting = new ColorSetting(fg, bg, tickColor, filter); |
| 211 | colorSettings.add(colorSetting); |
| 212 | } else if (localName.equals(FILTER_TAG)) { |
| 213 | filter = filterContentHandler.getTree(); |
| 214 | filterContentHandler = null; |
| 215 | } else if (filterContentHandler != null) { |
| 216 | filterContentHandler.endElement(uri, localName, qName); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | } |
| 221 | } |