ColorTable.java

Go to the documentation of this file.
00001 package edu.rice.cs.hpc.traceviewer.data.graph;
00002 
00003 import java.util.ArrayList;
00004 import java.util.HashMap;
00005 import java.util.Random;
00006 
00007 import org.eclipse.swt.SWT;
00008 import org.eclipse.swt.graphics.Color;
00009 import org.eclipse.swt.graphics.Image;
00010 import org.eclipse.swt.graphics.ImageData;
00011 import org.eclipse.swt.graphics.PaletteData;
00012 import org.eclipse.swt.graphics.RGB;
00013 import org.eclipse.swt.widgets.Display;
00014 
00015 import edu.rice.cs.hpc.common.ui.Util;
00016 import edu.rice.cs.hpc.common.util.ProcedureClassData;
00017 import edu.rice.cs.hpc.data.util.IProcedureTable;
00018 import edu.rice.cs.hpc.traceviewer.data.util.ProcedureClassMap;
00019 
00020 /**************************************************************
00021  * A data structure designed to hold all the name-color pairs
00022  * needed for the actual drawing.
00023  **************************************************************/
00024 public class ColorTable implements IProcedureTable
00025 {
00026     static final public int COLOR_ICON_SIZE = 8;
00027     static private ColorImagePair IMAGE_WHITE;
00028     // data members
00029     HashMap<String, ColorImagePair> colorMatcher;
00030     
00032     ArrayList<String> procNames;
00033     
00035     Display display;
00036     
00037     private ProcedureClassMap classMap;
00038     
00040     public ColorTable()
00041     {
00042         procNames = new ArrayList<String>();
00043         // Initializes the CSS that represents time values outside of the
00044         // time-line.
00045         procNames.add(CallPath.NULL_FUNCTION);
00046         
00047         display = Util.getActiveShell().getDisplay();
00048         
00049         // create our own white color so we can dispose later, instead of disposing
00050         //  Eclipse's white color
00051         final RGB rgb_white = display.getSystemColor(SWT.COLOR_WHITE).getRGB();
00052         IMAGE_WHITE = new ColorImagePair( new Color(display, rgb_white));
00053     }
00054     
00058     public void dispose() {
00059         for (ColorImagePair pair: colorMatcher.values()) {
00060             pair.dispose();
00061         }
00062         IMAGE_WHITE.dispose();
00063     }
00064     
00070     public Color getColor(String name)
00071     {
00072         return colorMatcher.get(name).getColor();
00073     }
00074     
00080     public Image getImage(String name) 
00081     {
00082         final ColorImagePair cipair = colorMatcher.get(name);
00083         if (cipair != null) {
00084             return cipair.getImage();
00085         } else {
00086             return null;
00087         }
00088     }
00089     
00090 
00091     /***
00092      * set the procedure name with a new color
00093      * @param name
00094      * @param color
00095      */
00096     public void setColor(String name, RGB rgb) {
00097         // dispose old value
00098         final ColorImagePair oldValue = colorMatcher.get(name);
00099         if (oldValue != null) {
00100             oldValue.dispose();
00101         }
00102         // create new value
00103         final ColorImagePair newValue = new ColorImagePair(new Color(display,rgb));
00104         colorMatcher.put(name, newValue);
00105     }
00106     
00107     /*********************************************************************
00108      * Fills the colorMatcher with unique "random" colors that correspond
00109      * to each function name in procNames.
00110      *********************************************************************/
00111     public void setColorTable()
00112     {   
00113         // initialize the procedure-color map
00114         classMap = new ProcedureClassMap(display);
00115 
00116         if (colorMatcher != null)
00117             dispose();
00118         
00119         //This is where the data file is converted to the colorTable using colorMatcher.
00120         //creates name-function-color colorMatcher for each function.
00121         colorMatcher = new HashMap<String,ColorImagePair>();
00122         {
00123             // rework the color assignment to use a single random number stream
00124             Random r = new Random((long)612543231);
00125             int cmin = 16;
00126             int cmax = 200 - cmin;
00127             for (int l=0; l<procNames.size(); l++) {
00128                 
00129                 String procName = procNames.get(l);
00130                 
00131                 if (procName != CallPath.NULL_FUNCTION) {
00132                     
00133                     if (!colorMatcher.containsKey(procName)) {
00134                         
00135                         RGB rgb = getProcedureColor( procName, cmin, cmax, r );
00136                         Color c = new Color(display, rgb);
00137                         colorMatcher.put(procName, new ColorImagePair(c));
00138                     }
00139                 } else {
00140                     colorMatcher.put(procName, IMAGE_WHITE);
00141                 }
00142             }
00143         }
00144     }
00145     
00146     /************************************************************************
00147      * Adds a name to the list of function names in this ColorTable.
00148      * NOTE: Doesn't create a color for this name. All the color creating
00149      * is done in setColorTable.
00150      * @param name The function name to be added.
00151      ************************************************************************/
00152     public void addProcedure(String name)
00153     {
00154         if(!procNames.contains(name))
00155             procNames.add(name);
00156     }
00157     
00158     
00159     /***********************************************************************
00160      * create an image based on the color
00161      * the caller is responsible to free the image
00162      * 
00163      * @param display
00164      * @param color
00165      * @return an image (to be freed)
00166      ***********************************************************************/
00167     static public Image createImage(Display display, RGB color) {
00168         PaletteData palette = new PaletteData(new RGB[] {color} );
00169         ImageData imgData = new ImageData(COLOR_ICON_SIZE, COLOR_ICON_SIZE, 1, palette);
00170         Image image = new Image(display, imgData);
00171         return image;
00172     }
00173     
00174     /***********************************************************************
00175      * retrieve color for a procedure. If the procedure has been assigned to
00176      *  a color, we'll return the allocated color, otherwise, create a new one
00177      *  randomly.
00178      * 
00179      * @param name
00180      * @param colorMin
00181      * @param colorMax
00182      * @param r
00183      * @return
00184      ***********************************************************************/
00185     private RGB getProcedureColor( String name, int colorMin, int colorMax, Random r ) {
00186         ProcedureClassData value = this.classMap.get(name);
00187         final RGB rgb;
00188         if (value != null)
00189             rgb = value.getRGB();
00190         else 
00191             rgb = new RGB(  colorMin + r.nextInt(colorMax), 
00192                             colorMin + r.nextInt(colorMax), 
00193                             colorMin + r.nextInt(colorMax));
00194         return rgb;
00195     }
00196     
00197     
00198     /************************************************************************
00199      * class to pair color and image
00200      * @author laksonoadhianto
00201      *
00202      ************************************************************************/
00203     private class ColorImagePair {
00204         private Color color;
00205         private Image image;
00206         
00207         /****
00208          * create a color-image pair
00209          * @param color c
00210          */
00211         ColorImagePair(Color c) {
00212             // create an empty image filled with color c
00213             image = ColorTable.createImage(display, c.getRGB());
00214             color = c;
00215         }
00216         
00217         /***
00218          * get the color 
00219          * @return
00220          */
00221         public Color getColor() {
00222             return this.color;
00223         }
00224         
00225         /***
00226          * get the image
00227          * @return
00228          */
00229         public Image getImage() {
00230             return this.image;
00231         }
00232         
00233         public void dispose() {
00234             this.color.dispose();
00235             this.image.dispose();
00236         }
00237     }
00238 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1