Skip to main content
summaryrefslogtreecommitdiffstats
blob: a3b34fef7de74c8d6fb5d468bd3826b0b3dcc776 (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
/*******************************************************************************
 * Copyright (c) 2010 Tasktop Technologies 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
 *******************************************************************************/

package org.eclipse.mylyn.internal.gerrit.ui.editor;

import java.util.Collections;
import java.util.List;

import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.mylyn.internal.gerrit.core.GerritConnector;
import org.eclipse.mylyn.internal.gerrit.core.GerritUtil;
import org.eclipse.mylyn.internal.gerrit.core.client.GerritChange;
import org.eclipse.mylyn.internal.gerrit.core.client.GerritClient;
import org.eclipse.mylyn.internal.gerrit.ui.operations.AddReviewersDialog;
import org.eclipse.mylyn.tasks.ui.TasksUi;
import org.eclipse.mylyn.tasks.ui.TasksUiUtil;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.ui.forms.IFormColors;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;

import com.google.gerrit.common.data.AccountInfo;
import com.google.gerrit.common.data.ApprovalDetail;
import com.google.gerrit.common.data.ApprovalType;
import com.google.gerrit.common.data.ChangeDetail;
import com.google.gerrit.common.data.ChangeInfo;
import com.google.gerrit.common.data.GerritConfig;
import com.google.gerrit.reviewdb.ApprovalCategory;
import com.google.gerrit.reviewdb.ApprovalCategoryValue;
import com.google.gerrit.reviewdb.Change;
import com.google.gerrit.reviewdb.PatchSetApproval;

/**
 * @author Steffen Pingel
 */
public class ReviewSection extends AbstractGerritSection {

	private Composite composite;

	private FormToolkit toolkit;

	public ReviewSection() {
		setPartName("Review");
	}

	@Override
	protected Control createContent(FormToolkit toolkit, Composite parent) {
		this.toolkit = toolkit;

		composite = toolkit.createComposite(parent);
		GridLayoutFactory.fillDefaults().extendedMargins(0, 0, 0, 5).applyTo(composite);

		GerritChange review = GerritUtil.getChange(getTaskData());
		if (review != null) {
			createReviewContent(toolkit, composite, review.getChangeDetail());
		}
		return composite;
	}

	private void createReviewContent(FormToolkit toolkit, Composite composite, ChangeDetail changeDetail) {
		GerritConfig config = getConfig();

		createPeopleSubSection(composite, changeDetail, config);

		if (changeDetail.getMissingApprovals() != null && changeDetail.getMissingApprovals().size() > 0) {
			createRequirementsSubSection(toolkit, composite, config, changeDetail);
		}

		if (changeDetail.getDependsOn() != null && changeDetail.getDependsOn().size() > 0) {
			createDependenciesSubSection(toolkit, composite, "Depends On", changeDetail, changeDetail.getDependsOn());
		}
		if (changeDetail.getNeededBy() != null && changeDetail.getNeededBy().size() > 0) {
			createDependenciesSubSection(toolkit, composite, "Needed By", changeDetail, changeDetail.getNeededBy());
		}
	}

	void createPeopleSubSection(Composite parent, ChangeDetail changeDetail, GerritConfig config) {
		if (changeDetail.getApprovals().isEmpty() && !canAddReviewers(changeDetail)) {
			return;
		}

		int style = ExpandableComposite.TWISTIE | ExpandableComposite.CLIENT_INDENT
				| ExpandableComposite.LEFT_TEXT_CLIENT_ALIGNMENT;

		final Section subSection = toolkit.createSection(parent, style);
		GridDataFactory.fillDefaults().grab(true, false).applyTo(subSection);
		subSection.setText("Reviewers");

		List<ApprovalType> approvalTypes;
		if (config != null) {
			approvalTypes = config.getApprovalTypes().getApprovalTypes();
		} else {
			approvalTypes = Collections.emptyList();
		}

		Composite composite = toolkit.createComposite(subSection);
		int numColumns = approvalTypes.size() + 1;
		GridLayoutFactory.fillDefaults()
				.numColumns(numColumns)
				.extendedMargins(0, 0, 0, 5)
				.spacing(20, 5)
				.applyTo(composite);
		subSection.setClient(composite);

		if (changeDetail.getApprovals().size() > 0) {
			StringBuilder names = new StringBuilder();

			// column headers
			Label label = new Label(composite, SWT.NONE);
			label.setForeground(toolkit.getColors().getColor(IFormColors.TITLE));
			label.setText(" "); //$NON-NLS-1$
			for (ApprovalType approvalType : approvalTypes) {
				label = new Label(composite, SWT.NONE);
				label.setForeground(toolkit.getColors().getColor(IFormColors.TITLE));
				label.setText(approvalType.getCategory().getName());
			}

			for (ApprovalDetail approvalDetail : changeDetail.getApprovals()) {
				AccountInfo user = changeDetail.getAccounts().get(approvalDetail.getAccount());

				label = new Label(composite, SWT.NONE);
				label.setForeground(toolkit.getColors().getColor(IFormColors.TITLE));
				label.setText(GerritUtil.getUserLabel(user));

				for (ApprovalType approvalType : approvalTypes) {
					PatchSetApproval approval = approvalDetail.getApprovalMap().get(approvalType.getCategory().getId());
					if (approval != null) {
						label = new Label(composite, SWT.NONE);
						GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label);

						ApprovalCategoryValue approvalValue = approvalType.getValue(approval.getValue());
						if (approvalValue != null) {
							label.setText(approvalValue.formatValue());
							label.setToolTipText(approvalValue.format());
						} else {
							label.setText(approval.getValue() + ""); //$NON-NLS-1$
						}
					} else {
						label = new Label(composite, SWT.NONE);
						label.setText(" "); //$NON-NLS-1$
					}
				}

				if (names.length() > 0) {
					names.append(", "); //$NON-NLS-1$
				}
				names.append(GerritUtil.getUserLabel(user));
			}

			if (names.length() > 0) {
				addTextClient(toolkit, subSection, names.toString());
			}
		}

		Composite buttonComposite = new Composite(composite, SWT.NONE);
		GridDataFactory.fillDefaults().span(numColumns, 1).applyTo(buttonComposite);
		RowLayout layout = new RowLayout();
		layout.center = true;
		layout.spacing = 10;
		buttonComposite.setLayout(layout);
		Button addReviewersButton = toolkit.createButton(buttonComposite, "Add Reviewers...", SWT.PUSH);
		addReviewersButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doAddReviewers();
			}
		});
	}

	private void doAddReviewers() {
		AddReviewersDialog dialog = new AddReviewersDialog(getShell(), getTask());
		openOperationDialog(dialog);
	}

	private boolean canAddReviewers(ChangeDetail changeDetail) {
		return changeDetail.getChange().getStatus() == Change.Status.NEW;
	}

	private void createRequirementsSubSection(final FormToolkit toolkit, final Composite parent, GerritConfig config,
			ChangeDetail changeDetail) {
		if (config == null) {
			return;
		}

		int style = ExpandableComposite.TWISTIE | ExpandableComposite.CLIENT_INDENT
				| ExpandableComposite.LEFT_TEXT_CLIENT_ALIGNMENT;

		final Section subSection = toolkit.createSection(parent, style);
		GridDataFactory.fillDefaults().grab(true, false).applyTo(subSection);
		subSection.setText("Requirements");

		Composite composite = toolkit.createComposite(subSection);
		GridLayoutFactory.fillDefaults().numColumns(2).spacing(20, 5).extendedMargins(0, 0, 0, 5).applyTo(composite);
		subSection.setClient(composite);

		StringBuilder sb = new StringBuilder();

		for (ApprovalCategory.Id approvalCategoryId : changeDetail.getMissingApprovals()) {
			ApprovalType type = config.getApprovalTypes().getApprovalType(approvalCategoryId);
			if (type != null) {
				ApprovalCategoryValue approvalValue = type.getMax();
				if (approvalValue != null) {
					Label label1 = new Label(composite, SWT.NONE);
					label1.setText(type.getCategory().getName());
					label1.setForeground(toolkit.getColors().getColor(IFormColors.TITLE));

					Label label2 = new Label(composite, SWT.NONE);
					label2.setText(approvalValue.format());
					//label2.setForeground(toolkit.getColors().getColor(IFormColors.TITLE));

					if (sb.length() > 0) {
						sb.append(", "); //$NON-NLS-1$
					}
					sb.append(type.getCategory().getName());
				}
			}
		}

		addTextClient(toolkit, subSection, sb.toString());
	}

	private void createDependenciesSubSection(final FormToolkit toolkit, final Composite parent, String title,
			ChangeDetail changeDetail, List<ChangeInfo> changeInfos) {
		int style = ExpandableComposite.TWISTIE | ExpandableComposite.CLIENT_INDENT;

		final Section subSection = toolkit.createSection(parent, style);
		GridDataFactory.fillDefaults().grab(true, false).applyTo(subSection);
		subSection.setText(title);

		Composite composite = toolkit.createComposite(subSection);
		GridLayoutFactory.fillDefaults().extendedMargins(0, 0, 0, 5).applyTo(composite);
		subSection.setClient(composite);

		for (final ChangeInfo changeInfo : changeInfos) {
			AccountInfo user = changeDetail.getAccounts().get(changeInfo.getOwner());
			Link link = new Link(composite, SWT.NONE);
			link.setText(NLS.bind(
					"<a>{0}</a>: {1} ({3}) by {2}",
					new String[] { Integer.toString(changeInfo.getId().get()), changeInfo.getSubject(),
							GerritUtil.getUserLabel(user), String.valueOf(changeInfo.getStatus()) }));
			link.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					TasksUiUtil.openTask(getTaskEditorPage().getTaskRepository(), changeInfo.getId() + ""); //$NON-NLS-1$
				}
			});
		}
	}

	private GerritConfig getConfig() {
		GerritConnector connector = (GerritConnector) TasksUi.getRepositoryConnector(getTaskData().getConnectorKind());
		GerritClient client = connector.getClient(getTaskEditorPage().getTaskRepository());
		return client.getConfig();
	}

	@Override
	protected boolean shouldExpandOnCreate() {
		return true;
	}

}

Back to the top