Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » GEF » How to determine size of parent figure based on a child label???
How to determine size of parent figure based on a child label??? [message #216522] Fri, 19 May 2006 13:33 Go to next message
Mark Robinson is currently offline Mark RobinsonFriend
Messages: 37
Registered: July 2009
Member
I'm new to draw2d and struggling ...

I'm trying to create a figure that will contain 2 children - a
parallelogram in the background, plus a label that will be centered inside
the parallelogram.

The size of the parallelogram is determined by the size of the label,
hence you need to know the bounds of the label before you can draw the
parallelogram.

I'm having a lot of difficulty figuring out how to do this as the bounds
of the label are not known until the figure is painted. I came up with a
solution that involved putting each figure (label, parallelogram) onto a
seperate layer, and revalidating the label to get its bounds. I'm pretty
sure there must be an easier way of doing this as this seems too
complicated?

It maybe I need to use a different layout (I'm using XYLayout at the
moment), such as delegating or stacklayout - I cannot find any useful
information on how these layouts work or how to use them.

Can somebody enlighten me please?
Re: How to determine size of parent figure based on a child label??? [message #216735 is a reply to message #216522] Wed, 24 May 2006 03:29 Go to previous messageGo to next message
Steven R. Shaw is currently offline Steven R. ShawFriend
Messages: 128
Registered: July 2009
Senior Member
This is a common use-case in GEF which is what the validation phase for
figures is for. This phase will invoke the layout managers on Figure
containers to determine the preferred size and location of the children
based on assigned constraints.

You would need to override the getPreferredSize or calculatePreferredSize of
the layout manager you install on the container to union the preferred sizes
of the children to get it's size. ToolbarLayout does something similar to
this in it's calculatePreferredSize method. Also, XYLayout will assume
auto-size behavior if the hints passed to calculatePreferredSize are -1, -1.

-Steve



"Mark Robinson" <mr@prismtech.com> wrote in message
news:51592213bf4511a2e88ed1bdbbdc15a9$1@www.eclipse.org...
> I'm new to draw2d and struggling ...
>
> I'm trying to create a figure that will contain 2 children - a
> parallelogram in the background, plus a label that will be centered inside
> the parallelogram.
>
> The size of the parallelogram is determined by the size of the label,
> hence you need to know the bounds of the label before you can draw the
> parallelogram.
>
> I'm having a lot of difficulty figuring out how to do this as the bounds
> of the label are not known until the figure is painted. I came up with a
> solution that involved putting each figure (label, parallelogram) onto a
> seperate layer, and revalidating the label to get its bounds. I'm pretty
> sure there must be an easier way of doing this as this seems too
> complicated?
>
> It maybe I need to use a different layout (I'm using XYLayout at the
> moment), such as delegating or stacklayout - I cannot find any useful
> information on how these layouts work or how to use them.
>
> Can somebody enlighten me please?
>
>
Re: How to determine size of parent figure based on a child label??? [message #217200 is a reply to message #216735] Thu, 01 June 2006 16:11 Go to previous message
Mark Robinson is currently offline Mark RobinsonFriend
Messages: 37
Registered: July 2009
Member
Steven Shaw wrote:

> This is a common use-case in GEF which is what the validation phase for
> figures is for. This phase will invoke the layout managers on Figure
> containers to determine the preferred size and location of the children
> based on assigned constraints.

> You would need to override the getPreferredSize or calculatePreferredSize of
> the layout manager you install on the container to union the preferred sizes
> of the children to get it's size. ToolbarLayout does something similar to
> this in it's calculatePreferredSize method. Also, XYLayout will assume
> auto-size behavior if the hints passed to calculatePreferredSize are -1, -1.

> -Steve



> "Mark Robinson" <mr@prismtech.com> wrote in message
> news:51592213bf4511a2e88ed1bdbbdc15a9$1@www.eclipse.org...
>> I'm new to draw2d and struggling ...
>>
>> I'm trying to create a figure that will contain 2 children - a
>> parallelogram in the background, plus a label that will be centered inside
>> the parallelogram.
>>
>> The size of the parallelogram is determined by the size of the label,
>> hence you need to know the bounds of the label before you can draw the
>> parallelogram.
>>
>> I'm having a lot of difficulty figuring out how to do this as the bounds
>> of the label are not known until the figure is painted. I came up with a
>> solution that involved putting each figure (label, parallelogram) onto a
>> seperate layer, and revalidating the label to get its bounds. I'm pretty
>> sure there must be an easier way of doing this as this seems too
>> complicated?
>>
>> It maybe I need to use a different layout (I'm using XYLayout at the
>> moment), such as delegating or stacklayout - I cannot find any useful
>> information on how these layouts work or how to use them.
>>
>> Can somebody enlighten me please?
>>
>>


Thanks for the info Stephen.

I actually used a different method. I don't know if this is correct but it
seems to work okay:

public class DCPS_imageFigure extends Figure
{
Label m_label;
Image m_img;
ImageFigure m_imgFig;

public DCPS_imageFigure (Image img, Label label)
{
super();
m_img = img;
m_imgFig = new ImageFigure (m_img);
m_label = label;

setLayoutManager (new XYLayout ());

org.eclipse.draw2d.geometry.Rectangle constraint = new
org.eclipse.draw2d.geometry.Rectangle (100,100,-1,-1);
add (m_imgFig, constraint);
add (m_label, constraint);
}

public void paintFigure(Graphics g)
{
//***************************************************
// Center the image around the label ...
//***************************************************

List children = getChildren();
Figure label = (Figure) children.get(1); // The label
Dimension size = label.getSize(); // Size of label
Point centerPt = label.getBounds().getCenter(); // Center point of
label

int scale = size.width + 40;
Image image = new Image (null, m_img.getImageData ().scaledTo
(scale, scale));

org.eclipse.swt.graphics.Rectangle bounds = image.getBounds();

int x, y;
x = centerPt.x - (bounds.width / 2);
y = centerPt.y - (bounds.height / 2);

m_imgFig.setLocation(new Point (x, y));
m_imgFig.setImage(image);
m_imgFig.setSize(bounds.width, bounds.height);
}
}
Previous Topic:TreeContainerEditPolicy commands are executed but do not appear in undo/redo stack?
Next Topic:text(-labels) musn't scale - possible?
Goto Forum:
  


Current Time: Fri Jan 17 09:16:15 GMT 2025

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

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

Back to the top