|
|
|
|
Re: [Draw2D] Remove figure from layout calculation [message #892441 is a reply to message #892428] |
Thu, 28 June 2012 08:45 |
Cedric Moonen Messages: 274 Registered: August 2009 |
Senior Member |
|
|
I finally found a working solution but it is a bit tricky. So here is what I did:
I still use a toolbar layout in which I have two figures: the "container" area on the center (BorderLayout.CENTER) and a title container figure on top (BorderLayout.TOP).
The title container is an empty figure (new Figure() ) which has a GridLayout as layout. The title label figure is a child of that figure with a GridData constraints with a small widthHint (grab excess space are both set to false).
Now the tricky part is to adjust the width hint when the column is resized by adding a layout listener on the top figure, and in the postLayout method, modify the width hint (if it has changed) of the figure and force a new layout for the label figure.
Here is the code if somebody finds it useful (this code is inside the constructor of the "column figure"):
public ColumnFigure(String columnName) {
setBorder(new LineBorder(ColorConstants.black, 1));
setBackgroundColor(FIGURE_BACKGROUND);
setOpaque(true);
setLayoutManager(new BorderLayout());
final Figure titleContainer = new Figure();
titleContainer.setBorder(new LineBorder());
final GridLayout gl = new GridLayout(1, true);
gl.marginHeight = gl.marginWidth = 0;
titleContainer.setLayoutManager(gl);
add(titleContainer, BorderLayout.TOP);
Font titleFont = new Font(null, "Arial", 8, SWT.BOLD);
title = new Label(columnName);
title.setFont(titleFont);
final GridData gd = new GridData(GridData.CENTER, GridData.CENTER,
false, false);
gd.widthHint = 50;
titleContainer.add(title, gd);
// The container area (code is simplified)
Figure containerArea = new Figure();
add(containerArea, BorderLayout.CENTER);
// This is needed in order to make sure that the label takes up to the
// maximum size allowed (otherwise it will be cropped to the width
// hint).
this.addLayoutListener(new LayoutListener() {
@Override
public void postLayout(IFigure container) {
int newWidth = getBounds().width;
// The -4 is to take into account the border. If we don't
// specify the -4, every time the figure is layout, the width
// increases by 4 pixels. This means that if we continuously
// click on an event, the column width will increase
// continuously.
if (gd.widthHint != newWidth - 4) {
gd.widthHint = newWidth - 4;
layout(titleContainer);
}
}
@Override
public void invalidate(IFigure container) {
}
@Override
public boolean layout(IFigure container) {
return false;
}
@Override
public void remove(IFigure child) {
}
@Override
public void setConstraint(IFigure child, Object constraint) {
}
});
}
Thanks for the help Jan !
|
|
|
|
Powered by
FUDForum. Page generated in 0.03215 seconds