Skip to main content
summaryrefslogtreecommitdiffstats
blob: bea459cc8da926a0fb060cbfd72151c08756f850 (plain) (blame)
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/*******************************************************************************
 * Copyright (c) 2013, 2015 Tasktop Technologies, Ericsson and others.
 * 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
 *
 * Contributors:
 *     Tasktop Technologies - initial API and implementation
 *     Sam Davis - improvements for bug 383592
 *******************************************************************************/

package org.eclipse.mylyn.reviews.ui.spi.editor;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.jface.layout.LayoutConstants;
import org.eclipse.jface.viewers.ColumnViewerToolTipSupport;
import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider;
import org.eclipse.jface.viewers.IOpenListener;
import org.eclipse.jface.viewers.ITreeSelection;
import org.eclipse.jface.viewers.ITreeViewerListener;
import org.eclipse.jface.viewers.OpenEvent;
import org.eclipse.jface.viewers.TreeExpansionEvent;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.window.ToolTip;
import org.eclipse.mylyn.commons.ui.compatibility.CommonColors;
import org.eclipse.mylyn.commons.workbench.forms.ScalingHyperlink;
import org.eclipse.mylyn.internal.reviews.ui.providers.ReviewsLabelProvider;
import org.eclipse.mylyn.internal.tasks.ui.editors.EditorUtil;
import org.eclipse.mylyn.reviews.core.model.IComment;
import org.eclipse.mylyn.reviews.core.model.ICommit;
import org.eclipse.mylyn.reviews.core.model.IFileItem;
import org.eclipse.mylyn.reviews.core.model.ILocation;
import org.eclipse.mylyn.reviews.core.model.IRepository;
import org.eclipse.mylyn.reviews.core.model.IReview;
import org.eclipse.mylyn.reviews.core.model.IReviewItemSet;
import org.eclipse.mylyn.reviews.core.spi.remote.emf.RemoteEmfConsumer;
import org.eclipse.mylyn.reviews.core.spi.remote.emf.RemoteEmfObserver;
import org.eclipse.mylyn.reviews.internal.core.ReviewFileCommentsMapper;
import org.eclipse.mylyn.reviews.internal.core.TaskBuildStatusMapper;
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
import org.eclipse.mylyn.tasks.ui.editors.AbstractAttributeEditor;
import org.eclipse.mylyn.tasks.ui.editors.AbstractTaskEditorPage;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.forms.FormColors;
import org.eclipse.ui.forms.IFormColors;
import org.eclipse.ui.forms.events.ExpansionAdapter;
import org.eclipse.ui.forms.events.ExpansionEvent;
import org.eclipse.ui.forms.events.HyperlinkAdapter;
import org.eclipse.ui.forms.events.HyperlinkEvent;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.Section;

/**
 * @author Steffen Pingel
 * @author Miles Parker
 * @author Sam Davis
 */
@SuppressWarnings("restriction")
public class ReviewSetContentSection {

	private static final int MAXIMUM_ITEMS_SHOWN = 30;

	private final ReviewSetSection parentSection;

	private final IReviewItemSet set;

	private final Section section;

	private TreeViewer viewer;

	private final RemoteEmfObserver<IReviewItemSet, List<IFileItem>, String, Long> itemListObserver = new RemoteEmfObserver<IReviewItemSet, List<IFileItem>, String, Long>() {

		@Override
		public void updated(boolean modified) {
			createItemSetTable();
			updateItemSetTable();
			updateMessage();
			createButtons();
		}

		@Override
		public void updating() {
			updateMessage();
		}
	};

	private final RemoteEmfConsumer<IReviewItemSet, List<IFileItem>, String, ?, ?, Long> itemSetConsumer;

	private Composite treeContainer;

	private Composite actionContainer;

	private final RemoteEmfObserver<IRepository, IReview, String, Date> reviewObserver;

	private final AbstractTaskEditorPage page;

	public ReviewSetContentSection(ReviewSetSection parentSection, final IReviewItemSet set,
			AbstractTaskEditorPage page) {
		this.parentSection = parentSection;
		this.set = set;
		this.page = page;
		int style = ExpandableComposite.TWISTIE | ExpandableComposite.CLIENT_INDENT
				| ExpandableComposite.LEFT_TEXT_CLIENT_ALIGNMENT;
		section = parentSection.getToolkit().createSection(parentSection.getComposite(), style);
		GridDataFactory.fillDefaults().grab(true, false).applyTo(section);
		section.setText(set.getName());
		section.setTitleBarForeground(parentSection.getToolkit().getColors().getColor(IFormColors.TITLE));

		parentSection.addTextClient(parentSection.getToolkit(), section, "", false); //$NON-NLS-1$
		itemSetConsumer = getParentSection().getReviewEditorPage()
				.getFactoryProvider()
				.getReviewItemSetContentFactory()
				.getConsumerForLocalKey(set, set.getId());
		itemListObserver.setConsumer(itemSetConsumer);
		final RemoteEmfConsumer<IRepository, IReview, String, ?, ?, Date> reviewConsumer = getParentSection()
				.getReviewEditorPage().getFactoryProvider().getReviewFactory().getConsumerForModel(
						set.getReview().getRepository(), set.getReview());
		reviewObserver = new RemoteEmfObserver<IRepository, IReview, String, Date>() {
			@Override
			public void updated(boolean modified) {
				if (reviewConsumer.getRemoteObject() != null && section.isExpanded() && modified) {
					itemSetConsumer.retrieve(false);
					updateMessage();
					createButtons();
				}
			}
		};
		reviewConsumer.addObserver(reviewObserver);

		section.addExpansionListener(new ExpansionAdapter() {
			@Override
			public void expansionStateChanged(ExpansionEvent e) {
				if (e.getState()) {
					if (set.getItems().isEmpty()) {
						itemSetConsumer.retrieve(false);
					}
					updateMessage();
					createButtons();
				}
			}
		});

		createMainSection();
		createItemSetTable();
		updateItemSetTable();
		updateMessage();
	}

	public void updateMessage() {
		if (section.isDisposed()) {
			return;
		}
		String message;

		if (itemListObserver.getConsumer().getStatus().isOK()) {
			String time = DateFormat.getDateTimeInstance().format(set.getCreationDate());
			int numComments = set.getAllComments().size();
			if (numComments > 0) {
				message = NLS.bind(Messages.ReviewSetContentSection_X_comma_Y_Comments, time, numComments);
			} else {
				message = time;
			}
			if (itemListObserver != null && itemListObserver.getConsumer().isRetrieving()) {
				message += " " + Messages.Reviews_RetrievingContents; //$NON-NLS-1$
			}
		} else {
			message = NLS.bind(Messages.Reviews_UpdateFailure_X,
					itemListObserver.getConsumer().getStatus().getMessage());
		}

		AbstractReviewSection.appendMessage(getSection(), message);
	}

	void createMainSection() {
		Composite composite = parentSection.getToolkit().createComposite(section);
		GridLayoutFactory.fillDefaults().numColumns(2).spacing(60, LayoutConstants.getSpacing().y).applyTo(composite);
		Composite leftColumn = parentSection.getToolkit().createComposite(composite);
		GridLayoutFactory.fillDefaults().numColumns(2).applyTo(leftColumn);
		Composite rightColumn = parentSection.getToolkit().createComposite(composite);
		GridLayoutFactory.fillDefaults().numColumns(2).applyTo(rightColumn);
		section.setClient(composite);
		FormColors colors = parentSection.getToolkit().getColors();

		createAuthorLabel(leftColumn, colors);
		createCommitterLabel(leftColumn, colors);
		createCommitLink(rightColumn, colors);
		createRefLabel(rightColumn, colors);
		createParentsLinks(composite, colors);

		TaskAttribute buildAttribute = parentSection.getTaskData().getRoot().getAttribute("PATCH_SET-" + set.getId()); //$NON-NLS-1$
		if (buildAttribute != null) {
			Composite buildComposite = parentSection.getToolkit().createComposite(composite);
			GridLayoutFactory.fillDefaults().numColumns(2).applyTo(buildComposite);
			GridDataFactory.fillDefaults().span(2, 1).applyTo(buildComposite);
			AbstractAttributeEditor editor = parentSection.getTaskEditorPage()
					.getAttributeEditorFactory()
					.createEditor(TaskBuildStatusMapper.BUILD_RESULT_TYPE, buildAttribute);
			editor.createLabelControl(buildComposite, parentSection.getToolkit());
			editor.createControl(buildComposite, parentSection.getToolkit());
			getTaskEditorPage().getAttributeEditorToolkit().adapt(editor);
		}

		treeContainer = new Composite(composite, SWT.NONE);
		treeContainer.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_DARK_GRAY));
		GridDataFactory.fillDefaults().span(4, 1).grab(true, true).applyTo(treeContainer);
		GridLayoutFactory.fillDefaults().numColumns(2).applyTo(treeContainer);

		actionContainer = new Composite(composite, SWT.NONE);
		GridDataFactory.fillDefaults().span(4, 1).grab(true, true).applyTo(actionContainer);
		GridLayoutFactory.fillDefaults().numColumns(4).applyTo(actionContainer);
		createButtons();

		reflow();
	}

	private AbstractTaskEditorPage getTaskEditorPage() {
		return page;
	}

	private void createAuthorLabel(Composite composite, FormColors colors) {
		createTitleLabel(composite, colors, Messages.ReviewSetContentSection_Author, SWT.NONE);

		Text authorText = new Text(composite, SWT.READ_ONLY);
		if (set.getAddedBy() != null) {
			authorText.setText(set.getAddedBy().getDisplayName());
		} else {
			authorText.setText(Messages.ReviewSetContentSection_Unspecified);
		}
	}

	private void createCommitterLabel(Composite composite, FormColors colors) {
		Label committerLabel = new Label(composite, SWT.NONE);
		committerLabel.setForeground(colors.getColor(IFormColors.TITLE));
		committerLabel.setText(Messages.ReviewSetContentSection_Committer);

		Text committerText = new Text(composite, SWT.READ_ONLY);
		if (set.getCommittedBy() != null) {
			committerText.setText(set.getCommittedBy().getDisplayName());
		} else {
			committerText.setText(Messages.ReviewSetContentSection_Unspecified);
		}
	}

	private void createCommitLink(Composite composite, FormColors colors) {
		Label commitLabel = new Label(composite, SWT.NONE);
		commitLabel.setForeground(colors.getColor(IFormColors.TITLE));
		commitLabel.setText(Messages.ReviewSetContentSection_Commit);

		ScalingHyperlink commitLink = new ScalingHyperlink(composite, SWT.READ_ONLY);
		commitLink.setText(set.getRevision());
		commitLink.setForeground(CommonColors.HYPERLINK_WIDGET);
		commitLink.registerMouseTrackListener();
		commitLink.addHyperlinkListener(new HyperlinkAdapter() {
			@Override
			public void linkActivated(HyperlinkEvent event) {
				getParentSection().getUiFactoryProvider()
						.getOpenCommitFactory(ReviewSetContentSection.this.getParentSection(), set)
						.execute();
			}
		});
	}

	private void createRefLabel(Composite composite, FormColors colors) {
		Label refLabel = new Label(composite, SWT.NONE);
		refLabel.setForeground(colors.getColor(IFormColors.TITLE));
		refLabel.setText(Messages.ReviewSetContentSection_Ref);

		Text refText = new Text(composite, SWT.READ_ONLY);
		refText.setText(set.getReference());
	}

	private void createParentsLinks(Composite composite, FormColors colors) {
		if (set.getParentCommits().isEmpty()) {
			return;// for Gerrit versions earlier than 2.8 we don't support getting the parents
		}
		Composite parentsComposite = new Composite(composite, SWT.NONE);
		GridLayoutFactory.fillDefaults().numColumns(3).applyTo(parentsComposite);
		GridDataFactory.fillDefaults().span(2, 1).applyTo(parentsComposite);
		Label parentsLabel = new Label(parentsComposite, SWT.NONE);
		parentsLabel.setForeground(colors.getColor(IFormColors.TITLE));
		parentsLabel.setText(Messages.ReviewSetContentSection_Parents);

		final List<String> parentCommitIds = new ArrayList<String>();
		for (ICommit commit : set.getParentCommits()) {
			parentCommitIds.add(commit.getId());
		}

		ScalingHyperlink parentOne = new ScalingHyperlink(parentsComposite, SWT.READ_ONLY);
		GridDataFactory.fillDefaults().indent(13, 0).applyTo(parentOne);
		if (parentCommitIds.size() > 0) {
			addParentCommitHyperlink(parentOne, parentCommitIds.get(0));
		}

		if (parentCommitIds.size() == 2 && parentCommitIds.get(1) != null) {
			ScalingHyperlink parentTwo = new ScalingHyperlink(parentsComposite, SWT.READ_ONLY);
			addParentCommitHyperlink(parentTwo, parentCommitIds.get(1));
		}
	}

	private void addParentCommitHyperlink(ScalingHyperlink commit, final String commitId) {
		commit.setText(commitId);
		commit.setForeground(CommonColors.HYPERLINK_WIDGET);
		commit.registerMouseTrackListener();
		commit.addHyperlinkListener(new HyperlinkAdapter() {
			@Override
			public void linkActivated(HyperlinkEvent event) {
				getParentSection().getUiFactoryProvider()
						.getOpenParentCommitFactory(ReviewSetContentSection.this.getParentSection(), set, commitId)
						.execute();
			}
		});
	}

	private Label createTitleLabel(Composite composite, FormColors colors, String text, int style) {
		Label titleLabel = new Label(composite, style);
		titleLabel.setForeground(colors.getColor(IFormColors.TITLE));
		titleLabel.setText(text);
		return titleLabel;
	}

	public void createItemSetTable() {
		if (viewer == null && !set.getItems().isEmpty()) {

			boolean fixedViewerSize = set.getItems().size() > MAXIMUM_ITEMS_SHOWN;
			int heightHint = fixedViewerSize ? 300 : SWT.DEFAULT;
			int style = SWT.SINGLE | SWT.BORDER | SWT.VIRTUAL;
			if (fixedViewerSize) {
				style |= SWT.V_SCROLL;
			} else {
				style |= SWT.NO_SCROLL;
			}
			viewer = new TreeViewer(treeContainer, style);
			GridDataFactory.fillDefaults()
					.span(2, 1)
					.grab(true, true)
					.hint(500, heightHint)
					.applyTo(viewer.getControl());

			final ReviewSetContentProvider contentProvider = new ReviewSetContentProvider();
			viewer.setContentProvider(contentProvider);

			ColumnViewerToolTipSupport.enableFor(viewer, ToolTip.NO_RECREATE);

			final DelegatingStyledCellLabelProvider styledLabelProvider = new DelegatingStyledCellLabelProvider(
					new ReviewsLabelProvider.Tree(true)) {
				@Override
				public String getToolTipText(Object element) {
					//For some reason tooltips are not delegated..
					return ReviewsLabelProvider.ITEMS_COLUMN.getToolTipText(element);
				};
			};
			viewer.setLabelProvider(styledLabelProvider);
			viewer.addOpenListener(new IOpenListener() {
				public void open(OpenEvent event) {
					ITreeSelection selection = (ITreeSelection) event.getSelection();
					TreePath[] paths = selection.getPaths();
					for (TreePath path : paths) {
						IFileItem file = null;
						IComment comment = null;
						for (int i = 0; i < path.getSegmentCount(); i++) {
							Object o = path.getSegment(i);
							if (o instanceof IFileItem) {
								file = (IFileItem) o;
							} else if (o instanceof ILocation) {
								Object[] children = contentProvider.getChildren(o);
								if (children.length > 0 && children[0] instanceof IComment) {
									comment = (IComment) children[0];
								}
							} else if (o instanceof IComment) {
								comment = (IComment) o;
							}
						}
						if (file != null && comment != null) {
							getParentSection().getUiFactoryProvider()
									.getOpenFileToCommentFactory(ReviewSetContentSection.this.getParentSection(), set,
											file, comment)
									.execute();
						} else if (file != null) {
							getParentSection().getUiFactoryProvider()
									.getOpenFileFactory(ReviewSetContentSection.this.getParentSection(), set, file)
									.execute();
						}
					}
				}
			});
			EditorUtil.addScrollListener(viewer.getTree());
			viewer.setInput(set);
			viewer.addTreeListener(new ITreeViewerListener() {

				@Override
				public void treeExpanded(TreeExpansionEvent event) {
					reflowAsync();
				}

				@Override
				public void treeCollapsed(TreeExpansionEvent event) {
					reflowAsync();
				}
			});
		}
	}

	private void updateItemSetTable() {
		if (set.getItems().size() > 0 && hasViewer()) {
			viewer.setInput(set);
			TaskAttribute fileComments = ReviewSetContentSection.this.page.getModel()
					.getTaskData()
					.getRoot()
					.getAttribute(ReviewFileCommentsMapper.FILE_ITEM_COMMENTS);

			final Set<IComment> toHighlight = new HashSet<IComment>();
			if (fileComments != null) {
				List<IFileItem> files = itemSetConsumer.getModelObject();
				for (IFileItem file : files) {
					for (IComment comment : file.getAllComments()) {
						if (fileComments.getAttribute(comment.getId()) == null) {
							toHighlight.add(comment);
						}
					}
				}
			}

			Display.getDefault().asyncExec(new Runnable() {

				@Override
				public void run() {
					if (hasViewer()) {
						viewer.refresh();
						viewer.expandAll();
						decorateItems(viewer.getTree().getItems(), toHighlight);
						reflow();
					}
				}
			});
		}
	}

	private void decorateItems(TreeItem[] items, Set<IComment> toHighlight) {
		for (TreeItem item : items) {
			if (toHighlight.contains(item.getData())) {
				item.setBackground(getTaskEditorPage().getAttributeEditorToolkit().getColorIncoming());
			}
			decorateItems(item.getItems(), toHighlight);
		}
	}

	public void createButtons() {
		if (!actionContainer.isDisposed()) {
			for (Control oldActionControl : actionContainer.getChildren()) {
				oldActionControl.dispose();
			}
			getParentSection().getUiFactoryProvider().createControls(getParentSection(), actionContainer,
					getParentSection().getToolkit(), set);
			actionContainer.layout();
			reflow();
		}
	}

	public Section getSection() {
		return section;
	}

	public ReviewSetSection getParentSection() {
		return parentSection;
	}

	private void reflowAsync() {
		Display.getDefault().asyncExec(new Runnable() {

			@Override
			public void run() {
				reflow();
			}
		});
	}

	private void reflow() {
		getParentSection().getTaskEditorPage().reflow();
	}

	private boolean hasViewer() {
		return viewer != null && !viewer.getControl().isDisposed();
	}

	public void dispose() {
		itemListObserver.dispose();
		reviewObserver.dispose();
		section.dispose();
	}
}

Back to the top