|
Lines 12-34
package org.eclipse.jubula.client.ui.rcp.handlers;
Link Here
|
| 12 |
|
12 |
|
| 13 |
import org.apache.commons.lang.StringUtils; |
13 |
import org.apache.commons.lang.StringUtils; |
| 14 |
import org.eclipse.core.commands.ExecutionEvent; |
14 |
import org.eclipse.core.commands.ExecutionEvent; |
| 15 |
import org.eclipse.jface.viewers.IStructuredSelection; |
15 |
import org.eclipse.jubula.client.ui.constants.Constants; |
| 16 |
import org.eclipse.jubula.client.core.model.TestResultNode; |
|
|
| 17 |
import org.eclipse.jubula.client.ui.handlers.AbstractSelectionBasedHandler; |
16 |
import org.eclipse.jubula.client.ui.handlers.AbstractSelectionBasedHandler; |
| 18 |
import org.eclipse.jubula.client.ui.utils.ErrorHandlingUtil; |
17 |
import org.eclipse.jubula.client.ui.views.imageview.ImageView; |
| 19 |
import org.eclipse.jubula.tools.messagehandling.MessageIDs; |
18 |
import org.eclipse.jubula.client.ui.views.imageview.ImageViewData; |
| 20 |
import org.eclipse.swt.SWT; |
19 |
import org.eclipse.swt.SWT; |
|
|
20 |
import org.eclipse.swt.graphics.ImageData; |
| 21 |
import org.eclipse.swt.graphics.ImageLoader; |
| 21 |
import org.eclipse.swt.widgets.FileDialog; |
22 |
import org.eclipse.swt.widgets.FileDialog; |
|
|
23 |
import org.eclipse.ui.IViewPart; |
| 22 |
import org.eclipse.ui.handlers.HandlerUtil; |
24 |
import org.eclipse.ui.handlers.HandlerUtil; |
| 23 |
|
25 |
|
| 24 |
import java.io.File; |
|
|
| 25 |
import java.io.FileNotFoundException; |
| 26 |
import java.io.FileOutputStream; |
| 27 |
import java.io.IOException; |
| 28 |
import java.io.OutputStream; |
| 29 |
import java.text.DateFormat; |
| 30 |
import java.text.SimpleDateFormat; |
| 31 |
|
| 32 |
/** |
26 |
/** |
| 33 |
* @author BREDEX GmbH |
27 |
* @author BREDEX GmbH |
| 34 |
* @created 11.09.2013 |
28 |
* @created 11.09.2013 |
|
Lines 36-97
import java.text.SimpleDateFormat;
Link Here
|
| 36 |
public class SaveImageAsHandler extends AbstractSelectionBasedHandler { |
30 |
public class SaveImageAsHandler extends AbstractSelectionBasedHandler { |
| 37 |
|
31 |
|
| 38 |
/** |
32 |
/** |
| 39 |
* suggest only 255 character long file names |
|
|
| 40 |
*/ |
| 41 |
private static final int MAX_FILE_NAME_LENGTH = 255; |
| 42 |
|
| 43 |
/** |
| 44 |
* {@inheritDoc} |
33 |
* {@inheritDoc} |
| 45 |
*/ |
34 |
*/ |
| 46 |
protected Object executeImpl(ExecutionEvent event) { |
35 |
protected Object executeImpl(ExecutionEvent event) { |
| 47 |
IStructuredSelection structuredSelection = getSelection(); |
36 |
IViewPart view = HandlerUtil.getActiveSite(event). |
| 48 |
Object selectedObject = structuredSelection.getFirstElement(); |
37 |
getPage().findView(Constants.IMAGEVIEW_ID); |
| 49 |
if (selectedObject instanceof TestResultNode) { |
38 |
|
| 50 |
TestResultNode result = (TestResultNode) selectedObject; |
39 |
if (view != null && view instanceof ImageView) { |
| 51 |
|
40 |
ImageView imageView = (ImageView)view; |
| 52 |
// necessary to get test suite name |
41 |
int maxFileNameLength = 255; |
| 53 |
TestResultNode parent = result; |
42 |
|
| 54 |
while (parent.getParent() != null) { |
43 |
ImageViewData imageViewData = imageView.getImageViewData(); |
| 55 |
parent = parent.getParent(); |
44 |
String imageName = imageViewData.getImageName(); |
| 56 |
} |
45 |
String imageDate = imageViewData.getImageDate(); |
| 57 |
|
46 |
|
| 58 |
// get the date of test from time stamp |
|
|
| 59 |
DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); //$NON-NLS-1$ |
| 60 |
String date = format.format(result.getTimeStamp()); |
| 61 |
|
| 62 |
FileDialog saveDialog = new FileDialog(HandlerUtil |
47 |
FileDialog saveDialog = new FileDialog(HandlerUtil |
| 63 |
.getActiveWorkbenchWindow(event).getShell(), SWT.SAVE); |
48 |
.getActiveWorkbenchWindow(event).getShell(), SWT.SAVE); |
| 64 |
String fileName = "ErrorInTest_" + parent.getName() + "_" //$NON-NLS-1$ //$NON-NLS-2$ |
49 |
String fileEnding = "_" + imageDate + ".png"; //$NON-NLS-1$ //$NON-NLS-2$ |
| 65 |
+ result.getNode().getName(); |
50 |
String fileName = StringUtils.substring(imageName, 0, |
| 66 |
// eliminate whitespaces and characters which are illegal in a file name |
51 |
maxFileNameLength |
| 67 |
fileName = fileName.replaceAll("[\\s\\?\\\\/:|<>\\*\"]", ""); //$NON-NLS-1$ //$NON-NLS-2$ |
|
|
| 68 |
|
| 69 |
String fileEnding = "_" + date + ".png"; //$NON-NLS-1$ //$NON-NLS-2$ |
| 70 |
fileName = StringUtils.substring(fileName, 0, |
| 71 |
MAX_FILE_NAME_LENGTH |
| 72 |
- fileEnding.length() |
52 |
- fileEnding.length() |
| 73 |
- saveDialog.getFilterPath().length()); |
53 |
- saveDialog.getFilterPath().length()); |
| 74 |
fileName = fileName + fileEnding; |
54 |
fileName = fileName + fileEnding; |
|
|
55 |
|
| 75 |
saveDialog.setFileName(fileName); |
56 |
saveDialog.setFileName(fileName); |
| 76 |
saveDialog.setFilterExtensions(new String[] { "*.png" }); //$NON-NLS-1$ |
57 |
saveDialog.setFilterExtensions(new String[] { "*.png" }); //$NON-NLS-1$ |
| 77 |
saveDialog.setOverwrite(true); |
58 |
saveDialog.setOverwrite(true); |
| 78 |
String path = saveDialog.open(); |
59 |
String path = saveDialog.open(); |
| 79 |
|
60 |
|
|
|
61 |
ImageData imageData = imageViewData.getImage().getImageData(); |
| 62 |
|
| 80 |
if (path != null) { |
63 |
if (path != null) { |
| 81 |
if (result.getScreenshot() != null) { |
64 |
if (imageData != null) { |
| 82 |
try { |
65 |
ImageLoader loader = new ImageLoader(); |
| 83 |
File file = new File(path); |
66 |
loader.data = new ImageData[] { imageData }; |
| 84 |
OutputStream out = new FileOutputStream(file); |
67 |
loader.save(path, SWT.IMAGE_PNG); |
| 85 |
out.write(result.getScreenshot()); |
|
|
| 86 |
out.flush(); |
| 87 |
out.close(); |
| 88 |
} catch (FileNotFoundException e) { |
| 89 |
ErrorHandlingUtil.createMessageDialog(MessageIDs. |
| 90 |
E_FILE_NO_PERMISSION); |
| 91 |
} catch (IOException e) { |
| 92 |
ErrorHandlingUtil.createMessageDialog(MessageIDs. |
| 93 |
E_IO_EXCEPTION); |
| 94 |
} |
| 95 |
} |
68 |
} |
| 96 |
} |
69 |
} |
| 97 |
} |
70 |
} |