Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » GEF » draw2d Label resize(resize labels to make histogram like bars)
draw2d Label resize [message #1021037] Tue, 19 March 2013 12:12 Go to next message
Mokhtar Abdu is currently offline Mokhtar Abdu
Messages: 104
Registered: November 2011
Senior Member
Hi,

I am trying to resize an org.eclipse.draw2d.Label inside my customized graph node but I couldn't. Could you help me with that please?

I used setSize() or setPreferredSize() after the label got an image, font and text. But didn't work. The goal is to make labels resize to constitute a chart diagram inside the nodes!!
Label histogramLabel = new Label();
histogramLabel.setIcon(image);
histogramLabel.setBackgroundColor(color);
histogramLabel.setOpaque(true);
histogramLabel.setText(Integer.toString(someNum));
histogramLabel.setFont(GraphicsUtils.nodeFont(7));
		
figPart.add(histogramLabel);


The figPart above is an instance of this class
public class CompartmentFigure extends Figure {

	  public CompartmentFigure(boolean border,ToolbarLayout layout) {
	    
	    layout.setHorizontal(true);
	    layout.setStretchMinorAxis(false);
	    layout.setSpacing(0);
	    setLayoutManager(layout);
	    
	    if (border) setBorder(new CompartmentFigureBorder());
	  }
	    
	  public 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());
	    }
	  }
	}

If the label could be resized by the layout of the containing figure, then please tell me how? I would also be not angary if some one introduced me a better way to do a histogram (chart diagram) inside a graph node.

Thank you,
Mok
Re: draw2d Label resize [message #1022093 is a reply to message #1021037] Thu, 21 March 2013 09:11 Go to previous messageGo to next message
Jan Krakora is currently offline Jan Krakora
Messages: 477
Registered: December 2009
Location: Prague
Senior Member
Hi,

the ToolbarLayout you use is responsible for setting size to the histogramLabel.
It depends on its internal algorithm if and how it uses the histogramLabel's preferred size.

What are you trying to achieve?
Re: draw2d Label resize [message #1022432 is a reply to message #1022093] Thu, 21 March 2013 21:32 Go to previous messageGo to next message
Mokhtar Abdu is currently offline Mokhtar Abdu
Messages: 104
Registered: November 2011
Senior Member
Hi, I am trying to resize the label to reflect the number it holds.
Domain-speaking, I have some dataset that I visualize in terms of a matrix of nodes. Every node has inner elements and I want to depict the node as a square graph node and its elements as histogram (bar chart). All is done sofar except stretching these freaky labels to look like a histogram. Any help is appreciated Smile

Thank you.
Mok
Re: draw2d Label resize [message #1022638 is a reply to message #1022432] Fri, 22 March 2013 09:21 Go to previous messageGo to next message
Jan Krakora is currently offline Jan Krakora
Messages: 477
Registered: December 2009
Location: Prague
Senior Member
I can't imagine it. Any image would be helpful.

Setting a text on the LabelFigure should cause to revalidate all figures up in the tree
and layout the LabelFigure to show its whole text accordingly unless you limit a parent with some kind of layout hints.
Re: draw2d Label resize [message #1022663 is a reply to message #1022638] Fri, 22 March 2013 09:59 Go to previous messageGo to next message
Mokhtar Abdu is currently offline Mokhtar Abdu
Messages: 104
Registered: November 2011
Senior Member
http://oi45.tinypic.com/sq1pbk.jpg
that is an image what I wish it to look like. Labels are resized according to their contents (numbers of elements)

snippets:
//label creation
Label histogramLabel = new Label();

histogramLabel.setIcon(image);
histogramLabel.setBackgroundColor(color);
histogramLabel.setOpaque(true);
histogramLabel.setText(Integer.toString(someNum));
histogramLabel.setFont(GraphicsUtils.nodeFont(7));
//HERE I WANT TO RESIZE THE LABEL ????!??
figPart.add(histogramLabel);


color is an org.eclipse.swt.graphics.Color. image is org.eclipse.swt.graphics.Image


Thank you for helping
Mok
Re: draw2d Label resize [message #1022672 is a reply to message #1022663] Fri, 22 March 2013 10:25 Go to previous messageGo to next message
Jan Krakora is currently offline Jan Krakora
Messages: 477
Registered: December 2009
Location: Prague
Senior Member
So the labels are not sized based on the text size, but on its contents meaning, right? 5 should be five times wider than 1?
Re: draw2d Label resize [message #1022675 is a reply to message #1022672] Fri, 22 March 2013 10:26 Go to previous messageGo to next message
Mokhtar Abdu is currently offline Mokhtar Abdu
Messages: 104
Registered: November 2011
Senior Member
yes
Re: draw2d Label resize [message #1022683 is a reply to message #1022672] Fri, 22 March 2013 10:54 Go to previous messageGo to next message
Mokhtar Abdu is currently offline Mokhtar Abdu
Messages: 104
Registered: November 2011
Senior Member
I got it Smile I will use space or special char to enforce the label size wider or narrower.
sth like
histogramLabel.setText("   "+ Integer.toString(count));


Thank you for help Smile
Mok
Re: draw2d Label resize [message #1022691 is a reply to message #1022683] Fri, 22 March 2013 11:12 Go to previous messageGo to next message
Jan Krakora is currently offline Jan Krakora
Messages: 477
Registered: December 2009
Location: Prague
Senior Member
I can see the Label figure clears its preferred size when invalidate. That's the reason why your setting are ignored.

You have a few ways to do what you want:
- You can modify the Label figure. I don't recommend that.
- You can use the grid layout. It can override child's preferred size.
- You can use your own layout. This is the most powerful and complex option.
I would recommend that when you would have more unusual layout requirements like this one.
See SWT layout. Draw2d layout is very similar.

There is a hint for you if you decide to the GridLayout:
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FigureCanvas;
import org.eclipse.draw2d.GridData;
import org.eclipse.draw2d.GridLayout;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.RectangleFigure;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;


public class XXX {
	
	public static void main(String[] args) {
		final int magicNumber = 10;
		
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());
		
		FigureCanvas canvas = new FigureCanvas(shell);
		
		IFigure contents = new Figure();
		contents.setOpaque(true);
		contents.setBackgroundColor(ColorConstants.white);
		contents.setLayoutManager(new XYLayout());
		canvas.setContents(contents);
		
		RectangleFigure container = new RectangleFigure();
		GridLayout layout = new GridLayout();
		container.setLayoutManager(layout);
		contents.add(container, new Rectangle(100, 100, -1, -1));
		
		Label histogramLabel = new Label("1");
		histogramLabel.setBackgroundColor(ColorConstants.lightBlue);
		histogramLabel.setOpaque(true);
		GridData layoutData = new GridData();
		layoutData.widthHint = magicNumber * 1;
		container.add(histogramLabel, layoutData);
		
		histogramLabel = new Label("5");
		histogramLabel.setBackgroundColor(ColorConstants.lightGray);
		histogramLabel.setOpaque(true);
		layoutData = new GridData();
		layoutData.widthHint = magicNumber * 5;
		container.add(histogramLabel, layoutData);
		
		histogramLabel = new Label("3");
		histogramLabel.setBackgroundColor(ColorConstants.lightGreen);
		histogramLabel.setOpaque(true);
		layoutData = new GridData();
		layoutData.widthHint = magicNumber * 3;
		container.add(histogramLabel, layoutData);
		
		shell.setSize(300, 300);
		shell.open();
		
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}
}

Re: draw2d Label resize [message #1022695 is a reply to message #1022691] Fri, 22 March 2013 11:19 Go to previous messageGo to next message
Mokhtar Abdu is currently offline Mokhtar Abdu
Messages: 104
Registered: November 2011
Senior Member
I really thank you Jan Krakora for this nice demo. I ran it and its nice Smile Just one thing. What is magicNumber?? I suppose it is the width of the containing rectangular.

Best Regards,
Mok from Bonn
Re: draw2d Label resize [message #1022729 is a reply to message #1022695] Fri, 22 March 2013 12:41 Go to previous message
Jan Krakora is currently offline Jan Krakora
Messages: 477
Registered: December 2009
Location: Prague
Senior Member
It's a constant value that converts values from labels to width pixels. You can convert label values to pixels any way you want.
Previous Topic:Movable Labels
Next Topic:disable nodes emerging
Goto Forum:
  


Current Time: Thu Oct 30 18:15:36 GMT 2014

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

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