Skip to main content
summaryrefslogtreecommitdiffstats
blob: f31940976a30048002789814a34a0d7e3046c282 (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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/*******************************************************************************
 * Copyright (c) 2009, 2015 Atlassian 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:
 *     Atlassian - initial API and implementation
 *     Guy Perron 423242: Add ability to edit comment from compare navigator popup
 ******************************************************************************/

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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.PopupDialog;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.jface.text.source.LineRange;
import org.eclipse.mylyn.commons.ui.ShellDragSupport;
import org.eclipse.mylyn.commons.workbench.forms.CommonFormUtil;
import org.eclipse.mylyn.internal.reviews.ui.IReviewActionListener;
import org.eclipse.mylyn.internal.reviews.ui.editors.parts.CommentPart;
import org.eclipse.mylyn.reviews.core.model.IComment;
import org.eclipse.mylyn.reviews.core.model.ILineLocation;
import org.eclipse.mylyn.reviews.core.model.ILocation;
import org.eclipse.mylyn.reviews.core.model.IReviewItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;

import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;

/**
 * Popup to show the information about the annotation in
 * 
 * @author Shawn Minto
 * @author Guy Perron
 */
public class CommentPopupDialog extends PopupDialog implements IReviewActionListener {
	private static final int MAX_WIDTH = 500;

	private static final int MIN_HEIGHT = 70;

	private static final int ICON_BUFFER = 16;

	private Text helpText;

	private int maxWidth;

	private CommentAnnotationHoverInput annotationInput;

	private FormToolkit toolkit;

	private Composite composite;

	private ScrolledComposite scrolledComposite;

	private CommentInformationControl informationControl;

	private IReviewItem reviewItem;

	private LineRange range;

	private static CommentPopupDialog currentPopupDialog;

	private boolean editable;

	private final boolean isCommentNavigator;

	private List<IComment> commentList;

	private InlineCommentEditor commentEditor;

	/**
	 * Creates a dialog that displays review comments associated to a line in a given file
	 * 
	 * @param parent
	 *            the parent shell of the dialog
	 * @param shellStyle
	 *            the SWT styles that will be applied to the dialog
	 * @param reviewitm
	 *            the item/file that is being viewed
	 * @param range
	 *            the line(s) that the comments are associated with
	 * @param isCommentNavigator
	 *            true if the dialog is being created via the Previous/Next comment action (and not via hover)
	 */
	public CommentPopupDialog(Shell parent, int shellStyle, IReviewItem reviewitm, LineRange range,
			boolean isCommentNavigator) {
		super(parent, shellStyle, false, false, false, false, false, null, null);
		this.reviewItem = reviewitm;
		this.range = range;
		this.editable = true;
		this.isCommentNavigator = isCommentNavigator;
	}

	/**
	 * Creates the scrolled composite and inner composite for the dialog
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		toolkit = new FormToolkit(CommonFormUtil.getSharedColors());

		scrolledComposite = new ScrolledComposite(parent, SWT.V_SCROLL);
		GridLayoutFactory.fillDefaults().applyTo(scrolledComposite);
		GridDataFactory.fillDefaults().grab(false, false).applyTo(scrolledComposite);
		scrolledComposite.setExpandHorizontal(true);
		scrolledComposite.setExpandVertical(true);
		scrolledComposite.getVerticalBar().setPageIncrement(4);
		toolkit.adapt(scrolledComposite);

		composite = toolkit.createComposite(scrolledComposite, SWT.NONE);
		composite.setLayout(new GridLayout());
		scrolledComposite.setContent(composite);
		new ShellDragSupport(composite);

		return scrolledComposite;
	}

	/**
	 * The default close action for the dialog (not force close)
	 */
	@Override
	public boolean close() {
		return close(false);
	}

	/**
	 * Closes the dialog
	 * 
	 * @param force
	 *            true if the dialog should close under all conditions, false if the dialog should check for edited text
	 *            in the editor
	 * @return true if the dialog has been closed, false otherwise
	 */
	protected boolean close(boolean force) {
		if (!force && hasEdits()) {
			return false;
		}
		if (editable) {
			InlineCommentEditor.removeFromEditMap(reviewItem.getId(), range.getStartLine());
		}
		return super.close();
	}

	/**
	 * Disposes the dialog
	 * 
	 * @param force
	 *            true if the dialog should be disposed under all conditions, false if the dialog should check for
	 *            edited text in the editor
	 */
	public void dispose(boolean force) {
		if (force || !hasEdits()) {
			currentPopupDialog = null;

			close(true);
			toolkit.dispose();
		}
	}

	/**
	 * Checks if there are any changes in the comment editor text
	 * 
	 * @return true if there is a change with the editor text, false otherwise
	 */
	public boolean hasEdits() {
		return commentEditor != null && commentEditor.hasEdits();
	}

	/**
	 * Sets the focus of the dialog and adjusts the size of the inner composite
	 */
	public void setFocus() {
		getShell().forceFocus();

		if (composite.getChildren().length > 0) {
			composite.getChildren()[0].setFocus();
		}

		Point computeSize = composite.computeSize(SWT.DEFAULT, SWT.DEFAULT);
		if (computeSize.y > scrolledComposite.getSize().y) {
			scrolledComposite.setExpandVertical(true);
			composite.setSize(computeSize);
		}
	}

	/**
	 * Computes the maximum width of the dialog
	 * 
	 * @return a {@link Point} containing the max width of the dialog and its default height
	 */
	public Point computeSizeHint() {
		int widthHint = MAX_WIDTH;
		if (maxWidth < widthHint) {
			widthHint = maxWidth;
		}

		return getShell().computeSize(widthHint, SWT.DEFAULT, true);
	}

	public void removeFocusListener(FocusListener listener) {
		composite.removeFocusListener(listener);
	}

	public void addFocusListener(FocusListener listener) {
		composite.addFocusListener(listener);
	}

	public void removeDisposeListener(DisposeListener listener) {
		getShell().removeDisposeListener(listener);
	}

	public void addDisposeListener(DisposeListener listener) {
		getShell().addDisposeListener(listener);
	}

	/**
	 * Checks to see if the active shell is the shell of this dialog
	 * 
	 * @return true if the active shell is the dialog's shell, false otherwise
	 */
	public boolean isFocusControl() {
		return getShell().getDisplay().getActiveShell() == getShell();
	}

	protected Composite getScrolledComposite() {
		return scrolledComposite;
	}

	protected Composite getComposite() {
		return composite;
	}

	protected InlineCommentEditor getCommentEditor() {
		return commentEditor;
	}

	protected IReviewItem getReviewItem() {
		return reviewItem;
	}

	protected LineRange getRange() {
		return range;
	}

	protected CommentAnnotationHoverInput getAnnotationInput() {
		return annotationInput;
	}

	protected Text getHelpText() {
		return helpText;
	}

	protected boolean getEditable() {
		return editable;
	}

	protected FormToolkit getToolkit() {
		return toolkit;
	}

	public Rectangle getBounds() {
		return getShell().getBounds();
	}

	public Rectangle computeTrim() {
		return getShell().computeTrim(0, 0, 0, 0);
	}

	public void setSizeConstraints(int newMaxWidth, int newMaxHeight) {
		this.maxWidth = newMaxWidth;
	}

	/**
	 * Sets the dialog's shell to a position in the monitor (constrained by the size of the user's monitor)
	 * 
	 * @param location
	 *            the desired location to place the dialog
	 */
	public void setLocation(Point location) {
		Rectangle bounds = getShell().getBounds();
		Rectangle monitorBounds = getShell().getMonitor().getClientArea();
		// ensure the popup fits on the shell's monitor
		bounds.x = constrain(location.x, monitorBounds.x, monitorBounds.x + monitorBounds.width - bounds.width);
		bounds.y = constrain(location.y, monitorBounds.y, monitorBounds.y + monitorBounds.height - bounds.height);

		getShell().setLocation(new Point(bounds.x, bounds.y));
	}

	private int constrain(int value, int min, int max) {
		return Math.max(min, Math.min(max, value));
	}

	/**
	 * Recomputes and sets the size of the dialog (and its shell and composites) to fit its contents
	 */
	protected void recomputeSize() {
		Rectangle bounds = getShell().getBounds();
		Point size = composite.computeSize(SWT.DEFAULT, SWT.DEFAULT);

		scrolledComposite.setMinSize(size);
		int height = constrain(size.y, MIN_HEIGHT, getShell().getMonitor().getClientArea().height);
		getShell().setSize(size.x, height);
		scrolledComposite.setSize(size.x, height);
		setLocation(new Point(bounds.x, bounds.y));

		if (commentEditor != null) {
			Composite editorComposite = commentEditor.getEditorComposite();

			if (scrolledComposite.getVerticalBar().isVisible() && editorComposite != null) {
				Point commentEditorSize = editorComposite.getSize();
				editorComposite.setSize(commentEditorSize.x - scrolledComposite.getVerticalBar().getSize().x,
						commentEditorSize.y);
			}

			if (editorComposite != null) {
				scrolledComposite.setOrigin(0, Integer.MAX_VALUE);
			}
		}
	}

	public void setHeightBasedOnMouse(int mouseY) {
		int mouseYFromBottom = getShell().getMonitor().getClientArea().height - mouseY;
		recomputeSize();
		setSize(MAX_WIDTH, constrain(mouseYFromBottom - ICON_BUFFER, MIN_HEIGHT, getShell().getSize().y));
	}

	/**
	 * Sets the size of the dialog's shell and scrolled composite. The height must be at least minimum height.
	 * 
	 * @param width
	 *            the width in pixels
	 * @param height
	 *            the height in pixels
	 */
	public void setSize(int width, int height) {
		Point computeSize = composite.computeSize(SWT.DEFAULT, SWT.DEFAULT);
		if (computeSize.x > width) {
			width = computeSize.x;
		}
		height = Math.max(height, MIN_HEIGHT);

		getShell().setSize(width, height);
		scrolledComposite.setSize(width, height);
	}

	/**
	 * Initializes the comment dialog with the comments (and review item/line range if it wasn't provided on
	 * construction) from the {@link CommentAnnotationHoverInput} provided
	 * 
	 * @param input
	 *            the input of the comment dialog
	 */
	public void setInput(Object input) {
		if (input instanceof CommentAnnotationHoverInput) {
			this.annotationInput = (CommentAnnotationHoverInput) input;

			// clear the composite in case we are re-using it
			for (Control control : composite.getChildren()) {
				control.dispose();
			}

			currentPopupDialog = this;

			commentList = new ArrayList<IComment>();
			for (CommentAnnotation annotation : annotationInput.getAnnotations()) {
				if (reviewItem == null) {
					if (annotation.getComment().getItem() instanceof IReviewItem) {
						reviewItem = (IReviewItem) annotation.getComment().getItem();
					}
				}
				if (range == null) {
					List<ILocation> locations = annotation.getComment().getLocations();
					if (!locations.isEmpty()) {
						ILocation location = locations.get(0);
						if (location instanceof ILineLocation) {
							range = new LineRange(((ILineLocation) location).getRangeMin(), 1);
						}
					}
				}

				if ((reviewItem != null) && reviewItem.getReview() != null
						&& reviewItem.getReview().getRepository() != null
						&& reviewItem.getReview().getRepository().getAccount() != null
						&& reviewItem.getReview().getRepository().getAccount() != annotation.getComment().getAuthor()
						&& annotation.getComment().isDraft()) {
					continue;
				}

				CommentPart part = new CommentPart(annotation.getComment(), annotationInput.getBehavior());
				commentList.add(annotation.getComment());
				part.hookCustomActionRunListener(this);
				Control control = part.createControl(composite, toolkit);
				toolkit.adapt(control, true, true);
			}

			composite.setBackground(toolkit.getColors().getBackground());

			scrolledComposite.layout(true, true);
			scrolledComposite.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));

			if (InlineCommentEditor.canAddCommentEditor(reviewItem.getId(), range.getStartLine())) {
				commentEditor = new InlineCommentEditor(this);
				scrolledComposite.addMouseListener(createRemoveCommentEditorListener());
				List<Section> sections = getScrolledCompositeSections();
				for (Section s : sections) {
					recursivelyAddCommentEditorListener(s, createAddCommentEditorListener());
				}

				if (!commentList.isEmpty()) {
					helpText = new Text(composite, SWT.NONE);
					helpText.setText(Messages.CommentPopupDialog_HelpText);
				}
			} else {
				this.editable = false;
			}

			if (isCommentNavigator) {
				InlineCommentEditor.addToEditMap(reviewItem.getId(), range.getStartLine());
			}
		}
	}

	/**
	 * Helper method to return all of the {@link Section} in the dialog's scrolled composite
	 * 
	 * @return the list of {@link Section} that are children of the dialog's scrolled composite
	 */
	protected List<Section> getScrolledCompositeSections() {
		List<Section> sections = new ArrayList<Section>();
		if (scrolledComposite.getChildren().length != 0 && scrolledComposite.getChildren()[0] instanceof Composite) {
			Composite sectionContainer = (Composite) scrolledComposite.getChildren()[0];
			sections = FluentIterable.from(Arrays.asList(sectionContainer.getChildren()))
					.filter(Section.class)
					.toList();
		}
		return sections;
	}

	/**
	 * Adds a mouse listener to the {@link Control} provided and its children
	 * 
	 * @param c
	 *            the {@link Control} and its {@link Control} children that you want to add mouse listeners to
	 * @param listener
	 *            the mouse listener that will be added to the provided {@link Control}
	 */
	private void recursivelyAddCommentEditorListener(Control c, MouseListener listener) {
		c.addMouseListener(listener);

		if (c instanceof Composite) {
			Control[] controls = ((Composite) c).getChildren();
			for (Control control : controls) {
				recursivelyAddCommentEditorListener(control, listener);
			}
		}

	}

	/**
	 * Given a {@link Control}, it attempts to find parent {@link Section} recursively
	 * 
	 * @param c
	 *            the {@link Control} that you want to find the parent {@link Section} of
	 * @return the parent {@link Section} or null if it can't be found
	 */
	private Section findParentSection(Control c) {
		if (c == null) {
			return null;
		} else if (c instanceof Section) {
			return (Section) c;
		} else {
			return findParentSection(c.getParent());
		}
	}

	/**
	 * Creates a mouse down {@link MouseAdapter} to add the comment editor to the dialog. It will attempt to get the
	 * comment associated with the {@link Control} you clicked on.
	 * 
	 * @return the {@link MouseAdapter} that can invoke the comment editor on mouse down events
	 */
	private MouseAdapter createAddCommentEditorListener() {
		return new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				getShell().setVisible(true);

				IComment comment = null;
				if (e.getSource() instanceof Control) {
					Section section;
					if (isCommentNavigator) {
						section = getScrolledCompositeSections().get(0);
					} else {
						section = findParentSection((Control) e.getSource());
					}
					if (section != null && section.getData() != null && section.getData() instanceof IComment) {
						comment = (IComment) section.getData();
					}
				}

				if (comment != null && editable) {
					commentEditor.createControl(composite, comment);
				}
			}
		};
	}

	/**
	 * Creates a mouse down {@link MouseAdapter} to remove the comment editor
	 * 
	 * @return the {@link MouseAdapter} that can remove the comment editor on mouse down events
	 */
	private MouseAdapter createRemoveCommentEditorListener() {
		return new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				if (commentEditor != null) {
					commentEditor.removeControl();
				}
			}
		};
	}

	/**
	 * Helper method to get the last comment draft in the dialog's comment list
	 * 
	 * @return the last {@link IComment} in the comment list if there is at least one draft in the comment list or null
	 *         otherwise
	 */
	protected IComment getLastCommentDraft() {
		return FluentIterable.from(commentList).filter(new Predicate<IComment>() {
			@Override
			public boolean apply(IComment input) {
				return input.isDraft();
			}
		}).last().orNull();
	}

	protected void hideHelpText() {
		if (helpText != null) {
			helpText.dispose();
			helpText = null;
		}
	}

	/**
	 * Force closes the dialog when an action is about to run
	 */
	public void actionAboutToRun(Action action) {
		close(true);
	}

	/**
	 * Force close the dialog when an action ran
	 */
	public void actionRan(Action action) {
		close(true);
	}

	public static CommentPopupDialog getCurrentPopupDialog() {
		return currentPopupDialog;
	}

	public void setInformationControl(CommentInformationControl crucibleInformationControl) {
		this.informationControl = crucibleInformationControl;
	}

	public CommentInformationControl getInformationControl() {
		return informationControl;
	}
}

Back to the top