Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

CommitGraphPane.java 7.9KB

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