Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » GEF » [Draw2D] Remove figure from layout calculation
[Draw2D] Remove figure from layout calculation [message #891870] Tue, 26 June 2012 11:42 Go to next message
Cedric Moonen is currently offline Cedric Moonen
Messages: 274
Registered: August 2009
Senior Member
Hello,

This is a question related to Draw2D, I'm not sure this is the correct location to ask this...

Anyway, I would like to draw a figure having two parts: a title area containing some text (a Label) and a column containing some other figures under this Label.

Now what I would like to have is that the width of my figure is only calculated based on the column width and not based on the label width. This means that if I have a very long label, it won't be fully visible (this is intentional).

So, basically I would like to specify to the layout manager (currently a toolbar layout) that the width of my Label should be ignored.

This is probably not possible this way but is there a way to achieve something similar ?

Thanks,
C├ędric
Re: [Draw2D] Remove figure from layout calculation [message #891931 is a reply to message #891870] Tue, 26 June 2012 15:51 Go to previous messageGo to next message
Jan Krakora is currently offline Jan Krakora
Messages: 477
Registered: December 2009
Location: Prague
Senior Member
Hi Cedric,

you could try to override the Label's getPreferredSize(int wHint, int hHint) to return something small
public Dimension getPreferredSize(int wHint, int hHint) {
	if (prefSize == null) {
		prefSize = new Dimension(10, super.getPreferredSize(wHint, hHint).height);
		Insets insets = getInsets();
		prefSize.expand(insets.getWidth(), insets.getHeight());
	}
	return prefSize;
}
Re: [Draw2D] Remove figure from layout calculation [message #892399 is a reply to message #891870] Thu, 28 June 2012 06:28 Go to previous messageGo to next message
Cedric Moonen is currently offline Cedric Moonen
Messages: 274
Registered: August 2009
Senior Member
Hello Jan,

Thanks for the suggestion but I already tried that. In that case, the width is indeed not taken into account to calculate the final width of the column but then my label is not positionned properly: since the layout thinks that its size is very small, it will position it in the middle of my column instead of centering the text.

I know this is a tricky problem since I try perhaps to go a bit beyong what Draw2D is capable of doing...
Re: [Draw2D] Remove figure from layout calculation [message #892428 is a reply to message #892399] Thu, 28 June 2012 08:13 Go to previous messageGo to next message
Jan Krakora is currently offline Jan Krakora
Messages: 477
Registered: December 2009
Location: Prague
Senior Member
You could then use another layout or try to modify the toolbar layout for your needs.
The grid layout could work here. Because AFAIK you can set the width hint and grab to true on the label's layout data.
Re: [Draw2D] Remove figure from layout calculation [message #892441 is a reply to message #892428] Thu, 28 June 2012 08:45 Go to previous messageGo to next message
Cedric Moonen is currently offline 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 !
Re: [Draw2D] Remove figure from layout calculation [message #892444 is a reply to message #892441] Thu, 28 June 2012 08:50 Go to previous message
Jan Krakora is currently offline Jan Krakora
Messages: 477
Registered: December 2009
Location: Prague
Senior Member
Nice:)
Previous Topic:curved connections
Next Topic:graphicalViewer.reveal(editPart) draws over other views
Goto Forum:
  


Current Time: Fri Oct 31 10:57:23 GMT 2014

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

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