|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2004 - 2007 University Of British Columbia and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* University Of British Columbia - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
|
| 12 |
package org.eclipse.mylyn.internal.tasks.ui.wizards; |
| 13 |
|
| 14 |
import org.eclipse.jface.layout.GridDataFactory; |
| 15 |
import org.eclipse.jface.wizard.IWizardPage; |
| 16 |
import org.eclipse.jface.wizard.WizardPage; |
| 17 |
import org.eclipse.mylyn.internal.tasks.core.LocalAttachment; |
| 18 |
import org.eclipse.swt.SWT; |
| 19 |
import org.eclipse.swt.custom.ScrolledComposite; |
| 20 |
import org.eclipse.swt.events.PaintEvent; |
| 21 |
import org.eclipse.swt.events.PaintListener; |
| 22 |
import org.eclipse.swt.events.SelectionEvent; |
| 23 |
import org.eclipse.swt.events.SelectionListener; |
| 24 |
import org.eclipse.swt.graphics.GC; |
| 25 |
import org.eclipse.swt.graphics.Image; |
| 26 |
import org.eclipse.swt.graphics.Point; |
| 27 |
import org.eclipse.swt.graphics.Rectangle; |
| 28 |
import org.eclipse.swt.layout.FillLayout; |
| 29 |
import org.eclipse.swt.layout.GridData; |
| 30 |
import org.eclipse.swt.layout.GridLayout; |
| 31 |
import org.eclipse.swt.widgets.Button; |
| 32 |
import org.eclipse.swt.widgets.Canvas; |
| 33 |
import org.eclipse.swt.widgets.Composite; |
| 34 |
import org.eclipse.swt.widgets.Display; |
| 35 |
import org.eclipse.swt.widgets.Shell; |
| 36 |
|
| 37 |
/** |
| 38 |
* A wizard page to create a screenshot from the display. |
| 39 |
* |
| 40 |
* @author Balazs Brinkus (bug 160572) |
| 41 |
*/ |
| 42 |
public class ScreenShotAttachmentPage extends WizardPage { |
| 43 |
|
| 44 |
private ScreenShotAttachmentPage page; |
| 45 |
|
| 46 |
private LocalAttachment attachment; |
| 47 |
|
| 48 |
private Button makeShotButton; |
| 49 |
|
| 50 |
private Button showShotButton; |
| 51 |
|
| 52 |
private Image screenshotImage; |
| 53 |
|
| 54 |
private Canvas canvas; |
| 55 |
|
| 56 |
protected ScreenShotAttachmentPage(LocalAttachment attachment) { |
| 57 |
super("ScreenShotAttachment"); |
| 58 |
setTitle("Create a screenshot"); |
| 59 |
this.attachment = attachment; |
| 60 |
this.page = this; |
| 61 |
} |
| 62 |
|
| 63 |
public void createControl(Composite parent) { |
| 64 |
Composite composite = new Composite(parent, SWT.NONE); |
| 65 |
GridLayout gridLayout = new GridLayout(); |
| 66 |
gridLayout.numColumns = 2; |
| 67 |
composite.setLayout(gridLayout); |
| 68 |
setControl(composite); |
| 69 |
|
| 70 |
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); |
| 71 |
composite.setLayout(new GridLayout(3, false)); |
| 72 |
|
| 73 |
makeShotButton = new Button(composite, SWT.PUSH); |
| 74 |
makeShotButton.setText("Take a screenshot"); |
| 75 |
makeShotButton.addSelectionListener(new SelectionListener() { |
| 76 |
|
| 77 |
public void widgetSelected(SelectionEvent e) { |
| 78 |
storeScreenshotContent(); |
| 79 |
page.setErrorMessage(null); |
| 80 |
showShotButton.setEnabled(true); |
| 81 |
} |
| 82 |
|
| 83 |
public void widgetDefaultSelected(SelectionEvent e) { |
| 84 |
} |
| 85 |
|
| 86 |
}); |
| 87 |
|
| 88 |
showShotButton = new Button(composite, SWT.PUSH); |
| 89 |
showShotButton.setText("Show the screenshot"); |
| 90 |
showShotButton.addSelectionListener(new SelectionListener() { |
| 91 |
|
| 92 |
public void widgetSelected(SelectionEvent e) { |
| 93 |
showScreenshotContent(); |
| 94 |
} |
| 95 |
|
| 96 |
public void widgetDefaultSelected(SelectionEvent e) { |
| 97 |
} |
| 98 |
|
| 99 |
}); |
| 100 |
|
| 101 |
canvas = new Canvas(composite, SWT.BORDER); |
| 102 |
canvas.setLayoutData(GridDataFactory.fillDefaults() |
| 103 |
.align(SWT.FILL, SWT.FILL) |
| 104 |
.grab(true, true) |
| 105 |
.span(2, 1) |
| 106 |
.create()); |
| 107 |
|
| 108 |
canvas.addPaintListener(new PaintListener() { |
| 109 |
public void paintControl(PaintEvent e) { |
| 110 |
if (screenshotImage != null) { |
| 111 |
Rectangle screenBounds = screenshotImage.getBounds(); |
| 112 |
Rectangle canvasBounds = canvas.getBounds(); |
| 113 |
e.gc.drawImage(screenshotImage, 0, 0, screenBounds.width, screenBounds.height, 0, 0, |
| 114 |
canvasBounds.width, canvasBounds.height); |
| 115 |
} else { |
| 116 |
page.setErrorMessage("Screenshot required"); |
| 117 |
showShotButton.setEnabled(false); |
| 118 |
} |
| 119 |
} |
| 120 |
}); |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
@Override |
| 125 |
public boolean isPageComplete() { |
| 126 |
if (screenshotImage == null) |
| 127 |
return false; |
| 128 |
return true; |
| 129 |
} |
| 130 |
|
| 131 |
@Override |
| 132 |
public IWizardPage getNextPage() { |
| 133 |
NewAttachmentPage page = (NewAttachmentPage) getWizard().getPage("AttachmentDetails"); |
| 134 |
attachment.setContentType("image/jpeg"); |
| 135 |
page.setFilePath(InputAttachmentSourcePage.SCREENSHOT_LABEL); |
| 136 |
page.setContentType(); |
| 137 |
return page; |
| 138 |
} |
| 139 |
|
| 140 |
@Override |
| 141 |
public boolean canFlipToNextPage() { |
| 142 |
return isPageComplete(); |
| 143 |
} |
| 144 |
|
| 145 |
private void storeScreenshotContent() { |
| 146 |
|
| 147 |
final Display display = Display.getDefault(); |
| 148 |
final Shell wizardShell = getWizard().getContainer().getShell(); |
| 149 |
wizardShell.setVisible(false); |
| 150 |
|
| 151 |
display.asyncExec(new Runnable() { |
| 152 |
public void run() { |
| 153 |
GC gc = new GC(display); |
| 154 |
screenshotImage = new Image(display, display.getBounds()); |
| 155 |
gc.copyArea(screenshotImage, 0, 0); |
| 156 |
gc.dispose(); |
| 157 |
canvas.redraw(); |
| 158 |
wizardShell.setVisible(true); |
| 159 |
if (screenshotImage != null) |
| 160 |
setPageComplete(true); |
| 161 |
} |
| 162 |
}); |
| 163 |
} |
| 164 |
|
| 165 |
private void showScreenshotContent() { |
| 166 |
Display display = Display.getDefault(); |
| 167 |
|
| 168 |
Shell popup = new Shell(display.getActiveShell(), SWT.SHELL_TRIM); |
| 169 |
popup.setLayout(new FillLayout()); |
| 170 |
popup.setText("Screenshot Image"); |
| 171 |
|
| 172 |
Rectangle displayBounds = Display.getDefault().getBounds(); |
| 173 |
Point dialogSize = new Point(0, 0); |
| 174 |
dialogSize.x = displayBounds.width / 2; |
| 175 |
dialogSize.y = displayBounds.height / 2; |
| 176 |
Point dialoglocation = new Point(0, 0); |
| 177 |
dialoglocation.x = displayBounds.x + displayBounds.width / 2 - dialogSize.x / 2; |
| 178 |
dialoglocation.y = displayBounds.y + displayBounds.height / 2 - dialogSize.y / 2; |
| 179 |
popup.setSize(dialogSize); |
| 180 |
popup.setLocation(dialoglocation); |
| 181 |
|
| 182 |
ScrolledComposite sc = new ScrolledComposite(popup, SWT.V_SCROLL | SWT.H_SCROLL); |
| 183 |
Canvas canvas = new Canvas(sc, SWT.NONE); |
| 184 |
sc.setContent(canvas); |
| 185 |
canvas.setBounds(display.getBounds()); |
| 186 |
canvas.addPaintListener(new PaintListener() { |
| 187 |
public void paintControl(PaintEvent e) { |
| 188 |
if (screenshotImage != null) |
| 189 |
e.gc.drawImage(screenshotImage, 0, 0); |
| 190 |
} |
| 191 |
}); |
| 192 |
popup.open(); |
| 193 |
} |
| 194 |
|
| 195 |
public Image getScreenshotImage() { |
| 196 |
return screenshotImage; |
| 197 |
} |
| 198 |
|
| 199 |
} |