Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Parent's size(When I can know the size of a prent of a Composite?)
Parent's size [message #497107] Thu, 12 November 2009 09:21 Go to next message
David Crecente is currently offline David Crecente
Messages: 18
Registered: July 2009
Junior Member
Hi all!,
I would like to know if the scenario I describe below is possible.

I would like to paint some labels in a Composite but its size and position need to be calculated depending on the size of this Composite (its parent).

is It possible?
When I can trust in the computeSize of a parent of a compoent?
can I make these calculations before the screen is shown?
could I call to the computeSize of a Composite from its child?

Thanks in advance.
Re: Parent's size [message #497678 is a reply to message #497107] Fri, 13 November 2009 05:16 Go to previous messageGo to next message
Viliam Durina is currently offline Viliam Durina
Messages: 13
Registered: July 2009
Junior Member
You probably need to set layout for the composite. It sets sizes and positions of the children. If there is no layout, the children must be sized and positioned manually (using setSize() and setPosition() methods). After children are laid out, the compoteSize of the composite will return the minimal size needed to accomodate all of the children.

But the verbalization of your question suggests that you have long way to go to understand SWT. I recommend you to look at some of the snippets here: http://eclipse.org/swt/snippets/

Viliam

David Crecente wrote / napĂ­sal(a):
> Hi all!,
> I would like to know if the scenario I describe below is possible.
>
> I would like to paint some labels in a Composite but its size and
> position need to be calculated depending on the size of this Composite
> (its parent).
>
> is It possible?
> When I can trust in the computeSize of a parent of a compoent?
> can I make these calculations before the screen is shown?
> could I call to the computeSize of a Composite from its child?
>
> Thanks in advance.
Re: Parent's size [message #498818 is a reply to message #497678] Wed, 18 November 2009 12:48 Go to previous messageGo to next message
David Crecente is currently offline David Crecente
Messages: 18
Registered: July 2009
Junior Member
Hi and thank you for your answer.

I've seen serveral of the Snippets in the url you gave me but i don't find the answer for my simple question.

I need to paint several events in a bar. I am using labels for it. This information has a size depending on the time of starting, ending and the available space in its container.

So, if the container has a size of 800 pixels and an event had a duration of 1 hour then, after several calculations, we know that the label for this event should have 150 pixels.

But if the container has a size of 1200 pixels the same event needs to be painted with a size 66% bigger.

Similar calculations must be done for the position of the labels in that bar. These depends on the time.

But before painting anything I need to know how big is the parent in which bars and events are going to be painted.

is It possible?

Currently I have several bars working, and the events are being painting right but I don't know exactly why it's working.
Even when I resize the window all bars are painted again and calculations are working.

About the layout I use always MigLayout in each Composite.

Re: Parent's size [message #499042 is a reply to message #498818] Thu, 19 November 2009 10:58 Go to previous messageGo to next message
Grant Gayed is currently offline Grant Gayed
Messages: 2148
Registered: July 2009
Senior Member
The easiest way to do this is to listen to Resize events on the parent and
adjust your size accordingly. I don't know about MigLayout, but it can
probably be used in a similar manner to the snippet below. (In some ways
FormLayout lends itself to this case better than GridLayout, but GridLayout
seems adequate and is much more straightforward).

public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setBounds(200,200,200,200);
shell.setLayout(new GridLayout());
final Label label1 = new Label(shell, SWT.BORDER);
label1.setText("I fill 50%");
label1.setLayoutData(new GridData());
final Label label2 = new Label(shell, SWT.BORDER);
label2.setText("I fill 25%");
label2.setLayoutData(new GridData());
shell.addListener(SWT.Resize, new Listener() {
public void handleEvent(Event event) {
Rectangle rect = shell.getClientArea();
((GridData)label1.getLayoutData()).widthHint = rect.width / 2;
((GridData)label2.getLayoutData()).widthHint = rect.width / 4;
shell.layout();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
shell.dispose();
display.dispose();
}

Grant


"David Crecente" <david.crecente@gmail.com> wrote in message
news:he1c10$f7r$1@build.eclipse.org...
> Hi and thank you for your answer.
>
> I've seen serveral of the Snippets in the url you gave me but i don't
find the answer for my simple question.
>
> I need to paint several events in a bar. I am using labels for it. This
information has a size depending on the time of starting, ending and the
available space in its container.
>
> So, if the container has a size of 800 pixels and an event had a
duration of 1 hour then, after several calculations, we know that the label
for this event should have 150 pixels.
>
> But if the container has a size of 1200 pixels the same event needs to
be painted with a size 66% bigger.
>
> Similar calculations must be done for the position of the labels in
that bar. These depends on the time.
>
> But before painting anything I need to know how big is the parent in
which bars and events are going to be painted.
>
> is It possible?
>
> Currently I have several bars working, and the events are being
painting right but I don't know exactly why it's working.
> Even when I resize the window all bars are painted again and
calculations are working.
>
> About the layout I use always MigLayout in each Composite.
>
>
Re: Parent's size [message #499201 is a reply to message #499042] Fri, 20 November 2009 06:52 Go to previous messageGo to next message
David Crecente is currently offline David Crecente
Messages: 18
Registered: July 2009
Junior Member
Hi,
thank you for your example. But I think GridLayout is not enoght flexible for this case.

I've written an small Snippet using MigLayout, and caught several images of this code.

In this figure you can see two events in the wrong position and wrong sizes.
http://farm3.static.flickr.com/2672/4119707624_6f96596e89.jpg

If you move the window the position and sizes for the events are corrected.
http://farm3.static.flickr.com/2752/4119707634_595d5a6177.jpg

You can make smaller the window and every thing work right. You can see that the events are smaller because they have less space to be represented. And their relative position has also changed.
http://farm3.static.flickr.com/2665/4119707638_8bf4478d89.jpg

In my real application I have serveral of these bars and I had to set over my composite an absolute size (and I don't like it).

What I really need is something like this one:

http://akweebeta.com/mediawiki-1.12.0/images/9/99/Stacked_Bar_Chart_Example.jpg

Where I can calcule each event depending of the time.

Any feedback will be appreciated.

Any other point of view to do this task will also be appreciated.

The Snippet:


package misejemplos;

import net.miginfocom.swt.MigLayout;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

public class AvailableSpaceTest {
// I need this references.
private static Display display;
private static Shell shell;
private static Composite evolutionBar;

public static void main(String[] args) {
display = new Display();
shell = new Shell(display);
shell.setLayout(new MigLayout("wrap 1, fillx"));
shell.setLayoutData("pushx, growx");

// A parent composite which never will be disposed.
Composite parentContainer = new Composite(shell, SWT.BORDER);
parentContainer.setLayout(new MigLayout("wrap 1, fillx"));
parentContainer.setLayoutData("pushx, growx");
parentContainer.pack(true);

// A container which will be dispose when it's required to calculate
// position,
// widths, heights, positions.
// We can understand this container like the evolution bar.
// It never will be disposed.
evolutionBar = new Composite(parentContainer, SWT.BORDER);
evolutionBar.setLayout(new MigLayout("wrap 1, fillx"));
evolutionBar.setLayoutData("pushx, growx, h 20");
evolutionBar.setBackground(new Color(display, 45, 32, 133));
evolutionBar.pack(true); // requered in order to get the size later.

// If the window is resized we need to repeat all calculates.
shell.addListener(SWT.Resize, new Listener() {
public void handleEvent(Event event) {
Composite parent = evolutionBar.getParent();
evolutionBar.dispose();
evolutionBar = new Composite(parent, SWT.BORDER);
evolutionBar.setBackground(new Color(display, 45, 32, 133));
evolutionBar.setLayout(new MigLayout("fillx"));
evolutionBar.setLayoutData("pushx, growx");
evolutionBar.pack(true); // requered in order to get the size
// later.
paint();
}
});

// The method which paints the progress bar
paint();

shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
shell.dispose();
display.dispose();
}

/**
* In my real scenario I get new events from a queue and I have a refresh
* each 4 seconds. Here I will only have resize.
*/
public static void paint() {
// The first event is painted in its evolution bar.
final Label label1 = new Label(evolutionBar, SWT.BORDER);
label1.setText("event 1");
int childWidth = findOutParentWidth(evolutionBar);

// We translate the time this event took to the available space.
// This is only an example.
childWidth /= 4;
label1.setSize(childWidth, 20);
label1.setLayoutData("pos 0 0, w " + childWidth + ", h 20");
label1.setBackground(new Color(display, 123, 32, 56));

// The second event is painted in its evolution bar.
final Label label2 = new Label(evolutionBar, SWT.BORDER);
label2.setText("event 2");
childWidth = findOutParentWidth(evolutionBar);

// We translate the time this event took to the available space.
// This event took less time than the previous. This is only an example.
childWidth /= 6;
label2.setSize(childWidth, 20);

// We need to find out it position in the evolution bar. So,
// we translate the time in the available space.
int position = findOutParentWidth(evolutionBar) / 2;
label2.setLayoutData("pos " + position + " 0, w " + childWidth
+ ", h 20");
label2.setBackground(new Color(display, 32, 123, 56));

evolutionBar.pack(true);
evolutionBar.getParent().pack(true);
}

/**
* finds out the width.
*/
private static int findOutParentWidth(Composite child) {
return child.getParent().getSize().x;
}
}

Re: Parent's size [message #499531 is a reply to message #499201] Mon, 23 November 2009 04:47 Go to previous message
David Crecente is currently offline David Crecente
Messages: 18
Registered: July 2009
Junior Member
Hi again,
I forgot to say that I'm using MigLayout 3.7
http://www.miglayout.com/

Does someone know any other Layout which I could use to my task.

Thanks in advance.
Previous Topic:SWT app running as collection of plugins, not JARs?
Next Topic:On SLED11, linkActivated() is not called the first time when I clicked on the link
Goto Forum:
  


Current Time: Fri Apr 18 11:50:18 EDT 2014

Powered by FUDForum. Page generated in 0.14794 seconds