Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4cf41632e54c98bf49decd11392cc3aa2f1ac56d (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
/*******************************************************************************
 * Copyright (c) 2010, 2014 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
 *     Jan Lohre (SAP) - improvements
 *     Sascha Scholz (SAP) - improvements
 *******************************************************************************/

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

import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.mylyn.commons.ui.CommonImages;
import org.eclipse.mylyn.internal.reviews.ui.ReviewsImages;
import org.eclipse.mylyn.reviews.core.model.IApprovalType;
import org.eclipse.mylyn.reviews.core.model.IChange;
import org.eclipse.mylyn.reviews.core.model.IRequirementEntry;
import org.eclipse.mylyn.reviews.core.model.IReview;
import org.eclipse.mylyn.reviews.core.model.IReviewerEntry;
import org.eclipse.mylyn.reviews.core.model.IUser;
import org.eclipse.mylyn.reviews.core.model.RequirementStatus;
import org.eclipse.mylyn.reviews.ui.spi.factories.AbstractUiFactoryProvider;
import org.eclipse.mylyn.tasks.ui.TasksUiUtil;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
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.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;

/**
 * Displays basic information about a given review corresponding to top sections of Gerrit web interface.
 * 
 * @author Steffen Pingel
 * @author Miles Parker
 */
public abstract class ReviewDetailSection extends AbstractReviewSection {

	public ReviewDetailSection() {
		setPartName(Messages.ReviewDetailSection_Review);
	}

	@Override
	protected Control createContent(FormToolkit toolkit, Composite parent) {
		Control content = super.createContent(toolkit, parent);
		createReviewersSubSection(composite);
		createDependenciesSubSection(toolkit, composite, Messages.ReviewDetailSection_Depends_On, getReview().getParents());
		createDependenciesSubSection(toolkit, composite, Messages.ReviewDetailSection_Needed_By, getReview().getChildren());
		return content;
	}

	protected void createReviewersSubSection(Composite parent) {
		if (getReview().getReviewerApprovals().isEmpty() && !canAddReviewers()) {
			return;
		}

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

		final Section subSection = toolkit.createSection(parent, style);
		GridDataFactory.fillDefaults().grab(true, false).applyTo(subSection);
		subSection.setTitleBarForeground(toolkit.getColors().getColor(IFormColors.TITLE));
		subSection.setText(Messages.ReviewDetailSection_Reviewers);

		Composite composite = toolkit.createComposite(subSection);
		List<IApprovalType> approvalTypes = getModelRepository().getApprovalTypes();
		List<IApprovalType> approvalTypesWithLabel = new ArrayList<IApprovalType>(approvalTypes.size());
		for (IApprovalType approvalType : approvalTypes) {
			if (!approvalType.getName().equals(approvalType.getKey())) {
				approvalTypesWithLabel.add(approvalType);
			}
		}

		int numColumns = approvalTypesWithLabel.size() + 1;
		GridLayoutFactory.fillDefaults()
				.numColumns(numColumns)
				.extendedMargins(0, 0, 0, 5)
				.equalWidth(true)
				.spacing(4, 5)
				.applyTo(composite);
		subSection.setClient(composite);

		if (!approvalTypesWithLabel.isEmpty()) {
			StringBuilder names = new StringBuilder();

			Label headerLabel = new Label(composite, SWT.NONE);
			headerLabel.setText(" "); //$NON-NLS-1$
			StringBuilder needs = new StringBuilder();

			for (IApprovalType approvalType : approvalTypesWithLabel) {
				IRequirementEntry requirementEntry = getReview().getRequirements().get(approvalType);
				Composite headerContainer = new Composite(composite, SWT.NONE);
				headerContainer.setForeground(toolkit.getColors().getColor(IFormColors.TB_BG));
				GridLayoutFactory.fillDefaults().numColumns(2).applyTo(headerContainer);
				GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.FILL).applyTo(headerContainer);
				CLabel approvalHeaderLabel = new CLabel(headerContainer, SWT.NONE);
				approvalHeaderLabel.setForeground(toolkit.getColors().getColor(IFormColors.TITLE));
				approvalHeaderLabel.setText(approvalType.getName());
				GridDataFactory.fillDefaults().align(SWT.RIGHT, SWT.CENTER).applyTo(approvalHeaderLabel);
				RequirementStatus status = null;
				if (requirementEntry != null) {
					status = requirementEntry.getStatus();
					switch (status) {
					case SATISFIED:
						approvalHeaderLabel.setImage(CommonImages.getImage(ReviewsImages.APPROVED));
						break;
					case NOT_SATISFIED:
						approvalHeaderLabel.setImage(CommonImages.getImage(ReviewsImages.UNKNOWN));
						break;
					case REJECTED:
						approvalHeaderLabel.setImage(CommonImages.getImage(ReviewsImages.REJECTED));
						break;
					default:
						//To ensure that label is aligned properly
						approvalHeaderLabel.setImage(CommonImages.getImage(ReviewsImages.BLANK));
						break;
					}
				}
				if (status != null && (status == RequirementStatus.UNKNOWN || status == RequirementStatus.REJECTED)) {
					if (needs.length() > 0) {
						needs.append(", "); //$NON-NLS-1$
					}
					needs.append(approvalType.getName());
				}
			}

			for (Entry<IUser, IReviewerEntry> entry : getReview().getReviewerApprovals().entrySet()) {

				Label reviewerRowLabel = new Label(composite, SWT.NONE);
				reviewerRowLabel.setForeground(toolkit.getColors().getColor(IFormColors.TITLE));
				reviewerRowLabel.setText(entry.getKey().getDisplayName());

				for (IApprovalType approvalType : approvalTypesWithLabel) {
					Integer value = entry.getValue().getApprovals().get(approvalType);
					Label approvalValueLabel = new Label(composite, SWT.NONE);
					GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.FILL).applyTo(approvalValueLabel);
					String rankingText = " "; //$NON-NLS-1$
					if (value != null && value != 0) {
						if (value > 0) {
							rankingText += "+"; //$NON-NLS-1$
							approvalValueLabel.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_DARK_GREEN));
						} else if (value < 0) {
							approvalValueLabel.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
						}
						rankingText += value;
						approvalValueLabel.setToolTipText(value + "  " + approvalType.getName()); //$NON-NLS-1$
					}
					approvalValueLabel.setText(rankingText);
				}
				if (names.length() > 0) {
					names.append(", "); //$NON-NLS-1$
				}
				names.append(entry.getKey().getDisplayName());
			}

			String headerText = names.toString();
			if (needs.length() > 0) {
				headerText += NLS.bind(Messages.ReviewDetailSection_Needs_X, needs);
			}
			if (headerText.length() > 0) {
				addTextClient(toolkit, subSection, headerText);
			}
		}
		if (getUiFactoryProvider() != null) {
			Composite actionComposite = getUiFactoryProvider().createControls(this, composite, getToolkit(),
					getReview());
			GridDataFactory.fillDefaults().span(2, 1).applyTo(actionComposite);
		}
	}

	protected boolean canAddReviewers() {
		return true;
	}

	protected void createDependenciesSubSection(final FormToolkit toolkit, final Composite parent, String title,
			List<IChange> changes) {
		if (changes.isEmpty()) {
			return;
		}

		int style = ExpandableComposite.TWISTIE | ExpandableComposite.CLIENT_INDENT;

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

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

		for (final IChange change : changes) {
			Link link = new Link(composite, SWT.NONE);
			String changeStatus = change.getState() != null ? NLS.bind(Messages.ReviewDetailSection_Bracket_X_bracket,
					String.valueOf(change.getState().getName())) : " "; //$NON-NLS-1$
			String ownerName = change.getOwner().getDisplayName();
			link.setText(NLS.bind(Messages.ReviewDetailSection_Link_W_X_Y_by_Z, new String[] { StringUtils.left(change.getKey(), 9),
					change.getSubject(), ownerName, changeStatus }));
			link.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					TasksUiUtil.openTask(getTaskEditorPage().getTaskRepository(), change.getId());
				}
			});
		}
	}

	protected abstract AbstractUiFactoryProvider<IReview> getUiFactoryProvider();

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

Back to the top