ContentPane and layout problems/questions [message #166897] |
Thu, 03 February 2005 15:06  |
Eclipse User |
|
|
|
Originally posted by: no.mail.please
Hi !
I have a problem setting correctly the layout of a figure (see code at
the end). This is just a RectangularShape with a smaller
RectangularShape inside which is set as the contentpane.
This works fine and I can add children to it but I encounter two minor
problems :
- the contentPane area doesn't resize with the main shape when I resize
this one
- children can be added to the main shape even if not dropped in the
contentPane area. So they aren't visible untill you drag them in the
contentPane area.
How can I :
- make the contentPane area resize automatically ? I tried using a
FlowLayout instead of the XYLayout, but I get a contenttPane area only
when I drop a child. If there are no children, it isn't visible.
- force children to be redirected in the contentPane area when dropped
outside it but inside the parent shape ?
TIA
--
Arnaud
|
|
|
Re: ContentPane and layout problems/questions [message #166906 is a reply to message #166897] |
Thu, 03 February 2005 15:13   |
Eclipse User |
|
|
|
Originally posted by: no.mail.please
Here is the code, sorry.
class BeanBoxEditPart extends AbstractGraphicalEditPart
implements PropertyChangeListener {
private IFigure contentPane;
private static final int BORDER_WIDTH = 10;
/**
* Upon activation, attach to the model element as a property change
listener.
*/
public void activate() {
if (!isActive()) {
super.activate();
((ModelElement) getModel()).addPropertyChangeListener(this);
}
}
/**
* Upon deactivation, detach from the model element as a property
change listener.
*/
public void deactivate() {
if (isActive()) {
super.deactivate();
((ModelElement) getModel()).removePropertyChangeListener(this);
}
}
/* (non-Javadoc)
* @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicie s()
*/
protected void createEditPolicies() {
// allow removal of the associated model element
//installEditPolicy(EditPolicy.COMPONENT_ROLE, new
ShapeComponentEditPolicy());
// handles constraint changes (e.g. moving and/or resizing) of model
elements
// and creation of new model elements
XYLayout layout = (XYLayout) getContentPane().getLayoutManager();
installEditPolicy(EditPolicy.LAYOUT_ROLE, new
ShapesXYLayoutEditPolicy(layout));
}
/*(non-Javadoc)
* @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFi gure()
*/
protected IFigure createFigure() {
IFigure f = new RectangleFigure();
XYLayout layout = new XYLayout();
f.setLayoutManager(layout);
f.setOpaque(true);
f.setBackgroundColor(ColorConstants.lightBlue);
contentPane = new RectangleFigure();
contentPane.setLayoutManager(new FreeformLayout());
contentPane.setOpaque(true);
contentPane.setBackgroundColor(ColorConstants.gray);
f.add(contentPane, new Rectangle(f.getClientArea().x + BORDER_WIDTH,
f.getClientArea().y + BORDER_WIDTH,f.getClientArea().width -
BORDER_WIDTH, f.getClientArea().height - BORDER_WIDTH ));
return f;
}
private BeanBox getCastedModel() {
return (BeanBox) getModel();
}
/* (non-Javadoc)
* @see org.eclipse.gef.editparts.AbstractEditPart#getModelChildren( )
*/
protected List getModelChildren() {
return getCastedModel().getChildren(); // return a list of shapes
}
/*
* (non-Javadoc)
* @see org.eclipse.gef.GraphicalEditPart#getContentPane()
*/
public IFigure getContentPane(){
return contentPane;
}
/* (non-Javadoc)
* @see
java.beans.PropertyChangeListener#propertyChange(java.beans. PropertyChangeEvent)
*/
public void propertyChange(PropertyChangeEvent evt) {
String prop = evt.getPropertyName();
if (BeanBox.SIZE_PROP.equals(prop) || BeanBox.LOCATION_PROP.equals(prop)) {
refreshVisuals();
}
// these properties are fired when Shapes are added into or removed from
// the ShapeDiagram instance and must cause a call of refreshChildren()
// to update the diagram's contents.
if (BeanBox.CHILD_ADDED_PROP.equals(prop)
|| BeanBox.CHILD_REMOVED_PROP.equals(prop)) {
refreshChildren();
}
}
protected void refreshVisuals() {
// notify parent container of changed position & location
// if this line is removed, the XYLayoutManager used by the parent
container
// (the Figure of the ShapesDiagramEditPart), will not know the bounds
of this figure
// and will not draw it correctly.
Rectangle bounds = new Rectangle(getCastedModel().getLocation(),
getCastedModel().getSize());
((GraphicalEditPart) getParent()).setLayoutConstraint(this,
getFigure(), bounds);
}
/**
* EditPolicy for the Figure used by this edit part.
* Children of XYLayoutEditPolicy can be used in Figures with XYLayout.
*/
private class ShapesXYLayoutEditPolicy extends XYLayoutEditPolicy {
/**
* Create a new instance of this edit policy.
* @param layout a non-null XYLayout instance. This should be the
layout of the editpart's
* figure where this instance is installed.
* @throws IllegalArgumentException if layout is null
* @see BeanBoxEditPart#createEditPolicies()
*/
ShapesXYLayoutEditPolicy(XYLayout layout) {
if (layout == null) {
throw new IllegalArgumentException();
}
setXyLayout(layout);
}
/* (non-Javadoc)
* @see
org.eclipse.gef.editpolicies.ConstrainedLayoutEditPolicy#cre ateAddCommand(org.eclipse.gef.EditPart,
java.lang.Object)
*/
protected Command createAddCommand(EditPart child, Object constraint) {
// not used in this example
return null;
}
/* (non-Javadoc)
* @see
org.eclipse.gef.editpolicies.ConstrainedLayoutEditPolicy#cre ateChangeConstraintCommand(org.eclipse.gef.requests.ChangeBo undsRequest,
org.eclipse.gef.EditPart, java.lang.Object)
*/
protected Command createChangeConstraintCommand(ChangeBoundsRequest request,
EditPart child, Object constraint) {
if (child instanceof ShapeEditPart && constraint instanceof Rectangle) {
// return a command that can move and/or resize a Shape
return new ShapeSetConstraintCommand(
(Shape) child.getModel(), request, (Rectangle) constraint);
}
return super.createChangeConstraintCommand(request, child, constraint);
}
/* (non-Javadoc)
* @see
org.eclipse.gef.editpolicies.ConstrainedLayoutEditPolicy#cre ateChangeConstraintCommand(org.eclipse.gef.EditPart,
java.lang.Object)
*/
protected Command createChangeConstraintCommand(EditPart child,
Object constraint) {
// not used in this example
return null;
}
/* (non-Javadoc)
* @see
org.eclipse.gef.editpolicies.LayoutEditPolicy#getCreateComma nd(org.eclipse.gef.requests.CreateRequest)
*/
protected Command getCreateCommand(CreateRequest request) {
Object childClass = request.getNewObjectType();
if (childClass == Shape.class) {
// return a command that can add a Shape to a BeanBox
return new ShapeCreateCommand(BeanBoxEditPart.this.getCastedModel(),
request);
}
return null;
}
/* (non-Javadoc)
* @see
org.eclipse.gef.editpolicies.LayoutEditPolicy#getDeleteDepen dantCommand(org.eclipse.gef.Request)
*/
protected Command getDeleteDependantCommand(Request request) {
// not used in this example
return null;
}
}
}
|
|
|
|
Re: ContentPane and layout problems/questions [message #167153 is a reply to message #166914] |
Sun, 06 February 2005 02:14  |
Eclipse User |
|
|
|
When you get the request to add the child, your edit policy can check for
this case and change the coordinates that the command gets, if required.
"Arnaud" <no@mail.please> wrote in message
news:ctu1oa$rnq$1@www.eclipse.org...
> OK, another time I found the answer myself (I shouldn't work so late,
> it's not good for my mind !).
>
> I just added :
> Rectangle childbounds = new Rectangle(10, 10, bounds.width - 20,
> bounds.height - 50);
> XYLayout layout = (XYLayout) getFigure().getLayoutManager();
> layout.setConstraint(contentPane, childbounds);
>
> in the refreshVisuals() method to get the contentPane area resize with
> the main shape ("bounds" are the Rectangle value of the main shape
bounds).
>
> But I've still got the second problem that is forcing the children to be
> redirected in the contentPane area when added outside it but inside the
> main shape. I guess this should be a layout edit policy.
>
> --
> Arnaud
|
|
|
Powered by
FUDForum. Page generated in 0.03837 seconds