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
00022
00023
00024 public class ColorTable implements IProcedureTable
00025 {
00026 static final public int COLOR_ICON_SIZE = 8;
00027 static private ColorImagePair IMAGE_WHITE;
00028
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
00044
00045 procNames.add(CallPath.NULL_FUNCTION);
00046
00047 display = Util.getActiveShell().getDisplay();
00048
00049
00050
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
00093
00094
00095
00096 public void setColor(String name, RGB rgb) {
00097
00098 final ColorImagePair oldValue = colorMatcher.get(name);
00099 if (oldValue != null) {
00100 oldValue.dispose();
00101 }
00102
00103 final ColorImagePair newValue = new ColorImagePair(new Color(display,rgb));
00104 colorMatcher.put(name, newValue);
00105 }
00106
00107
00108
00109
00110
00111 public void setColorTable()
00112 {
00113
00114 classMap = new ProcedureClassMap(display);
00115
00116 if (colorMatcher != null)
00117 dispose();
00118
00119
00120
00121 colorMatcher = new HashMap<String,ColorImagePair>();
00122 {
00123
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
00148
00149
00150
00151
00152 public void addProcedure(String name)
00153 {
00154 if(!procNames.contains(name))
00155 procNames.add(name);
00156 }
00157
00158
00159
00160
00161
00162
00163
00164
00165
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
00176
00177
00178
00179
00180
00181
00182
00183
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
00200
00201
00202
00203 private class ColorImagePair {
00204 private Color color;
00205 private Image image;
00206
00207
00208
00209
00210
00211 ColorImagePair(Color c) {
00212
00213 image = ColorTable.createImage(display, c.getRGB());
00214 color = c;
00215 }
00216
00217
00218
00219
00220
00221 public Color getColor() {
00222 return this.color;
00223 }
00224
00225
00226
00227
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 }