Search in sources :

Example 1 with Layout

use of org.kohsuke.graph_layouter.Layout in project hudson-2.x by hudson.

the class DependencyGraph method doGraph.

/**
     * Experimental visualization of project dependencies.
     */
public void doGraph(StaplerRequest req, StaplerResponse rsp) throws IOException {
    // Require admin permission for now (avoid exposing project names with restricted permissions)
    Hudson.getInstance().checkPermission(Hudson.ADMINISTER);
    try {
        // creates a dummy graphics just so that we can measure font metrics
        BufferedImage emptyImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = emptyImage.createGraphics();
        graphics.setFont(FONT);
        final FontMetrics fontMetrics = graphics.getFontMetrics();
        // TODO: timestamp check
        Layout<AbstractProject> layout = new Layout<AbstractProject>(new Navigator<AbstractProject>() {

            public Collection<AbstractProject> vertices() {
                // only include projects that have some dependency
                List<AbstractProject> r = new ArrayList<AbstractProject>();
                for (AbstractProject p : Hudson.getInstance().getAllItems(AbstractProject.class)) {
                    if (!getDownstream(p).isEmpty() || !getUpstream(p).isEmpty())
                        r.add(p);
                }
                return r;
            }

            public Collection<AbstractProject> edge(AbstractProject p) {
                return getDownstream(p);
            }

            public Dimension getSize(AbstractProject p) {
                int w = fontMetrics.stringWidth(p.getDisplayName()) + MARGIN * 2;
                return new Dimension(w, fontMetrics.getHeight() + MARGIN * 2);
            }
        }, Direction.LEFTRIGHT);
        Rectangle area = layout.calcDrawingArea();
        // give it a bit of margin
        area.grow(4, 4);
        BufferedImage image = new BufferedImage(area.width, area.height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = image.createGraphics();
        g2.setTransform(AffineTransform.getTranslateInstance(-area.x, -area.y));
        g2.setPaint(Color.WHITE);
        g2.fill(area);
        g2.setFont(FONT);
        g2.setPaint(Color.BLACK);
        for (AbstractProject p : layout.vertices()) {
            final Point sp = center(layout.vertex(p));
            for (AbstractProject q : layout.edges(p)) {
                Point cur = sp;
                for (Point pt : layout.edge(p, q)) {
                    g2.drawLine(cur.x, cur.y, pt.x, pt.y);
                    cur = pt;
                }
                final Point ep = center(layout.vertex(q));
                g2.drawLine(cur.x, cur.y, ep.x, ep.y);
            }
        }
        int diff = fontMetrics.getAscent() + fontMetrics.getLeading() / 2;
        for (AbstractProject p : layout.vertices()) {
            Rectangle r = layout.vertex(p);
            g2.setPaint(Color.WHITE);
            g2.fillRect(r.x, r.y, r.width, r.height);
            g2.setPaint(Color.BLACK);
            g2.drawRect(r.x, r.y, r.width, r.height);
            g2.drawString(p.getDisplayName(), r.x + MARGIN, r.y + MARGIN + diff);
        }
        rsp.setContentType("image/png");
        ServletOutputStream os = rsp.getOutputStream();
        ImageIO.write(image, "PNG", os);
        os.close();
    } catch (HeadlessException e) {
        // not available. send out error message
        rsp.sendRedirect2(req.getContextPath() + "/images/headless.png");
    }
}
Also used : HeadlessException(java.awt.HeadlessException) ServletOutputStream(javax.servlet.ServletOutputStream) Rectangle(java.awt.Rectangle) Dimension(java.awt.Dimension) Point(java.awt.Point) BufferedImage(java.awt.image.BufferedImage) Point(java.awt.Point) Graphics2D(java.awt.Graphics2D) Layout(org.kohsuke.graph_layouter.Layout) FontMetrics(java.awt.FontMetrics) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Dimension (java.awt.Dimension)1 FontMetrics (java.awt.FontMetrics)1 Graphics2D (java.awt.Graphics2D)1 HeadlessException (java.awt.HeadlessException)1 Point (java.awt.Point)1 Rectangle (java.awt.Rectangle)1 BufferedImage (java.awt.image.BufferedImage)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 List (java.util.List)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 Layout (org.kohsuke.graph_layouter.Layout)1