001    package nl.cwi.sen1.gui.plugin.prefusedot;
002    
003    import java.awt.Shape;
004    import java.awt.geom.Ellipse2D;
005    import java.awt.geom.Point2D;
006    import java.awt.geom.Rectangle2D;
007    
008    import prefuse.render.AbstractShapeRenderer;
009    import prefuse.visual.VisualItem;
010    
011    public class DotNodeRenderer extends AbstractShapeRenderer {
012            private Rectangle2D box = new Rectangle2D.Double();
013    
014            private Ellipse2D ellipse = new Ellipse2D.Double();
015    
016            private Point2D intersection[] = new Point2D[2];
017    
018            public DotNodeRenderer() {
019                    super();
020                    intersection[0] = new Point2D.Float();
021                    intersection[1] = new Point2D.Float();
022                    System.err.println("Init DotNodeRenderer");
023            }
024    
025            
026    
027            public Shape getRawShape(VisualItem item) {
028                    Rectangle2D bounds = ((Rectangle2D) item.get(VisualItem.BOUNDS))
029                                    .getFrame();
030                    double x = item.getX();
031                    double y = item.getY();
032                    double w = bounds.getWidth();
033                    double h = bounds.getHeight();
034                    String shapeName = item.getString(DotAdapter.DOT_SHAPE);
035                    Shape result;
036    
037                    // TODO: implement diamond and other shapes
038                    if (shapeName != null) {
039                            if (shapeName.equals("box")) {
040                                    result = getBoxShape(x, y, w, h);
041                            } else if (shapeName.equals("circle")) {
042                                    result = getCircleShape(x, y, w, h);
043                            } else if (shapeName.equals("ellipse")) {
044                                    result = getEllipseShape(x, y, w, h);
045                            } else {
046                                    result = getBoxShape(x, y, w, h);
047                            }
048                    } else {
049                            result = getBoxShape(x, y, w, h);
050                    }
051    
052                    return result;
053            }
054    
055            private Shape getBoxShape(double x, double y, double width, double height) {
056                    box.setFrame(x, y, width, height);
057                    return box;
058            }
059    
060            private Shape getCircleShape(double x, double y, double width, double height) {
061                    double radius = Math.max(width, height);
062                    ellipse.setFrame(x, y, radius, radius);
063                    return ellipse;
064            }
065    
066            private Shape getEllipseShape(double x, double y, double width,
067                            double height) {
068                    ellipse.setFrame(x, y, width, height);
069                    return ellipse;
070            }
071    
072    }