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