sysperfana/memspyext/com.nokia.s60tools.swmtanalyser/src/com/nokia/s60tools/swmtanalyser/ui/graphs/SystemDataGraph.java
changeset 7 8e12a575a9b5
equal deleted inserted replaced
6:f65f740e69f9 7:8e12a575a9b5
       
     1 /*
       
     2 * Copyright (c) 2009 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 */
       
    17 package com.nokia.s60tools.swmtanalyser.ui.graphs;
       
    18 
       
    19 import java.util.ArrayList;
       
    20 import java.util.HashMap;
       
    21 
       
    22 import org.eclipse.draw2d.Graphics;
       
    23 import org.eclipse.draw2d.Polyline;
       
    24 import org.eclipse.draw2d.geometry.PointList;
       
    25 import org.eclipse.swt.graphics.Color;
       
    26 import org.eclipse.swt.graphics.GC;
       
    27 import org.eclipse.swt.graphics.Image;
       
    28 import org.eclipse.swt.graphics.Point;
       
    29 import org.eclipse.swt.graphics.Transform;
       
    30 import org.eclipse.swt.widgets.Display;
       
    31 
       
    32 import com.nokia.s60tools.swmtanalyser.data.KernelElements;
       
    33 import com.nokia.s60tools.swmtanalyser.model.SWMTLogReaderUtils;
       
    34 import com.nokia.s60tools.util.debug.DbgUtility;
       
    35 
       
    36 /**
       
    37  * This class contains all needed logic to paint data related to System data. 
       
    38  */
       
    39 public class SystemDataGraph extends GenericGraph
       
    40 {
       
    41 	private ArrayList<KernelElements> kernelElements;
       
    42 	private HashMap <String, Polyline> pointsData = new HashMap<String, Polyline>();
       
    43 	
       
    44 	private double visY;
       
    45 	private double multiplier;
       
    46 
       
    47 	/* (non-Javadoc)
       
    48 	 * @see com.nokia.s60tools.swmtanalyser.ui.graphs.GenericGraph#paint(org.eclipse.draw2d.Graphics)
       
    49 	 */
       
    50 	public void paint(Graphics graphics) {
       
    51 		
       
    52 		DbgUtility.println(DbgUtility.PRIORITY_OPERATION, this.getClass().getSimpleName() + "/paint START");
       
    53 		
       
    54 		ArrayList<String> selectedItems = this.getUserSelectedItems();
       
    55 		
       
    56 		 if(selectedItems == null)
       
    57 			 return;
       
    58 		
       
    59 		 int [] listX = this.calculateTimeIntervals();
       
    60 		 this.lastSampleTime = listX[listX.length-1];
       
    61 		 		 
       
    62 		 int k=0;
       
    63 
       
    64 		 // Storing drawing settings
       
    65 		 Color origColor = graphics.getForegroundColor();
       
    66 		 int origLineWidth = graphics.getLineWidth();
       
    67 		 // Setting drawing settings
       
    68 		 graphics.setLineWidth(CommonGraphConstants.DEFAULT_GRAPH_LINE_WIDTH);
       
    69 		 
       
    70 		 for(String item: selectedItems)
       
    71 		 { 
       
    72 			 int [] valuesToBePlotted = getValuesForGivenKerenelElement(item, kernelElements);
       
    73 			 
       
    74 			 if(valuesToBePlotted == null)
       
    75 				 return;
       
    76 			 
       
    77 			 int [] points = new int[valuesToBePlotted.length *2];
       
    78 			 
       
    79 			 double visY = visualSizeY - CommonGraphConstants.XLEGENDSPACE;
       
    80 			 
       
    81 			 for (int i = 0, j = 0; i < valuesToBePlotted.length; i++)
       
    82 			 {
       
    83 			    	points[j++] = (int)(listX[i]/getScale());
       
    84 			    	
       
    85 			    	points[j] =(int) (visY - valuesToBePlotted[i] /multiplier);
       
    86 			    	
       
    87 			    	if (points[j] < 0)
       
    88 			    		points[j] = 0;
       
    89 			    	
       
    90 			    	j++;
       
    91 			 }
       
    92 			 
       
    93 			 graphics.setForegroundColor(this.getColors().get(k));
       
    94 			 // Drawing graph
       
    95 			 graphics.drawPolyline(points);
       
    96 			 // Drawing markers to the data points
       
    97 			 GraphsUtils.drawMarkers(graphics, points);
       
    98 			
       
    99 			 Polyline line = new Polyline();
       
   100 			 line.setPoints(new PointList(points));
       
   101 			 pointsData.put(item, line);
       
   102 			 
       
   103 			 k++;
       
   104 		}
       
   105 		 
       
   106 		// Restoring drawing settings
       
   107 		 graphics.setForegroundColor(origColor);
       
   108 		 graphics.setLineWidth(origLineWidth);		 
       
   109 			 
       
   110 		 DbgUtility.println(DbgUtility.PRIORITY_OPERATION, this.getClass().getSimpleName() + "/paint END");
       
   111 	}
       
   112 
       
   113 	/* (non-Javadoc)
       
   114 	 * @see com.nokia.s60tools.swmtanalyser.ui.graphs.GenericGraph#paintYAxis(org.eclipse.swt.graphics.GC)
       
   115 	 */
       
   116 	public void paintYAxis( GC gc) {
       
   117 		
       
   118 		visY = visualSizeY - CommonGraphConstants.XLEGENDSPACE; 
       
   119 		multiplier = GraphsUtils.roundToNearestNumber(maxBytes) / visY;
       
   120 			
       
   121 		double yIncrement = visY / 10;
       
   122 		int previousBottom = 0;
       
   123 		
       
   124 		for (int k = 10; k >= 0; k--)
       
   125 		{
       
   126 			// location for the value indicator is k * 1/10 the height of the display
       
   127 			int y = (int) (visY - (yIncrement * k));
       
   128 		
       
   129 			int bytes = (int)(yIncrement * multiplier)* k;
       
   130 
       
   131 			String legend = "";
       
   132 			legend += bytes ;
       
   133 			
       
   134 			Point extent = gc.stringExtent(legend);
       
   135 			
       
   136 			gc.drawLine(CommonGraphConstants.YLEGENDSPACE - 3, (int)y + 1, CommonGraphConstants.YLEGENDSPACE, (int)y + 1);
       
   137 			
       
   138 			if (y >= previousBottom)
       
   139 			{
       
   140 				gc.drawString(legend, CommonGraphConstants.YLEGENDSPACE - extent.x -2, (int)y);
       
   141 				previousBottom = (int)y + extent.y;
       
   142 			}
       
   143 		}
       
   144 
       
   145 		final Image image = this.getVerticalLabel("Count");
       
   146 		gc.setAdvanced(true);
       
   147 		final org.eclipse.swt.graphics.Rectangle rect2 = image.getBounds();
       
   148 		Transform transform = new Transform(Display.getDefault());
       
   149 
       
   150 		transform.translate(rect2.height / 2f, rect2.width / 2f);
       
   151 		transform.rotate(-90);
       
   152 		transform.translate(-rect2.width / 2f, -rect2.height / 2f);
       
   153 
       
   154 		gc.setTransform(transform);
       
   155 		gc.drawImage(image, -(int)visY/3, 0);
       
   156 
       
   157 		transform.dispose();
       
   158 		gc.dispose();				
       
   159 
       
   160 	}
       
   161 
       
   162 	/* (non-Javadoc)
       
   163 	 * @see com.nokia.s60tools.swmtanalyser.ui.graphs.GenericGraph#prepareData()
       
   164 	 */
       
   165 	public void prepareData() {
       
   166 		
       
   167 		SWMTLogReaderUtils utils = new SWMTLogReaderUtils();
       
   168 		
       
   169 		kernelElements = utils.getKerenelElemsFromAllCycles(this.getCyclesData());
       
   170 		
       
   171 		for(String item:getUserSelectedItems())
       
   172 		 {
       
   173 			 int [] values = getValuesForGivenKerenelElement(item, kernelElements);
       
   174 			 
       
   175 			 int maxValue = calculateMaxValue(values);
       
   176 			 
       
   177 			 if(maxValue > maxBytes){
       
   178 					maxBytes = maxValue;
       
   179 			 }
       
   180 		 }		
       
   181 	}
       
   182 
       
   183 	
       
   184 	/* (non-Javadoc)
       
   185 	 * @see com.nokia.s60tools.swmtanalyser.ui.graphs.GenericGraph#getToolTipText(int, int)
       
   186 	 */
       
   187 	public String getToolTipText(int x, int y)
       
   188 	{
       
   189 		if(y > (int)visY)
       
   190 			return null;
       
   191 		
       
   192 		String text = "";
       
   193 		
       
   194 		double xValue = x + timeOffset;
       
   195 		int scaledX = (int)(xValue * getScale());
       
   196 		int valY = (int)visY - y;
       
   197 		int scaledY = (int)(valY * multiplier);
       
   198 		
       
   199 		text += scaledX + " s, " + scaledY;
       
   200 		
       
   201 		for(String elem: getUserSelectedItems())
       
   202 		{
       
   203 			Polyline line = pointsData.get(elem);
       
   204 		
       
   205 			if(line != null && line.containsPoint(x, y))
       
   206 				text += "\n" + elem;
       
   207 		}
       
   208 		
       
   209 		return text;
       
   210 	}
       
   211 	
       
   212 	/**
       
   213 	 * Get kernel data
       
   214 	 * @return kernel data
       
   215 	 */
       
   216 	public ArrayList<KernelElements> getKernelData()
       
   217 	{
       
   218 		return this.kernelElements;
       
   219 	}
       
   220 }