001 package nl.cwi.sen1.gui.plugin; 002 003 import java.awt.FontMetrics; 004 import java.awt.Graphics2D; 005 import java.awt.Polygon; 006 import java.awt.Shape; 007 import java.awt.geom.Ellipse2D; 008 import java.awt.geom.Rectangle2D; 009 010 import prefuse.render.LabelRenderer; 011 import prefuse.util.ColorLib; 012 import prefuse.visual.VisualItem; 013 014 public class GraphNodeRenderer extends LabelRenderer { 015 016 public GraphNodeRenderer(String string) { 017 super(string); 018 } 019 020 protected Shape getRawShape(VisualItem item) { 021 Rectangle2D bounds = super.getRawShape(item).getBounds2D(); 022 double x = bounds.getX(); 023 double y = bounds.getY(); 024 double width = bounds.getWidth(); 025 double height = bounds.getHeight(); 026 Shape result = null; 027 028 nl.cwi.sen1.graph.types.Shape shape = (nl.cwi.sen1.graph.types.Shape) item 029 .get(GraphConstants.SHAPE); 030 if (shape.isDiamond()) { 031 result = getDiamondShape(x, y, width, height); 032 } else if (shape.isBox()) { 033 result = getBoxShape(x, y, width, height); 034 } else if (shape.isEllipse()) { 035 result = getEllipseShape(x, y, width, height); 036 } else if (shape.isCircle()) { 037 result = getCircleShape(x, y, width, height); 038 } else { 039 result = getBoxShape(x, y, width, height); 040 } 041 042 return new GraphCompositeShape(super.getRawShape(item), result); 043 } 044 045 private Shape getCircleShape(double x, double y, double width, double height) { 046 double radius = Math.max(width, height); 047 return new Ellipse2D.Double(x, y, radius, radius); 048 } 049 050 private Shape getEllipseShape(double x, double y, double width, 051 double height) { 052 return new Ellipse2D.Double(x, y, width, height); 053 } 054 055 private Shape getBoxShape(double x, double y, double width, double height) { 056 return new Rectangle2D.Double(x, y, width, height); 057 } 058 059 private Shape getDiamondShape(double x, double y, double width, 060 double height) { 061 int ix = (int) x; 062 int iy = (int) y; 063 int iw = (int) width; 064 int ih = (int) height; 065 int[] xs = new int[] { ix - (iw / ih) * (ih / 2), ix + iw / 2, 066 ix + iw + (iw / ih) * (ih / 2), ix + iw / 2 }; 067 int[] ys = new int[] { iy + ih / 2, iy + ih + (ih / iw) * (iw / 2), 068 iy + ih / 2, iy - (ih / iw) * (iw / 2) }; 069 070 return new Polygon(xs, ys, 4); 071 } 072 073 public void render(Graphics2D g, VisualItem item) { 074 GraphCompositeShape comp = (GraphCompositeShape) getShape(item); 075 076 Shape outer = comp.getOuterShape(); 077 drawShape(g, item, outer); 078 079 Shape inner = comp.getInnerShape(); 080 renderText(g, item, inner); 081 } 082 083 public void renderText(Graphics2D g, VisualItem item, Shape shape) { 084 String s = getText(item); 085 int textColor = item.getTextColor(); 086 if (s != null) { 087 Rectangle2D r = shape.getBounds2D(); 088 g.setPaint(ColorLib.getColor(textColor)); 089 g.setFont(m_font); 090 FontMetrics fm = g.getFontMetrics(); 091 double size = item.getSize(); 092 double x = r.getX() + size * m_horizBorder; 093 double y = r.getY() + size * m_vertBorder; 094 g.drawString(s, (float) x, (float) y + fm.getAscent()); 095 // if ( isHyperlink(item) ) { 096 // int lx = (int)Math.round(x), ly = (int)Math.round(y); 097 // g.drawLine(lx,ly,lx+fm.stringWidth(s),ly+fm.getHeight()-1); 098 // } 099 } 100 } 101 }