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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. * </ol>
  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,
  90. "ÅOj", false, false, 0, i); //$NON-NLS-1$
  91. h = Math.max(h, c.getPreferredSize().height);
  92. }
  93. setRowHeight(h + getRowMargin());
  94. }
  95. /**
  96. * Get the commit list this pane renders from.
  97. *
  98. * @return the list the caller must populate.
  99. */
  100. public PlotCommitList getCommitList() {
  101. return allCommits;
  102. }
  103. @Override
  104. public void setModel(final TableModel dataModel) {
  105. if (dataModel != null && !(dataModel instanceof CommitTableModel))
  106. throw new ClassCastException(UIText.get().mustBeSpecialTableModel);
  107. super.setModel(dataModel);
  108. }
  109. @Override
  110. protected TableModel createDefaultDataModel() {
  111. return new CommitTableModel();
  112. }
  113. private void configureHeader() {
  114. final JTableHeader th = getTableHeader();
  115. final TableColumnModel cols = th.getColumnModel();
  116. final TableColumn graph = cols.getColumn(0);
  117. final TableColumn author = cols.getColumn(1);
  118. final TableColumn date = cols.getColumn(2);
  119. graph.setHeaderValue(""); //$NON-NLS-1$
  120. author.setHeaderValue(UIText.get().author);
  121. date.setHeaderValue(UIText.get().date);
  122. graph.setCellRenderer(new GraphCellRender());
  123. author.setCellRenderer(new NameCellRender());
  124. date.setCellRenderer(new DateCellRender());
  125. }
  126. class CommitTableModel extends AbstractTableModel {
  127. private static final long serialVersionUID = 1L;
  128. PlotCommit<SwingLane> lastCommit;
  129. PersonIdent lastAuthor;
  130. public int getColumnCount() {
  131. return 3;
  132. }
  133. public int getRowCount() {
  134. return allCommits != null ? allCommits.size() : 0;
  135. }
  136. public Object getValueAt(final int rowIndex, final int columnIndex) {
  137. final PlotCommit<SwingLane> c = allCommits.get(rowIndex);
  138. switch (columnIndex) {
  139. case 0:
  140. return c;
  141. case 1:
  142. return authorFor(c);
  143. case 2:
  144. return authorFor(c);
  145. default:
  146. return null;
  147. }
  148. }
  149. PersonIdent authorFor(final PlotCommit<SwingLane> c) {
  150. if (c != lastCommit) {
  151. lastCommit = c;
  152. lastAuthor = c.getAuthorIdent();
  153. }
  154. return lastAuthor;
  155. }
  156. }
  157. static class NameCellRender extends DefaultTableCellRenderer {
  158. private static final long serialVersionUID = 1L;
  159. public Component getTableCellRendererComponent(final JTable table,
  160. final Object value, final boolean isSelected,
  161. final boolean hasFocus, final int row, final int column) {
  162. final PersonIdent pi = (PersonIdent) value;
  163. final String valueStr;
  164. if (pi != null)
  165. valueStr = pi.getName() + " <" + pi.getEmailAddress() + ">"; //$NON-NLS-1$ //$NON-NLS-2$
  166. else
  167. valueStr = ""; //$NON-NLS-1$
  168. return super.getTableCellRendererComponent(table, valueStr,
  169. isSelected, hasFocus, row, column);
  170. }
  171. }
  172. static class DateCellRender extends DefaultTableCellRenderer {
  173. private static final long serialVersionUID = 1L;
  174. private final DateFormat fmt = new SimpleDateFormat(
  175. "yyyy-MM-dd HH:mm:ss"); //$NON-NLS-1$
  176. public Component getTableCellRendererComponent(final JTable table,
  177. final Object value, final boolean isSelected,
  178. final boolean hasFocus, final int row, final int column) {
  179. final PersonIdent pi = (PersonIdent) value;
  180. final String valueStr;
  181. if (pi != null)
  182. valueStr = fmt.format(pi.getWhen());
  183. else
  184. valueStr = ""; //$NON-NLS-1$
  185. return super.getTableCellRendererComponent(table, valueStr,
  186. isSelected, hasFocus, row, column);
  187. }
  188. }
  189. static class GraphCellRender extends DefaultTableCellRenderer {
  190. private static final long serialVersionUID = 1L;
  191. private final AWTPlotRenderer renderer = new AWTPlotRenderer(this);
  192. PlotCommit<SwingLane> commit;
  193. @SuppressWarnings("unchecked")
  194. public Component getTableCellRendererComponent(final JTable table,
  195. final Object value, final boolean isSelected,
  196. final boolean hasFocus, final int row, final int column) {
  197. super.getTableCellRendererComponent(table, value, isSelected,
  198. hasFocus, row, column);
  199. commit = (PlotCommit<SwingLane>) value;
  200. return this;
  201. }
  202. @Override
  203. protected void paintComponent(final Graphics inputGraphics) {
  204. if (inputGraphics == null)
  205. return;
  206. renderer.paint(inputGraphics, commit);
  207. }
  208. }
  209. static final Stroke[] strokeCache;
  210. static {
  211. strokeCache = new Stroke[4];
  212. for (int i = 1; i < strokeCache.length; i++)
  213. strokeCache[i] = new BasicStroke(i);
  214. }
  215. static Stroke stroke(final int width) {
  216. if (width < strokeCache.length)
  217. return strokeCache[width];
  218. return new BasicStroke(width);
  219. }
  220. }