Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 160572 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/mylyn/internal/tasks/ui/wizards/ScreenShotAttachmentPage.java (-53 / +264 lines)
Lines 12-29 Link Here
12
import org.eclipse.jface.wizard.IWizardPage;
12
import org.eclipse.jface.wizard.IWizardPage;
13
import org.eclipse.jface.wizard.WizardPage;
13
import org.eclipse.jface.wizard.WizardPage;
14
import org.eclipse.mylyn.internal.tasks.core.LocalAttachment;
14
import org.eclipse.mylyn.internal.tasks.core.LocalAttachment;
15
import org.eclipse.mylyn.tasks.ui.TasksUiPlugin;
15
import org.eclipse.swt.SWT;
16
import org.eclipse.swt.SWT;
16
import org.eclipse.swt.custom.ScrolledComposite;
17
import org.eclipse.swt.custom.ScrolledComposite;
18
import org.eclipse.swt.events.ControlAdapter;
19
import org.eclipse.swt.events.ControlEvent;
20
import org.eclipse.swt.events.MouseEvent;
21
import org.eclipse.swt.events.MouseListener;
22
import org.eclipse.swt.events.MouseMoveListener;
17
import org.eclipse.swt.events.PaintEvent;
23
import org.eclipse.swt.events.PaintEvent;
18
import org.eclipse.swt.events.PaintListener;
24
import org.eclipse.swt.events.PaintListener;
19
import org.eclipse.swt.events.SelectionEvent;
25
import org.eclipse.swt.events.SelectionEvent;
20
import org.eclipse.swt.events.SelectionListener;
26
import org.eclipse.swt.events.SelectionListener;
27
import org.eclipse.swt.graphics.Cursor;
21
import org.eclipse.swt.graphics.GC;
28
import org.eclipse.swt.graphics.GC;
22
import org.eclipse.swt.graphics.Image;
29
import org.eclipse.swt.graphics.Image;
23
import org.eclipse.swt.graphics.Point;
30
import org.eclipse.swt.graphics.Point;
24
import org.eclipse.swt.graphics.Rectangle;
31
import org.eclipse.swt.graphics.Rectangle;
25
import org.eclipse.swt.layout.FillLayout;
26
import org.eclipse.swt.layout.GridData;
27
import org.eclipse.swt.layout.GridLayout;
32
import org.eclipse.swt.layout.GridLayout;
28
import org.eclipse.swt.widgets.Button;
33
import org.eclipse.swt.widgets.Button;
29
import org.eclipse.swt.widgets.Canvas;
34
import org.eclipse.swt.widgets.Canvas;
Lines 42-55 Link Here
42
47
43
	private LocalAttachment attachment;
48
	private LocalAttachment attachment;
44
49
45
	private Button makeShotButton;
50
	private Button captureButton;
46
51
47
	private Button showShotButton;
52
	private Button fitButton;
48
53
49
	private Image screenshotImage;
54
	private Image screenshotImage;
50
55
51
	private Canvas canvas;
56
	private Canvas canvas;
52
57
58
	private ScrolledComposite scrolledComposite;
59
60
	private Rectangle cropRegionBounds;
61
62
	private boolean isCropFit;
63
64
	private boolean isCropActive;
65
53
	protected ScreenShotAttachmentPage(LocalAttachment attachment) {
66
	protected ScreenShotAttachmentPage(LocalAttachment attachment) {
54
		super("ScreenShotAttachment");
67
		super("ScreenShotAttachment");
55
		setTitle("Create a screenshot");
68
		setTitle("Create a screenshot");
Lines 58-117 Link Here
58
	}
71
	}
59
72
60
	public void createControl(Composite parent) {
73
	public void createControl(Composite parent) {
74
61
		Composite composite = new Composite(parent, SWT.NONE);
75
		Composite composite = new Composite(parent, SWT.NONE);
62
		GridLayout gridLayout = new GridLayout();
76
		composite.setLayout(new GridLayout());
63
		gridLayout.numColumns = 2;
64
		composite.setLayout(gridLayout);
65
		setControl(composite);
77
		setControl(composite);
66
78
67
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
79
		Composite buttonsComposite = new Composite(composite, SWT.NONE);
68
		composite.setLayout(new GridLayout(3, false));
80
		buttonsComposite.setLayout(new GridLayout(2, false));
69
81
		captureButton = new Button(buttonsComposite, SWT.PUSH);
70
		makeShotButton = new Button(composite, SWT.PUSH);
82
		captureButton.setText("Capture");
71
		makeShotButton.setText("Take a screenshot");
83
		captureButton.setImage(TasksUiPlugin.imageDescriptorFromPlugin(TasksUiPlugin.ID_PLUGIN,
72
		makeShotButton.addSelectionListener(new SelectionListener() {
84
				"icons/etool16/scr-capture.gif").createImage());
85
		captureButton.addSelectionListener(new SelectionListener() {
73
86
74
			public void widgetSelected(SelectionEvent e) {
87
			public void widgetSelected(SelectionEvent e) {
75
				storeScreenshotContent();
88
				storeScreenshotContent();
76
				page.setErrorMessage(null);
89
				page.setErrorMessage(null);
77
				showShotButton.setEnabled(true);
90
				fitButton.setEnabled(true);
78
			}
91
			}
79
92
80
			public void widgetDefaultSelected(SelectionEvent e) {
93
			public void widgetDefaultSelected(SelectionEvent e) {
94
				//ignore
81
			}
95
			}
82
96
83
		});
97
		});
84
98
85
		showShotButton = new Button(composite, SWT.PUSH);
99
		fitButton = new Button(buttonsComposite, SWT.TOGGLE);
86
		showShotButton.setText("Show the screenshot");
100
		fitButton.setText("Fit image");
87
		showShotButton.addSelectionListener(new SelectionListener() {
101
		fitButton.setImage(TasksUiPlugin.imageDescriptorFromPlugin(TasksUiPlugin.ID_PLUGIN, "icons/etool16/scr-fit.gif")
102
				.createImage());
103
		fitButton.addSelectionListener(new SelectionListener() {
88
104
89
			public void widgetSelected(SelectionEvent e) {
105
			public void widgetSelected(SelectionEvent e) {
90
				showScreenshotContent();
106
				drawCanvas();
91
			}
107
			}
92
108
93
			public void widgetDefaultSelected(SelectionEvent e) {
109
			public void widgetDefaultSelected(SelectionEvent e) {
110
				// ignore
94
			}
111
			}
95
112
96
		});
113
		});
114
		fitButton.setSelection(true);
115
		fitButton.setEnabled(false);
116
		isCropFit = true;
117
		isCropActive = false;
97
118
98
		canvas = new Canvas(composite, SWT.BORDER);
119
		scrolledComposite = new ScrolledComposite(composite, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
99
		canvas.setLayoutData(GridDataFactory.fillDefaults()
120
		scrolledComposite.setLayoutData(GridDataFactory.fillDefaults()
100
				.align(SWT.FILL, SWT.FILL)
121
				.align(SWT.FILL, SWT.FILL)
101
				.grab(true, true)
122
				.grab(true, true)
102
				.span(2, 1)
103
				.create());
123
				.create());
104
124
125
		canvas = new Canvas(scrolledComposite, SWT.DOUBLE_BUFFERED | SWT.NONE);
126
		scrolledComposite.setContent(canvas);
105
		canvas.addPaintListener(new PaintListener() {
127
		canvas.addPaintListener(new PaintListener() {
106
			public void paintControl(PaintEvent e) {
128
			public void paintControl(PaintEvent e) {
107
				if (screenshotImage != null) {
129
				if (screenshotImage != null) {
108
					Rectangle screenBounds = screenshotImage.getBounds();
130
					Rectangle imageBounds = screenshotImage.getBounds();
109
					Rectangle canvasBounds = canvas.getBounds();
131
					Rectangle canvasBounds = canvas.getBounds();
110
					e.gc.drawImage(screenshotImage, 0, 0, screenBounds.width, screenBounds.height, 0, 0,
132
111
							canvasBounds.width, canvasBounds.height);
133
					if (fitButton.getSelection())
134
						e.gc.drawImage(screenshotImage, 0, 0, imageBounds.width, imageBounds.height, 0, 0,
135
								canvasBounds.width, canvasBounds.height);
136
					else
137
						e.gc.drawImage(screenshotImage, 0, 0);
138
139
					if (cropRegionBounds != null)
140
						drawRegion(e.gc);
141
112
				} else {
142
				} else {
113
					page.setErrorMessage("Screenshot required");
143
					page.setErrorMessage("Screenshot required");
114
					showShotButton.setEnabled(false);
144
					fitButton.setEnabled(false);
145
				}
146
			}
147
		});
148
149
		scrolledComposite.addControlListener(new ControlAdapter() {
150
			@Override
151
			public void controlResized(ControlEvent e) {
152
				drawCanvas();
153
			}
154
		});
155
156
		canvas.addMouseMoveListener(new MouseMoveListener() {
157
			public void mouseMove(MouseEvent e) {
158
				if (!isCropActive && screenshotImage != null) {
159
					drawCanvas();
160
					cropScreenshotContent();
115
				}
161
				}
116
			}
162
			}
117
		});
163
		});
Lines 131-136 Link Here
131
		attachment.setContentType("image/jpeg");
177
		attachment.setContentType("image/jpeg");
132
		page.setFilePath(InputAttachmentSourcePage.SCREENSHOT_LABEL);
178
		page.setFilePath(InputAttachmentSourcePage.SCREENSHOT_LABEL);
133
		page.setContentType();
179
		page.setContentType();
180
		getCropScreenshot();
134
		return page;
181
		return page;
135
	}
182
	}
136
183
Lines 144-157 Link Here
144
		final Display display = Display.getDefault();
191
		final Display display = Display.getDefault();
145
		final Shell wizardShell = getWizard().getContainer().getShell();
192
		final Shell wizardShell = getWizard().getContainer().getShell();
146
		wizardShell.setVisible(false);
193
		wizardShell.setVisible(false);
194
		isCropFit = fitButton.getSelection();
147
195
148
		display.asyncExec(new Runnable() {
196
		display.asyncExec(new Runnable() {
149
			public void run() {
197
			public void run() {
150
				GC gc = new GC(display);
198
				GC gc = new GC(display);
151
				screenshotImage = new Image(display, display.getBounds());
199
				Rectangle displayBounds = display.getBounds();
200
				screenshotImage = new Image(display, displayBounds);
152
				gc.copyArea(screenshotImage, 0, 0);
201
				gc.copyArea(screenshotImage, 0, 0);
202
				gc.drawRectangle(0, 0, displayBounds.width - 1, displayBounds.height - 1);
153
				gc.dispose();
203
				gc.dispose();
154
				canvas.redraw();
204
				drawCanvas();
155
				wizardShell.setVisible(true);
205
				wizardShell.setVisible(true);
156
				if (screenshotImage != null)
206
				if (screenshotImage != null)
157
					setPageComplete(true);
207
					setPageComplete(true);
Lines 159-196 Link Here
159
		});
209
		});
160
	}
210
	}
161
211
162
	private void showScreenshotContent() {
212
	private void cropScreenshotContent() {
163
		Display display = Display.getDefault();
164
213
165
		Shell popup = new Shell(display.getActiveShell(), SWT.SHELL_TRIM);
214
		final Point downPoint = new Point(-1, -1);
166
		popup.setLayout(new FillLayout());
167
		popup.setText("Screenshot Image");
168
215
169
		Rectangle displayBounds = Display.getDefault().getBounds();
216
		final Display display = Display.getDefault();
170
		Point dialogSize = new Point(0, 0);
217
171
		dialogSize.x = displayBounds.width / 2;
218
		final Rectangle originalBounds = screenshotImage.getBounds();
172
		dialogSize.y = displayBounds.height / 2;
219
173
		Point dialoglocation = new Point(0, 0);
220
		final GC cropRegionGC = new GC(canvas);
174
		dialoglocation.x = displayBounds.x + displayBounds.width / 2 - dialogSize.x / 2;
221
175
		dialoglocation.y = displayBounds.y + displayBounds.height / 2 - dialogSize.y / 2;
222
		isCropActive = true;
176
		popup.setSize(dialogSize);
223
177
		popup.setLocation(dialoglocation);
224
		final MouseMoveListener mouseMoveListener = new MouseMoveListener() {
178
225
			public void mouseMove(MouseEvent e) {
179
		ScrolledComposite sc = new ScrolledComposite(popup, SWT.V_SCROLL | SWT.H_SCROLL);
226
				if (downPoint.x != -1 && downPoint.y != -1) {
180
		Canvas canvas = new Canvas(sc, SWT.NONE);
227
					int width = e.x - downPoint.x;
181
		sc.setContent(canvas);
228
					int height = e.y - downPoint.y;
182
		canvas.setBounds(display.getBounds());
229
					canvas.redraw();
183
		canvas.addPaintListener(new PaintListener() {
230
					cropRegionGC.drawRectangle(downPoint.x, downPoint.y, width, height);
184
			public void paintControl(PaintEvent e) {
231
				}
185
				if (screenshotImage != null)
186
					e.gc.drawImage(screenshotImage, 0, 0);
187
			}
232
			}
188
		});
233
		};
189
		popup.open();
234
235
		final MouseListener mouseListener = new MouseListener() {
236
			public void mouseUp(MouseEvent e) {
237
				if (downPoint.x != -1 && downPoint.y != -1) {
238
239
					if (downPoint.x > e.x) {
240
						int x = downPoint.x;
241
						downPoint.x = e.x;
242
						e.x = x;
243
					}
244
					if (downPoint.y > e.y) {
245
						int y = downPoint.y;
246
						downPoint.y = e.y;
247
						e.y = y;
248
					}
249
250
					isCropActive = false;
251
252
					int width;
253
					if (e.x > originalBounds.width) {
254
						width = originalBounds.width - downPoint.x;
255
					} else if (e.x < 0) {
256
						width = downPoint.x;
257
						downPoint.x = 0;
258
					} else {
259
						width = e.x - downPoint.x;
260
					}
261
262
					int height;
263
					if (e.y > originalBounds.height) {
264
						height = originalBounds.height - downPoint.y;
265
					} else if (e.y < 0) {
266
						height = downPoint.y;
267
						downPoint.y = 0;
268
					} else {
269
						height = e.y - downPoint.y;
270
					}
271
272
					display.getActiveShell().setCursor(new Cursor(null, SWT.CURSOR_ARROW));
273
274
					if (width == 0 || height == 0)
275
						cropRegionBounds = null;
276
					else
277
						cropRegionBounds = new Rectangle(downPoint.x, downPoint.y, width, height);
278
279
					canvas.removeMouseMoveListener(mouseMoveListener);
280
					canvas.removeMouseListener(this);
281
					downPoint.x = -1;
282
					downPoint.y = -1;
283
284
					cropRegionGC.dispose();
285
286
					drawCanvas();
287
				}
288
			}
289
290
			public void mouseDown(MouseEvent e) {
291
				if (downPoint.x == -1 && downPoint.y == -1) {
292
					cropRegionBounds = null;
293
					isCropFit = fitButton.getSelection();
294
					display.getActiveShell().setCursor(new Cursor(null, SWT.CURSOR_CROSS));
295
					downPoint.x = e.x;
296
					downPoint.y = e.y;
297
					canvas.addMouseMoveListener(mouseMoveListener);
298
				}
299
			}
300
301
			public void mouseDoubleClick(MouseEvent e) {
302
				// ignore
303
			}
304
		};
305
306
		canvas.addMouseListener(mouseListener);
307
308
	}
309
310
	private void drawCanvas() {
311
		if (fitButton.getSelection()) {
312
			Rectangle bounds = scrolledComposite.getBounds();
313
			bounds.x = 0;
314
			bounds.y = 0;
315
			bounds.width -= 5;
316
			bounds.height -= 5;
317
			canvas.setBounds(bounds);
318
		} else {
319
			canvas.setBounds(screenshotImage.getBounds());
320
		}
321
		scrolledComposite.redraw();
322
		canvas.redraw();
323
	}
324
325
	private void drawRegion(GC gc) {
326
327
		Rectangle displayBounds = Display.getDefault().getBounds();
328
		Rectangle bounds = scrolledComposite.getBounds();
329
		bounds.width -= 5;
330
		bounds.height -= 5;
331
332
		double ratioX = (double) displayBounds.width / (double) bounds.width;
333
		double ratioY = (double) displayBounds.height / (double) bounds.height;
334
335
		Rectangle newCropRegion = new Rectangle(0, 0, 0, 0);
336
337
		if (isCropFit && !fitButton.getSelection()) {
338
			newCropRegion.x = (int) (cropRegionBounds.x * ratioX);
339
			newCropRegion.width = (int) (cropRegionBounds.width * ratioX);
340
			newCropRegion.y = (int) (cropRegionBounds.y * ratioY);
341
			newCropRegion.height = (int) (cropRegionBounds.height * ratioY);
342
		} else if (!isCropFit && fitButton.getSelection()) {
343
			newCropRegion.x = (int) (cropRegionBounds.x / ratioX);
344
			newCropRegion.width = (int) (cropRegionBounds.width / ratioX);
345
			newCropRegion.y = (int) (cropRegionBounds.y / ratioY);
346
			newCropRegion.height = (int) (cropRegionBounds.height / ratioY);
347
		} else {
348
			newCropRegion = cropRegionBounds;
349
		}
350
351
		Rectangle topRegionBounds = new Rectangle(0, 0, displayBounds.width, newCropRegion.y);
352
		int bottomRegionY = newCropRegion.y + newCropRegion.height;
353
		int bottomRegionHeight = displayBounds.height - bottomRegionY;
354
		Rectangle bottomRegionBounds = new Rectangle(0, bottomRegionY, displayBounds.width, bottomRegionHeight);
355
		Rectangle leftRegionBounds = new Rectangle(0, newCropRegion.y, newCropRegion.x, newCropRegion.height);
356
		int rightRegionX = newCropRegion.x + newCropRegion.width;
357
		int rightRegionWidth = displayBounds.width - rightRegionX;
358
		Rectangle rightRegionBounds = new Rectangle(rightRegionX, newCropRegion.y, rightRegionWidth,
359
				newCropRegion.height);
360
361
		gc.setLineStyle(SWT.LINE_DASH);
362
		gc.setAdvanced(true);
363
		gc.setAlpha(200);
364
365
		gc.fillRectangle(topRegionBounds);
366
		gc.fillRectangle(bottomRegionBounds);
367
		gc.fillRectangle(leftRegionBounds);
368
		gc.fillRectangle(rightRegionBounds);
369
370
		gc.drawRectangle(newCropRegion);
371
	}
372
373
	private Image getCropScreenshot() {
374
375
		if (cropRegionBounds == null)
376
			return screenshotImage;
377
378
		Rectangle bounds;
379
		if (!isCropFit) {
380
			bounds = cropRegionBounds;
381
		} else {
382
			bounds = new Rectangle(0, 0, 0, 0);
383
			Rectangle displayBounds = Display.getDefault().getBounds();
384
			Rectangle scrolledBounds = scrolledComposite.getBounds();
385
			double ratioX = (double) displayBounds.width / (double) scrolledBounds.width;
386
			double ratioY = (double) displayBounds.height / (double) scrolledBounds.height;
387
			bounds.x = (int) (cropRegionBounds.x * ratioX);
388
			bounds.width = (int) (cropRegionBounds.width * ratioX);
389
			bounds.y = (int) (cropRegionBounds.y * ratioY);
390
			bounds.height = (int) (cropRegionBounds.height * ratioY);
391
		}
392
393
		Image image = new Image(Display.getDefault(), bounds);
394
		GC gc = new GC(image);
395
		gc.drawImage(screenshotImage, bounds.x, bounds.y, bounds.width, bounds.height, 0, 0, bounds.width,
396
				bounds.height);
397
		gc.drawRectangle(0, 0, bounds.width - 1, bounds.height - 1);
398
		gc.dispose();
399
400
		return image;
190
	}
401
	}
191
402
192
	public Image getScreenshotImage() {
403
	public Image getScreenshotImage() {
193
		return screenshotImage;
404
		return getCropScreenshot();
194
	}
405
	}
195
406
196
}
407
}

Return to bug 160572