Author: achabatar
Date: 2007-12-13 10:46:03 -0500 (Thu, 13 Dec 2007)
New Revision: 5285
Added:
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/actions/ExportImageAction.java
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/icons/export.png
Modified:
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/VisualEditor.java
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/popup/PopupMenuProvider.java
Log:
http://jira.jboss.com/jira/browse/JBIDE-1418
Modified:
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/VisualEditor.java
===================================================================
---
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/VisualEditor.java 2007-12-13
15:45:38 UTC (rev 5284)
+++
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/VisualEditor.java 2007-12-13
15:46:03 UTC (rev 5285)
@@ -34,6 +34,7 @@
import org.hibernate.console.ConsoleConfiguration;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.RootClass;
+import org.jboss.tools.hibernate.ui.veditor.editors.actions.ExportImageAction;
import org.jboss.tools.hibernate.ui.veditor.editors.actions.OpenMappingAction;
import org.jboss.tools.hibernate.ui.veditor.editors.actions.OpenSourceAction;
import org.jboss.tools.hibernate.ui.veditor.editors.model.ModelElement;
@@ -73,7 +74,11 @@
viewer.setContextMenu(provider);
getSite().registerContextMenu("FlowDiagramContextmenu", provider, viewer);
}
-
+
+ public GraphicalViewer getEditPartViewer() {
+ return getGraphicalViewer();
+ }
+
protected void createActions() {
getEditorSite().getActionBars().setGlobalActionHandler(ActionFactory.REFRESH.getId(),new
WorkbenchPartAction(this){
@@ -98,6 +103,10 @@
action = new OpenSourceAction(this);
registry.registerAction(action);
+
+ action = new ExportImageAction(this);
+ registry.registerAction(action);
+
}
private TransferDropTargetListener createTransferDropTargetListener() {
Added:
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/actions/ExportImageAction.java
===================================================================
---
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/actions/ExportImageAction.java
(rev 0)
+++
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/actions/ExportImageAction.java 2007-12-13
15:46:03 UTC (rev 5285)
@@ -0,0 +1,126 @@
+package org.jboss.tools.hibernate.ui.veditor.editors.actions;
+
+import java.io.ByteArrayOutputStream;
+import java.io.FileOutputStream;
+
+import org.eclipse.draw2d.Graphics;
+import org.eclipse.draw2d.IFigure;
+import org.eclipse.draw2d.SWTGraphics;
+import org.eclipse.draw2d.geometry.Rectangle;
+import org.eclipse.gef.LayerConstants;
+import org.eclipse.gef.editparts.ScalableFreeformRootEditPart;
+import org.eclipse.jface.action.Action;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.graphics.Device;
+import org.eclipse.swt.graphics.GC;
+import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.graphics.ImageData;
+import org.eclipse.swt.graphics.ImageLoader;
+import org.eclipse.swt.widgets.FileDialog;
+import org.jboss.tools.hibernate.ui.veditor.editors.VisualEditor;
+
+public class ExportImageAction extends Action {
+
+ public static final String ACTION_ID = "Export as Image";
+
+ private VisualEditor editor;
+
+ public ExportImageAction(VisualEditor editor) {
+ this.editor = editor;
+ setId(ACTION_ID);
+ setText(ACTION_ID);
+ setImageDescriptor(ImageDescriptor.createFromFile(
+ VisualEditor.class,"icons/export.png"));
+ }
+
+ public void run() {
+
+ FileDialog saveDialog = new FileDialog(
+ this.editor.getSite().getShell(), SWT.SAVE);
+ saveDialog
+ .setFilterExtensions(new String[] { "*.png", "*.jpg",
"*.bmp" });
+ saveDialog.setFilterNames(new String[] { "PNG format (*.png)",
+ "JPEG format (*.jpg)", "Bitmap format (*.bmp)" });
+
+ String filePath = saveDialog.open();
+ if (filePath == null || filePath.trim().length() == 0) {
+ return;
+ }
+
+ IFigure fig = ((ScalableFreeformRootEditPart) this.editor
+ .getEditPartViewer().getRootEditPart())
+ .getLayer(LayerConstants.PRINTABLE_LAYERS);
+ try {
+ int imageType = SWT.IMAGE_BMP;
+ if (filePath.toLowerCase().endsWith(".jpg")) {
+ imageType = SWT.IMAGE_JPEG;
+ } else if (filePath.toLowerCase().endsWith(".png")) {
+ imageType = SWT.IMAGE_PNG;
+ }
+
+ byte[] imageData = createImage(fig, imageType);
+ FileOutputStream outStream = new FileOutputStream(filePath);
+ outStream.write(imageData);
+ outStream.flush();
+ outStream.close();
+ } catch (Throwable e) {
+ MessageDialog.openInformation(this.editor.getSite().getShell(),
+ "Error", "Failed to export image: " + e.getMessage());
+ return;
+ }
+ }
+
+ /***
+ * Returns the bytes of an encoded image for the specified
+ * IFigure in the specified format.
+ *
+ * @param figure the Figure to create an image for.
+ * @param format one of SWT.IMAGE_BMP, SWT.IMAGE_BMP_RLE, SWT.IMAGE_GIF
+ * SWT.IMAGE_ICO, SWT.IMAGE_JPEG, or SWT.IMAGE_PNG
+ * @return the bytes of an encoded image for the specified Figure
+ */
+ private byte[] createImage(IFigure figure, int format) throws Exception {
+
+ Device device = this.editor.getEditPartViewer().getControl()
+ .getDisplay();
+ Rectangle r = figure.getBounds();
+
+ ByteArrayOutputStream result = new ByteArrayOutputStream();
+
+ Image image = null;
+ GC gc = null;
+ Graphics g = null;
+ Exception error = null;
+ try {
+ image = new Image(device, r.width, r.height);
+ gc = new GC(image);
+ g = new SWTGraphics(gc);
+ g.translate(r.x * -1, r.y * -1);
+
+ figure.paint(g);
+
+ ImageLoader imageLoader = new ImageLoader();
+ imageLoader.data = new ImageData[] { image.getImageData() };
+ imageLoader.save(result, format);
+ } catch (Exception ex) {
+ error = ex;
+ } finally {
+ if (g != null) {
+ g.dispose();
+ }
+ if (gc != null) {
+ gc.dispose();
+ }
+ if (image != null) {
+ image.dispose();
+ }
+ }
+ if (error != null) {
+ throw error;
+ }
+ return result.toByteArray();
+ }
+
+}
Added:
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/icons/export.png
===================================================================
(Binary files differ)
Property changes on:
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/icons/export.png
___________________________________________________________________
Name: svn:mime-type
+ application/octet-stream
Modified:
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/popup/PopupMenuProvider.java
===================================================================
---
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/popup/PopupMenuProvider.java 2007-12-13
15:45:38 UTC (rev 5284)
+++
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/popup/PopupMenuProvider.java 2007-12-13
15:46:03 UTC (rev 5285)
@@ -13,6 +13,7 @@
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.ui.actions.ActionFactory;
+import org.jboss.tools.hibernate.ui.veditor.editors.actions.ExportImageAction;
import org.jboss.tools.hibernate.ui.veditor.editors.actions.OpenMappingAction;
import org.jboss.tools.hibernate.ui.veditor.editors.actions.OpenSourceAction;
@@ -38,6 +39,10 @@
appendToGroup(GEFActionConstants.MB_ADDITIONS, action);
createMenuItem(getMenu(), action);
+ action = getActionRegistry().getAction(ExportImageAction.ACTION_ID);
+ appendToGroup(GEFActionConstants.MB_ADDITIONS, action);
+ createMenuItem(getMenu(), action);
+
// Add actions to the menu
menu.appendToGroup(
GEFActionConstants.GROUP_UNDO, // target group id