xref: /JGit/org.eclipse.jgit.ui/src/org/eclipse/jgit/awtui/AWTPlotRenderer.java (revision 5c5f7c6b146b24f2bd4afae1902df85ad6e57ea3)
1cf2edb65SMykola Nikishov /*
201b5392cSShawn O. Pearce  * Copyright (C) 2010, Google Inc.
3cf2edb65SMykola Nikishov  * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
4*5c5f7c6bSMatthias Sohn  * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
5cf2edb65SMykola Nikishov  *
6*5c5f7c6bSMatthias Sohn  * This program and the accompanying materials are made available under the
7*5c5f7c6bSMatthias Sohn  * terms of the Eclipse Distribution License v. 1.0 which is available at
8*5c5f7c6bSMatthias Sohn  * https://www.eclipse.org/org/documents/edl-v10.php.
9cf2edb65SMykola Nikishov  *
10*5c5f7c6bSMatthias Sohn  * SPDX-License-Identifier: BSD-3-Clause
11cf2edb65SMykola Nikishov  */
12cf2edb65SMykola Nikishov 
13cf2edb65SMykola Nikishov package org.eclipse.jgit.awtui;
14cf2edb65SMykola Nikishov 
15cf2edb65SMykola Nikishov import java.awt.Color;
16cf2edb65SMykola Nikishov import java.awt.Graphics;
17cf2edb65SMykola Nikishov import java.awt.Graphics2D;
18cf2edb65SMykola Nikishov import java.awt.Polygon;
19258f516aSRobin Stocker import java.io.Serializable;
20cf2edb65SMykola Nikishov 
21cf2edb65SMykola Nikishov import org.eclipse.jgit.awtui.CommitGraphPane.GraphCellRender;
22cf2edb65SMykola Nikishov import org.eclipse.jgit.awtui.SwingCommitList.SwingLane;
23cf2edb65SMykola Nikishov import org.eclipse.jgit.lib.Constants;
24cf2edb65SMykola Nikishov import org.eclipse.jgit.lib.Ref;
25cf2edb65SMykola Nikishov import org.eclipse.jgit.revplot.AbstractPlotRenderer;
26cf2edb65SMykola Nikishov import org.eclipse.jgit.revplot.PlotCommit;
27cf2edb65SMykola Nikishov 
28258f516aSRobin Stocker final class AWTPlotRenderer extends AbstractPlotRenderer<SwingLane, Color>
29258f516aSRobin Stocker 		implements Serializable {
30258f516aSRobin Stocker 	private static final long serialVersionUID = 1L;
31cf2edb65SMykola Nikishov 
32cf2edb65SMykola Nikishov 	final GraphCellRender cell;
33cf2edb65SMykola Nikishov 
34258f516aSRobin Stocker 	transient Graphics2D g;
35cf2edb65SMykola Nikishov 
AWTPlotRenderer(GraphCellRender c)36f3ec7cf3SHan-Wen Nienhuys 	AWTPlotRenderer(GraphCellRender c) {
37cf2edb65SMykola Nikishov 		cell = c;
38cf2edb65SMykola Nikishov 	}
39cf2edb65SMykola Nikishov 
paint(Graphics in, PlotCommit<SwingLane> commit)40f3ec7cf3SHan-Wen Nienhuys 	void paint(Graphics in, PlotCommit<SwingLane> commit) {
41cf2edb65SMykola Nikishov 		g = (Graphics2D) in.create();
42cf2edb65SMykola Nikishov 		try {
43cf2edb65SMykola Nikishov 			final int h = cell.getHeight();
44cf2edb65SMykola Nikishov 			g.setColor(cell.getBackground());
45cf2edb65SMykola Nikishov 			g.fillRect(0, 0, cell.getWidth(), h);
46cf2edb65SMykola Nikishov 			if (commit != null)
47cf2edb65SMykola Nikishov 				paintCommit(commit, h);
48cf2edb65SMykola Nikishov 		} finally {
49cf2edb65SMykola Nikishov 			g.dispose();
50cf2edb65SMykola Nikishov 			g = null;
51cf2edb65SMykola Nikishov 		}
52cf2edb65SMykola Nikishov 	}
53cf2edb65SMykola Nikishov 
546ac27ee2SMatthias Sohn 	/** {@inheritDoc} */
55cf2edb65SMykola Nikishov 	@Override
drawLine(final Color color, int x1, int y1, int x2, int y2, int width)56cf2edb65SMykola Nikishov 	protected void drawLine(final Color color, int x1, int y1, int x2,
57cf2edb65SMykola Nikishov 			int y2, int width) {
58cf2edb65SMykola Nikishov 		if (y1 == y2) {
59cf2edb65SMykola Nikishov 			x1 -= width / 2;
60cf2edb65SMykola Nikishov 			x2 -= width / 2;
61cf2edb65SMykola Nikishov 		} else if (x1 == x2) {
62cf2edb65SMykola Nikishov 			y1 -= width / 2;
63cf2edb65SMykola Nikishov 			y2 -= width / 2;
64cf2edb65SMykola Nikishov 		}
65cf2edb65SMykola Nikishov 
66cf2edb65SMykola Nikishov 		g.setColor(color);
67cf2edb65SMykola Nikishov 		g.setStroke(CommitGraphPane.stroke(width));
68cf2edb65SMykola Nikishov 		g.drawLine(x1, y1, x2, y2);
69cf2edb65SMykola Nikishov 	}
70cf2edb65SMykola Nikishov 
716ac27ee2SMatthias Sohn 	/** {@inheritDoc} */
72cf2edb65SMykola Nikishov 	@Override
drawCommitDot(final int x, final int y, final int w, final int h)73cf2edb65SMykola Nikishov 	protected void drawCommitDot(final int x, final int y, final int w,
74cf2edb65SMykola Nikishov 			final int h) {
75cf2edb65SMykola Nikishov 		g.setColor(Color.blue);
76cf2edb65SMykola Nikishov 		g.setStroke(CommitGraphPane.strokeCache[1]);
77cf2edb65SMykola Nikishov 		g.fillOval(x, y, w, h);
78cf2edb65SMykola Nikishov 		g.setColor(Color.black);
79cf2edb65SMykola Nikishov 		g.drawOval(x, y, w, h);
80cf2edb65SMykola Nikishov 	}
81cf2edb65SMykola Nikishov 
826ac27ee2SMatthias Sohn 	/** {@inheritDoc} */
83cf2edb65SMykola Nikishov 	@Override
drawBoundaryDot(final int x, final int y, final int w, final int h)84cf2edb65SMykola Nikishov 	protected void drawBoundaryDot(final int x, final int y, final int w,
85cf2edb65SMykola Nikishov 			final int h) {
86cf2edb65SMykola Nikishov 		g.setColor(cell.getBackground());
87cf2edb65SMykola Nikishov 		g.setStroke(CommitGraphPane.strokeCache[1]);
88cf2edb65SMykola Nikishov 		g.fillOval(x, y, w, h);
89cf2edb65SMykola Nikishov 		g.setColor(Color.black);
90cf2edb65SMykola Nikishov 		g.drawOval(x, y, w, h);
91cf2edb65SMykola Nikishov 	}
92cf2edb65SMykola Nikishov 
936ac27ee2SMatthias Sohn 	/** {@inheritDoc} */
94cf2edb65SMykola Nikishov 	@Override
drawText(String msg, int x, int y)956d370d83SHan-Wen Nienhuys 	protected void drawText(String msg, int x, int y) {
96cf2edb65SMykola Nikishov 		final int texth = g.getFontMetrics().getHeight();
971a721437SMichael Keppler 		final int y0 = (y - texth) / 2 + (cell.getHeight() - texth) / 2;
98cf2edb65SMykola Nikishov 		g.setColor(cell.getForeground());
99cf2edb65SMykola Nikishov 		g.drawString(msg, x, y0 + texth - g.getFontMetrics().getDescent());
100cf2edb65SMykola Nikishov 	}
101cf2edb65SMykola Nikishov 
1026ac27ee2SMatthias Sohn 	/** {@inheritDoc} */
103cf2edb65SMykola Nikishov 	@Override
laneColor(SwingLane myLane)1046d370d83SHan-Wen Nienhuys 	protected Color laneColor(SwingLane myLane) {
105cf2edb65SMykola Nikishov 		return myLane != null ? myLane.color : Color.black;
106cf2edb65SMykola Nikishov 	}
107cf2edb65SMykola Nikishov 
paintTriangleDown(int cx, int y, int h)108f3ec7cf3SHan-Wen Nienhuys 	void paintTriangleDown(int cx, int y, int h) {
109cf2edb65SMykola Nikishov 		final int tipX = cx;
110cf2edb65SMykola Nikishov 		final int tipY = y + h;
111cf2edb65SMykola Nikishov 		final int baseX1 = cx - 10 / 2;
112cf2edb65SMykola Nikishov 		final int baseX2 = tipX + 10 / 2;
113cf2edb65SMykola Nikishov 		final int baseY = y;
114cf2edb65SMykola Nikishov 		final Polygon triangle = new Polygon();
115cf2edb65SMykola Nikishov 		triangle.addPoint(tipX, tipY);
116cf2edb65SMykola Nikishov 		triangle.addPoint(baseX1, baseY);
117cf2edb65SMykola Nikishov 		triangle.addPoint(baseX2, baseY);
118cf2edb65SMykola Nikishov 		g.fillPolygon(triangle);
119cf2edb65SMykola Nikishov 		g.drawPolygon(triangle);
120cf2edb65SMykola Nikishov 	}
121cf2edb65SMykola Nikishov 
1226ac27ee2SMatthias Sohn 	/** {@inheritDoc} */
123cf2edb65SMykola Nikishov 	@Override
drawLabel(int x, int y, Ref ref)124cf2edb65SMykola Nikishov 	protected int drawLabel(int x, int y, Ref ref) {
125cf2edb65SMykola Nikishov 		String txt;
12601b5392cSShawn O. Pearce 		String name = ref.getName();
127cf2edb65SMykola Nikishov 		if (name.startsWith(Constants.R_HEADS)) {
128cf2edb65SMykola Nikishov 			g.setBackground(Color.GREEN);
129cf2edb65SMykola Nikishov 			txt = name.substring(Constants.R_HEADS.length());
130cf2edb65SMykola Nikishov 		} else if (name.startsWith(Constants.R_REMOTES)){
131cf2edb65SMykola Nikishov 			g.setBackground(Color.LIGHT_GRAY);
132cf2edb65SMykola Nikishov 			txt = name.substring(Constants.R_REMOTES.length());
133cf2edb65SMykola Nikishov 		} else if (name.startsWith(Constants.R_TAGS)){
134cf2edb65SMykola Nikishov 			g.setBackground(Color.YELLOW);
135cf2edb65SMykola Nikishov 			txt = name.substring(Constants.R_TAGS.length());
136cf2edb65SMykola Nikishov 		} else {
137cf2edb65SMykola Nikishov 			// Whatever this would be
138cf2edb65SMykola Nikishov 			g.setBackground(Color.WHITE);
139cf2edb65SMykola Nikishov 			if (name.startsWith(Constants.R_REFS))
140cf2edb65SMykola Nikishov 				txt = name.substring(Constants.R_REFS.length());
141cf2edb65SMykola Nikishov 			else
142cf2edb65SMykola Nikishov 				txt = name; // HEAD and such
143cf2edb65SMykola Nikishov 		}
144cf2edb65SMykola Nikishov 		if (ref.getPeeledObjectId() != null) {
145cf2edb65SMykola Nikishov 			float[] colorComponents = g.getBackground().getRGBColorComponents(null);
14664162ab4SJonathan Nieder 			colorComponents[0] *= 0.9f;
14764162ab4SJonathan Nieder 			colorComponents[1] *= 0.9f;
14864162ab4SJonathan Nieder 			colorComponents[2] *= 0.9f;
149cf2edb65SMykola Nikishov 			g.setBackground(new Color(colorComponents[0],colorComponents[1],colorComponents[2]));
150cf2edb65SMykola Nikishov 		}
151cf2edb65SMykola Nikishov 		if (txt.length() > 12)
152c310fa0cSRobin Rosenberg 			txt = txt.substring(0,11) + "\u2026"; // ellipsis "…" (in UTF-8) //$NON-NLS-1$
153cf2edb65SMykola Nikishov 
154cf2edb65SMykola Nikishov 		final int texth = g.getFontMetrics().getHeight();
155cf2edb65SMykola Nikishov 		int textw = g.getFontMetrics().stringWidth(txt);
156cf2edb65SMykola Nikishov 		g.setColor(g.getBackground());
157cf2edb65SMykola Nikishov 		int arcHeight = texth/4;
158cf2edb65SMykola Nikishov 		int y0 = y - texth/2 + (cell.getHeight() - texth)/2;
159cf2edb65SMykola Nikishov 		g.fillRoundRect(x , y0, textw + arcHeight*2, texth -1, arcHeight, arcHeight);
160cf2edb65SMykola Nikishov 		g.setColor(g.getColor().darker());
161cf2edb65SMykola Nikishov 		g.drawRoundRect(x, y0, textw + arcHeight*2, texth -1 , arcHeight, arcHeight);
162cf2edb65SMykola Nikishov 		g.setColor(Color.BLACK);
163cf2edb65SMykola Nikishov 		g.drawString(txt, x + arcHeight, y0 + texth - g.getFontMetrics().getDescent());
164cf2edb65SMykola Nikishov 
165cf2edb65SMykola Nikishov 		return arcHeight * 3 + textw;
166cf2edb65SMykola Nikishov 	}
167cf2edb65SMykola Nikishov }
168