Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » GMF (Graphical Modeling Framework) » Custom Layout in Compartment
Custom Layout in Compartment [message #233589] Sun, 14 June 2009 16:35 Go to next message
No real name is currently offline No real nameFriend
Messages: 3
Registered: July 2009
Junior Member
Hi all,

I am working on a diagram with nodes that use compartments to hold
subnodes recursively. A problem is the layout of these subnodes in the
compartments. I want to use a custom layout there. As mentioned in older
Q&A this should be done by overriding the "createFigure" in the
CompartmentEditPart like:

public IFigure createFigure() {
ResizableCompartmentFigure result =
(ResizableCompartmentFigure)super.createFigure();
result.getContentPane().setLayoutManager(new StackLayout());
return result;
}

This does not work for the current GMF version. If I use anything other
than FlowLayout the width of the compartments infinitely grows.

Is there an other way to do use a layout in a compartment?

Any help is very appreciated!

Cheers
Balthasar
Re: Custom Layout in Compartment [message #233753 is a reply to message #233589] Mon, 15 June 2009 16:06 Go to previous messageGo to next message
Alexander Shatalin is currently offline Alexander ShatalinFriend
Messages: 2928
Registered: July 2009
Senior Member
Hello Balthasar,

I've generated simple code for a diagram using latest GMF version, put following


> Hi all,
>
> I am working on a diagram with nodes that use compartments to hold
> subnodes recursively. A problem is the layout of these subnodes in the
> compartments. I want to use a custom layout there. As mentioned in
> older Q&A this should be done by overriding the "createFigure" in the
> CompartmentEditPart like:
>
> public IFigure createFigure() {
> ResizableCompartmentFigure result =
> (ResizableCompartmentFigure)super.createFigure();
> result.getContentPane().setLayoutManager(new StackLayout());
> return result;
> }
> This does not work for the current GMF version. If I use anything
> other than FlowLayout the width of the compartments infinitely grows.
>
> Is there an other way to do use a layout in a compartment?
>
> Any help is very appreciated!
>
> Cheers
> Balthasar
-----------------
Alex Shatalin
Re: Custom Layout in Compartment [message #233760 is a reply to message #233589] Mon, 15 June 2009 17:59 Go to previous messageGo to next message
Alexander Shatalin is currently offline Alexander ShatalinFriend
Messages: 2928
Registered: July 2009
Senior Member
Hello Balthasar,

I've generated simple code for a diagram using latest GMF version, put following
createFigure method into CompartmentEditPart:

public IFigure createFigure() {
ResizableCompartmentFigure result = (ResizableCompartmentFigure) super.createFigure();
result.getContentPane().setLayoutManager(new StackLayout());
return result;
}

and run diagram editor - I can see all the child elements ontop of each other
(StackLayout).

-----------------
Alex Shatalin
Re: Custom Layout in Compartment [message #233787 is a reply to message #233760] Tue, 16 June 2009 11:15 Go to previous messageGo to next message
No real name is currently offline No real nameFriend
Messages: 3
Registered: July 2009
Junior Member
Hi Alex,

thanks that you tried that out! I also made a protoype for that, but the
result is not like intended.
The diagram uses recursion in the gmfmap, childReference.referencedChild
points to my node again, so I can add more children in every compartment.

The figures are very simple: A main rectangle, a label for the name and a
rectangle for the compartment. I define the compartment then with accessor
to that rectangle.

In the generated code I replaced the createFigure in the compartmentEditPart
just like you wrote.

Without adding a LayoutEditPolicy I can not add any nodes in the
compartment, so I added a fake OrderedLayoutEditPolicy that returns empty
commands.

Now I can add the nodes in the compartments, but they get use the complete
space in the compartment, so only one node per compartment is visible.

It seems to be that the "calculatePreferredSize" of the StackLayout is never
called.

Could you explain me how you got your example working?

Cheers
Balthasar
Re: Custom Layout in Compartment [message #233803 is a reply to message #233787] Tue, 16 June 2009 13:24 Go to previous messageGo to next message
Alexander Shatalin is currently offline Alexander ShatalinFriend
Messages: 2928
Registered: July 2009
Senior Member
Hello Balthasar,

Ok. I can reproduce this problem now (have to create same hierarchy like
you).
If you open .log file you'll see ClassCast exception there and this is a
reason for problems with setting custom layout.
Workarounds:
1. add removeEditPolicy(EditPolicy.LAYOUT_ROLE); call to the end of createDefaultEditPolicies()
method in CompartmentEditPart
2. or open .gmfgen model, locate two (both) GenCompartment instances there
and set "ListLayout" property to "true". (looks like Q&A was written for
the case of compartment with list layout).

-----------------
Alex Shatalin
Re: Custom Layout in Compartment [message #233812 is a reply to message #233803] Tue, 16 June 2009 14:47 Go to previous messageGo to next message
Sven Krause is currently offline Sven KrauseFriend
Messages: 64
Registered: July 2009
Member
Hi,

I've had the same problem. After inspecting the gmf templates I've seen
a switch how to affect the generated result. A nested compartment figure
edit part is extending ListCompartmentEditPart where the inital main
compartment extends ShapeCompartmentEditPart. The key is the value for
ListLayout at the specific Gen Compartment within the created gmfgen
model. I have no glue what the initial value drives, but changing the
ListLayout to false remains recreation via gmfmap -> create generator model.

Hope this helps,

Sven
Alex Shatalin wrote:
> Hello Balthasar,
>
> Ok. I can reproduce this problem now (have to create same hierarchy
> like you).
> If you open .log file you'll see ClassCast exception there and this is
> a reason for problems with setting custom layout.
> Workarounds:
> 1. add removeEditPolicy(EditPolicy.LAYOUT_ROLE); call to the end of
> createDefaultEditPolicies() method in CompartmentEditPart
> 2. or open .gmfgen model, locate two (both) GenCompartment instances
> there and set "ListLayout" property to "true". (looks like Q&A was
> written for the case of compartment with list layout).
>
> -----------------
> Alex Shatalin
>
>
Re: Custom Layout in Compartment [message #233847 is a reply to message #233589] Wed, 17 June 2009 06:45 Go to previous messageGo to next message
Srinath Anantha is currently offline Srinath AnanthaFriend
Messages: 1
Registered: July 2009
Junior Member
Hi Balthasar,
I was working on the same problem recently and this is how i solved it.
As you were rightly thinking it can be solved by setting custom layout to
the contentPane in createFigure() method. Using StackLayout will cause
ClassCastException as the ShapeCompartmentFigure expects an XYLayout. here
is the code:-

public class CompartmentEditPart extends ShapeCompartmentEditPart
{
....
....
/**
* @generated
*/
public IFigure createFigure()
{
ResizableCompartmentFigure result = (ResizableCompartmentFigure)
super.createFigure();
result.getContentPane().setLayoutManager(new VerticalLayout());
return result;
}

/**
* @generated NOT
*/
private static class VerticalLayout extends XYLayout
{
@Override
public void layout(IFigure parent)
{
Iterator<IFigure> fIterator = parent.getChildren().iterator();
int y = 10;
while (fIterator.hasNext())
{
IFigure figure = fIterator.next();
int height=figure.getPreferredSize().height;
int width=figure.getPreferredSize().width;
figure.setBounds(new Rectangle(10, 10, width,height));
y = y + height + 30; // Spacing of 30 between children
}
}
}


}//End of Class


Hope this code comes useful to you.


cheers,
Srinath
Re: Custom Layout in Compartment [message #234161 is a reply to message #233847] Thu, 18 June 2009 20:03 Go to previous message
No real name is currently offline No real nameFriend
Messages: 3
Registered: July 2009
Junior Member
Hi Alex, Sven and Srinath,

thanks a lot for your suggestions. Finally I found a solution using all
of them:

Instead of using the ShapeCompartmentEditPart I directly used the
ResizableCompartmentEditPart, copied what I need from
ShapeCompartmentEditPart and customized it so that it has the right
LayoutEditPolicy and the right Figures.
Furthermore I let my own layout extend XYLayout.

The complete solution:

public class MyCompartmentEditPart extends ResizableCompartmentEditPart{
....
public IFigure createFigure() {
ResizableCompartmentFigure rcf;
if (getParent() == getTopGraphicEditPart()){
rcf = (ResizableCompartmentFigure) super.createFigure();
} else {
rcf = new NestedResizableCompartmentFigure(getMapMode());
}
rcf.getContentPane().setLayoutManager(new MyLayoutManager());
return rcf;
}
....
protected void createDefaultEditPolicies() {
super.createDefaultEditPolicies();
installEditPolicy(EditPolicy.LAYOUT_ROLE,
new MyLayoutEditPolicy());
}
}


My custom layout is a bit large, it arranges the children in a grid. For
anyone who wants to do a custom layout in a compartment, too:
Take the XYLayout as a basis and look how calculatePreferredSize(..) and
layout(..) is done there. At first I made the mistake that I did not use
the constraints that the layout stores and directly called
child.getPreferredSize(..). This returns much too big values, because an
other layout manager of the child then tries to use the complete space.

Again, thank you all for your help, finally I got it working :-)

Cheers,
Balthasar
Previous Topic:OffscreenEditPart factory during build requires UI thread
Next Topic:Outline, hierarchicaly, Diagram input a subelement
Goto Forum:
  


Current Time: Sat May 30 09:17:15 GMT 2020

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

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

Back to the top