I try to get a simple working GEF editor. I have a GraphicalEditorWithPalette
that creates my example model. I have a RootEditPart
that overrides createFigure
and getModelChildren
like this:
public class RootEditPart extends AbstractGraphicalEditPart {
@Override
protected IFigure createFigure() {
Figure figure = new Figure();
figure.setOpaque(true);
figure.setLayoutManager(new XYLayout());
figure.setSize(1000, 1000);
return figure;
}
@Override
protected List<Positionable> getModelChildren() {
return getModel().getChildren();
}
// More methods ...
}
I have a ChildEditPart that creates a label and override refreshVisuals
like this:
public class ChildEditPart extends AbstractGraphicalEditPart {
@Override
protected void refreshVisuals() {
RDC rdc = getModel();
Label nameLabel = (Label) getFigure();
nameLabel.setText(rdc.getName());
Point position = rdc.getPosition();
Rectangle r = new Rectangle(position.x, position.y, -1,-1);
((GraphicalEditPart) getParent()).setLayoutConstraint(this, nameLabel, r);
}
// More methods ...
}
This works as expected. But I don't want to have a fixed size for the container. Instead I try to achieve that the containers size adapts to the children. If I remove the figure.setSize(1000, 1000);
the children are not drawn any more. What is my mistake?
Edit:
Maybe it is important to know that my editor configures the root edit part like this: viewer.setRootEditPart(new ScalableFreeformRootEditPart());
Edit2:
Seems like in fact the call viewer.setRootEditPart(new ScalableFreeformRootEditPart())开发者_Python百科;
is the problem. If I remove this it works as expected. Currently I don't fully understand why ... would be great if someone could explain.
You have to set size of parent's figure manually when it needed.
figure.setSize(figure.getPreferredSize());
figure delegates computing it's prefer size to it's layout(if exist). XYLayout will compute minimal size to represent contents.
I think best place to insert this code is setLayoutConstraint of parent EditPart.
精彩评论