AWTPlotRenderer.java

  1. /*
  2.  * Copyright (C) 2010, Google Inc.
  3.  * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  4.  * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
  5.  *
  6.  * This program and the accompanying materials are made available under the
  7.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  8.  * https://www.eclipse.org/org/documents/edl-v10.php.
  9.  *
  10.  * SPDX-License-Identifier: BSD-3-Clause
  11.  */

  12. package org.eclipse.jgit.awtui;

  13. import java.awt.Color;
  14. import java.awt.Graphics;
  15. import java.awt.Graphics2D;
  16. import java.awt.Polygon;
  17. import java.io.Serializable;

  18. import org.eclipse.jgit.awtui.CommitGraphPane.GraphCellRender;
  19. import org.eclipse.jgit.awtui.SwingCommitList.SwingLane;
  20. import org.eclipse.jgit.lib.Constants;
  21. import org.eclipse.jgit.lib.Ref;
  22. import org.eclipse.jgit.revplot.AbstractPlotRenderer;
  23. import org.eclipse.jgit.revplot.PlotCommit;

  24. final class AWTPlotRenderer extends AbstractPlotRenderer<SwingLane, Color>
  25.         implements Serializable {
  26.     private static final long serialVersionUID = 1L;

  27.     final GraphCellRender cell;

  28.     transient Graphics2D g;

  29.     AWTPlotRenderer(GraphCellRender c) {
  30.         cell = c;
  31.     }

  32.     void paint(Graphics in, PlotCommit<SwingLane> commit) {
  33.         g = (Graphics2D) in.create();
  34.         try {
  35.             final int h = cell.getHeight();
  36.             g.setColor(cell.getBackground());
  37.             g.fillRect(0, 0, cell.getWidth(), h);
  38.             if (commit != null)
  39.                 paintCommit(commit, h);
  40.         } finally {
  41.             g.dispose();
  42.             g = null;
  43.         }
  44.     }

  45.     /** {@inheritDoc} */
  46.     @Override
  47.     protected void drawLine(final Color color, int x1, int y1, int x2,
  48.             int y2, int width) {
  49.         if (y1 == y2) {
  50.             x1 -= width / 2;
  51.             x2 -= width / 2;
  52.         } else if (x1 == x2) {
  53.             y1 -= width / 2;
  54.             y2 -= width / 2;
  55.         }

  56.         g.setColor(color);
  57.         g.setStroke(CommitGraphPane.stroke(width));
  58.         g.drawLine(x1, y1, x2, y2);
  59.     }

  60.     /** {@inheritDoc} */
  61.     @Override
  62.     protected void drawCommitDot(final int x, final int y, final int w,
  63.             final int h) {
  64.         g.setColor(Color.blue);
  65.         g.setStroke(CommitGraphPane.strokeCache[1]);
  66.         g.fillOval(x, y, w, h);
  67.         g.setColor(Color.black);
  68.         g.drawOval(x, y, w, h);
  69.     }

  70.     /** {@inheritDoc} */
  71.     @Override
  72.     protected void drawBoundaryDot(final int x, final int y, final int w,
  73.             final int h) {
  74.         g.setColor(cell.getBackground());
  75.         g.setStroke(CommitGraphPane.strokeCache[1]);
  76.         g.fillOval(x, y, w, h);
  77.         g.setColor(Color.black);
  78.         g.drawOval(x, y, w, h);
  79.     }

  80.     /** {@inheritDoc} */
  81.     @Override
  82.     protected void drawText(String msg, int x, int y) {
  83.         final int texth = g.getFontMetrics().getHeight();
  84.         final int y0 = (y - texth) / 2 + (cell.getHeight() - texth) / 2;
  85.         g.setColor(cell.getForeground());
  86.         g.drawString(msg, x, y0 + texth - g.getFontMetrics().getDescent());
  87.     }

  88.     /** {@inheritDoc} */
  89.     @Override
  90.     protected Color laneColor(SwingLane myLane) {
  91.         return myLane != null ? myLane.color : Color.black;
  92.     }

  93.     void paintTriangleDown(int cx, int y, int h) {
  94.         final int tipX = cx;
  95.         final int tipY = y + h;
  96.         final int baseX1 = cx - 10 / 2;
  97.         final int baseX2 = tipX + 10 / 2;
  98.         final int baseY = y;
  99.         final Polygon triangle = new Polygon();
  100.         triangle.addPoint(tipX, tipY);
  101.         triangle.addPoint(baseX1, baseY);
  102.         triangle.addPoint(baseX2, baseY);
  103.         g.fillPolygon(triangle);
  104.         g.drawPolygon(triangle);
  105.     }

  106.     /** {@inheritDoc} */
  107.     @Override
  108.     protected int drawLabel(int x, int y, Ref ref) {
  109.         String txt;
  110.         String name = ref.getName();
  111.         if (name.startsWith(Constants.R_HEADS)) {
  112.             g.setBackground(Color.GREEN);
  113.             txt = name.substring(Constants.R_HEADS.length());
  114.         } else if (name.startsWith(Constants.R_REMOTES)){
  115.             g.setBackground(Color.LIGHT_GRAY);
  116.             txt = name.substring(Constants.R_REMOTES.length());
  117.         } else if (name.startsWith(Constants.R_TAGS)){
  118.             g.setBackground(Color.YELLOW);
  119.             txt = name.substring(Constants.R_TAGS.length());
  120.         } else {
  121.             // Whatever this would be
  122.             g.setBackground(Color.WHITE);
  123.             if (name.startsWith(Constants.R_REFS))
  124.                 txt = name.substring(Constants.R_REFS.length());
  125.             else
  126.                 txt = name; // HEAD and such
  127.         }
  128.         if (ref.getPeeledObjectId() != null) {
  129.             float[] colorComponents = g.getBackground().getRGBColorComponents(null);
  130.             colorComponents[0] *= 0.9f;
  131.             colorComponents[1] *= 0.9f;
  132.             colorComponents[2] *= 0.9f;
  133.             g.setBackground(new Color(colorComponents[0],colorComponents[1],colorComponents[2]));
  134.         }
  135.         if (txt.length() > 12)
  136.             txt = txt.substring(0,11) + "\u2026"; // ellipsis "…" (in UTF-8) //$NON-NLS-1$

  137.         final int texth = g.getFontMetrics().getHeight();
  138.         int textw = g.getFontMetrics().stringWidth(txt);
  139.         g.setColor(g.getBackground());
  140.         int arcHeight = texth/4;
  141.         int y0 = y - texth/2 + (cell.getHeight() - texth)/2;
  142.         g.fillRoundRect(x , y0, textw + arcHeight*2, texth -1, arcHeight, arcHeight);
  143.         g.setColor(g.getColor().darker());
  144.         g.drawRoundRect(x, y0, textw + arcHeight*2, texth -1 , arcHeight, arcHeight);
  145.         g.setColor(Color.BLACK);
  146.         g.drawString(txt, x + arcHeight, y0 + texth - g.getFontMetrics().getDescent());

  147.         return arcHeight * 3 + textw;
  148.     }
  149. }