Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

AbstractPlotRenderer.java 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright (C) 2008, 2014 Shawn O. Pearce <spearce@spearce.org>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.revplot;
  44. import org.eclipse.jgit.lib.Ref;
  45. import org.eclipse.jgit.revwalk.RevFlag;
  46. /**
  47. * Basic commit graph renderer for graphical user interfaces.
  48. * <p>
  49. * Lanes are drawn as columns left-to-right in the graph, and the commit short
  50. * message is drawn to the right of the lane lines for this cell. It is assumed
  51. * that the commits are being drawn as rows of some sort of table.
  52. * <p>
  53. * Client applications can subclass this implementation to provide the necessary
  54. * drawing primitives required to display a commit graph. Most of the graph
  55. * layout is handled by this class, allowing applications to implement only a
  56. * handful of primitive stubs.
  57. * <p>
  58. * This class is suitable for us within an AWT TableCellRenderer or within a SWT
  59. * PaintListener registered on a Table instance. It is meant to rubber stamp the
  60. * graphics necessary for one row of a plotted commit list.
  61. * <p>
  62. * Subclasses should call {@link #paintCommit(PlotCommit, int)} after they have
  63. * otherwise configured their instance to draw one commit into the current
  64. * location.
  65. * <p>
  66. * All drawing methods assume the coordinate space for the current commit's cell
  67. * starts at (upper left corner is) 0,0. If this is not true (like say in SWT)
  68. * the implementation must perform the cell offset computations within the
  69. * various draw methods.
  70. *
  71. * @param <TLane>
  72. * type of lane being used by the application.
  73. * @param <TColor>
  74. * type of color object used by the graphics library.
  75. */
  76. public abstract class AbstractPlotRenderer<TLane extends PlotLane, TColor> {
  77. private static final int LANE_WIDTH = 14;
  78. private static final int LINE_WIDTH = 2;
  79. private static final int LEFT_PAD = 2;
  80. /**
  81. * Paint one commit using the underlying graphics library.
  82. *
  83. * @param commit
  84. * the commit to render in this cell. Must not be null.
  85. * @param h
  86. * total height (in pixels) of this cell.
  87. */
  88. @SuppressWarnings("unchecked")
  89. protected void paintCommit(final PlotCommit<TLane> commit, final int h) {
  90. final int dotSize = computeDotSize(h);
  91. final TLane myLane = commit.getLane();
  92. final int myLaneX = laneC(myLane);
  93. final TColor myColor = laneColor(myLane);
  94. int maxCenter = myLaneX;
  95. for (final TLane passingLane : (TLane[]) commit.passingLanes) {
  96. final int cx = laneC(passingLane);
  97. final TColor c = laneColor(passingLane);
  98. drawLine(c, cx, 0, cx, h, LINE_WIDTH);
  99. maxCenter = Math.max(maxCenter, cx);
  100. }
  101. final int dotX = myLaneX - dotSize / 2 - 1;
  102. final int dotY = (h - dotSize) / 2;
  103. final int nParent = commit.getParentCount();
  104. if (nParent > 0) {
  105. drawLine(myColor, myLaneX, h, myLaneX, (h + dotSize) / 2,
  106. LINE_WIDTH);
  107. for (int i = 0; i < commit.mergingLanes.length; i++) {
  108. final TLane pLane = (TLane) commit.mergingLanes[i];
  109. final TColor pColor = laneColor(pLane);
  110. final int cx = laneC(pLane);
  111. if (Math.abs(myLaneX - cx) > LANE_WIDTH) {
  112. final int ix;
  113. if (myLaneX < cx)
  114. ix = cx - LANE_WIDTH / 2;
  115. else
  116. ix = cx + LANE_WIDTH / 2;
  117. drawLine(pColor, myLaneX, h / 2, ix, h / 2, LINE_WIDTH);
  118. drawLine(pColor, ix, h / 2, cx, h, LINE_WIDTH);
  119. } else
  120. drawLine(pColor, myLaneX, h / 2, cx, h, LINE_WIDTH);
  121. maxCenter = Math.max(maxCenter, cx);
  122. }
  123. }
  124. if (commit.getChildCount() > 0) {
  125. for (int i = 0; i < commit.forkingOffLanes.length; i++) {
  126. final TLane childLane = (TLane) commit.forkingOffLanes[i];
  127. final TColor cColor = laneColor(childLane);
  128. final int cx = laneC(childLane);
  129. if (Math.abs(myLaneX - cx) > LANE_WIDTH) {
  130. final int ix;
  131. if (myLaneX < cx)
  132. ix = cx - LANE_WIDTH / 2;
  133. else
  134. ix = cx + LANE_WIDTH / 2;
  135. drawLine(cColor, myLaneX, h / 2, ix, h / 2, LINE_WIDTH);
  136. drawLine(cColor, ix, h / 2, cx, 0, LINE_WIDTH);
  137. } else {
  138. drawLine(cColor, myLaneX, h / 2, cx, 0, LINE_WIDTH);
  139. }
  140. maxCenter = Math.max(maxCenter, cx);
  141. }
  142. int nonForkingChildren = commit.getChildCount()
  143. - commit.forkingOffLanes.length;
  144. if (nonForkingChildren > 0)
  145. drawLine(myColor, myLaneX, 0, myLaneX, dotY, LINE_WIDTH);
  146. }
  147. if (commit.has(RevFlag.UNINTERESTING))
  148. drawBoundaryDot(dotX, dotY, dotSize, dotSize);
  149. else
  150. drawCommitDot(dotX, dotY, dotSize, dotSize);
  151. int textx = Math.max(maxCenter + LANE_WIDTH / 2, dotX + dotSize) + 8;
  152. int n = commit.refs.length;
  153. for (int i = 0; i < n; ++i) {
  154. textx += drawLabel(textx + dotSize, h/2, commit.refs[i]);
  155. }
  156. final String msg = commit.getShortMessage();
  157. drawText(msg, textx + dotSize, h);
  158. }
  159. /**
  160. * Draw a decoration for the Ref ref at x,y
  161. *
  162. * @param x
  163. * left
  164. * @param y
  165. * top
  166. * @param ref
  167. * A peeled ref
  168. * @return width of label in pixels
  169. */
  170. protected abstract int drawLabel(int x, int y, Ref ref);
  171. private static int computeDotSize(final int h) {
  172. int d = (int) (Math.min(h, LANE_WIDTH) * 0.50f);
  173. d += (d & 1);
  174. return d;
  175. }
  176. /**
  177. * Obtain the color reference used to paint this lane.
  178. * <p>
  179. * Colors returned by this method will be passed to the other drawing
  180. * primitives, so the color returned should be application specific.
  181. * <p>
  182. * If a null lane is supplied the return value must still be acceptable to a
  183. * drawing method. Usually this means the implementation should return a
  184. * default color.
  185. *
  186. * @param myLane
  187. * the current lane. May be null.
  188. * @return graphics specific color reference. Must be a valid color.
  189. */
  190. protected abstract TColor laneColor(TLane myLane);
  191. /**
  192. * Draw a single line within this cell.
  193. *
  194. * @param color
  195. * the color to use while drawing the line.
  196. * @param x1
  197. * starting X coordinate, 0 based.
  198. * @param y1
  199. * starting Y coordinate, 0 based.
  200. * @param x2
  201. * ending X coordinate, 0 based.
  202. * @param y2
  203. * ending Y coordinate, 0 based.
  204. * @param width
  205. * number of pixels wide for the line. Always at least 1.
  206. */
  207. protected abstract void drawLine(TColor color, int x1, int y1, int x2,
  208. int y2, int width);
  209. /**
  210. * Draw a single commit dot.
  211. * <p>
  212. * Usually the commit dot is a filled oval in blue, then a drawn oval in
  213. * black, using the same coordinates for both operations.
  214. *
  215. * @param x
  216. * upper left of the oval's bounding box.
  217. * @param y
  218. * upper left of the oval's bounding box.
  219. * @param w
  220. * width of the oval's bounding box.
  221. * @param h
  222. * height of the oval's bounding box.
  223. */
  224. protected abstract void drawCommitDot(int x, int y, int w, int h);
  225. /**
  226. * Draw a single boundary commit (aka uninteresting commit) dot.
  227. * <p>
  228. * Usually a boundary commit dot is a light gray oval with a white center.
  229. *
  230. * @param x
  231. * upper left of the oval's bounding box.
  232. * @param y
  233. * upper left of the oval's bounding box.
  234. * @param w
  235. * width of the oval's bounding box.
  236. * @param h
  237. * height of the oval's bounding box.
  238. */
  239. protected abstract void drawBoundaryDot(int x, int y, int w, int h);
  240. /**
  241. * Draw a single line of text.
  242. * <p>
  243. * The font and colors used to render the text are left up to the
  244. * implementation.
  245. *
  246. * @param msg
  247. * the text to draw. Does not contain LFs.
  248. * @param x
  249. * first pixel from the left that the text can be drawn at.
  250. * Character data must not appear before this position.
  251. * @param y
  252. * pixel coordinate of the baseline of the text. Implementations
  253. * must adjust this coordinate to account for the way their
  254. * implementation handles font rendering.
  255. */
  256. protected abstract void drawText(String msg, int x, int y);
  257. private static int laneX(final PlotLane myLane) {
  258. final int p = myLane != null ? myLane.getPosition() : 0;
  259. return LEFT_PAD + LANE_WIDTH * p;
  260. }
  261. private static int laneC(final PlotLane myLane) {
  262. return laneX(myLane) + LANE_WIDTH / 2;
  263. }
  264. }