You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CommitGraphPane.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright (C) 2008, 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.awtui;
  44. import java.awt.BasicStroke;
  45. import java.awt.Component;
  46. import java.awt.Graphics;
  47. import java.awt.Stroke;
  48. import java.text.DateFormat;
  49. import java.text.SimpleDateFormat;
  50. import javax.swing.JTable;
  51. import javax.swing.ListSelectionModel;
  52. import javax.swing.table.AbstractTableModel;
  53. import javax.swing.table.DefaultTableCellRenderer;
  54. import javax.swing.table.JTableHeader;
  55. import javax.swing.table.TableCellRenderer;
  56. import javax.swing.table.TableColumn;
  57. import javax.swing.table.TableColumnModel;
  58. import javax.swing.table.TableModel;
  59. import org.eclipse.jgit.awtui.SwingCommitList.SwingLane;
  60. import org.eclipse.jgit.lib.PersonIdent;
  61. import org.eclipse.jgit.revplot.PlotCommit;
  62. import org.eclipse.jgit.revplot.PlotCommitList;
  63. /**
  64. * Draws a commit graph in a JTable.
  65. * <p>
  66. * This class is currently a very primitive commit visualization tool. It shows
  67. * a table of 3 columns:
  68. * <ol>
  69. * <li>Commit graph and short message</li>
  70. * <li>Author name and email address</li>
  71. * <li>Author date and time</li>
  72. * </ul>
  73. */
  74. public class CommitGraphPane extends JTable {
  75. private static final long serialVersionUID = 1L;
  76. private final SwingCommitList allCommits;
  77. /** Create a new empty panel. */
  78. public CommitGraphPane() {
  79. allCommits = new SwingCommitList();
  80. configureHeader();
  81. setShowHorizontalLines(false);
  82. setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  83. configureRowHeight();
  84. }
  85. private void configureRowHeight() {
  86. int h = 0;
  87. for (int i = 0; i<getColumnCount(); ++i) {
  88. TableCellRenderer renderer = getDefaultRenderer(getColumnClass(i));
  89. Component c = renderer.getTableCellRendererComponent(this, "ÅOj", false, false, 0, i);
  90. h = Math.max(h, c.getPreferredSize().height);
  91. }
  92. setRowHeight(h + getRowMargin());
  93. }
  94. /**
  95. * Get the commit list this pane renders from.
  96. *
  97. * @return the list the caller must populate.
  98. */
  99. public PlotCommitList getCommitList() {
  100. return allCommits;
  101. }
  102. @Override
  103. public void setModel(final TableModel dataModel) {
  104. if (dataModel != null && !(dataModel instanceof CommitTableModel))
  105. throw new ClassCastException(UIText.get().mustBeSpecialTableModel);
  106. super.setModel(dataModel);
  107. }
  108. @Override
  109. protected TableModel createDefaultDataModel() {
  110. return new CommitTableModel();
  111. }
  112. private void configureHeader() {
  113. final JTableHeader th = getTableHeader();
  114. final TableColumnModel cols = th.getColumnModel();
  115. final TableColumn graph = cols.getColumn(0);
  116. final TableColumn author = cols.getColumn(1);
  117. final TableColumn date = cols.getColumn(2);
  118. graph.setHeaderValue("");
  119. author.setHeaderValue(UIText.get().author);
  120. date.setHeaderValue(UIText.get().date);
  121. graph.setCellRenderer(new GraphCellRender());
  122. author.setCellRenderer(new NameCellRender());
  123. date.setCellRenderer(new DateCellRender());
  124. }
  125. class CommitTableModel extends AbstractTableModel {
  126. private static final long serialVersionUID = 1L;
  127. PlotCommit<SwingLane> lastCommit;
  128. PersonIdent lastAuthor;
  129. public int getColumnCount() {
  130. return 3;
  131. }
  132. public int getRowCount() {
  133. return allCommits != null ? allCommits.size() : 0;
  134. }
  135. public Object getValueAt(final int rowIndex, final int columnIndex) {
  136. final PlotCommit<SwingLane> c = allCommits.get(rowIndex);
  137. switch (columnIndex) {
  138. case 0:
  139. return c;
  140. case 1:
  141. return authorFor(c);
  142. case 2:
  143. return authorFor(c);
  144. default:
  145. return null;
  146. }
  147. }
  148. PersonIdent authorFor(final PlotCommit<SwingLane> c) {
  149. if (c != lastCommit) {
  150. lastCommit = c;
  151. lastAuthor = c.getAuthorIdent();
  152. }
  153. return lastAuthor;
  154. }
  155. }
  156. class NameCellRender extends DefaultTableCellRenderer {
  157. private static final long serialVersionUID = 1L;
  158. public Component getTableCellRendererComponent(final JTable table,
  159. final Object value, final boolean isSelected,
  160. final boolean hasFocus, final int row, final int column) {
  161. final PersonIdent pi = (PersonIdent) value;
  162. final String valueStr;
  163. if (pi != null)
  164. valueStr = pi.getName() + " <" + pi.getEmailAddress() + ">";
  165. else
  166. valueStr = "";
  167. return super.getTableCellRendererComponent(table, valueStr,
  168. isSelected, hasFocus, row, column);
  169. }
  170. }
  171. class DateCellRender extends DefaultTableCellRenderer {
  172. private static final long serialVersionUID = 1L;
  173. private final DateFormat fmt = new SimpleDateFormat(
  174. "yyyy-MM-dd HH:mm:ss");
  175. public Component getTableCellRendererComponent(final JTable table,
  176. final Object value, final boolean isSelected,
  177. final boolean hasFocus, final int row, final int column) {
  178. final PersonIdent pi = (PersonIdent) value;
  179. final String valueStr;
  180. if (pi != null)
  181. valueStr = fmt.format(pi.getWhen());
  182. else
  183. valueStr = "";
  184. return super.getTableCellRendererComponent(table, valueStr,
  185. isSelected, hasFocus, row, column);
  186. }
  187. }
  188. class GraphCellRender extends DefaultTableCellRenderer {
  189. private static final long serialVersionUID = 1L;
  190. private final AWTPlotRenderer renderer = new AWTPlotRenderer(this);
  191. PlotCommit<SwingLane> commit;
  192. public Component getTableCellRendererComponent(final JTable table,
  193. final Object value, final boolean isSelected,
  194. final boolean hasFocus, final int row, final int column) {
  195. super.getTableCellRendererComponent(table, value, isSelected,
  196. hasFocus, row, column);
  197. commit = (PlotCommit<SwingLane>) value;
  198. return this;
  199. }
  200. @Override
  201. protected void paintComponent(final Graphics inputGraphics) {
  202. if (inputGraphics == null)
  203. return;
  204. renderer.paint(inputGraphics, commit);
  205. }
  206. }
  207. static final Stroke[] strokeCache;
  208. static {
  209. strokeCache = new Stroke[4];
  210. for (int i = 1; i < strokeCache.length; i++)
  211. strokeCache[i] = new BasicStroke(i);
  212. }
  213. static Stroke stroke(final int width) {
  214. if (width < strokeCache.length)
  215. return strokeCache[width];
  216. return new BasicStroke(width);
  217. }
  218. }