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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. @Override
  131. public int getColumnCount() {
  132. return 3;
  133. }
  134. @Override
  135. public int getRowCount() {
  136. return allCommits != null ? allCommits.size() : 0;
  137. }
  138. @Override
  139. public Object getValueAt(final int rowIndex, final int columnIndex) {
  140. final PlotCommit<SwingLane> c = allCommits.get(rowIndex);
  141. switch (columnIndex) {
  142. case 0:
  143. return c;
  144. case 1:
  145. return authorFor(c);
  146. case 2:
  147. return authorFor(c);
  148. default:
  149. return null;
  150. }
  151. }
  152. PersonIdent authorFor(final PlotCommit<SwingLane> c) {
  153. if (c != lastCommit) {
  154. lastCommit = c;
  155. lastAuthor = c.getAuthorIdent();
  156. }
  157. return lastAuthor;
  158. }
  159. }
  160. static class NameCellRender extends DefaultTableCellRenderer {
  161. private static final long serialVersionUID = 1L;
  162. @Override
  163. public Component getTableCellRendererComponent(final JTable table,
  164. final Object value, final boolean isSelected,
  165. final boolean hasFocus, final int row, final int column) {
  166. final PersonIdent pi = (PersonIdent) value;
  167. final String valueStr;
  168. if (pi != null)
  169. valueStr = pi.getName() + " <" + pi.getEmailAddress() + ">"; //$NON-NLS-1$ //$NON-NLS-2$
  170. else
  171. valueStr = ""; //$NON-NLS-1$
  172. return super.getTableCellRendererComponent(table, valueStr,
  173. isSelected, hasFocus, row, column);
  174. }
  175. }
  176. static class DateCellRender extends DefaultTableCellRenderer {
  177. private static final long serialVersionUID = 1L;
  178. private final DateFormat fmt = new SimpleDateFormat(
  179. "yyyy-MM-dd HH:mm:ss"); //$NON-NLS-1$
  180. @Override
  181. public Component getTableCellRendererComponent(final JTable table,
  182. final Object value, final boolean isSelected,
  183. final boolean hasFocus, final int row, final int column) {
  184. final PersonIdent pi = (PersonIdent) value;
  185. final String valueStr;
  186. if (pi != null)
  187. valueStr = fmt.format(pi.getWhen());
  188. else
  189. valueStr = ""; //$NON-NLS-1$
  190. return super.getTableCellRendererComponent(table, valueStr,
  191. isSelected, hasFocus, row, column);
  192. }
  193. }
  194. static class GraphCellRender extends DefaultTableCellRenderer {
  195. private static final long serialVersionUID = 1L;
  196. private final AWTPlotRenderer renderer = new AWTPlotRenderer(this);
  197. PlotCommit<SwingLane> commit;
  198. @Override
  199. @SuppressWarnings("unchecked")
  200. public Component getTableCellRendererComponent(final JTable table,
  201. final Object value, final boolean isSelected,
  202. final boolean hasFocus, final int row, final int column) {
  203. super.getTableCellRendererComponent(table, value, isSelected,
  204. hasFocus, row, column);
  205. commit = (PlotCommit<SwingLane>) value;
  206. return this;
  207. }
  208. @Override
  209. protected void paintComponent(final Graphics inputGraphics) {
  210. if (inputGraphics == null)
  211. return;
  212. renderer.paint(inputGraphics, commit);
  213. }
  214. }
  215. static final Stroke[] strokeCache;
  216. static {
  217. strokeCache = new Stroke[4];
  218. for (int i = 1; i < strokeCache.length; i++)
  219. strokeCache[i] = new BasicStroke(i);
  220. }
  221. static Stroke stroke(final int width) {
  222. if (width < strokeCache.length)
  223. return strokeCache[width];
  224. return new BasicStroke(width);
  225. }
  226. }