How to determine size of parent figure based on a child label??? [message #216522] |
Fri, 19 May 2006 09:33  |
Eclipse User |
|
|
|
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 12:11  |
Eclipse User |
|
|
|
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);
}
}
|
|
|
Powered by
FUDForum. Page generated in 0.03804 seconds