1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 package org.eclipse.jgit.awtui;
47
48 import java.awt.Color;
49 import java.awt.Graphics;
50 import java.awt.Graphics2D;
51 import java.awt.Polygon;
52 import java.io.Serializable;
53
54 import org.eclipse.jgit.awtui.CommitGraphPane.GraphCellRender;
55 import org.eclipse.jgit.awtui.SwingCommitList.SwingLane;
56 import org.eclipse.jgit.lib.Constants;
57 import org.eclipse.jgit.lib.Ref;
58 import org.eclipse.jgit.revplot.AbstractPlotRenderer;
59 import org.eclipse.jgit.revplot.PlotCommit;
60
61 final class AWTPlotRenderer extends AbstractPlotRenderer<SwingLane, Color>
62 implements Serializable {
63 private static final long serialVersionUID = 1L;
64
65 final GraphCellRender cell;
66
67 transient Graphics2D g;
68
69 AWTPlotRenderer(GraphCellRender c) {
70 cell = c;
71 }
72
73 void paint(Graphics in, PlotCommit<SwingLane> commit) {
74 g = (Graphics2D) in.create();
75 try {
76 final int h = cell.getHeight();
77 g.setColor(cell.getBackground());
78 g.fillRect(0, 0, cell.getWidth(), h);
79 if (commit != null)
80 paintCommit(commit, h);
81 } finally {
82 g.dispose();
83 g = null;
84 }
85 }
86
87
88 @Override
89 protected void drawLine(final Color color, int x1, int y1, int x2,
90 int y2, int width) {
91 if (y1 == y2) {
92 x1 -= width / 2;
93 x2 -= width / 2;
94 } else if (x1 == x2) {
95 y1 -= width / 2;
96 y2 -= width / 2;
97 }
98
99 g.setColor(color);
100 g.setStroke(CommitGraphPane.stroke(width));
101 g.drawLine(x1, y1, x2, y2);
102 }
103
104
105 @Override
106 protected void drawCommitDot(final int x, final int y, final int w,
107 final int h) {
108 g.setColor(Color.blue);
109 g.setStroke(CommitGraphPane.strokeCache[1]);
110 g.fillOval(x, y, w, h);
111 g.setColor(Color.black);
112 g.drawOval(x, y, w, h);
113 }
114
115
116 @Override
117 protected void drawBoundaryDot(final int x, final int y, final int w,
118 final int h) {
119 g.setColor(cell.getBackground());
120 g.setStroke(CommitGraphPane.strokeCache[1]);
121 g.fillOval(x, y, w, h);
122 g.setColor(Color.black);
123 g.drawOval(x, y, w, h);
124 }
125
126
127 @Override
128 protected void drawText(String msg, int x, int y) {
129 final int texth = g.getFontMetrics().getHeight();
130 final int y0 = (y - texth) / 2 + (cell.getHeight() - texth) / 2;
131 g.setColor(cell.getForeground());
132 g.drawString(msg, x, y0 + texth - g.getFontMetrics().getDescent());
133 }
134
135
136 @Override
137 protected Color laneColor(SwingLane myLane) {
138 return myLane != null ? myLane.color : Color.black;
139 }
140
141 void paintTriangleDown(int cx, int y, int h) {
142 final int tipX = cx;
143 final int tipY = y + h;
144 final int baseX1 = cx - 10 / 2;
145 final int baseX2 = tipX + 10 / 2;
146 final int baseY = y;
147 final Polygon triangle = new Polygon();
148 triangle.addPoint(tipX, tipY);
149 triangle.addPoint(baseX1, baseY);
150 triangle.addPoint(baseX2, baseY);
151 g.fillPolygon(triangle);
152 g.drawPolygon(triangle);
153 }
154
155
156 @Override
157 protected int drawLabel(int x, int y, Ref ref) {
158 String txt;
159 String name = ref.getName();
160 if (name.startsWith(Constants.R_HEADS)) {
161 g.setBackground(Color.GREEN);
162 txt = name.substring(Constants.R_HEADS.length());
163 } else if (name.startsWith(Constants.R_REMOTES)){
164 g.setBackground(Color.LIGHT_GRAY);
165 txt = name.substring(Constants.R_REMOTES.length());
166 } else if (name.startsWith(Constants.R_TAGS)){
167 g.setBackground(Color.YELLOW);
168 txt = name.substring(Constants.R_TAGS.length());
169 } else {
170
171 g.setBackground(Color.WHITE);
172 if (name.startsWith(Constants.R_REFS))
173 txt = name.substring(Constants.R_REFS.length());
174 else
175 txt = name;
176 }
177 if (ref.getPeeledObjectId() != null) {
178 float[] colorComponents = g.getBackground().getRGBColorComponents(null);
179 colorComponents[0] *= 0.9f;
180 colorComponents[1] *= 0.9f;
181 colorComponents[2] *= 0.9f;
182 g.setBackground(new Color(colorComponents[0],colorComponents[1],colorComponents[2]));
183 }
184 if (txt.length() > 12)
185 txt = txt.substring(0,11) + "\u2026";
186
187 final int texth = g.getFontMetrics().getHeight();
188 int textw = g.getFontMetrics().stringWidth(txt);
189 g.setColor(g.getBackground());
190 int arcHeight = texth/4;
191 int y0 = y - texth/2 + (cell.getHeight() - texth)/2;
192 g.fillRoundRect(x , y0, textw + arcHeight*2, texth -1, arcHeight, arcHeight);
193 g.setColor(g.getColor().darker());
194 g.drawRoundRect(x, y0, textw + arcHeight*2, texth -1 , arcHeight, arcHeight);
195 g.setColor(Color.BLACK);
196 g.drawString(txt, x + arcHeight, y0 + texth - g.getFontMetrics().getDescent());
197
198 return arcHeight * 3 + textw;
199 }
200 }