Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » GEF » How to implement Container and Layout Policy
How to implement Container and Layout Policy [message #226050] Sat, 04 November 2006 17:18 Go to next message
Eclipse UserFriend
Originally posted by: xylo3.o2.pl

Hello,

I try to add to shape source code one feature :
One more model object called "Box" that can contain other shapes (shapes
can be drop into Box).

I have following problem:
When I drop shape into box :

model : shape is added to box children list
view : shape is still outside the box , but its location is changed (it
seems
that coordinates relative to left upper corner of box is used to
move
shape but, it is added to left upper corner of diagram instead of
left
corner of box)

What is more, when I reopen diagram I have box with shape inside it and
one extra shape ...
Below I attach my "Box" source code .
I look forward to hearing any suggestion from you :)

===================BOX.JAVA================================= ==================
public class Box extends Shape {

private static final Image BOX_ICON = createImage("icons/box16.gif");
List<Shape> children = new LinkedList<Shape>();
@Override
public Image getIcon() {
// TODO Auto-generated method stub
return BOX_ICON;
}

@Override
public String toString() {
return "Box " + hashCode();
}

public void addShape(Shape child) {
if (!children.contains(child)){
children.add(child);
}
}

public List<Shape> getChildren() {
return children;
}

public void removeShape(Shape child) {
children.remove(child);

}

}


===================================BOX_FIGURE.JAVA========== ==================

public class BoxFigure extends Figure{
IFigure contentPane;
public BoxFigure(){
setBorder(new FrameBorder("BOX"));
ScrollPane scrollpane = new ScrollPane();
contentPane = new FreeformLayer();
contentPane.setLayoutManager(new FreeformLayout());
contentPane.setBackgroundColor(ColorConstants.lightGray);
contentPane.setOpaque(true);
setLayoutManager(new StackLayout());
add(scrollpane);
scrollpane.setViewport(new FreeformViewport());
scrollpane.setContents(contentPane);

}
public IFigure getContentPane(){
return contentPane;
}


==================================BOX_EDIT_PART.JAVA======== =====================



public class BoxEditPart extends ShapeEditPart {

@Override
protected IFigure createFigure() {
return new BoxFigure();
}

@Override
public IFigure getContentPane() {
return ((BoxFigure)getFigure()).getContentPane();
}

@Override
protected void createEditPolicies() {
super.createEditPolicies();
installEditPolicy(EditPolicy.CONTAINER_ROLE,new BoxContainerPolicy());
installEditPolicy(EditPolicy.LAYOUT_ROLE, new BoxXYLayoutEditPolicy(
(XYLayout)getContentPane().getLayoutManager()));
}

@Override
protected List getModelChildren() {
List children = ((Box)getModel()).getChildren();
System.out.println("BoxEditPart - getModelChildren() - children = " +
children.size());
return children;
}
}



==========================BOX_CONTAINER_EDIT_POLICY========= ==================


public class BoxContainerPolicy extends ContainerEditPolicy {

@Override
protected Command getCreateCommand(CreateRequest request)
return null;
}

@Override
protected Command getOrphanChildrenCommand(GroupRequest request) {
//TODO : RETURN COMMAND THAT REMOVE SHAPE FROM BOX
return null;
}


=============================BOXXY_LAYOUT_EDIT_POLICY.JAVA== ===================

public class BoxXYLayoutEditPolicy extends XYLayoutEditPolicy {

public BoxXYLayoutEditPolicy(XYLayout layout) {
setXyLayout(layout);
}



@Override
protected Command createAddCommand(EditPart child, Object constraint) {
System.out.println("BoxXYLayoutEditPolicy - createAddCommand");
Shape part = (Shape)child.getModel();
Rectangle rect = (Rectangle)translateToModelConstraint(constraint);
AddToBoxCommand add = new AddToBoxCommand();
add.setParent((Box)getHost().getModel());
add.setChild(part);
SetConstraintCommand setConstraint = new SetConstraintCommand();
setConstraint.setLocation(rect);
setConstraint.setPart(part);
return add.chain(setConstraint);
}

protected Command createChangeConstraintCommand(
ChangeBoundsRequest request, EditPart child, Object constraint) {
SetConstraintCommand cmd = new SetConstraintCommand();
Shape part = (Shape) child.getModel();
cmd.setPart(part);
cmd.setLocation((Rectangle) constraint);
return cmd;
}

@Override

protected Command createChangeConstraintCommand(EditPart child,
Object constraint) {
return null;
}

@Override
protected Command getCreateCommand(CreateRequest request) {
return null;
}

@Override
protected Command getDeleteDependantCommand(Request request) {
return null;
}

=============================ADD_TO_BOX_COMMAND.JAVA======== ===================
public class AddToBoxCommand extends Command {
private Box parent;
private Shape child;
public void execute() {
parent.addShape(child);
}

public void setParent(Box box) {
parent = box;

}

public void setChild(Shape part) {
child = part;
}
@Override
public void undo() {
parent.removeShape(child);
}

}

============================SET_CONSTRAINT_COMMAND.JAVA===== ==================
public class SetConstraintCommand
extends org.eclipse.gef.commands.Command
{

private Point newPos;
private Dimension newSize;
private Point oldPos;
private Dimension oldSize;
private Shape part;

public void execute() {
oldSize = part.getSize();
oldPos = part.getLocation();
redo();
}

public void redo() {
part.setSize(newSize);
part.setLocation(newPos);
}

public void setLocation(Rectangle r) {
setLocation(r.getLocation());
setSize(r.getSize());
}

public void setLocation(Point p) {
newPos = p;
}

public void setPart(Shape part) {
this.part = part;
}

public void setSize(Dimension p) {
newSize = p;
}

public void undo() {
part.setSize(oldSize);
part.setLocation(oldPos);
}

}
Re: How to implement Container and Layout Policy [message #226075 is a reply to message #226050] Sun, 05 November 2006 12:55 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: xylo3.o2.pl

Ok, I will answer myself ;)
I' ve forgot :
1. Add to Diagram (root class object of entire model) edit part Container
policy.
2. addShape and removeShape method in Box.java should fire property change
that is caught by propertyChange() method (in BoxEditPart), where
refreshing view is invoked.

Now I can drop shapes into Box and moving Box with children shapes is
serviced properly. But I can not take shapes from Box and drop in diagram
again...

I guess that there is still something wrong with my Container policy
implementation.

I have implemented Container policy that invoke the same command for
Diagram and Box as well.

==============ORPHAN_CHILD_COMMAND========================== =================

public class OrphanChildCommand extends Command {
private IShapeContainer parent;
private Point oldLocation;
private Shape child;


public OrphanChildCommand(){
}

public void setChild(Shape child) {
this.child = child;
}

public void setParent(IShapeContainer parent) {
this.parent = parent;
}

@Override
public void execute() {
System.out.println("OrphanChildCommand -- execute()");
oldLocation = child.getLocation();
parent.removeChild(child);

}

@Override
public void undo() {
parent.addChild(child);
}
}

This command is created in getOrphanChildCommand() that looks the same
for DiagramContainerPolicy and BoxContainerPolicy :

protected Command getOrphanChildrenCommand(GroupRequest request) {
List parts = request.getEditParts();
CompoundCommand result = new CompoundCommand();
for (int i = 0; i < parts.size(); i++) {
OrphanChildCommand orphan = new OrphanChildCommand();
orphan.setChild((Shape) ((EditPart) parts.get(i))
.getModel());
orphan.setParent((IShapeContainer) getHost().getModel());
result.add(orphan);
}
return result.unwrap();
}


When I try unpack shape from box, OrphanChildCommand object is created by
its execute() method is not invoked....

Any help will be appreciated

Regards,
Max
Re: How to implement Container and Layout Policy [message #226113 is a reply to message #226075] Mon, 06 November 2006 06:04 Go to previous message
Eclipse UserFriend
Originally posted by: none.unknown.com

Your box get the ORPHAN request, and your diagram gets the ADD request. The
reparenting happens IFF both return executable commands.

"Max" <xylo3@o2.pl> wrote in message
news:eedda54edc6a1ea12a6ffc7f70c1d2a7$1@www.eclipse.org...
>
> Ok, I will answer myself ;)
> I' ve forgot :
> 1. Add to Diagram (root class object of entire model) edit part Container
> policy.
> 2. addShape and removeShape method in Box.java should fire property change
> that is caught by propertyChange() method (in BoxEditPart), where
> refreshing view is invoked.
>
> Now I can drop shapes into Box and moving Box with children shapes is
> serviced properly. But I can not take shapes from Box and drop in diagram
> again...
>
> I guess that there is still something wrong with my Container policy
> implementation.
>
> I have implemented Container policy that invoke the same command for
> Diagram and Box as well.
>
> ==============ORPHAN_CHILD_COMMAND========================== =================
>
> public class OrphanChildCommand extends Command {
> private IShapeContainer parent;
> private Point oldLocation;
> private Shape child;
>
>
> public OrphanChildCommand(){
> }
>
> public void setChild(Shape child) {
> this.child = child;
> }
>
> public void setParent(IShapeContainer parent) {
> this.parent = parent;
> }
>
> @Override
> public void execute() {
> System.out.println("OrphanChildCommand -- execute()");
> oldLocation = child.getLocation();
> parent.removeChild(child);
>
> }
>
> @Override
> public void undo() {
> parent.addChild(child);
> }
> }
>
> This command is created in getOrphanChildCommand() that looks the same
> for DiagramContainerPolicy and BoxContainerPolicy :
>
> protected Command getOrphanChildrenCommand(GroupRequest request) {
> List parts = request.getEditParts();
> CompoundCommand result = new CompoundCommand();
> for (int i = 0; i < parts.size(); i++) {
> OrphanChildCommand orphan = new OrphanChildCommand();
> orphan.setChild((Shape) ((EditPart) parts.get(i))
> .getModel());
> orphan.setParent((IShapeContainer) getHost().getModel());
> result.add(orphan);
> }
> return result.unwrap();
> }
>
>
> When I try unpack shape from box, OrphanChildCommand object is created by
> its execute() method is not invoked....
>
> Any help will be appreciated
>
> Regards,
> Max
>
>
>
Previous Topic:Placing child figure at fixed relative position
Next Topic:Suppress PropertySheet Undo/Redo
Goto Forum:
  


Current Time: Thu Apr 25 23:03:10 GMT 2024

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

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

Back to the top