trace/traceviewer/com.nokia.traceviewer/src/com/nokia/traceviewer/engine/dataprocessor/ColorProcessor.java
changeset 11 5b9d4d8641ce
equal deleted inserted replaced
10:ed1c9f64298a 11:5b9d4d8641ce
       
     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  * Colorer DataProcessor
       
    17  *
       
    18  */
       
    19 package com.nokia.traceviewer.engine.dataprocessor;
       
    20 
       
    21 import java.util.ArrayList;
       
    22 import java.util.List;
       
    23 
       
    24 import org.eclipse.jface.text.IDocument;
       
    25 import org.eclipse.swt.custom.StyleRange;
       
    26 import org.eclipse.swt.custom.StyledText;
       
    27 import org.eclipse.swt.graphics.Color;
       
    28 
       
    29 import com.nokia.traceviewer.TraceViewerPlugin;
       
    30 import com.nokia.traceviewer.dialog.BasePropertyDialog;
       
    31 import com.nokia.traceviewer.dialog.ColorDialog;
       
    32 import com.nokia.traceviewer.dialog.treeitem.ColorTreeBaseItem;
       
    33 import com.nokia.traceviewer.dialog.treeitem.ColorTreeComponentItem;
       
    34 import com.nokia.traceviewer.dialog.treeitem.ColorTreeItem;
       
    35 import com.nokia.traceviewer.dialog.treeitem.ColorTreeTextItem;
       
    36 import com.nokia.traceviewer.dialog.treeitem.TreeItemContentProvider;
       
    37 import com.nokia.traceviewer.dialog.treeitem.TreeItemListener;
       
    38 import com.nokia.traceviewer.engine.TraceInformation;
       
    39 import com.nokia.traceviewer.engine.TraceProperties;
       
    40 import com.nokia.traceviewer.engine.TraceViewerGlobals;
       
    41 import com.nokia.traceviewer.engine.TraceViewerDialogInterface.Dialog;
       
    42 import com.nokia.traceviewer.engine.preferences.PreferenceConstants;
       
    43 import com.nokia.traceviewer.engine.preferences.XMLColorConfigurationImporter;
       
    44 
       
    45 /**
       
    46  * Colorer DataProcessor
       
    47  * 
       
    48  */
       
    49 public final class ColorProcessor {
       
    50 
       
    51 	/**
       
    52 	 * Color dialog used in setting color rules
       
    53 	 */
       
    54 	private ColorDialog colorDialog;
       
    55 
       
    56 	/**
       
    57 	 * Content provider for the filter dialog
       
    58 	 */
       
    59 	private TreeItemContentProvider contentProvider;
       
    60 
       
    61 	/**
       
    62 	 * First visible object in the filter dialog tree
       
    63 	 */
       
    64 	private ColorTreeItem root;
       
    65 
       
    66 	/**
       
    67 	 * Text rules that are applied
       
    68 	 */
       
    69 	private final List<ColorTreeTextItem> textRules;
       
    70 
       
    71 	/**
       
    72 	 * Component rules that are applied
       
    73 	 */
       
    74 	private final List<ColorTreeComponentItem> componentRules;
       
    75 
       
    76 	/**
       
    77 	 * Style ranges
       
    78 	 */
       
    79 	private final List<StyleRange> ranges;
       
    80 
       
    81 	/**
       
    82 	 * Constructor
       
    83 	 */
       
    84 	public ColorProcessor() {
       
    85 		// Create initial tree
       
    86 		createInitialColorTree();
       
    87 
       
    88 		// Create rule arrays
       
    89 		textRules = new ArrayList<ColorTreeTextItem>();
       
    90 		componentRules = new ArrayList<ColorTreeComponentItem>();
       
    91 		ranges = new ArrayList<StyleRange>();
       
    92 	}
       
    93 
       
    94 	/**
       
    95 	 * Creates initial color tree
       
    96 	 */
       
    97 	public void createInitialColorTree() {
       
    98 		contentProvider = new TreeItemContentProvider();
       
    99 
       
   100 		// Create root node
       
   101 		ColorTreeItem treeRoot = new ColorTreeBaseItem(contentProvider, null,
       
   102 				"root", //$NON-NLS-1$
       
   103 				ColorTreeItem.Rule.GROUP, null, null);
       
   104 		root = new ColorTreeBaseItem(contentProvider, treeRoot,
       
   105 				TraceViewerPlugin.getDefault().getPreferenceStore().getString(
       
   106 						PreferenceConstants.CONFIGURATION_FILE),
       
   107 				ColorTreeItem.Rule.GROUP, null, null);
       
   108 		treeRoot.addChild(root);
       
   109 	}
       
   110 
       
   111 	/**
       
   112 	 * Imports color rules from configuration file
       
   113 	 */
       
   114 	public void importColorRules() {
       
   115 		XMLColorConfigurationImporter importer = new XMLColorConfigurationImporter(
       
   116 				root, TraceViewerPlugin.getDefault().getPreferenceStore()
       
   117 						.getString(PreferenceConstants.CONFIGURATION_FILE),
       
   118 				true);
       
   119 		importer.importData();
       
   120 	}
       
   121 
       
   122 	/**
       
   123 	 * Create color rules
       
   124 	 */
       
   125 	public void createColorRules() {
       
   126 		if (isColoring()) {
       
   127 
       
   128 			// Get trace information ready if there for possible component rules
       
   129 			ArrayList<TraceInformation> information = null;
       
   130 			if (!componentRules.isEmpty()) {
       
   131 
       
   132 				// Get properties from the traces in the view.
       
   133 				int showingTracesFrom = TraceViewerGlobals.getTraceViewer()
       
   134 						.getView().getShowingTracesFrom();
       
   135 				int widgetLineCount = TraceViewerGlobals.getTraceViewer()
       
   136 						.getView().getViewer().getTextWidget().getLineCount();
       
   137 				int start = showingTracesFrom;
       
   138 
       
   139 				// -2 comes from empty line in the end of widget and because
       
   140 				// traces start from offset 0, not 1
       
   141 				int end = showingTracesFrom + widgetLineCount - 2;
       
   142 				List<TraceProperties> traces = TraceViewerGlobals
       
   143 						.getTraceViewer().getTraces(start, end);
       
   144 
       
   145 				// Get the informations array
       
   146 				if (traces != null) {
       
   147 					information = new ArrayList<TraceInformation>(traces.size());
       
   148 					for (int i = 0; i < traces.size(); i++) {
       
   149 						information.add(traces.get(i).information);
       
   150 					}
       
   151 				}
       
   152 			}
       
   153 
       
   154 			IDocument document = TraceViewerGlobals.getTraceViewer().getView()
       
   155 					.getViewer().getDocument();
       
   156 			StyledText widget = TraceViewerGlobals.getTraceViewer().getView()
       
   157 					.getViewer().getTextWidget();
       
   158 			int lineCount = widget.getLineCount() - 1;
       
   159 
       
   160 			// Loop through all lines
       
   161 			for (int i = 0; i < lineCount; i++) {
       
   162 				try {
       
   163 					int lineStart = document.getLineOffset(i);
       
   164 					int lineLength = document.getLineLength(i);
       
   165 					String line = document.get(lineStart, lineLength);
       
   166 
       
   167 					boolean ruleAdded = false;
       
   168 
       
   169 					// Loop through all component / group rules if there are any
       
   170 					if (information != null) {
       
   171 						ruleAdded = processComponentRules(lineStart,
       
   172 								lineLength, information.get(i));
       
   173 					}
       
   174 
       
   175 					// Loop through all text rules
       
   176 					if (!ruleAdded) {
       
   177 						ruleAdded = processTextRules(lineStart, lineLength,
       
   178 								line);
       
   179 					}
       
   180 
       
   181 				} catch (Throwable t) {
       
   182 				}
       
   183 			}
       
   184 		} else {
       
   185 			clearRanges();
       
   186 		}
       
   187 
       
   188 		// Apply made rules
       
   189 		TraceViewerGlobals.getTraceViewer().getView().applyColorRules(
       
   190 				ranges.toArray(new StyleRange[ranges.size()]));
       
   191 
       
   192 		// Clear all rules
       
   193 		clearRanges();
       
   194 	}
       
   195 
       
   196 	/**
       
   197 	 * Processes component rules
       
   198 	 * 
       
   199 	 * @param lineStart
       
   200 	 *            line start offset
       
   201 	 * @param lineLength
       
   202 	 *            line length
       
   203 	 * @param information
       
   204 	 *            trace information
       
   205 	 * @return true if rule was added to this line
       
   206 	 */
       
   207 	private boolean processComponentRules(int lineStart, int lineLength,
       
   208 			TraceInformation information) {
       
   209 		boolean ruleAdded = false;
       
   210 
       
   211 		// Information must be defined
       
   212 		if (information != null && information.isDefined()) {
       
   213 
       
   214 			// Loop through the component rules
       
   215 			int len = componentRules.size();
       
   216 			ColorTreeComponentItem rule;
       
   217 			for (int i = 0; i < len; i++) {
       
   218 				rule = componentRules.get(i);
       
   219 				// Get component ID
       
   220 				int compId = rule.getComponentId();
       
   221 
       
   222 				// Component ID matches
       
   223 				if (compId == BasePropertyDialog.WILDCARD_INTEGER
       
   224 						|| compId == information.getComponentId()) {
       
   225 
       
   226 					// Get group ID
       
   227 					int groupId = rule.getGroupId();
       
   228 
       
   229 					// Group ID matches
       
   230 					if (groupId == BasePropertyDialog.WILDCARD_INTEGER
       
   231 							|| groupId == information.getGroupId()) {
       
   232 
       
   233 						addRange(lineStart, lineLength, rule
       
   234 								.getForegroundColor(), rule
       
   235 								.getBackgroundColor());
       
   236 						ruleAdded = true;
       
   237 						break;
       
   238 					}
       
   239 				}
       
   240 			}
       
   241 		}
       
   242 
       
   243 		return ruleAdded;
       
   244 	}
       
   245 
       
   246 	/**
       
   247 	 * Processes text rules
       
   248 	 * 
       
   249 	 * @param lineStart
       
   250 	 *            line start offset
       
   251 	 * @param lineLength
       
   252 	 *            line length
       
   253 	 * @param line
       
   254 	 *            line string
       
   255 	 * @return true if rule was added to this line
       
   256 	 */
       
   257 	private boolean processTextRules(int lineStart, int lineLength, String line) {
       
   258 		boolean ruleAdded = false;
       
   259 
       
   260 		// Get the rules and loop through them
       
   261 		int len = textRules.size();
       
   262 		ColorTreeTextItem rule;
       
   263 		for (int i = 0; i < len; i++) {
       
   264 			rule = textRules.get(i);
       
   265 			String colorRule = rule.getTextToCompare();
       
   266 
       
   267 			// Case insensitive
       
   268 			if (!rule.isMatchCase()) {
       
   269 				line = line.toLowerCase();
       
   270 			}
       
   271 
       
   272 			if (line.contains(colorRule)) {
       
   273 				addRange(lineStart, lineLength, rule.getForegroundColor(), rule
       
   274 						.getBackgroundColor());
       
   275 				ruleAdded = true;
       
   276 				break;
       
   277 			}
       
   278 		}
       
   279 		return ruleAdded;
       
   280 	}
       
   281 
       
   282 	/**
       
   283 	 * Gets color dialog
       
   284 	 * 
       
   285 	 * @return color dialog
       
   286 	 */
       
   287 	public ColorDialog getColorDialog() {
       
   288 		if (colorDialog == null) {
       
   289 			colorDialog = (ColorDialog) TraceViewerGlobals.getTraceViewer()
       
   290 					.getDialogs().createDialog(Dialog.COLOR);
       
   291 		}
       
   292 		return colorDialog;
       
   293 	}
       
   294 
       
   295 	/**
       
   296 	 * Gets style ranges
       
   297 	 * 
       
   298 	 * @return style ranges
       
   299 	 */
       
   300 	public List<StyleRange> getRanges() {
       
   301 		return ranges;
       
   302 	}
       
   303 
       
   304 	/**
       
   305 	 * Clears style ranges
       
   306 	 */
       
   307 	public void clearRanges() {
       
   308 		ranges.clear();
       
   309 	}
       
   310 
       
   311 	/**
       
   312 	 * Add new style range
       
   313 	 * 
       
   314 	 * @param lineStart
       
   315 	 *            offset of line start
       
   316 	 * @param lineLength
       
   317 	 *            line length
       
   318 	 * @param foregroundColor
       
   319 	 *            foreground color
       
   320 	 * @param backgroundColor
       
   321 	 *            background color
       
   322 	 */
       
   323 	public void addRange(int lineStart, int lineLength, Color foregroundColor,
       
   324 			Color backgroundColor) {
       
   325 		ranges.add(new StyleRange(lineStart, lineLength, foregroundColor,
       
   326 				backgroundColor));
       
   327 	}
       
   328 
       
   329 	/**
       
   330 	 * Get visible root of the color tree
       
   331 	 * 
       
   332 	 * @return the root
       
   333 	 */
       
   334 	public ColorTreeItem getRoot() {
       
   335 		return root;
       
   336 	}
       
   337 
       
   338 	/**
       
   339 	 * Returns tree item listener
       
   340 	 * 
       
   341 	 * @return the contentProvider
       
   342 	 */
       
   343 	public TreeItemListener getTreeItemListener() {
       
   344 		return contentProvider;
       
   345 	}
       
   346 
       
   347 	/**
       
   348 	 * Gets plain text color rules
       
   349 	 * 
       
   350 	 * @return plain text color rules
       
   351 	 */
       
   352 	public List<ColorTreeTextItem> getTextRules() {
       
   353 		return textRules;
       
   354 	}
       
   355 
       
   356 	/**
       
   357 	 * Gets component color rules
       
   358 	 * 
       
   359 	 * @return component color rules
       
   360 	 */
       
   361 	public List<ColorTreeComponentItem> getComponentRules() {
       
   362 		return componentRules;
       
   363 	}
       
   364 
       
   365 	/**
       
   366 	 * Indicates that some coloring rules are applied
       
   367 	 * 
       
   368 	 * @return true if some coloring rules are applied
       
   369 	 */
       
   370 	public boolean isColoring() {
       
   371 		boolean hasRules = false;
       
   372 		if (!textRules.isEmpty() || !componentRules.isEmpty()) {
       
   373 			hasRules = true;
       
   374 		}
       
   375 		return hasRules;
       
   376 	}
       
   377 
       
   378 	/**
       
   379 	 * Enable color rule
       
   380 	 * 
       
   381 	 * @param item
       
   382 	 *            the rule item
       
   383 	 */
       
   384 	public void enableRule(ColorTreeItem item) {
       
   385 		if (item instanceof ColorTreeTextItem) {
       
   386 			textRules.add((ColorTreeTextItem) item);
       
   387 		} else if (item instanceof ColorTreeComponentItem) {
       
   388 			componentRules.add((ColorTreeComponentItem) item);
       
   389 		}
       
   390 	}
       
   391 
       
   392 }