sysperfana/analyzetoolext/com.nokia.s60tools.analyzetool/src/com/nokia/s60tools/analyzetool/internal/ui/graph/YAxis.java
changeset 1 1050670c6980
child 6 f65f740e69f9
equal deleted inserted replaced
0:5ad7ad99af01 1:1050670c6980
       
     1 /*
       
     2  * Copyright (c) 2008-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:  Definitions for the class YAxis
       
    15  *
       
    16  */
       
    17 package com.nokia.s60tools.analyzetool.internal.ui.graph;
       
    18 import java.text.DecimalFormat;
       
    19 
       
    20 import org.eclipse.core.runtime.IProgressMonitor;
       
    21 import org.eclipse.core.runtime.IStatus;
       
    22 import org.eclipse.core.runtime.Status;
       
    23 import org.eclipse.swt.SWT;
       
    24 import org.eclipse.swt.graphics.GC;
       
    25 import org.eclipse.swt.graphics.Image;
       
    26 import org.eclipse.swt.graphics.Transform;
       
    27 import org.eclipse.swt.widgets.Canvas;
       
    28 import org.eclipse.swt.widgets.Composite;
       
    29 import org.eclipse.swt.widgets.Display;
       
    30 import org.eclipse.ui.progress.UIJob;
       
    31 
       
    32 import com.nokia.s60tools.analyzetool.engine.IMemoryActivityModel;
       
    33 import com.nokia.s60tools.analyzetool.engine.IMemoryActivityModelChangeListener;
       
    34 import com.nokia.s60tools.analyzetool.engine.statistic.ProcessInfo;
       
    35 import com.nokia.s60tools.analyzetool.internal.ui.util.GraphUtils;
       
    36 
       
    37 /**
       
    38  * FigureCanvas containing the Y axis of the AnalyzeTool graph
       
    39  *
       
    40  */
       
    41 public class YAxis extends Canvas implements IMemoryActivityModelChangeListener {
       
    42 	private static final int YLEGENDSPACE = 60;
       
    43 	private static final DecimalFormat MB_FORMAT = new DecimalFormat("#####.0");
       
    44 	private IMemoryActivityModel model;
       
    45 	protected int visualSizeY = 0;
       
    46 	private UIJob iRefreshUIJob;
       
    47 	private Image bytesImage;
       
    48 
       
    49 	/**
       
    50 	 * Constructor
       
    51 	 * @param parent
       
    52 	 */
       
    53 	public YAxis(Composite parent) {
       
    54 		super(parent, SWT.NONE);
       
    55 	}
       
    56 	/**
       
    57 	 * To be called once after construction.
       
    58 	 */
       
    59 	public void createContent() {
       
    60 		iRefreshUIJob = new UIJob("Updating Memory Activity Graph"){
       
    61 
       
    62 			@Override
       
    63 			public IStatus runInUIThread(IProgressMonitor arg0) {
       
    64 				// Refresh the graph
       
    65 				redraw();
       
    66 				return Status.OK_STATUS;
       
    67 			}
       
    68 			
       
    69 		};
       
    70 		iRefreshUIJob.setSystem(true);		
       
    71 	}
       
    72 
       
    73 	/**
       
    74 	 * Sets a new model as input
       
    75 	 * @param model
       
    76 	 */
       
    77 	public void setInput(IMemoryActivityModel model) {
       
    78 		if (this.model != null){
       
    79 			this.model.removeListener(this);
       
    80 		}
       
    81 		this.model = model;
       
    82 		this.model.addListener(this);
       
    83 	}
       
    84 	
       
    85 	/**
       
    86 	 * Called when paint event occurs. 
       
    87 	 * @param gc The GC to draw on
       
    88 	 */
       
    89 	@SuppressWarnings("cast")
       
    90 	public void paintYAxis(GC gc){
       
    91 		
       
    92 		
       
    93 		double visY = visualSizeY - 50;
       
    94 		//max value influences unit of measure on graph
       
    95 		int prettyBytes = GraphUtils.prettyMaxBytes(model.getHighestCumulatedMemoryAlloc());
       
    96 		double multiplier = prettyBytes / visY;
       
    97 		
       
    98 		double yIncrement = visY / 10;
       
    99 		int previousBottom = 0;
       
   100 		
       
   101 		for (double k = visY; k >= 0; k-=yIncrement)
       
   102 		{
       
   103 			// location for the value indicator is k * 1/10 the height of the display
       
   104 			int y = (int) (visY - k);
       
   105 		
       
   106 			int bytes = (int)(Math.ceil(k * multiplier));
       
   107 
       
   108 			String legend = "";
       
   109 			
       
   110 			if (prettyBytes < 10*1024)
       
   111 			{
       
   112 				legend += bytes + " B"; 
       
   113 			}
       
   114 			else if (prettyBytes <= 500 * 1024)
       
   115 			{
       
   116 				legend += (bytes / 1024) + " KB"; 
       
   117 			}
       
   118 			else
       
   119 			{
       
   120 				legend +=  MB_FORMAT.format(((float) bytes / (1024 * 1024)))  + " MB";
       
   121 			}
       
   122 			org.eclipse.swt.graphics.Point extent = gc.stringExtent(legend);
       
   123 			
       
   124 			gc.drawLine(YLEGENDSPACE - 3, (int)y + 1, YLEGENDSPACE, (int)y + 1);
       
   125 			
       
   126 			if (y >= previousBottom)
       
   127 			{
       
   128 				gc.drawString(legend, YLEGENDSPACE - extent.x -2, (int)y);
       
   129 				previousBottom = (int)y + extent.y;
       
   130 			}
       
   131 		}
       
   132 	
       
   133 		if (bytesImage == null){
       
   134 			bytesImage = GraphUtils.getVerticalLabel("Bytes");			
       
   135 		}
       
   136 		gc.setAdvanced(true);
       
   137 	    final org.eclipse.swt.graphics.Rectangle rect2 = bytesImage.getBounds();
       
   138         Transform transform = new Transform(Display.getDefault());
       
   139 
       
   140         transform.translate(rect2.height / 2f, rect2.width / 2f);
       
   141         transform.rotate(-90);
       
   142         transform.translate(-rect2.width / 2f, -rect2.height / 2f);
       
   143 
       
   144         gc.setTransform(transform);
       
   145         gc.drawImage(bytesImage, -(int)visY/3, 1);
       
   146         
       
   147         transform.dispose();
       
   148 	}
       
   149 	
       
   150 	/**
       
   151 	 * @return the visible height of the control
       
   152 	 */
       
   153 	public int getHeight() {
       
   154 		return visualSizeY;
       
   155 	}
       
   156 	/**
       
   157 	 * Sets the visible height of the control
       
   158 	 * @param height the height to set
       
   159 	 */
       
   160 	public void setHeight(int height) {
       
   161 		visualSizeY = height;
       
   162 	}
       
   163 
       
   164 	/* (non-Javadoc)
       
   165 	 * @see com.nokia.s60tools.analyzetool.engine.IMemoryActivityModelChangeListener#onProcessesAdded()
       
   166 	 */
       
   167 	public void onProcessesAdded() {
       
   168 		iRefreshUIJob.cancel();
       
   169 		iRefreshUIJob.schedule();
       
   170 	}
       
   171 
       
   172 	/* (non-Javadoc)
       
   173 	 * @see com.nokia.s60tools.analyzetool.engine.IMemoryActivityModelChangeListener#onProcessSelected(com.nokia.s60tools.analyzetool.engine.statistic.ProcessInfo)
       
   174 	 */
       
   175 	public void onProcessSelected(ProcessInfo p) {
       
   176 		iRefreshUIJob.cancel();
       
   177 		iRefreshUIJob.schedule();
       
   178 	}
       
   179 	
       
   180 //	public void dispose() {
       
   181 //	//TODO cancel ui jobs here and remove listeners
       
   182 //	//there is no dispose() entry point in this class; we may have to call this via MainView.dispose()
       
   183 //}
       
   184 
       
   185 }