|
1 /* |
|
2 * Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 * Exports Color configuration to XML file |
|
17 * |
|
18 */ |
|
19 package com.nokia.traceviewer.engine.preferences; |
|
20 |
|
21 import javax.xml.transform.stream.StreamResult; |
|
22 |
|
23 import org.eclipse.swt.graphics.Color; |
|
24 import org.w3c.dom.Attr; |
|
25 import org.w3c.dom.Document; |
|
26 import org.w3c.dom.NamedNodeMap; |
|
27 import org.w3c.dom.Node; |
|
28 import org.w3c.dom.NodeList; |
|
29 |
|
30 import com.nokia.traceviewer.dialog.treeitem.ColorTreeComponentItem; |
|
31 import com.nokia.traceviewer.dialog.treeitem.ColorTreeItem; |
|
32 import com.nokia.traceviewer.dialog.treeitem.ColorTreeTextItem; |
|
33 import com.nokia.traceviewer.dialog.treeitem.TreeItem; |
|
34 import com.nokia.traceviewer.engine.TraceViewerGlobals; |
|
35 |
|
36 /** |
|
37 * Exports Color configuration to XML file |
|
38 */ |
|
39 public final class XMLColorConfigurationExporter extends |
|
40 XMLBaseConfigurationExporter { |
|
41 |
|
42 /** |
|
43 * Constructor |
|
44 * |
|
45 * @param root |
|
46 * root of the Color elements |
|
47 * @param configurationFileName |
|
48 * file name where to export data |
|
49 * @param pathRelative |
|
50 * tells is the path relative |
|
51 */ |
|
52 public XMLColorConfigurationExporter(TreeItem root, |
|
53 String configurationFileName, boolean pathRelative) { |
|
54 super(root, configurationFileName, pathRelative); |
|
55 } |
|
56 |
|
57 /** |
|
58 * Exports data |
|
59 */ |
|
60 public void export() { |
|
61 Document doc = getDocument(); |
|
62 Node parent; |
|
63 |
|
64 // Get root node of color rules |
|
65 NodeList list = doc.getElementsByTagName(COLOR_TAG); |
|
66 if (list.getLength() == 0) { |
|
67 parent = doc.createElement(COLOR_TAG); |
|
68 // Get the main configurations tag |
|
69 NodeList list2 = doc.getElementsByTagName(CONFIGURATIONS_TAG); |
|
70 Node mainRoot = list2.item(0); |
|
71 mainRoot.appendChild(parent); |
|
72 } else { |
|
73 parent = list.item(0); |
|
74 } |
|
75 |
|
76 // Delete old color information |
|
77 NodeList colorChilds = parent.getChildNodes(); |
|
78 int len = colorChilds.getLength(); |
|
79 for (int i = 0; i < len; i++) { |
|
80 parent.removeChild(colorChilds.item(0)); |
|
81 } |
|
82 |
|
83 // Process through the tree |
|
84 for (int i = 0; i < root.getChildren().length; i++) { |
|
85 processChild((ColorTreeItem) root.getChildren()[i], doc, parent); |
|
86 } |
|
87 |
|
88 // Get result from document |
|
89 StreamResult result = getResultFromDocument(doc); |
|
90 |
|
91 // Print out |
|
92 String xmlString = result.getWriter().toString(); |
|
93 writeFile(xmlString); |
|
94 } |
|
95 |
|
96 /** |
|
97 * Process child |
|
98 * |
|
99 * @param item |
|
100 * child to process |
|
101 * @param doc |
|
102 * document |
|
103 * @param root |
|
104 * node to insert this child to |
|
105 */ |
|
106 private void processChild(ColorTreeItem item, Document doc, Node root) { |
|
107 // Process through the tree |
|
108 Node newItem = null; |
|
109 // Group rule |
|
110 if (item.getRule() == ColorTreeItem.Rule.GROUP) { |
|
111 newItem = doc.createElement(GROUP_TAG); |
|
112 // Create a configuration |
|
113 } else { |
|
114 newItem = doc.createElement(CONFIGURATION_TAG); |
|
115 } |
|
116 |
|
117 // Get attributes |
|
118 NamedNodeMap itemAttributes = newItem.getAttributes(); |
|
119 |
|
120 // Set name |
|
121 Attr name = doc.createAttribute(NAME_TAG); |
|
122 name.setValue(item.getName()); |
|
123 itemAttributes.setNamedItem(name); |
|
124 |
|
125 // Check is the item group |
|
126 if (item.getRule() != ColorTreeItem.Rule.GROUP) { |
|
127 |
|
128 // Add rule type attribute |
|
129 Attr rule = doc.createAttribute(RULE_TAG); |
|
130 if (item.getRule() == ColorTreeItem.Rule.TEXT_RULE) { |
|
131 rule.setValue(TEXTRULE_TAG); |
|
132 } else if (item.getRule() == ColorTreeItem.Rule.COMPONENT_RULE) { |
|
133 rule.setValue(COMPONENTRULE_TAG); |
|
134 } else { |
|
135 String unknownText = Messages |
|
136 .getString("XMLColorConfigurationExporter.UnknownText"); //$NON-NLS-1$ |
|
137 rule.setValue(unknownText); |
|
138 } |
|
139 itemAttributes.setNamedItem(rule); |
|
140 |
|
141 // Add enabled attribute |
|
142 Attr enabled = doc.createAttribute(ENABLED_TAG); |
|
143 boolean checked = isChecked(item); |
|
144 if (checked) { |
|
145 enabled.setValue(YES_TAG); |
|
146 } else { |
|
147 enabled.setValue(NO_TAG); |
|
148 } |
|
149 itemAttributes.setNamedItem(enabled); |
|
150 } |
|
151 |
|
152 // Set as child to parent |
|
153 root.appendChild(newItem); |
|
154 |
|
155 // Set common attributes for non-group items |
|
156 if (item.getRule() != ColorTreeItem.Rule.GROUP) { |
|
157 |
|
158 // ForeColor |
|
159 Node foreColor = doc.createElement(FORECOLOR_TAG); |
|
160 Color foregroundColor = item.getForegroundColor(); |
|
161 String foregroundColorString = createHexStringFromColor(foregroundColor); |
|
162 foreColor.setTextContent(foregroundColorString); |
|
163 newItem.appendChild(foreColor); |
|
164 |
|
165 // BackColor |
|
166 Node backColor = doc.createElement(BACKCOLOR_TAG); |
|
167 Color backgroundColor = item.getBackgroundColor(); |
|
168 String backgroundColorString = createHexStringFromColor(backgroundColor); |
|
169 backColor.setTextContent(backgroundColorString); |
|
170 newItem.appendChild(backColor); |
|
171 } |
|
172 |
|
173 // Set rest of the attributes from text rule |
|
174 if (item.getRule() == ColorTreeItem.Rule.TEXT_RULE) { |
|
175 ColorTreeTextItem textItem = (ColorTreeTextItem) item; |
|
176 |
|
177 // Text |
|
178 Node text = doc.createElement(TEXT_TAG); |
|
179 text.setTextContent(textItem.getText()); |
|
180 newItem.appendChild(text); |
|
181 |
|
182 // Match case |
|
183 Node matchCase = doc.createElement(MATCHCASE_TAG); |
|
184 if (textItem.isMatchCase()) { |
|
185 matchCase.setTextContent(YES_TAG); |
|
186 } else { |
|
187 matchCase.setTextContent(NO_TAG); |
|
188 } |
|
189 newItem.appendChild(matchCase); |
|
190 |
|
191 // Component rule |
|
192 } else if (item.getRule() == ColorTreeItem.Rule.COMPONENT_RULE) { |
|
193 ColorTreeComponentItem componentItem = (ColorTreeComponentItem) item; |
|
194 |
|
195 // Component ID |
|
196 Node component = doc.createElement(COMPONENTID_TAG); |
|
197 component.setTextContent(String.valueOf(componentItem |
|
198 .getComponentId())); |
|
199 newItem.appendChild(component); |
|
200 |
|
201 // Group ID |
|
202 Node group = doc.createElement(GROUPID_TAG); |
|
203 group.setTextContent(String.valueOf(componentItem.getGroupId())); |
|
204 newItem.appendChild(group); |
|
205 } |
|
206 |
|
207 // Loop through own children |
|
208 for (int i = 0; i < item.getChildren().length; i++) { |
|
209 processChild((ColorTreeItem) item.getChildren()[i], doc, newItem); |
|
210 } |
|
211 } |
|
212 |
|
213 /** |
|
214 * Checks it the given item is checked in the color dialog |
|
215 * |
|
216 * @param item |
|
217 * item to be checked |
|
218 * @return true if the given item is checked |
|
219 */ |
|
220 private boolean isChecked(ColorTreeItem item) { |
|
221 boolean checked = false; |
|
222 if (TraceViewerGlobals.getTraceViewer().getDataProcessorAccess() |
|
223 .getColorer().getTextRules().contains(item) |
|
224 || TraceViewerGlobals.getTraceViewer().getDataProcessorAccess() |
|
225 .getColorer().getComponentRules().contains(item)) { |
|
226 checked = true; |
|
227 } |
|
228 return checked; |
|
229 } |
|
230 |
|
231 /** |
|
232 * Creates hex string from color |
|
233 * |
|
234 * @param color |
|
235 * color to create hex string from |
|
236 * @return hex string |
|
237 */ |
|
238 private String createHexStringFromColor(Color color) { |
|
239 String colorString = ""; //$NON-NLS-1$ |
|
240 if (color != null) { |
|
241 |
|
242 // Add red color as hex |
|
243 String colorPart = Integer.toHexString(color.getRed()); |
|
244 if (colorPart.equals("0")) { //$NON-NLS-1$ |
|
245 colorString += "0"; //$NON-NLS-1$ |
|
246 } |
|
247 colorString += colorPart; |
|
248 |
|
249 // Add green color as hex |
|
250 colorPart = Integer.toHexString(color.getGreen()); |
|
251 if (colorPart.equals("0")) { //$NON-NLS-1$ |
|
252 colorString += "0"; //$NON-NLS-1$ |
|
253 } |
|
254 colorString += colorPart; |
|
255 |
|
256 // Add blue color as hex |
|
257 colorPart = Integer.toHexString(color.getBlue()); |
|
258 if (colorPart.equals("0")) { //$NON-NLS-1$ |
|
259 colorString += "0"; //$NON-NLS-1$ |
|
260 } |
|
261 colorString += colorPart; |
|
262 } |
|
263 return colorString; |
|
264 } |
|
265 } |