package org.gvt.figure;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import java.util.HashMap;
import java.util.Map;
/**
* The specific information box.
*
* @author Ozgun Babur
*/
public class InfoFigure extends Figure
{
/**
* Full info to be displayed.
*/
private String info;
/**
* Order number of the info box.
*/
private int i;
/**
* Width and height of this box.
*/
private Dimension dim;
public InfoFigure(String info, int i, Rectangle parentBounds, Dimension dim)
{
this.i = i;
this.dim = dim;
// Debug code -- Prints distinct info strings
// if (!encountered.contains(info))
// {
// System.out.println("info = " + info);
// encountered.add(info);
// }
//------------------------
updateBounds(parentBounds);
Label label = new Label(getLetter(info));
label.setToolTip(new Label(info));
Rectangle r = getBounds().getCopy();
r.y -= 2;
r.x += 1;
label.setBounds(r);
label.setFont(FONT);
// Get rid of position information for accurate coloring using color maps
if (info.indexOf("@") > 0) this.info = info.substring(0, info.indexOf("@")).trim();
else if (info.indexOf("[") > 0) this.info = info.substring(0, info.indexOf("[")).trim();
else this.info = info;
label.setForegroundColor(getForeColor(this.info));
this.add(label);
}
protected void paintFigure(Graphics g)
{
g.setAntialias(SWT.ON);
Rectangle r = getParent().getBounds().getCopy();
r.x += 2;
r.width -= 4;
updateBounds(r);
String lett = getLetter(info);
// Calculate the point to show the info
Point p = getLoc(r);
// Adjust background and foregound colors
g.setBackgroundColor(getBackColor(info));
g.setForegroundColor(getBordColor(info));
// Draw the info base according to the corresponding shape
int shp = getInfoShape(lett);
switch(shp)
{
case OVAL:
g.fillOval(p.x, p.y, dim.width, dim.height);
g.drawOval(p.x, p.y, dim.width-1, dim.height-1);
break;
case RECT:
g.fillRectangle(p.x, p.y, dim.width, dim.height);
g.drawRectangle(p.x, p.y, dim.width-1, dim.height-1);
break;
}
}
private void updateBounds(Rectangle parentBounds)
{
setBounds(new Rectangle(getLoc(parentBounds), dim));
}
private Point getLoc(Rectangle rec)
{
int spannum = i / 4;
int spansign = i % 2 == 0 ? 1 : -1;
int y = (i % 4) < 2 ? rec.y : rec.height + rec.y - dim.height;
int x = (i % 2 == 0 ? rec.x : rec.width + rec.x - dim.width) +
(spannum * spansign * dim.width);
return new Point(x, y);
}
protected Color getBackColor(String info)
{
Color c = backColorMap.get(info.toLowerCase());
return c == null ? Character.isDigit(info.charAt(0)) ?
DIGIT_BACK_COLOR : DEFAULT_BACK_COLOR : c;
}
protected Color getForeColor(String info)
{
Color c = foreColorMap.get(info.toLowerCase());
return c == null ? Character.isDigit(info.charAt(0)) ?
DIGIT_FORE_COLOR : DEFAULT_FORE_COLOR : c;
}
protected Color getBordColor(String info)
{
Color c = bordColorMap.get(info.toLowerCase());
return c == null ? DEFAULT_BORD_COLOR : c;
}
protected int getInfoShape(String letter)
{
if (Character.isDigit(letter.charAt(0)) ||
info.equals("active") || info.equals("active tf") ||
info.equals("native") || info.equals("inactive"))
{
return RECT;
}
else
{
return OVAL;
}
}
protected String getLetter(String info)
{
String let = letterMap.get(info);
return let == null ? info.substring(0, 1).toLowerCase() : let;
}
protected static final Font FONT = new Font(null, "Segoe UI", 7, 0);
protected static final Color DIGIT_BACK_COLOR = new Color(null, 250, 250, 250);
protected static final Color DIGIT_FORE_COLOR = new Color(null, 150, 0, 0);
protected static final Color DEFAULT_BACK_COLOR = new Color(null, 50, 50, 50);
protected static final Color DEFAULT_FORE_COLOR = new Color(null, 255, 255, 255);
protected static final Color DEFAULT_BORD_COLOR = new Color(null, 0, 0, 0);
protected static final Map<String, Color> backColorMap = new HashMap<String, Color>();
protected static final Map<String, Color> foreColorMap = new HashMap<String, Color>();
protected static final Map<String, Color> bordColorMap = new HashMap<String, Color>();
protected static final Map<String, String> letterMap = new HashMap<String, String>();
static
{
final Color PHOSPHO_BG = new Color(null, 230, 230, 100);
final Color PHOSPHO_FORE = new Color(null, 0, 0, 50);
backColorMap.put("phosphorylation", PHOSPHO_BG); // yellow
foreColorMap.put("phosphorylation", PHOSPHO_FORE); // black like blue
backColorMap.put("phosphorylation site", PHOSPHO_BG); // yellow
foreColorMap.put("phosphorylation site", PHOSPHO_FORE); // black like blue
backColorMap.put("phosphate group", PHOSPHO_BG); // yellow
foreColorMap.put("phosphate group", PHOSPHO_FORE); // black like blue
backColorMap.put("active", new Color(null, 50, 150, 50)); // green
foreColorMap.put("active", new Color(null, 255, 255, 255)); // white
backColorMap.put("active tf", new Color(null, 70, 150, 70)); // green
foreColorMap.put("active tf", new Color(null, 255, 255, 255)); // white
letterMap.put("active tf", "t");
backColorMap.put("inactive", new Color(null, 150, 50, 50)); // red
foreColorMap.put("inactive", new Color(null, 255, 255, 255)); // white
backColorMap.put("native", new Color(null, 200, 200, 200)); // light gray
foreColorMap.put("native", new Color(null, 100, 100, 100)); // dark gray
backColorMap.put("ubiquitination site", new Color(null, 150, 80, 80)); // red
foreColorMap.put("ubiquitination site", new Color(null, 255, 255, 255)); // white
backColorMap.put("chain coordinates", new Color(null, 150, 150, 150)); // light gray
foreColorMap.put("chain coordinates", new Color(null, 255, 255, 255)); // white
}
protected static final int OVAL = 0;
protected static final int RECT = 1;
// public static final Set<String> encountered = new HashSet<String>();
}