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 8.0KB

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