1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.eclipse.jgit.awtui;
45
46 import java.awt.BasicStroke;
47 import java.awt.Component;
48 import java.awt.Graphics;
49 import java.awt.Stroke;
50 import java.text.DateFormat;
51 import java.text.SimpleDateFormat;
52
53 import javax.swing.JTable;
54 import javax.swing.ListSelectionModel;
55 import javax.swing.table.AbstractTableModel;
56 import javax.swing.table.DefaultTableCellRenderer;
57 import javax.swing.table.JTableHeader;
58 import javax.swing.table.TableCellRenderer;
59 import javax.swing.table.TableColumn;
60 import javax.swing.table.TableColumnModel;
61 import javax.swing.table.TableModel;
62
63 import org.eclipse.jgit.awtui.SwingCommitList.SwingLane;
64 import org.eclipse.jgit.lib.PersonIdent;
65 import org.eclipse.jgit.revplot.PlotCommit;
66 import org.eclipse.jgit.revplot.PlotCommitList;
67
68
69
70
71
72
73
74
75
76
77
78
79 public class CommitGraphPane extends JTable {
80 private static final long serialVersionUID = 1L;
81
82 private final SwingCommitList allCommits;
83
84
85
86
87 public CommitGraphPane() {
88 allCommits = new SwingCommitList();
89 configureHeader();
90 setShowHorizontalLines(false);
91 setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
92 configureRowHeight();
93 }
94
95 private void configureRowHeight() {
96 int h = 0;
97 for (int i = 0; i<getColumnCount(); ++i) {
98 TableCellRenderer renderer = getDefaultRenderer(getColumnClass(i));
99 Component c = renderer.getTableCellRendererComponent(this,
100 "ÅOj", false, false, 0, i);
101 h = Math.max(h, c.getPreferredSize().height);
102 }
103 setRowHeight(h + getRowMargin());
104 }
105
106
107
108
109
110
111 public PlotCommitList getCommitList() {
112 return allCommits;
113 }
114
115
116 @Override
117 public void setModel(TableModel dataModel) {
118 if (dataModel != null && !(dataModel instanceof CommitTableModel))
119 throw new ClassCastException(UIText.get().mustBeSpecialTableModel);
120 super.setModel(dataModel);
121 }
122
123
124 @Override
125 protected TableModel createDefaultDataModel() {
126 return new CommitTableModel();
127 }
128
129 private void configureHeader() {
130 final JTableHeader th = getTableHeader();
131 final TableColumnModel cols = th.getColumnModel();
132
133 final TableColumn graph = cols.getColumn(0);
134 final TableColumn author = cols.getColumn(1);
135 final TableColumn date = cols.getColumn(2);
136
137 graph.setHeaderValue("");
138 author.setHeaderValue(UIText.get().author);
139 date.setHeaderValue(UIText.get().date);
140
141 graph.setCellRenderer(new GraphCellRender());
142 author.setCellRenderer(new NameCellRender());
143 date.setCellRenderer(new DateCellRender());
144 }
145
146 class CommitTableModel extends AbstractTableModel {
147 private static final long serialVersionUID = 1L;
148
149 PlotCommit<SwingLane> lastCommit;
150
151 PersonIdent lastAuthor;
152
153 @Override
154 public int getColumnCount() {
155 return 3;
156 }
157
158 @Override
159 public int getRowCount() {
160 return allCommits != null ? allCommits.size() : 0;
161 }
162
163 @Override
164 public Object getValueAt(int rowIndex, int columnIndex) {
165 final PlotCommit<SwingLane> c = allCommits.get(rowIndex);
166 switch (columnIndex) {
167 case 0:
168 return c;
169 case 1:
170 return authorFor(c);
171 case 2:
172 return authorFor(c);
173 default:
174 return null;
175 }
176 }
177
178 PersonIdent authorFor(PlotCommit<SwingLane> c) {
179 if (c != lastCommit) {
180 lastCommit = c;
181 lastAuthor = c.getAuthorIdent();
182 }
183 return lastAuthor;
184 }
185 }
186
187 static class NameCellRender extends DefaultTableCellRenderer {
188 private static final long serialVersionUID = 1L;
189
190 @Override
191 public Component getTableCellRendererComponent(final JTable table,
192 final Object value, final boolean isSelected,
193 final boolean hasFocus, final int row, final int column) {
194 final PersonIdent./../../../org/eclipse/jgit/lib/PersonIdent.html#PersonIdent">PersonIdent pi = (PersonIdent) value;
195
196 final String valueStr;
197 if (pi != null)
198 valueStr = pi.getName() + " <" + pi.getEmailAddress() + ">";
199 else
200 valueStr = "";
201 return super.getTableCellRendererComponent(table, valueStr,
202 isSelected, hasFocus, row, column);
203 }
204 }
205
206 static class DateCellRender extends DefaultTableCellRenderer {
207 private static final long serialVersionUID = 1L;
208
209 private final DateFormat fmt = new SimpleDateFormat(
210 "yyyy-MM-dd HH:mm:ss");
211
212 @Override
213 public Component getTableCellRendererComponent(final JTable table,
214 final Object value, final boolean isSelected,
215 final boolean hasFocus, final int row, final int column) {
216 final PersonIdent./../../../org/eclipse/jgit/lib/PersonIdent.html#PersonIdent">PersonIdent pi = (PersonIdent) value;
217
218 final String valueStr;
219 if (pi != null)
220 valueStr = fmt.format(pi.getWhen());
221 else
222 valueStr = "";
223 return super.getTableCellRendererComponent(table, valueStr,
224 isSelected, hasFocus, row, column);
225 }
226 }
227
228 static class GraphCellRender extends DefaultTableCellRenderer {
229 private static final long serialVersionUID = 1L;
230
231 private final AWTPlotRendererhtml#AWTPlotRenderer">AWTPlotRenderer renderer = new AWTPlotRenderer(this);
232
233 PlotCommit<SwingLane> commit;
234
235 @Override
236 @SuppressWarnings("unchecked")
237 public Component getTableCellRendererComponent(final JTable table,
238 final Object value, final boolean isSelected,
239 final boolean hasFocus, final int row, final int column) {
240 super.getTableCellRendererComponent(table, value, isSelected,
241 hasFocus, row, column);
242 commit = (PlotCommit<SwingLane>) value;
243 return this;
244 }
245
246 @Override
247 protected void paintComponent(Graphics inputGraphics) {
248 if (inputGraphics == null)
249 return;
250 renderer.paint(inputGraphics, commit);
251 }
252 }
253
254 static final Stroke[] strokeCache;
255
256 static {
257 strokeCache = new Stroke[4];
258 for (int i = 1; i < strokeCache.length; i++)
259 strokeCache[i] = new BasicStroke(i);
260 }
261
262 static Stroke stroke(int width) {
263 if (width < strokeCache.length)
264 return strokeCache[width];
265 return new BasicStroke(width);
266 }
267
268 }