/** {@inheritDoc} */ public List getAffectedFiles() { if (viewer != null) { EditPart editpart = viewer.getRootEditPart().getContents(); if (editpart instanceof IGraphicalEditPart) { View view = (View) ((IGraphicalEditPart) editpart).getModel(); if (view != null) { IFile f = WorkspaceSynchronizer.getFile(view.eResource()); return f != null ? Collections.singletonList(f) : Collections.EMPTY_LIST; } } } return super.getAffectedFiles(); }
/** * Ask for a file name and save the viewer containing the currently selected GraphicalEditPart to * a image file. */ @Override public void run() { final IEditorInput editorInput = (IEditorInput) getWorkbenchPart().getAdapter(IEditorInput.class); final SaveAsDialog dialog = new SaveAsDialog(new Shell(Display.getDefault())); dialog.setBlockOnOpen(true); if (editorInput == null) { dialog.setOriginalName("viewerImage" + counter + ".png"); } else { final String fileName = editorInput.getName(); final int extensionPos = fileName.lastIndexOf("."); dialog.setOriginalName(fileName.substring(0, extensionPos) + ".png"); } dialog.create(); dialog.setMessage( "If you don't choose an extension png will be used a default. " + "\n" + "Export is limited to file size of 15MByte -> you may use file ..(read more) " + "\n" + "type svg for very large graphs." + "\n" + "After export the workspace needs a manual refresh to show new image file."); dialog.setTitle("Specify file to export viewer image (png, jpeg, bmp, svg)"); dialog.open(); if (Window.CANCEL == dialog.getReturnCode()) { return; } final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); final IPath path = root.getLocation().append(dialog.getResult()); int format; final String ext = path.getFileExtension(); if (ext.equals("png")) { format = SWT.IMAGE_PNG; // GIFs work only with 8Bit and not with 32 Bits color depth // } else if (ext.equals("gif")) { // format = SWT.IMAGE_GIF; } else if (ext.equals("jpeg")) { format = SWT.IMAGE_JPEG; } else if (ext.equals("bmp")) { format = SWT.IMAGE_BMP; // } else if (ext.equals("ico")) { // format = SWT.IMAGE_ICO; } else if (ext.equalsIgnoreCase("svg")) { format = SWT.IMAGE_UNDEFINED; // TODO: find a more appropriate constant for representing SVGs } else { MessageDialog.openError( null, "Invalid file extension!", "The specified extension (" + ext + ") is not a valid image format extension.\nPlease use png, jpeg, bmp or svg!"); return; } IFigure figure = ((SimpleRootEditPart) viewer.getRootEditPart()).getFigure(); if (figure instanceof Viewport) { // This seems to trim the figure to the smallest rectangle containing all child figures ((Viewport) figure).setSize(1, 1); ((Viewport) figure).validate(); figure = ((Viewport) figure).getContents(); } if (format == SWT.IMAGE_UNDEFINED) { try { final File file = path.toFile(); this.exportToSVG(file, figure); } catch (final IOException e) { e.printStackTrace(); } } else { final byte[] imageCode = createImage(figure, format); try { final File file = path.toFile(); final FileOutputStream fos = new FileOutputStream(file); fos.write(imageCode); fos.flush(); fos.close(); counter++; } catch (final IOException e) { e.printStackTrace(); } } }