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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
/*******************************************************************************
* Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.eclipse.egit.ui.internal.push;
import org.eclipse.egit.core.op.PushOperationResult;
import org.eclipse.egit.ui.UIIcons;
import org.eclipse.egit.ui.UIText;
import org.eclipse.egit.ui.internal.components.CenteredImageLabelProvider;
import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.jface.resource.ColorRegistry;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ColumnViewerToolTipSupport;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.IElementComparer;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.RemoteRefUpdate;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.jgit.transport.RemoteRefUpdate.Status;
/**
* Table displaying push operation results.
*/
class PushResultTable {
private static final int TABLE_PREFERRED_WIDTH = 650;
private static final int TABLE_PREFERRED_HEIGHT = 300;
private static final int COLUMN_STATUS_WEIGHT = 40;
private static final int COLUMN_DST_WEIGHT = 40;
private static final int COLUMN_SRC_WEIGHT = 40;
private static final int COLUMN_MODE_WEIGHT = 15;
private static final String IMAGE_DELETE = "MODE_DELETE"; //$NON-NLS-1$
private static final String IMAGE_ADD = "MODE_ADD"; //$NON-NLS-1$
private static final String COLOR_REJECTED_KEY = "REJECTED"; //$NON-NLS-1$
private static final RGB COLOR_REJECTED = new RGB(255, 0, 0);
private static final String COLOR_UPDATED_KEY = "UPDATED"; //$NON-NLS-1$
private static final RGB COLOR_UPDATED = new RGB(0, 255, 0);
private static final String COLOR_UP_TO_DATE_KEY = "UP_TO_DATE"; //$NON-NLS-1$
private static final RGB COLOR_UP_TO_DATE = new RGB(245, 245, 245);
private final TableViewer tableViewer;
private final Composite tablePanel;
private final ImageRegistry imageRegistry;
private final ColorRegistry colorRegistry;
PushResultTable(final Composite parent) {
tablePanel = new Composite(parent, SWT.NONE);
tablePanel.setLayout(new GridLayout());
final GridData layoutData = new GridData(SWT.FILL, SWT.FILL, true, true);
layoutData.heightHint = TABLE_PREFERRED_HEIGHT;
layoutData.widthHint = TABLE_PREFERRED_WIDTH;
tableViewer = new TableViewer(tablePanel);
ColumnViewerToolTipSupport.enableFor(tableViewer);
final Table table = tableViewer.getTable();
table.setLinesVisible(true);
table.setHeaderVisible(true);
imageRegistry = new ImageRegistry(table.getDisplay());
imageRegistry.put(IMAGE_ADD, UIIcons.ELCL16_ADD);
imageRegistry.put(IMAGE_DELETE, UIIcons.ELCL16_DELETE);
colorRegistry = new ColorRegistry(table.getDisplay());
colorRegistry.put(COLOR_REJECTED_KEY, COLOR_REJECTED);
colorRegistry.put(COLOR_UPDATED_KEY, COLOR_UPDATED);
colorRegistry.put(COLOR_UP_TO_DATE_KEY, COLOR_UP_TO_DATE);
tablePanel.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
imageRegistry.dispose();
}
});
tableViewer.setComparer(new IElementComparer() {
// we need this to keep refresh() working while having custom
// equals() in PushOperationResult
public boolean equals(Object a, Object b) {
return a == b;
}
public int hashCode(Object element) {
return element.hashCode();
}
});
tableViewer.setContentProvider(new RefUpdateContentProvider());
tableViewer.setInput(null);
}
void setData(final Repository localDb, final PushOperationResult result) {
// We have to recreate columns.
for (final TableColumn tc : tableViewer.getTable().getColumns())
tc.dispose();
// Set empty result for a while.
tableViewer.setInput(null);
// Layout should be recreated to work properly.
final TableColumnLayout layout = new TableColumnLayout();
tablePanel.setLayout(layout);
final TableViewerColumn modeViewer = createColumn(layout,
UIText.PushResultTable_columnMode, COLUMN_MODE_WEIGHT,
SWT.CENTER);
modeViewer.setLabelProvider(new CenteredImageLabelProvider() {
@Override
public Image getImage(Object element) {
if (((RefUpdateElement) element).isDelete())
return imageRegistry.get(IMAGE_DELETE);
return imageRegistry.get(IMAGE_ADD);
}
@Override
public String getToolTipText(Object element) {
if (((RefUpdateElement) element).isDelete())
return UIText.RefSpecPanel_modeDeleteDescription;
return UIText.RefSpecPanel_modeUpdateDescription;
}
});
final TableViewerColumn srcViewer = createColumn(layout,
UIText.PushResultTable_columnSrc, COLUMN_SRC_WEIGHT, SWT.LEFT);
srcViewer.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
return ((RefUpdateElement) element).getSrcRefName();
}
});
final TableViewerColumn dstViewer = createColumn(layout,
UIText.PushResultTable_columnDst, COLUMN_DST_WEIGHT, SWT.LEFT);
dstViewer.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
return ((RefUpdateElement) element).getDstRefName();
}
});
if (result == null) {
tablePanel.layout();
return;
}
int i = 0;
for (final URIish uri : result.getURIs()) {
final TableViewerColumn statusViewer = createColumn(layout, NLS
.bind(UIText.PushResultTable_columnStatusRepo, Integer
.toString(++i)), COLUMN_STATUS_WEIGHT, SWT.CENTER);
statusViewer.getColumn().setToolTipText(uri.toString());
statusViewer.setLabelProvider(new UpdateStatusLabelProvider(
localDb, uri));
}
tableViewer.setInput(result);
tablePanel.layout();
}
Control getControl() {
return tablePanel;
}
private TableViewerColumn createColumn(
final TableColumnLayout columnLayout, final String text,
final int weight, final int style) {
final TableViewerColumn viewerColumn = new TableViewerColumn(
tableViewer, style);
final TableColumn column = viewerColumn.getColumn();
column.setText(text);
columnLayout.setColumnData(column, new ColumnWeightData(weight));
return viewerColumn;
}
private class UpdateStatusLabelProvider extends ColumnLabelProvider {
private Repository localDb;
private final URIish uri;
UpdateStatusLabelProvider(final Repository localDb, final URIish uri) {
this.localDb = localDb;
this.uri = uri;
}
@Override
public String getText(Object element) {
final RefUpdateElement rue = (RefUpdateElement) element;
if (!rue.isSuccessfulConnection(uri))
return UIText.PushResultTable_statusConnectionFailed;
final RemoteRefUpdate rru = rue.getRemoteRefUpdate(uri);
switch (rru.getStatus()) {
case OK:
if (rru.isDelete())
return UIText.PushResultTable_statusOkDeleted;
final Ref oldRef = rue.getAdvertisedRemoteRef(uri);
if (oldRef == null) {
if (rue.getDstRefName().startsWith(Constants.R_TAGS))
return UIText.PushResultTable_statusOkNewTag;
return UIText.PushResultTable_statusOkNewBranch;
}
return oldRef.getObjectId().abbreviate(localDb).name()
+ (rru.isFastForward() ? ".." : "...") //$NON-NLS-1$ //$NON-NLS-2$
+ rru.getNewObjectId().abbreviate(localDb).name();
case UP_TO_DATE:
return UIText.PushResultTable_statusUpToDate;
case NON_EXISTING:
return UIText.PushResultTable_statusNoMatch;
case REJECTED_NODELETE:
case REJECTED_NONFASTFORWARD:
case REJECTED_REMOTE_CHANGED:
return UIText.PushResultTable_statusRejected;
case REJECTED_OTHER_REASON:
return UIText.PushResultTable_statusRemoteRejected;
default:
throw new IllegalArgumentException(NLS.bind(
UIText.PushResultTable_statusUnexpected, rru
.getStatus()));
}
}
@Override
public Color getBackground(Object element) {
final RefUpdateElement rue = (RefUpdateElement) element;
if (!rue.isSuccessfulConnection(uri))
return colorRegistry.get(COLOR_REJECTED_KEY);
final Status status = rue.getRemoteRefUpdate(uri).getStatus();
switch (status) {
case OK:
return colorRegistry.get(COLOR_UPDATED_KEY);
case UP_TO_DATE:
case NON_EXISTING:
return colorRegistry.get(COLOR_UP_TO_DATE_KEY);
case REJECTED_NODELETE:
case REJECTED_NONFASTFORWARD:
case REJECTED_REMOTE_CHANGED:
case REJECTED_OTHER_REASON:
return colorRegistry.get(COLOR_REJECTED_KEY);
default:
throw new IllegalArgumentException(NLS.bind(
UIText.PushResultTable_statusUnexpected, status));
}
}
@Override
public String getToolTipText(Object element) {
final RefUpdateElement rue = (RefUpdateElement) element;
if (!rue.isSuccessfulConnection(uri))
return rue.getErrorMessage(uri);
final RemoteRefUpdate rru = rue.getRemoteRefUpdate(uri);
final Ref oldRef = rue.getAdvertisedRemoteRef(uri);
switch (rru.getStatus()) {
case OK:
if (rru.isDelete())
return NLS.bind(UIText.PushResultTable_statusDetailDeleted,
oldRef.getObjectId().abbreviate(localDb).name());
if (oldRef == null)
return null;
if (rru.isFastForward())
return UIText.PushResultTable_statusDetailFastForward;
return UIText.PushResultTable_statusDetailForcedUpdate;
case UP_TO_DATE:
return null;
case NON_EXISTING:
return UIText.PushResultTable_statusDetailNonExisting;
case REJECTED_NODELETE:
return UIText.PushResultTable_statusDetailNoDelete;
case REJECTED_NONFASTFORWARD:
return UIText.PushResultTable_statusDetailNonFastForward;
case REJECTED_REMOTE_CHANGED:
final Ref remoteRef = oldRef;
final String curVal;
if (remoteRef == null)
curVal = UIText.PushResultTable_refNonExisting;
else
curVal = remoteRef.getObjectId().abbreviate(localDb).name();
final ObjectId expectedOldObjectId = rru
.getExpectedOldObjectId();
final String expVal;
if (expectedOldObjectId.equals(ObjectId.zeroId()))
expVal = UIText.PushResultTable_refNonExisting;
else
expVal = expectedOldObjectId.abbreviate(localDb).name();
return NLS.bind(UIText.PushResultTable_statusDetailChanged,
curVal, expVal);
case REJECTED_OTHER_REASON:
return rru.getMessage();
default:
throw new IllegalArgumentException(NLS.bind(
UIText.PushResultTable_statusUnexpected, rru
.getStatus()));
}
}
}
}
|