Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » GEF » Problem saving Draw2d diagram to image from within plugin (works standalone)
Problem saving Draw2d diagram to image from within plugin (works standalone) [message #244297] Tue, 15 July 2008 08:00 Go to next message
Vikram Sjn is currently offline Vikram SjnFriend
Messages: 39
Registered: July 2009
Member
I have a draw2d only (no gef involved) diagram, to be saved as image
without presenting the diagram.

In standalone SWT application I am able to save diagram as image - but
this doesn't work when done from within plugin.

What could be going wrong? Please help.


Code when run from SWT application:

public class UMLClassFigureTest {
public static void main(String args[]) {
final Display display = new Display();
Shell shell = new Shell(display);
shell.setText("UML Class Diagram");
shell.setBounds(10, 10, 200, 200);

Button button = new Button(shell, SWT.PUSH);
button.setText("Play");
button.setBounds(10, 140, 50, 20);

final TheDiagram diagram = new TheDiagram();
diagram.initialize();

button.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
doPlay(display, diagram);
}
});

shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();

}

private static void doPlay(final Display display,
final TheDiagram internalDemo2) {

Image img1 = new Image(null, new org.eclipse.swt.graphics.Rectangle(10,
102, 600, 600));
GC gc1 = new GC(img1);

Graphics grap1 = new SWTGraphics(gc1);
internalDemo2.contents.paint(grap1);

ImageLoader loader1 = new ImageLoader();
loader1.data = new ImageData[] { img1.getImageData() };
loader1.save("draw2d.jpeg", SWT.IMAGE_JPEG);
img1.dispose();
gc1.dispose();

}
}

Code when run from plugin:

Shell shell =
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShel l();
final TheDiagram diagram = new TheDiagram();
diagram.initialize(shell);

Image img1 = new Image(null, new org.eclipse.swt.graphics.Rectangle(10,
102, 600, 600));
GC gc1 = new GC(img1);
Graphics grap1 = new SWTGraphics(gc1);
diagram.contents.paint(grap1);
ImageLoader loader1 = new ImageLoader();
loader1.data = new ImageData[] {img1.getImageData()};
loader1.save("e:\\draw2d.jpeg", SWT.IMAGE_JPEG);
img1.dispose();
gc1.dispose();
Re: Problem saving Draw2d diagram to image from within plugin (works standalone) [message #244304 is a reply to message #244297] Tue, 15 July 2008 08:03 Go to previous messageGo to next message
Vikram Sjn is currently offline Vikram SjnFriend
Messages: 39
Registered: July 2009
Member
Here is the entire SWT application for reference (where saving to image
works):

/*
* Created on Jul 9, 2008
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.vikramsjn.swtapp.componenttoimage;

import org.eclipse.draw2d.AbstractBorder;
import org.eclipse.draw2d.ChopboxAnchor;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.ConnectionEndpointLocator;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LightweightSystem;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.PolygonDecoration;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.draw2d.SWTGraphics;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.PointList;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
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.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

/**
* A test class to display a UMLFigure
*/
public class UMLClassFigureTest {
public static void main(String args[]) {
final Display display = new Display();
Shell shell = new Shell(display);
shell.setText("UML Class Diagram");
shell.setBounds(10, 10, 200, 200);

Button button = new Button(shell, SWT.PUSH);
button.setText("Play");
button.setBounds(10, 140, 50, 20);

final TheDiagram diagram = new TheDiagram();
diagram.initialize();

button.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
doPlay(display, diagram);
}
});

shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();

}

private static void doPlay(final Display display,
final TheDiagram internalDemo2) {

Image img1 = new Image(null, new org.eclipse.swt.graphics.Rectangle(10,
102, 600, 600));
GC gc1 = new GC(img1);

Graphics grap1 = new SWTGraphics(gc1);
internalDemo2.contents.paint(grap1);

ImageLoader loader1 = new ImageLoader();
loader1.data = new ImageData[] { img1.getImageData() };
loader1.save("draw2d.jpeg", SWT.IMAGE_JPEG);
img1.dispose();
gc1.dispose();

}
}

class CompartmentFigure extends Figure {

public CompartmentFigure() {
ToolbarLayout layout = new ToolbarLayout();
layout.setMinorAlignment(ToolbarLayout.ALIGN_TOPLEFT);
layout.setStretchMinorAxis(false);
layout.setSpacing(2);
setLayoutManager(layout);
setBorder(new CompartmentFigureBorder());
}
}

class CompartmentFigureBorder extends AbstractBorder {
public Insets getInsets(IFigure figure) {
return new Insets(1, 0, 0, 0);
}

public void paint(IFigure figure, Graphics graphics, Insets insets) {
graphics.drawLine(getPaintRectangle(figure, insets).getTopLeft(),
tempRect.getTopRight());
}
}

class UMLClassFigure extends Figure {
public static Color classColor = new Color(null, 255, 255, 206);

private CompartmentFigure attributeFigure = new CompartmentFigure();

private CompartmentFigure methodFigure = new CompartmentFigure();

public UMLClassFigure(Label name) {
ToolbarLayout layout = new ToolbarLayout();
setLayoutManager(layout);
setBorder(new LineBorder(ColorConstants.black, 1));
setBackgroundColor(classColor);
setOpaque(true);

add(name);
add(attributeFigure);
add(methodFigure);
}

public CompartmentFigure getAttributesCompartment() {
return attributeFigure;
}

public CompartmentFigure getMethodsCompartment() {
return methodFigure;
}
}

class TheDiagram {
public Shell shell;

public Figure contents;

LightweightSystem lws;

public void initialize() {

shell = new Shell(Display.getDefault());
lws = new LightweightSystem(shell);
contents = new Figure();
XYLayout contentsLayout = new XYLayout();
contents.setLayoutManager(contentsLayout);
lws.setContents(contents);

Font classFont = new Font(null, "Arial", 12, SWT.BOLD);
Label classLabel1 = new Label("Table");
classLabel1.setFont(classFont);

Label classLabel2 = new Label("Column");
classLabel2.setFont(classFont);

final UMLClassFigure classFigure = new UMLClassFigure(classLabel1);
final UMLClassFigure classFigure2 = new UMLClassFigure(classLabel2);

Label attribute1 = new Label("columns: Column[]");
Label attribute2 = new Label("rows: Row[]");
Label attribute3 = new Label("columnID: int");
Label attribute4 = new Label("items: List");

classFigure.getAttributesCompartment().add(attribute1);
classFigure.getAttributesCompartment().add(attribute2);
classFigure2.getAttributesCompartment().add(attribute3);
classFigure2.getAttributesCompartment().add(attribute4);

Label method1 = new Label("getColumns(): Column[]");
Label method2 = new Label("getRows(): Row[]");
Label method3 = new Label("getColumnID(): int");
Label method4 = new Label("getItems(): List");

classFigure.getMethodsCompartment().add(method1);
classFigure.getMethodsCompartment().add(method2);
classFigure2.getMethodsCompartment().add(method3);
classFigure2.getMethodsCompartment().add(method4);

contentsLayout
.setConstraint(classFigure, new Rectangle(10, 10, -1, -1));
contentsLayout.setConstraint(classFigure2, new Rectangle(200, 200, -1,
-1));

PolylineConnection c = new PolylineConnection();
ChopboxAnchor sourceAnchor = new ChopboxAnchor(classFigure);
ChopboxAnchor targetAnchor = new ChopboxAnchor(classFigure2);
c.setSourceAnchor(sourceAnchor);
c.setTargetAnchor(targetAnchor);

PolygonDecoration decoration = new PolygonDecoration();
PointList decorationPointList = new PointList();
decorationPointList.addPoint(0, 0);
decorationPointList.addPoint(-2, 2);
decorationPointList.addPoint(-4, 0);
decorationPointList.addPoint(-2, -2);
decoration.setTemplate(decorationPointList);
c.setSourceDecoration(decoration);

ConnectionEndpointLocator targetEndpointLocator = new
ConnectionEndpointLocator(
c, true);
targetEndpointLocator.setVDistance(15);
Label targetMultiplicityLabel = new Label("1..*");
c.add(targetMultiplicityLabel, targetEndpointLocator);

ConnectionEndpointLocator sourceEndpointLocator = new
ConnectionEndpointLocator(
c, false);
sourceEndpointLocator.setVDistance(15);
Label sourceMultiplicityLabel = new Label("1");
c.add(sourceMultiplicityLabel, sourceEndpointLocator);

ConnectionEndpointLocator relationshipLocator = new
ConnectionEndpointLocator(
c, true);
relationshipLocator.setUDistance(10);
relationshipLocator.setVDistance(-20);
Label relationshipLabel = new Label("contains");
c.add(relationshipLabel, relationshipLocator);

contents.add(classFigure);
contents.add(classFigure2);
contents.add(c);
}
}
Re: Problem saving Draw2d diagram to image from within plugin (works standalone) [message #244314 is a reply to message #244297] Tue, 15 July 2008 12:50 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: lifesting.gmail.com

any exception message? did you debug this code to check all variable
contained correct info as you expected?

Vikram wrote:
> I have a draw2d only (no gef involved) diagram, to be saved as image
> without presenting the diagram.
>
> In standalone SWT application I am able to save diagram as image - but
> this doesn't work when done from within plugin.
>
> What could be going wrong? Please help.
>
>
> Code when run from SWT application:
>
> public class UMLClassFigureTest {
> public static void main(String args[]) {
> final Display display = new Display();
> Shell shell = new Shell(display);
> shell.setText("UML Class Diagram");
> shell.setBounds(10, 10, 200, 200);
>
> Button button = new Button(shell, SWT.PUSH);
> button.setText("Play");
> button.setBounds(10, 140, 50, 20);
>
> final TheDiagram diagram = new TheDiagram();
> diagram.initialize();
>
> button.addListener(SWT.Selection, new Listener() {
> public void handleEvent(Event event) {
> doPlay(display, diagram);
> }
> });
>
> shell.open();
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch())
> display.sleep();
> }
> display.dispose();
>
> }
>
> private static void doPlay(final Display display,
> final TheDiagram internalDemo2) {
>
> Image img1 = new Image(null, new
> org.eclipse.swt.graphics.Rectangle(10,
> 102, 600, 600));
> GC gc1 = new GC(img1);
>
> Graphics grap1 = new SWTGraphics(gc1);
> internalDemo2.contents.paint(grap1);
>
> ImageLoader loader1 = new ImageLoader();
> loader1.data = new ImageData[] { img1.getImageData() };
> loader1.save("draw2d.jpeg", SWT.IMAGE_JPEG);
> img1.dispose();
> gc1.dispose();
>
> }
> }
>
> Code when run from plugin:
>
> Shell shell =
> PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShel l();
> final TheDiagram diagram = new TheDiagram();
> diagram.initialize(shell);
>
> Image img1 = new Image(null, new
> org.eclipse.swt.graphics.Rectangle(10, 102, 600, 600));
> GC gc1 = new GC(img1);
> Graphics grap1 = new SWTGraphics(gc1);
> diagram.contents.paint(grap1);
> ImageLoader loader1 = new ImageLoader();
> loader1.data = new ImageData[] {img1.getImageData()};
> loader1.save("e:\\draw2d.jpeg", SWT.IMAGE_JPEG);
> img1.dispose();
> gc1.dispose();
>
>
Re: Problem saving Draw2d diagram to image from within plugin (works standalone) [message #244396 is a reply to message #244314] Thu, 17 July 2008 07:45 Go to previous message
Vikram Sjn is currently offline Vikram SjnFriend
Messages: 39
Registered: July 2009
Member
There is no exception.

On inspecting 'diagram.contents' figure's has the children bounds all set
to zeros - except for the connection, which is still incorrect, {0, 0,
101, 101}
Previous Topic:Help! Reparent view
Next Topic:Centered FlowLayout stopped working?
Goto Forum:
  


Current Time: Tue Mar 19 05:08:30 GMT 2024

Powered by FUDForum. Page generated in 0.01431 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top