Skip to main content
summaryrefslogtreecommitdiffstats
blob: 653f479efb9b24c7bd410bcc1bfc0aede2280353 (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
/*******************************************************************************
 * Copyright (c) 2014, 2015 Ericsson AB 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:
 *   Guy Perron - original version
 *
 ******************************************************************************/

package org.eclipse.mylyn.internal.reviews.ui.dialogs;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import org.apache.commons.lang.StringUtils;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.jface.text.source.LineRange;
import org.eclipse.mylyn.internal.reviews.ui.ReviewsUiPlugin;
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;
import org.eclipse.mylyn.reviews.core.model.IComment;
import org.eclipse.mylyn.reviews.core.model.IFileItem;
import org.eclipse.mylyn.reviews.core.model.IFileVersion;
import org.eclipse.mylyn.reviews.core.model.ILineLocation;
import org.eclipse.mylyn.reviews.core.model.ILineRange;
import org.eclipse.mylyn.reviews.core.model.IReviewItem;
import org.eclipse.mylyn.reviews.core.model.IReviewItemSet;
import org.eclipse.mylyn.reviews.core.model.IReviewsFactory;
import org.eclipse.mylyn.reviews.core.model.IUser;
import org.eclipse.mylyn.reviews.core.spi.ReviewsConnector;
import org.eclipse.mylyn.reviews.core.spi.remote.emf.RemoteEmfConsumer;
import org.eclipse.mylyn.reviews.core.spi.remote.review.IReviewRemoteFactoryProvider;
import org.eclipse.mylyn.reviews.ui.ReviewBehavior;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.ui.TasksUi;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.FormDialog;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.widgets.ScrolledForm;

/**
 * This class implements the dialog used to fill-in the Comment element details.
 *
 * @author Guy Perron
 */
public class CommentInputDialog extends FormDialog {

	private Text fCommentInputTextField;

	private static final int BUTTON_REPLY_ID = 2;

	private static final int BUTTON_REPLY_DONE_ID = 3;

	private static final int BUTTON_DISCARD_ID = 4;

	private static final int BUTTON_EDIT_ID = 5;

	private static final int BUTTON_SAVE_ID = 6;

	private static final String RESIZABLE_DIALOG_SETTINGS = "MyResizableDialogSettings"; //$NON-NLS-1$

	private final ReviewBehavior reviewBehavior;

	private final IReviewItem reviewitem;

	private final LineRange range;

	private List<IComment> commentList;

	private List<Button> commentButtons;

	private boolean isUpdate = false;

	private boolean isDiscard = false;

	private boolean isSave = false;

	private boolean isDone = false;

	private boolean isReply = false;

	private String currentUuid;

	private Composite buttonparent;

	private final Shell parent;

	public CommentInputDialog(Shell aParentShell, ReviewBehavior reviewBehavior, IReviewItem reviewitm, LineRange range) {
		super(aParentShell);
		setShellStyle(SWT.CLOSE | SWT.TITLE | SWT.RESIZE | SWT.MODELESS);
		this.reviewBehavior = reviewBehavior;
		this.reviewitem = reviewitm;
		this.range = range;
		this.parent = aParentShell;

	}

	@Override
	protected IDialogSettings getDialogBoundsSettings() {
		IDialogSettings settings = ReviewsUiPlugin.getDefault().getDialogSettings();

		IDialogSettings section = settings.getSection(RESIZABLE_DIALOG_SETTINGS);
		if (section == null) {
			return settings.addNewSection(RESIZABLE_DIALOG_SETTINGS);
		}
		return section;
	}

	@Override
	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.CANCEL_ID) {
			super.buttonPressed(IDialogConstants.OK_ID);
			parent.setFocus();
			return;
		}

		IComment comment = IReviewsFactory.INSTANCE.createComment();
		comment.setDescription(fCommentInputTextField.getText().trim());
		switch (buttonId) {
		case BUTTON_EDIT_ID:
			comment.setId(currentUuid);
			disposeButtons();
			createSaveDiscard();
			return;
		case BUTTON_DISCARD_ID:
			comment.setId(currentUuid);
			isDiscard = true;
			break;
		case BUTTON_REPLY_ID:
			comment.setId(currentUuid);
			isSave = true;
			isReply = true;
			disposeButtons();
			createSave();
			fCommentInputTextField.setText(""); //$NON-NLS-1$
			break;
		case BUTTON_REPLY_DONE_ID:
			isSave = true;
			isDone = true;
			comment.setDescription(Messages.CommentInputDialog_Done);
			comment.setId(""); //$NON-NLS-1$
			break;
		case BUTTON_SAVE_ID:
			comment.setId(currentUuid);
			isSave = true;
			if (isReply) {
				isDone = true;
				comment.setId(""); //$NON-NLS-1$
				isReply = false;
			}
		}
		comment.setDraft(true);
		comment.setAuthor(getCurrentUser());
		comment.setCreationDate(new Date());

		ILineLocation location = getSelectedLineLocation();
		if (location != null) {
			comment.getLocations().add(location);
		}
		if (buttonId != BUTTON_REPLY_ID) {
			performOperation(comment);
			super.buttonPressed(0);
		}

	}

	private ILineLocation getSelectedLineLocation() {
		ILineLocation location = IReviewsFactory.INSTANCE.createLineLocation();
		ILineRange lineRange = IReviewsFactory.INSTANCE.createLineRange();
		lineRange.setStart(range.getStartLine());
		lineRange.setEnd(range.getStartLine() + range.getNumberOfLines());
		location.getRanges().add(lineRange);
		return location;
	}

	private IUser getCurrentUser() {
		if (reviewitem != null && reviewitem.getReview() != null && reviewitem.getReview().getRepository() != null) {
			return reviewitem.getReview().getRepository().getAccount();
		} else {
			return null;
		}
	}

	private boolean performOperation(final IComment comment) {
		final IReviewItem item = reviewitem;
		final AtomicReference<IStatus> result = new AtomicReference<IStatus>();

		if (isSave || isDiscard) {
			final Job job = new Job(Messages.CommandServerOperation) {

				@Override
				protected IStatus run(IProgressMonitor monitor) {
					IStatus status = null;
					if (isSave) {
						status = reviewBehavior.addComment(item, comment, monitor);
						if (status.isOK()) {
							result.set(status);
							updateClient(comment, item);
							return Status.OK_STATUS;
						}
					} else {
						status = reviewBehavior.discardComment(item, comment, monitor);
						if (status.isOK()) {
							result.set(status);
							updateClient(comment, item);
							return Status.OK_STATUS;
						}
					}
					processServerError(status.getMessage());

					return Status.OK_STATUS;
				}

			};
			job.setUser(true);
			job.schedule();
		}

		return true;

	}

	private void updateClient(final IComment comment, final IReviewItem item) {
		comment.setAuthor(getCurrentUser());

		if (!isUpdate || isDone) {
			item.getComments().add(comment);
		} else if (isDiscard) {
			List<IComment> commentlist = item.getComments();
			for (Iterator<IComment> iter = commentlist.iterator(); iter.hasNext();) {
				IComment element = iter.next();
				if (element.getId() != null && element.getId().equals(currentUuid)) {
					iter.remove();
					break;
				}
			}
		} else {
			List<IComment> commentlist = item.getComments();
			for (int i = 0; i < commentlist.size(); i++) {
				if (commentlist.get(i).getId() != null && commentlist.get(i).getId().equals(currentUuid)) {
					item.getComments().set(i, comment);
					break;
				}
			}

		}

		IFileItem file = null;
		if (item instanceof IFileItem) {
			file = (IFileItem) item;
		} else if (item instanceof IFileVersion) {
			file = ((IFileVersion) item).getFile();
		}
		if (file != null && file.getReview() != null) {
			// Update any review item set observers IFF we belong to a review. (The set might represent a compare,
			// in which case we won't have a relevant model object.)
			updateConsumer(file);

		}
	}

	private void processServerError(final String message) {
		Display.getDefault().syncExec(new Runnable() {
			public void run() {
				final MessageDialog dialog = new MessageDialog(null, Messages.CommentInputDialog_ServerError, null,
						message, MessageDialog.ERROR, new String[] { IDialogConstants.CANCEL_LABEL }, 0);
				dialog.open();
			}
		});
	}

	private void updateConsumer(IFileItem file) {
		TaskRepository taskRepository = TasksUi.getRepositoryManager().getRepository(
				reviewBehavior.getTask().getConnectorKind(), reviewBehavior.getTask().getRepositoryUrl());
		@SuppressWarnings("restriction")
		ReviewsConnector connector = (ReviewsConnector) TasksUiPlugin.getConnector(reviewBehavior.getTask()
				.getConnectorKind());
		IReviewRemoteFactoryProvider factoryProvider = (IReviewRemoteFactoryProvider) connector.getReviewClient(
				taskRepository).getFactoryProvider();

		RemoteEmfConsumer<IReviewItemSet, List<IFileItem>, String, ?, ?, Long> consumer = factoryProvider.getReviewItemSetContentFactory()
				.getConsumerForLocalKey(file.getSet(), file.getSet().getId());
		consumer.updateObservers();
		consumer.release();
	}

	@Override
	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		selectComment(commentList.get(commentList.size() - 1));
		commentButtons.get(commentButtons.size() - 1).setSelection(true);
		return contents;
	}

	@Override
	protected void createFormContent(final IManagedForm mform) {
		final ScrolledForm scrolledform = mform.getForm();
		scrolledform.setExpandVertical(true);
		scrolledform.setExpandHorizontal(true);
		final Composite composite = scrolledform.getBody();

		scrolledform.setContent(composite);

		final GridLayout layout = new GridLayout(1, false);

		composite.setLayout(layout);
		GridData textGridData = null;

		commentButtons = new ArrayList<Button>();
		for (final IComment comment : commentList) {
			final Button commentSelectionButton = new Button(composite, SWT.RADIO);
			commentSelectionButton.setBackground(composite.getBackground());

			final String authorAndDate;
			if (comment.getAuthor() != null) {
				authorAndDate = comment.getAuthor().getDisplayName() + " " //$NON-NLS-1$
						+ comment.getCreationDate().toString();
			} else {
				authorAndDate = Messages.CommentInputDialog_No_author + " " //$NON-NLS-1$
						+ comment.getCreationDate().toString();
			}

			String commentPrefix = StringUtils.abbreviate(comment.getDescription(), 50);
			if (comment.isDraft()) {
				commentSelectionButton.setText(NLS.bind(Messages.CommentInputDialog_Draft, authorAndDate, commentPrefix));
			} else {
				commentSelectionButton.setText(authorAndDate + " " + commentPrefix); //$NON-NLS-1$
			}

			commentSelectionButton.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					selectComment(comment);
				}
			});
			GridDataFactory.fillDefaults().span(2, 1).applyTo(commentSelectionButton);
			commentButtons.add(commentSelectionButton);
		}

		fCommentInputTextField = new Text(composite, SWT.BORDER | SWT.MULTI | SWT.WRAP);

		textGridData = new GridData(GridData.FILL, GridData.FILL, true, true);
		textGridData.horizontalSpan = 2;
		textGridData.minimumHeight = fCommentInputTextField.getLineHeight() * 4;
		fCommentInputTextField.setToolTipText(Messages.ReviewsCommentToolTip);
		fCommentInputTextField.setLayoutData(textGridData);
		fCommentInputTextField.setEnabled(false);

		//Set default focus
		fCommentInputTextField.setFocus();

		this.setHelpAvailable(false);
	}

	@Override
	protected Control createButtonBar(final Composite parent) {
		final Composite composite = new Composite(parent, SWT.NONE);
		GridLayoutFactory.fillDefaults().spacing(0, 0).applyTo(composite);
		composite.setLayoutData(new GridData(SWT.LEFT, SWT.BOTTOM, true, false));
		composite.setFont(parent.getFont());

		final Control buttonSection = super.createButtonBar(composite);
		((GridData) buttonSection.getLayoutData()).grabExcessHorizontalSpace = true;
		((GridData) buttonSection.getLayoutData()).grabExcessVerticalSpace = false;
		buttonSection.addControlListener(new ControlAdapter() {
			@Override
			public void controlResized(ControlEvent e) {
				buttonBar.pack();
			}
		});

		return composite;

	}

	@Override
	public void createButtonsForButtonBar(Composite parent) {
		buttonparent = parent;
		createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
	}

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

	public void setComments(List<IComment> commentList) {
		this.commentList = commentList;
	}

	private void selectComment(final IComment comment) {
		disposeButtons();
		if (comment.isDraft()) {
			createEdit();
		} else {
			createReplyReplyDone();
		}
		currentUuid = comment.getId();
		fCommentInputTextField.setText(comment.getDescription());
		isUpdate = true;
	}

	private void disposeButtons() {
		for (int buttonId : new int[] { BUTTON_REPLY_ID, BUTTON_REPLY_DONE_ID, BUTTON_DISCARD_ID, BUTTON_EDIT_ID,
				BUTTON_SAVE_ID }) {
			Button button = getButton(buttonId);
			if (button != null) {
				button.dispose();
			}
		}
		if (fCommentInputTextField != null) {
			fCommentInputTextField.setEnabled(false);
		}
	}

	private void createEdit() {
		createButton(buttonparent, BUTTON_EDIT_ID, Messages.CommentInputDialog_Edit, true);
		buttonBar.pack();
	}

	private void createReplyReplyDone() {
		createButton(buttonparent, BUTTON_REPLY_ID, Messages.CommentInputDialog_Reply, true);
		createButton(buttonparent, BUTTON_REPLY_DONE_ID, Messages.CommentInputDialog_ReplyDone, true);
		buttonBar.pack();
	}

	private void createSaveDiscard() {
		createButton(buttonparent, BUTTON_SAVE_ID, Messages.CommentInputDialog_Save, true);
		createButton(buttonparent, BUTTON_DISCARD_ID, Messages.CommentInputDialog_Discard, true);
		fCommentInputTextField.setEnabled(true);
		buttonBar.pack();

	}

	private void createSave() {
		createButton(buttonparent, BUTTON_SAVE_ID, Messages.CommentInputDialog_Save, true);
		fCommentInputTextField.setEnabled(true);
		buttonBar.pack();

	}
}

Back to the top