|
Added
Link Here
|
| 1 |
/****************************************************************************** |
| 2 |
* Copyright (c) 2012 Liferay |
| 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 |
* Gregory Amerson - initial implementation |
| 10 |
******************************************************************************/ |
| 11 |
|
| 12 |
package org.eclipse.sapphire.ui.swt.gef.actions; |
| 13 |
|
| 14 |
import java.io.FileOutputStream; |
| 15 |
|
| 16 |
import org.eclipse.draw2d.IFigure; |
| 17 |
import org.eclipse.draw2d.SWTGraphics; |
| 18 |
import org.eclipse.draw2d.geometry.Rectangle; |
| 19 |
import org.eclipse.gef.GraphicalViewer; |
| 20 |
import org.eclipse.gef.LayerConstants; |
| 21 |
import org.eclipse.gef.editparts.ScalableFreeformRootEditPart; |
| 22 |
import org.eclipse.sapphire.ui.SapphireRenderingContext; |
| 23 |
import org.eclipse.sapphire.ui.diagram.SapphireDiagramActionHandler; |
| 24 |
import org.eclipse.sapphire.ui.swt.gef.DiagramRenderingContext; |
| 25 |
import org.eclipse.sapphire.ui.swt.gef.SapphireDiagramEditor; |
| 26 |
import org.eclipse.swt.SWT; |
| 27 |
import org.eclipse.swt.graphics.GC; |
| 28 |
import org.eclipse.swt.graphics.Image; |
| 29 |
import org.eclipse.swt.graphics.ImageData; |
| 30 |
import org.eclipse.swt.graphics.ImageLoader; |
| 31 |
import org.eclipse.swt.widgets.Display; |
| 32 |
import org.eclipse.swt.widgets.FileDialog; |
| 33 |
import org.eclipse.ui.IEditorInput; |
| 34 |
import org.eclipse.ui.IFileEditorInput; |
| 35 |
|
| 36 |
/** |
| 37 |
* @author <a href="mailto:gregory.amerson@liferay.com">Gregory Amerson</a> |
| 38 |
*/ |
| 39 |
public class SaveAsImageDiagramActionHandler extends SapphireDiagramActionHandler |
| 40 |
{ |
| 41 |
|
| 42 |
@Override |
| 43 |
public boolean canExecute( Object obj ) |
| 44 |
{ |
| 45 |
return true; |
| 46 |
} |
| 47 |
|
| 48 |
@Override |
| 49 |
protected Object run( SapphireRenderingContext context ) |
| 50 |
{ |
| 51 |
DiagramRenderingContext diagramContext = (DiagramRenderingContext) context; |
| 52 |
SapphireDiagramEditor diagramEditor = diagramContext.getDiagramEditor(); |
| 53 |
|
| 54 |
if( diagramEditor != null ) |
| 55 |
{ |
| 56 |
FileDialog dialog = new FileDialog( context.getShell(), SWT.SAVE ); |
| 57 |
|
| 58 |
IEditorInput editorInput = diagramEditor.getPart().adapt( IEditorInput.class ); |
| 59 |
|
| 60 |
if( editorInput instanceof IFileEditorInput ) |
| 61 |
{ |
| 62 |
dialog.setFilterPath( ( (IFileEditorInput) editorInput ).getFile().getParent().getRawLocation().toOSString() ); |
| 63 |
} |
| 64 |
|
| 65 |
dialog.setFileName( editorInput.getName().replaceAll( ".xml", ".png" ) ); |
| 66 |
|
| 67 |
dialog.setFilterExtensions( new String[] { "*.png" } ); |
| 68 |
|
| 69 |
dialog.setText( "Save as image" ); |
| 70 |
|
| 71 |
dialog.setOverwrite( true ); |
| 72 |
|
| 73 |
String filePath = dialog.open(); |
| 74 |
|
| 75 |
if( filePath == null ) |
| 76 |
{ |
| 77 |
return null; |
| 78 |
} |
| 79 |
|
| 80 |
GraphicalViewer graphicalViewer = (GraphicalViewer) diagramEditor.getAdapter( GraphicalViewer.class ); |
| 81 |
|
| 82 |
ScalableFreeformRootEditPart rootEditPart = |
| 83 |
(ScalableFreeformRootEditPart) graphicalViewer.getRootEditPart(); |
| 84 |
|
| 85 |
IFigure figure = rootEditPart.getLayer( LayerConstants.PRINTABLE_LAYERS ); |
| 86 |
|
| 87 |
Rectangle rectangle = figure.getBounds(); |
| 88 |
|
| 89 |
Image image = new Image( Display.getDefault(), rectangle.width, rectangle.height ); |
| 90 |
|
| 91 |
GC gc = new GC( image ); |
| 92 |
SWTGraphics graphics = new SWTGraphics( gc ); |
| 93 |
figure.paint( graphics ); |
| 94 |
|
| 95 |
ImageLoader loader = new ImageLoader(); |
| 96 |
loader.data = new ImageData[] { image.getImageData() }; |
| 97 |
|
| 98 |
try |
| 99 |
{ |
| 100 |
final FileOutputStream output = new FileOutputStream( filePath ); |
| 101 |
|
| 102 |
loader.save( output, SWT.IMAGE_PNG ); |
| 103 |
output.flush(); |
| 104 |
output.close(); |
| 105 |
|
| 106 |
image.dispose(); |
| 107 |
gc.dispose(); |
| 108 |
graphics.dispose(); |
| 109 |
} |
| 110 |
catch( Exception e ) |
| 111 |
{ |
| 112 |
} |
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
return null; |
| 117 |
} |
| 118 |
|
| 119 |
} |