|
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 * Imports Color configuration from XML file |
|
17 * |
|
18 */ |
|
19 package com.nokia.traceviewer.engine.preferences; |
|
20 |
|
21 import java.io.File; |
|
22 |
|
23 import org.eclipse.swt.custom.StyleRange; |
|
24 import org.eclipse.swt.graphics.Color; |
|
25 import org.eclipse.ui.PlatformUI; |
|
26 import org.w3c.dom.Attr; |
|
27 import org.w3c.dom.Document; |
|
28 import org.w3c.dom.Element; |
|
29 import org.w3c.dom.NamedNodeMap; |
|
30 import org.w3c.dom.Node; |
|
31 import org.w3c.dom.NodeList; |
|
32 |
|
33 import com.nokia.traceviewer.dialog.BasePropertyDialog; |
|
34 import com.nokia.traceviewer.dialog.treeitem.ColorTreeBaseItem; |
|
35 import com.nokia.traceviewer.dialog.treeitem.ColorTreeComponentItem; |
|
36 import com.nokia.traceviewer.dialog.treeitem.ColorTreeItem; |
|
37 import com.nokia.traceviewer.dialog.treeitem.ColorTreeTextItem; |
|
38 import com.nokia.traceviewer.dialog.treeitem.TreeItem; |
|
39 import com.nokia.traceviewer.dialog.treeitem.TreeItemListener; |
|
40 import com.nokia.traceviewer.engine.TraceViewerGlobals; |
|
41 import com.nokia.traceviewer.engine.dataprocessor.ColorProcessor; |
|
42 |
|
43 /** |
|
44 * Imports Color configuration from XML file |
|
45 * |
|
46 */ |
|
47 public final class XMLColorConfigurationImporter extends |
|
48 XMLBaseConfigurationImporter { |
|
49 |
|
50 /** |
|
51 * Constructor |
|
52 * |
|
53 * @param root |
|
54 * root of the Color elements |
|
55 * @param configurationFileName |
|
56 * file name where to import data from |
|
57 * @param pathRelative |
|
58 * tells is the path relative |
|
59 */ |
|
60 public XMLColorConfigurationImporter(TreeItem root, |
|
61 String configurationFileName, boolean pathRelative) { |
|
62 super(root, configurationFileName, pathRelative); |
|
63 } |
|
64 |
|
65 /** |
|
66 * Imports data |
|
67 */ |
|
68 public void importData() { |
|
69 File file = new File(filePath); |
|
70 if (file.exists()) { |
|
71 Document doc = getDocument(); |
|
72 |
|
73 if (doc != null) { |
|
74 |
|
75 // Delete old color rules |
|
76 Object[] oldColorRules = root.getChildren(); |
|
77 for (int i = 0; i < oldColorRules.length; i++) { |
|
78 |
|
79 // Dispose colors |
|
80 if ((((ColorTreeItem) oldColorRules[i]) |
|
81 .getForegroundColor() != null) |
|
82 && ((ColorTreeItem) oldColorRules[i]) |
|
83 .getBackgroundColor() != null) { |
|
84 ((ColorTreeItem) oldColorRules[i]).getForegroundColor() |
|
85 .dispose(); |
|
86 ((ColorTreeItem) oldColorRules[i]).getBackgroundColor() |
|
87 .dispose(); |
|
88 } |
|
89 root.removeChild((ColorTreeItem) oldColorRules[i]); |
|
90 } |
|
91 |
|
92 // Clear all old rules |
|
93 ColorProcessor proc = TraceViewerGlobals.getTraceViewer() |
|
94 .getDataProcessorAccess().getColorer(); |
|
95 proc.getTextRules().clear(); |
|
96 proc.getComponentRules().clear(); |
|
97 proc.clearRanges(); |
|
98 TraceViewerGlobals.getTraceViewer().getView().applyColorRules( |
|
99 proc.getRanges().toArray(new StyleRange[0])); |
|
100 |
|
101 // Get color node from the XML file |
|
102 NodeList list = doc.getElementsByTagName(COLOR_TAG); |
|
103 Node parent = list.item(0); |
|
104 |
|
105 // Import colors |
|
106 if (parent != null) { |
|
107 NodeList colors = parent.getChildNodes(); |
|
108 for (int i = 0; i < colors.getLength(); i++) { |
|
109 if (colors.item(i) instanceof Element) { |
|
110 processColorRule(colors.item(i), root); |
|
111 } |
|
112 } |
|
113 } |
|
114 |
|
115 // Create new rules |
|
116 proc.createColorRules(); |
|
117 } |
|
118 } |
|
119 } |
|
120 |
|
121 /** |
|
122 * Import this color rule to root |
|
123 * |
|
124 * @param node |
|
125 * Color node to import |
|
126 * @param root |
|
127 * Root node of color dialog |
|
128 */ |
|
129 private void processColorRule(Node node, TreeItem root) { |
|
130 String name = null; |
|
131 NamedNodeMap itemAttributes = node.getAttributes(); |
|
132 |
|
133 // Get name |
|
134 Attr nameAttr = (Attr) itemAttributes.getNamedItem(NAME_TAG); |
|
135 name = nameAttr.getValue(); |
|
136 |
|
137 // Group item |
|
138 TreeItem newItem = null; |
|
139 if (node.getNodeName().equals(GROUP_TAG)) { |
|
140 newItem = new ColorTreeBaseItem(TraceViewerGlobals.getTraceViewer() |
|
141 .getDataProcessorAccess().getColorer() |
|
142 .getTreeItemListener(), root, name, |
|
143 ColorTreeItem.Rule.GROUP, null, null); |
|
144 |
|
145 // Process normal rules |
|
146 } else if (node.getNodeName().equals(CONFIGURATION_TAG)) { |
|
147 |
|
148 // Get tree item listener from the processor |
|
149 TreeItemListener listener = TraceViewerGlobals.getTraceViewer() |
|
150 .getDataProcessorAccess().getColorer() |
|
151 .getTreeItemListener(); |
|
152 |
|
153 // Get rule name |
|
154 Attr ruleAttr = (Attr) itemAttributes.getNamedItem(RULE_TAG); |
|
155 String rule = ruleAttr.getValue(); |
|
156 |
|
157 // Create Text Item |
|
158 if (rule.equals(TEXTRULE_TAG)) { |
|
159 newItem = processTextRule(node, root, name, listener); |
|
160 // Create Component Item |
|
161 } else if (rule.equals(COMPONENTRULE_TAG)) { |
|
162 newItem = processComponentRule(node, root, name, listener); |
|
163 } else { |
|
164 // Unknown rule, shouldn't come here |
|
165 } |
|
166 |
|
167 // Check if the color rules should be kept over boot |
|
168 Attr enabledAttr = (Attr) itemAttributes.getNamedItem(ENABLED_TAG); |
|
169 if (enabledAttr != null) { |
|
170 String enabled = enabledAttr.getValue(); |
|
171 if (enabled.equals(YES_TAG)) { |
|
172 TraceViewerGlobals.getTraceViewer() |
|
173 .getDataProcessorAccess().getColorer().enableRule( |
|
174 (ColorTreeItem) newItem); |
|
175 } |
|
176 } |
|
177 |
|
178 } else { |
|
179 // Unknown node name, shouldn't come here |
|
180 } |
|
181 |
|
182 // Add the child to the parent node |
|
183 if (newItem != null) { |
|
184 root.addChild(newItem); |
|
185 } |
|
186 |
|
187 // If node was group, process it's children |
|
188 if (node.getNodeName().equals(GROUP_TAG)) { |
|
189 // Get children |
|
190 NodeList children = node.getChildNodes(); |
|
191 for (int i = 0; i < children.getLength(); i++) { |
|
192 if (children.item(i) instanceof Element) { |
|
193 processColorRule(children.item(i), newItem); |
|
194 } |
|
195 } |
|
196 } |
|
197 |
|
198 } |
|
199 |
|
200 /** |
|
201 * Processes text rule |
|
202 * |
|
203 * @param node |
|
204 * Node to process |
|
205 * @param root |
|
206 * root node |
|
207 * @param name |
|
208 * name of the rule |
|
209 * @param listener |
|
210 * TreeItem listener |
|
211 * @return the newly created text item |
|
212 */ |
|
213 private TreeItem processTextRule(Node node, TreeItem root, String name, |
|
214 TreeItemListener listener) { |
|
215 TreeItem newItem; |
|
216 String text = null; |
|
217 boolean matchCase = false; |
|
218 Color foregroundColor = null; |
|
219 Color backgroundColor = null; |
|
220 NodeList properties = node.getChildNodes(); |
|
221 for (int i = 0; i < properties.getLength(); i++) { |
|
222 if (properties.item(i) instanceof Element) { |
|
223 String nodeName = properties.item(i).getNodeName(); |
|
224 if (nodeName.equals(FORECOLOR_TAG)) { |
|
225 foregroundColor = createColorFromHexString(properties.item( |
|
226 i).getTextContent()); |
|
227 } else if (nodeName.equals(BACKCOLOR_TAG)) { |
|
228 backgroundColor = createColorFromHexString(properties.item( |
|
229 i).getTextContent()); |
|
230 } else if (nodeName.equals(TEXT_TAG)) { |
|
231 text = properties.item(i).getTextContent(); |
|
232 } else if (nodeName.equals(MATCHCASE_TAG)) { |
|
233 if (properties.item(i).getTextContent().equals(NO_TAG)) { |
|
234 matchCase = false; |
|
235 } else { |
|
236 matchCase = true; |
|
237 } |
|
238 } |
|
239 } |
|
240 } |
|
241 |
|
242 // Create new text item |
|
243 newItem = new ColorTreeTextItem(listener, root, name, |
|
244 ColorTreeItem.Rule.TEXT_RULE, foregroundColor, backgroundColor, |
|
245 text, matchCase); |
|
246 return newItem; |
|
247 } |
|
248 |
|
249 /** |
|
250 * Processes component rule |
|
251 * |
|
252 * @param node |
|
253 * Node to process |
|
254 * @param root |
|
255 * root node |
|
256 * @param name |
|
257 * name of the rule |
|
258 * @param listener |
|
259 * TreeItem listener |
|
260 * @return the newly created component item |
|
261 */ |
|
262 private TreeItem processComponentRule(Node node, TreeItem root, |
|
263 String name, TreeItemListener listener) { |
|
264 TreeItem newItem; |
|
265 Color foregroundColor = null; |
|
266 Color backgroundColor = null; |
|
267 int componentId = BasePropertyDialog.WILDCARD_INTEGER; |
|
268 int groupId = BasePropertyDialog.WILDCARD_INTEGER; |
|
269 NodeList properties = node.getChildNodes(); |
|
270 for (int i = 0; i < properties.getLength(); i++) { |
|
271 if (properties.item(i) instanceof Element) { |
|
272 String nodeName = properties.item(i).getNodeName(); |
|
273 if (nodeName.equals(FORECOLOR_TAG)) { |
|
274 foregroundColor = createColorFromHexString(properties.item( |
|
275 i).getTextContent()); |
|
276 } else if (nodeName.equals(BACKCOLOR_TAG)) { |
|
277 backgroundColor = createColorFromHexString(properties.item( |
|
278 i).getTextContent()); |
|
279 } else if (nodeName.equals(COMPONENTID_TAG)) { |
|
280 // Get component ID. If parse fails, it means that component |
|
281 // ID is a wildcard |
|
282 try { |
|
283 componentId = Integer.parseInt(properties.item(i) |
|
284 .getTextContent()); |
|
285 } catch (NumberFormatException e) { |
|
286 } |
|
287 } else if (nodeName.equals(GROUPID_TAG)) { |
|
288 // Get group ID. If parse fails, it means that group |
|
289 // ID is a wildcard |
|
290 try { |
|
291 groupId = Integer.parseInt(properties.item(i) |
|
292 .getTextContent()); |
|
293 } catch (NumberFormatException e) { |
|
294 } |
|
295 } |
|
296 } |
|
297 } |
|
298 |
|
299 // Create new component item |
|
300 newItem = new ColorTreeComponentItem(listener, root, name, |
|
301 ColorTreeItem.Rule.COMPONENT_RULE, foregroundColor, |
|
302 backgroundColor, componentId, groupId); |
|
303 return newItem; |
|
304 } |
|
305 |
|
306 /** |
|
307 * Creates color from hex string |
|
308 * |
|
309 * @param hexString |
|
310 * hex string |
|
311 * @return color |
|
312 */ |
|
313 private Color createColorFromHexString(String hexString) { |
|
314 final int hex = 16; |
|
315 int red = Integer.parseInt(hexString.substring(0, 2), hex); |
|
316 int green = Integer.parseInt(hexString.substring(2, 4), hex); |
|
317 int blue = Integer.parseInt(hexString.substring(4, 6), hex); |
|
318 Color color = new Color(PlatformUI.getWorkbench().getDisplay(), red, |
|
319 green, blue); |
|
320 return color; |
|
321 } |
|
322 } |