Home » Eclipse Projects » GEF » Figure not displaying in viewer ?
Figure not displaying in viewer ? [message #166693] |
Wed, 02 February 2005 15:42  |
Eclipse User |
|
|
|
Originally posted by: no.mail.please
Hi !
To learn GEF architecture, I wrote a simple GEF editor (based on
ShapeDiagram example) working like that :
- model root = ShapeDiagram which hosts several BeanBox which themselves
should host Shapes
- the Shapes are not allowed to be added to ShapeDiagram, only to BeanBox
So I wrote the few classes given at the end of this post, but I
encounter a problem : adding BeanBox works fine but the Shapes are not
added to BeanBox. In fact, I get not error message, so it seems to me
that Shapes are added to BeanBox (talking on a model point of view) but
that the corresponding figure isn't displayed correctly in the viewer.
Could someone look at my code and show me where I am wrong ?
I specify that these features work fine :
- display of the ShapeDiagram (the IFigure created by the
DiagramEditPart is a red FreeformLayer)
- addition and display of BeanBox from the palette (the IFigure created
by the BeanBoxEditPart is a blue Rectangle). Move / Resize Policies work
fine too.
- addition of Shape isn't allowed if we point on a ShapeDiagram portion
of the viewer.
Thanks in advance
--
Arnaud
Snippets : I put almost all the classes(in 3 parts : model, editparts,
others), sorry for the lenght ! :
1) Model
------------------------------------------------------------ --------------
public abstract class ModelElement {
private static final String filePath = TestGUIBuilderPlugin.ID_IMAGE_DIR;
protected static Image createImage(String name) {
try {
Image image = new Image(null, new FileInputStream(filePath + name));
return image;
} catch (FileNotFoundException e) {
}
return null;
}
/** Delegate used to implemenent property-change-support. */
private PropertyChangeSupport pcsDelegate = new PropertyChangeSupport(this);
/**
* Attach a non-null PropertyChangeListener to this object.
* @param l a non-null PropertyChangeListener instance
* @throws IllegalArgumentException if the parameter is null
*/
public synchronized void
addPropertyChangeListener(PropertyChangeListener l) {
if (l == null) {
throw new IllegalArgumentException();
}
pcsDelegate.addPropertyChangeListener(l);
}
/**
* Report a property change to registered listeners (for example edit
parts).
* @param property the programmatic name of the property that changed
* @param oldValue the old value of this property
* @param newValue the new value of this property
*/
protected void firePropertyChange(String property, Object oldValue,
Object newValue) {
if (pcsDelegate.hasListeners(property)) {
pcsDelegate.firePropertyChange(property, oldValue, newValue);
}
}
/**
* Remove a PropertyChangeListener from this component.
* @param l a PropertyChangeListener instance
*/
public synchronized void
removePropertyChangeListener(PropertyChangeListener l) {
if (l != null) {
pcsDelegate.removePropertyChangeListener(l);
}
}
}
------------------------------------------------------------ --------------
/**
* This is the "root" of the model data structure.
*/
public class ShapesDiagram extends ModelElement {
/** Property ID to use when a child is added to this diagram. */
public static final String CHILD_ADDED_PROP = "ShapesDiagram.ChildAdded";
/** Property ID to use when a child is removed from this diagram. */
public static final String CHILD_REMOVED_PROP =
"ShapesDiagram.ChildRemoved";
private static final long serialVersionUID = 1;
private Collection beanboxlist = new Vector();
/**
* Add a beanbox to this diagram.
* @param b a non-null beanbox instance
* @return true, if the beanbox was added, false otherwise
*/
public boolean addChild(BeanBox b) {
if (b != null && beanboxlist.add(b)) {
firePropertyChange(CHILD_ADDED_PROP, null, b);
return true;
}
return false;
}
/** Return a List of BeanBox in this diagram. */
public List getChildren() {
return new Vector(beanboxlist);
}
/**
* Remove a beanbox from this diagram.
* @param b a non-null beanbox instance;
* @return true, if the beanbox was removed, false otherwise
*/
public boolean removeChild(BeanBox b) {
if (b != null && beanboxlist.remove(b)) {
firePropertyChange(CHILD_REMOVED_PROP, null, b);
return true;
}
return false;
}
}
------------------------------------------------------------ --------------
public class BeanBox extends ModelElement {
/** Property ID to use when a child is added to this beanbox. */
public static final String CHILD_ADDED_PROP = "BeanBox.ChildAdded";
/** Property ID to use when a child is removed from this beanbox. */
public static final String CHILD_REMOVED_PROP = "BeanBox.ChildRemoved";
private Collection shapes = new Vector();
/**
* Add a shape to this beanbox.
* @param s a non-null shape instance
* @return true, if the shape was added, false otherwise
*/
public boolean addChild(Shape s) {
if (s != null && shapes.add(s)) {
firePropertyChange(CHILD_ADDED_PROP, null, s);
return true;
}
return false;
}
/** Return a List of Shapes in this BeanBox. */
public List getChildren() {
return new Vector(shapes);
}
/**
* Remove a shape from this beanbox.
* @param s a non-null shape instance;
* @return true, if the shape was removed, false otherwise
*/
public boolean removeChild(Shape s) {
if (s != null && shapes.remove(s)) {
firePropertyChange(CHILD_REMOVED_PROP, null, s);
return true;
}
return false;
}
/** Location of this beanbox. */
private Point location = new Point(0, 0);
/** Size of this beanbox. */
private Dimension size = new Dimension(50, 50);
/** Property ID to use when the location of this beanbox is modified. */
public static final String LOCATION_PROP = "BeanBox.Location";
/** Property ID to use then the size of this beanbox is modified. */
public static final String SIZE_PROP = "BeanBox.Size";
private static final Image BEANBOX_ICON = createImage("ellipse16.gif");
public Image getIcon() {
return BEANBOX_ICON;
}
public String toString() {
return "BeanBox " + hashCode();
}
/**
* Return the Location of this beanbox.
* @return a non-null location instance
*/
public Point getLocation() {
return location.getCopy();
}
/**
* Return the Size of this beanbox.
* @return a non-null Dimension instance
*/
public Dimension getSize() {
return size.getCopy();
}
/**
* Set the Location of this beanbox.
* @param newLocation a non-null Point instance
* @throws IllegalArgumentException if the parameter is null
*/
public void setLocation(Point newLocation) {
if (newLocation == null) {
throw new IllegalArgumentException();
}
location.setLocation(newLocation);
firePropertyChange(LOCATION_PROP, null, location);
}
/**
* Set the Size of this beanbox.
* Will not modify the size if newSize is null.
* @param newSize a non-null Dimension instance or null
*/
public void setSize(Dimension newSize) {
if (newSize != null) {
size.setSize(newSize);
firePropertyChange(SIZE_PROP, null, size);
}
}
/**
* Return a List of outgoing Connections.
*/
public List getSourceConnections() {
return null;
}
/**
* Return a List of incoming Connections.
*/
public List getTargetConnections() {
return null;
}
}
------------------------------------------------------------ --------------
public class Shape extends ModelElement {
private static final String filePath = TestGUIBuilderPlugin.ID_IMAGE_DIR;
/** Property ID to use when the location of this shape is modified. */
public static final String LOCATION_PROP = "Shape.Location";
/** Property ID to use then the size of this shape is modified. */
public static final String SIZE_PROP = "Shape.Size";
/** Location of this shape. */
private Point location = new Point(0, 0);
/** Size of this shape. */
private Dimension size = new Dimension(10, 10);
/** A 16x16 pictogram of an elliptical shape. */
private static final Image SHAPE_ICON = createImage("ellipse16.gif");
public Image getIcon() {
return SHAPE_ICON;
}
public String toString() {
return "Shape " + hashCode();
}
/**
* Return the Location of this shape.
* @return a non-null location instance
*/
public Point getLocation() {
return location.getCopy();
}
/**
* Return the Size of this shape.
* @return a non-null Dimension instance
*/
public Dimension getSize() {
return size.getCopy();
}
/**
* Set the Location of this shape.
* @param newLocation a non-null Point instance
* @throws IllegalArgumentException if the parameter is null
*/
public void setLocation(Point newLocation) {
if (newLocation == null) {
throw new IllegalArgumentException();
}
location.setLocation(newLocation);
firePropertyChange(LOCATION_PROP, null, location);
}
/**
* Set the Size of this shape.
* Will not modify the size if newSize is null.
* @param newSize a non-null Dimension instance or null
*/
public void setSize(Dimension newSize) {
if (newSize != null) {
size.setSize(newSize);
firePropertyChange(SIZE_PROP, null, size);
}
}
/**
* Return a List of outgoing Connections.
*/
public List getSourceConnections() {
return null;
}
/**
* Return a List of incoming Connections.
*/
public List getTargetConnections() {
return null;
}
}
------------------------------------------------------------ --------------
------------------------------------------------------------ --------------
2) EditParts :
------------------------------------------------------------ --------------
class DiagramEditPart extends AbstractGraphicalEditPart
implements PropertyChangeListener {
/**
* 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() {
// disallows the removal of this edit part from its parent
installEditPolicy(EditPolicy.COMPONENT_ROLE, new
RootComponentEditPolicy());
// 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
BeanBoxXYLayoutEditPolicy(layout));
// disable selection feedback for this edit part
installEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE, null);
}
/* (non-Javadoc)
* @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFi gure()
*/
protected IFigure createFigure() {
FreeformLayer f = new FreeformLayer();
f.setLayoutManager(new FreeformLayout());
f.setOpaque(true);
f.setBackgroundColor(ColorConstants.red);
return f;
}
private ShapesDiagram getCastedModel() {
return (ShapesDiagram) getModel();
}
/* (non-Javadoc)
* @see org.eclipse.gef.editparts.AbstractEditPart#getModelChildren( )
*/
protected List getModelChildren() {
return getCastedModel().getChildren(); // return a list of shapes
}
/* (non-Javadoc)
* @see
java.beans.PropertyChangeListener#propertyChange(java.beans. PropertyChangeEvent)
*/
public void propertyChange(PropertyChangeEvent evt) {
String prop = evt.getPropertyName();
// 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 (ShapesDiagram.CHILD_ADDED_PROP.equals(prop)
|| ShapesDiagram.CHILD_REMOVED_PROP.equals(prop)) {
refreshChildren();
}
}
/**
* EditPolicy for the Figure used by this edit part.
* Children of XYLayoutEditPolicy can be used in Figures with XYLayout.
* @author Elias Volanakis
*/
private class BeanBoxXYLayoutEditPolicy 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 DiagramEditPart#createEditPolicies()
*/
BeanBoxXYLayoutEditPolicy(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 BeanBoxEditPart && constraint instanceof Rectangle) {
// return a command that can move and/or resize a Shape
return new BeanBoxSetConstraintCommand(
(BeanBox) 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 == BeanBox.class) {
// return a command that can add a Shape to a ShapesDiagram
return new BeanBoxCreateCommand(DiagramEditPart.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;
}
}
}
------------------------------------------------------------ --------------
class BeanBoxEditPart extends AbstractGraphicalEditPart
implements PropertyChangeListener {
/**
* 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();
f.setLayoutManager(new FreeformLayout());
f.setOpaque(true);
f.setBackgroundColor(ColorConstants.lightBlue);
return f;
}
private BeanBox getCastedModel() {
return (BeanBox) getModel();
}
/* (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();
}
}
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;
}
}
}
------------------------------------------------------------ --------------
class ShapeEditPart extends AbstractGraphicalEditPart
implements PropertyChangeListener {
/**
* 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());
}
/*(non-Javadoc)
* @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFi gure()
*/
protected IFigure createFigure() {
IFigure f = new RectangleFigure();
f.setOpaque(true);
f.setBackgroundColor(ColorConstants.lightGreen);
return f;
}
private Shape getCastedModel() {
return (Shape) getModel();
}
/* (non-Javadoc)
* @see
java.beans.PropertyChangeListener#propertyChange(java.beans. PropertyChangeEvent)
*/
public void propertyChange(PropertyChangeEvent evt) {
String prop = evt.getPropertyName();
if (Shape.SIZE_PROP.equals(prop) || Shape.LOCATION_PROP.equals(prop)) {
refreshVisuals();
}
}
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);
}
}
------------------------------------------------------------ --------------
------------------------------------------------------------ --------------
3) Others
------------------------------------------------------------ --------------
/**
* Factory that maps model elements to edit parts.
*/
public class ShapesEditPartFactory implements EditPartFactory {
/*
* (non-Javadoc)
* @see
org.eclipse.gef.EditPartFactory#createEditPart(org.eclipse.g ef.EditPart,
java.lang.Object)
*/
public EditPart createEditPart(EditPart context, Object modelElement) {
// get EditPart for model element
EditPart part = getPartForElement(modelElement);
// store model element in EditPart
part.setModel(modelElement);
return part;
}
/**
* Maps an object to an EditPart.
* @throws RuntimeException if no match was found (programming error)
*/
private EditPart getPartForElement(Object modelElement) {
if (modelElement instanceof ShapesDiagram) {
return new DiagramEditPart();
}
if (modelElement instanceof BeanBox) {
return new BeanBoxEditPart();
}
if (modelElement instanceof Shape) {
return new ShapeEditPart();
}
throw new RuntimeException(
"Can't create part for model element: "
+ ((modelElement != null) ? modelElement.getClass().getName() : "null"));
}
}
------------------------------------------------------------ --------------
/**
* This edit policy enables the removal of a Shapes instance from its
container.
*/
class ShapeComponentEditPolicy extends ComponentEditPolicy {
/* (non-Javadoc)
* @see
org.eclipse.gef.editpolicies.ComponentEditPolicy#createDelet eCommand(org.eclipse.gef.requests.GroupRequest)
*/
protected Command createDeleteCommand(GroupRequest deleteRequest) {
Object parent = getHost().getParent().getModel();
Object child = getHost().getModel();
if (parent instanceof BeanBox && child instanceof Shape) {
return new ShapeDeleteCommand((BeanBox) parent, (Shape) child);
}
return super.createDeleteCommand(deleteRequest);
}
}
------------------------------------------------------------ --------------
/**
* A command to add a BeanBox to a ShapeDiagram.
* The command can be undone or redone.
*/
public class BeanBoxCreateCommand extends Command {
/** The new beanbox. */
private BeanBox newBeanBox;
private final ShapesDiagram parent;
/** A request to create a new BeanBox. */
private final CreateRequest request;
/** True, if newBeanBox was added to parent. */
private boolean beanboxAdded;
/**
* Create a command that will add a new Beanbox to a ShapesDiagram.
* @param parent the ShapesDiagram that will hold the new element
* @param req a request to create a new BeanBox
* @throws IllegalArgumentException if any parameter is null, or the
request
* does not provide a new BeanBox instance
*/
public BeanBoxCreateCommand(ShapesDiagram parent, CreateRequest req) {
if (parent == null || req == null || !(req.getNewObject() instanceof
BeanBox)) {
throw new IllegalArgumentException();
}
this.parent = parent;
this.request = req;
setLabel("beanbox creation");
}
/* (non-Javadoc)
* @see org.eclipse.gef.commands.Command#canUndo()
*/
public boolean canUndo() {
return beanboxAdded;
}
/* (non-Javadoc)
* @see org.eclipse.gef.commands.Command#execute()
*/
public void execute() {
// Obtain the new BeanBox instance from the request.
// This causes the factory stored in the request to create a new instance.
// The factory is supplied in the palette-tool-entry, see
// TestGUIEditorPaletteFactory#createComponentsGroup()
newBeanBox = (BeanBox) request.getNewObject();
// Get desired location and size from the request
newBeanBox.setSize(request.getSize()); // might be null!
newBeanBox.setLocation(request.getLocation());
redo();
}
/* (non-Javadoc)
* @see org.eclipse.gef.commands.Command#redo()
*/
public void redo() {
beanboxAdded = parent.addChild(newBeanBox);
}
/* (non-Javadoc)
* @see org.eclipse.gef.commands.Command#undo()
*/
public void undo() {
parent.removeChild(newBeanBox);
}
}
------------------------------------------------------------ --------------
/**
* A command to resize and/or move a beanbox.
* The command can be undone or redone.
*/
public class BeanBoxSetConstraintCommand extends Command {
/** Stores the new size and location. */
private final Rectangle newBounds;
/** Stores the old size and location. */
private Rectangle oldBounds;
/** A request to move/resize an edit part. */
private final ChangeBoundsRequest request;
/** BeanBox to manipulate. */
private final BeanBox beanbox;
/**
* Create a command that can resize and/or move a BeanBox.
* @param beanbox the BeanBox to manipulate
* @param req the move and resize request
* @param newBounds the new size and location
* @throws IllegalArgumentException if any of the parameters is null
*/
public BeanBoxSetConstraintCommand(BeanBox beanbox, ChangeBoundsRequest
req,
Rectangle newBounds) {
if (beanbox == null || req == null || newBounds == null) {
throw new IllegalArgumentException();
}
this.beanbox = beanbox;
this.request = req;
this.newBounds = newBounds.getCopy();
setLabel("move / resize");
}
/* (non-Javadoc)
* @see org.eclipse.gef.commands.Command#canExecute()
*/
public boolean canExecute() {
Object type = request.getType();
// make sure the Request is of a type we support:
return (RequestConstants.REQ_MOVE.equals(type)
|| RequestConstants.REQ_MOVE_CHILDREN.equals(type)
|| RequestConstants.REQ_RESIZE.equals(type)
|| RequestConstants.REQ_RESIZE_CHILDREN.equals(type));
}
/* (non-Javadoc)
* @see org.eclipse.gef.commands.Command#execute()
*/
public void execute() {
oldBounds = new Rectangle(beanbox.getLocation(), beanbox.getSize());
redo();
}
/* (non-Javadoc)
* @see org.eclipse.gef.commands.Command#redo()
*/
public void redo() {
beanbox.setSize(newBounds.getSize());
beanbox.setLocation(newBounds.getLocation());
}
/* (non-Javadoc)
* @see org.eclipse.gef.commands.Command#undo()
*/
public void undo() {
beanbox.setSize(oldBounds.getSize());
beanbox.setLocation(oldBounds.getLocation());
}
}
------------------------------------------------------------ --------------
/**
* A command to add a Shape to a BeanBox.
* The command can be undone or redone.
*/
public class ShapeCreateCommand extends Command {
/** The new shape. */
private Shape newShape;
/** ShapeDiagram to add to. */
private final BeanBox parent;
/** A request to create a new Shape. */
private final CreateRequest request;
/** True, if newShape was added to parent. */
private boolean shapeAdded;
/**
* Create a command that will add a new Shape to a BeanBox.
* @param parent the ShapesDiagram that will hold the new element
* @param req a request to create a new Shape
* @throws IllegalArgumentException if any parameter is null, or the
request
* does not provide a new Shape instance
*/
public ShapeCreateCommand(BeanBox parent, CreateRequest req) {
if (parent == null || req == null || !(req.getNewObject() instanceof
Shape)) {
throw new IllegalArgumentException();
}
this.parent = parent;
this.request = req;
setLabel("shape creation");
}
/* (non-Javadoc)
* @see org.eclipse.gef.commands.Command#canUndo()
*/
public boolean canUndo() {
return shapeAdded;
}
/* (non-Javadoc)
* @see org.eclipse.gef.commands.Command#execute()
*/
public void execute() {
// Obtain the new Shape instance from the request.
// This causes the factory stored in the request to create a new instance.
// The factory is supplied in the palette-tool-entry, see
// ShapesEditorPaletteFactory#createComponentsGroup()
newShape = (Shape) request.getNewObject();
// Get desired location and size from the request
newShape.setSize(request.getSize()); // might be null!
newShape.setLocation(request.getLocation());
redo();
}
/* (non-Javadoc)
* @see org.eclipse.gef.commands.Command#redo()
*/
public void redo() {
shapeAdded = parent.addChild(newShape);
}
/* (non-Javadoc)
* @see org.eclipse.gef.commands.Command#undo()
*/
public void undo() {
parent.removeChild(newShape);
}
}
------------------------------------------------------------ --------------
/**
* A command to remove a shape from its parent.
* The command can be undone or redone.
*/
public class ShapeDeleteCommand extends Command {
/** Shape to remove. */
private final Shape child;
/** ShapeDiagram to remove from. */
private final BeanBox parent;
/** True, if child was removed from its parent. */
private boolean wasRemoved;
/**
* Create a command that will remove the shape from its parent.
* @param parent the ShapesDiagram containing the child
* @param child the Shape to remove
* @throws IllegalArgumentException if any parameter is null
*/
public ShapeDeleteCommand(BeanBox parent, Shape child) {
if (parent == null || child == null) {
throw new IllegalArgumentException();
}
setLabel("shape deletion");
this.parent = parent;
this.child = child;
}
/* (non-Javadoc)
* @see org.eclipse.gef.commands.Command#canUndo()
*/
public boolean canUndo() {
return wasRemoved;
}
/* (non-Javadoc)
* @see org.eclipse.gef.commands.Command#execute()
*/
public void execute() {
redo();
}
/* (non-Javadoc)
* @see org.eclipse.gef.commands.Command#redo()
*/
public void redo() {
// remove the child and disconnect its connections
wasRemoved = parent.removeChild(child);
}
/* (non-Javadoc)
* @see org.eclipse.gef.commands.Command#undo()
*/
public void undo() {
// add the child and reconnect its connections
parent.addChild(child);
}
}
------------------------------------------------------------ --------------
/**
* A command to resize and/or move a shape.
* The command can be undone or redone.
*/
public class ShapeSetConstraintCommand extends Command {
/** Stores the new size and location. */
private final Rectangle newBounds;
/** Stores the old size and location. */
private Rectangle oldBounds;
/** A request to move/resize an edit part. */
private final ChangeBoundsRequest request;
/** Shape to manipulate. */
private final Shape shape;
/**
* Create a command that can resize and/or move a shape.
* @param shape the shape to manipulate
* @param req the move and resize request
* @param newBounds the new size and location
* @throws IllegalArgumentException if any of the parameters is null
*/
public ShapeSetConstraintCommand(Shape shape, ChangeBoundsRequest req,
Rectangle newBounds) {
if (shape == null || req == null || newBounds == null) {
throw new IllegalArgumentException();
}
this.shape = shape;
this.request = req;
this.newBounds = newBounds.getCopy();
setLabel("move / resize");
}
/* (non-Javadoc)
* @see org.eclipse.gef.commands.Command#canExecute()
*/
public boolean canExecute() {
Object type = request.getType();
// make sure the Request is of a type we support:
return (RequestConstants.REQ_MOVE.equals(type)
|| RequestConstants.REQ_MOVE_CHILDREN.equals(type)
|| RequestConstants.REQ_RESIZE.equals(type)
|| RequestConstants.REQ_RESIZE_CHILDREN.equals(type));
}
/* (non-Javadoc)
* @see org.eclipse.gef.commands.Command#execute()
*/
public void execute() {
oldBounds = new Rectangle(shape.getLocation(), shape.getSize());
redo();
}
/* (non-Javadoc)
* @see org.eclipse.gef.commands.Command#redo()
*/
public void redo() {
shape.setSize(newBounds.getSize());
shape.setLocation(newBounds.getLocation());
}
/* (non-Javadoc)
* @see org.eclipse.gef.commands.Command#undo()
*/
public void undo() {
shape.setSize(oldBounds.getSize());
shape.setLocation(oldBounds.getLocation());
}
}
------------------------------------------------------------ --------------
|
|
|
Re: Figure not displaying in viewer ? [message #166749 is a reply to message #166693] |
Wed, 02 February 2005 19:49   |
Eclipse User |
|
|
|
Your BeanBoxEditPart is not overriding getModelChildren().
"Arnaud" <no@mail.please> wrote in message
news:ctre0f$tpk$1@www.eclipse.org...
> Hi !
>
> To learn GEF architecture, I wrote a simple GEF editor (based on
> ShapeDiagram example) working like that :
> - model root = ShapeDiagram which hosts several BeanBox which themselves
> should host Shapes
> - the Shapes are not allowed to be added to ShapeDiagram, only to BeanBox
>
> So I wrote the few classes given at the end of this post, but I
> encounter a problem : adding BeanBox works fine but the Shapes are not
> added to BeanBox. In fact, I get not error message, so it seems to me
> that Shapes are added to BeanBox (talking on a model point of view) but
> that the corresponding figure isn't displayed correctly in the viewer.
>
> Could someone look at my code and show me where I am wrong ?
>
> I specify that these features work fine :
> - display of the ShapeDiagram (the IFigure created by the
> DiagramEditPart is a red FreeformLayer)
> - addition and display of BeanBox from the palette (the IFigure created
> by the BeanBoxEditPart is a blue Rectangle). Move / Resize Policies work
> fine too.
> - addition of Shape isn't allowed if we point on a ShapeDiagram portion
> of the viewer.
>
>
> Thanks in advance
>
> --
> Arnaud
>
> Snippets : I put almost all the classes(in 3 parts : model, editparts,
> others), sorry for the lenght ! :
>
> 1) Model
> ------------------------------------------------------------ --------------
> public abstract class ModelElement {
>
> private static final String filePath = TestGUIBuilderPlugin.ID_IMAGE_DIR;
>
>
> protected static Image createImage(String name) {
> try {
> Image image = new Image(null, new FileInputStream(filePath + name));
> return image;
> } catch (FileNotFoundException e) {
> }
> return null;
> }
>
> /** Delegate used to implemenent property-change-support. */
> private PropertyChangeSupport pcsDelegate = new
PropertyChangeSupport(this);
>
> /**
> * Attach a non-null PropertyChangeListener to this object.
> * @param l a non-null PropertyChangeListener instance
> * @throws IllegalArgumentException if the parameter is null
> */
> public synchronized void
> addPropertyChangeListener(PropertyChangeListener l) {
> if (l == null) {
> throw new IllegalArgumentException();
> }
> pcsDelegate.addPropertyChangeListener(l);
> }
>
> /**
> * Report a property change to registered listeners (for example edit
> parts).
> * @param property the programmatic name of the property that changed
> * @param oldValue the old value of this property
> * @param newValue the new value of this property
> */
> protected void firePropertyChange(String property, Object oldValue,
> Object newValue) {
> if (pcsDelegate.hasListeners(property)) {
> pcsDelegate.firePropertyChange(property, oldValue, newValue);
> }
> }
>
>
> /**
> * Remove a PropertyChangeListener from this component.
> * @param l a PropertyChangeListener instance
> */
> public synchronized void
> removePropertyChangeListener(PropertyChangeListener l) {
> if (l != null) {
> pcsDelegate.removePropertyChangeListener(l);
> }
> }
>
> }
> ------------------------------------------------------------ --------------
> /**
> * This is the "root" of the model data structure.
> */
> public class ShapesDiagram extends ModelElement {
>
> /** Property ID to use when a child is added to this diagram. */
> public static final String CHILD_ADDED_PROP = "ShapesDiagram.ChildAdded";
> /** Property ID to use when a child is removed from this diagram. */
> public static final String CHILD_REMOVED_PROP =
> "ShapesDiagram.ChildRemoved";
>
> private static final long serialVersionUID = 1;
>
> private Collection beanboxlist = new Vector();
>
> /**
> * Add a beanbox to this diagram.
> * @param b a non-null beanbox instance
> * @return true, if the beanbox was added, false otherwise
> */
> public boolean addChild(BeanBox b) {
> if (b != null && beanboxlist.add(b)) {
> firePropertyChange(CHILD_ADDED_PROP, null, b);
> return true;
> }
> return false;
> }
>
> /** Return a List of BeanBox in this diagram. */
> public List getChildren() {
> return new Vector(beanboxlist);
> }
>
> /**
> * Remove a beanbox from this diagram.
> * @param b a non-null beanbox instance;
> * @return true, if the beanbox was removed, false otherwise
> */
> public boolean removeChild(BeanBox b) {
> if (b != null && beanboxlist.remove(b)) {
> firePropertyChange(CHILD_REMOVED_PROP, null, b);
> return true;
> }
> return false;
> }
> }
> ------------------------------------------------------------ --------------
> public class BeanBox extends ModelElement {
>
> /** Property ID to use when a child is added to this beanbox. */
> public static final String CHILD_ADDED_PROP = "BeanBox.ChildAdded";
> /** Property ID to use when a child is removed from this beanbox. */
> public static final String CHILD_REMOVED_PROP = "BeanBox.ChildRemoved";
>
> private Collection shapes = new Vector();
>
> /**
> * Add a shape to this beanbox.
> * @param s a non-null shape instance
> * @return true, if the shape was added, false otherwise
> */
> public boolean addChild(Shape s) {
> if (s != null && shapes.add(s)) {
> firePropertyChange(CHILD_ADDED_PROP, null, s);
> return true;
> }
> return false;
> }
>
> /** Return a List of Shapes in this BeanBox. */
> public List getChildren() {
> return new Vector(shapes);
> }
>
> /**
> * Remove a shape from this beanbox.
> * @param s a non-null shape instance;
> * @return true, if the shape was removed, false otherwise
> */
> public boolean removeChild(Shape s) {
> if (s != null && shapes.remove(s)) {
> firePropertyChange(CHILD_REMOVED_PROP, null, s);
> return true;
> }
> return false;
> }
>
>
> /** Location of this beanbox. */
> private Point location = new Point(0, 0);
> /** Size of this beanbox. */
> private Dimension size = new Dimension(50, 50);
>
> /** Property ID to use when the location of this beanbox is modified. */
> public static final String LOCATION_PROP = "BeanBox.Location";
> /** Property ID to use then the size of this beanbox is modified. */
> public static final String SIZE_PROP = "BeanBox.Size";
>
> private static final Image BEANBOX_ICON = createImage("ellipse16.gif");
>
>
> public Image getIcon() {
> return BEANBOX_ICON;
> }
>
> public String toString() {
> return "BeanBox " + hashCode();
> }
>
> /**
> * Return the Location of this beanbox.
> * @return a non-null location instance
> */
> public Point getLocation() {
> return location.getCopy();
> }
>
> /**
> * Return the Size of this beanbox.
> * @return a non-null Dimension instance
> */
> public Dimension getSize() {
> return size.getCopy();
> }
>
> /**
> * Set the Location of this beanbox.
> * @param newLocation a non-null Point instance
> * @throws IllegalArgumentException if the parameter is null
> */
> public void setLocation(Point newLocation) {
> if (newLocation == null) {
> throw new IllegalArgumentException();
> }
> location.setLocation(newLocation);
> firePropertyChange(LOCATION_PROP, null, location);
> }
>
> /**
> * Set the Size of this beanbox.
> * Will not modify the size if newSize is null.
> * @param newSize a non-null Dimension instance or null
> */
> public void setSize(Dimension newSize) {
> if (newSize != null) {
> size.setSize(newSize);
> firePropertyChange(SIZE_PROP, null, size);
> }
> }
>
>
> /**
> * Return a List of outgoing Connections.
> */
> public List getSourceConnections() {
> return null;
> }
>
> /**
> * Return a List of incoming Connections.
> */
> public List getTargetConnections() {
> return null;
> }
> }
>
> ------------------------------------------------------------ --------------
> public class Shape extends ModelElement {
>
> private static final String filePath = TestGUIBuilderPlugin.ID_IMAGE_DIR;
>
> /** Property ID to use when the location of this shape is modified. */
> public static final String LOCATION_PROP = "Shape.Location";
>
> /** Property ID to use then the size of this shape is modified. */
> public static final String SIZE_PROP = "Shape.Size";
>
> /** Location of this shape. */
> private Point location = new Point(0, 0);
> /** Size of this shape. */
> private Dimension size = new Dimension(10, 10);
>
> /** A 16x16 pictogram of an elliptical shape. */
> private static final Image SHAPE_ICON = createImage("ellipse16.gif");
>
> public Image getIcon() {
> return SHAPE_ICON;
> }
>
> public String toString() {
> return "Shape " + hashCode();
> }
>
> /**
> * Return the Location of this shape.
> * @return a non-null location instance
> */
> public Point getLocation() {
> return location.getCopy();
> }
>
> /**
> * Return the Size of this shape.
> * @return a non-null Dimension instance
> */
> public Dimension getSize() {
> return size.getCopy();
> }
>
> /**
> * Set the Location of this shape.
> * @param newLocation a non-null Point instance
> * @throws IllegalArgumentException if the parameter is null
> */
> public void setLocation(Point newLocation) {
> if (newLocation == null) {
> throw new IllegalArgumentException();
> }
> location.setLocation(newLocation);
> firePropertyChange(LOCATION_PROP, null, location);
> }
>
> /**
> * Set the Size of this shape.
> * Will not modify the size if newSize is null.
> * @param newSize a non-null Dimension instance or null
> */
> public void setSize(Dimension newSize) {
> if (newSize != null) {
> size.setSize(newSize);
> firePropertyChange(SIZE_PROP, null, size);
> }
> }
>
> /**
> * Return a List of outgoing Connections.
> */
> public List getSourceConnections() {
> return null;
> }
>
> /**
> * Return a List of incoming Connections.
> */
> public List getTargetConnections() {
> return null;
> }
> }
>
> ------------------------------------------------------------ --------------
> ------------------------------------------------------------ --------------
>
>
> 2) EditParts :
> ------------------------------------------------------------ --------------
> class DiagramEditPart extends AbstractGraphicalEditPart
> implements PropertyChangeListener {
>
> /**
> * 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() {
> // disallows the removal of this edit part from its parent
> installEditPolicy(EditPolicy.COMPONENT_ROLE, new
> RootComponentEditPolicy());
> // 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
> BeanBoxXYLayoutEditPolicy(layout));
> // disable selection feedback for this edit part
> installEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE, null);
> }
>
> /* (non-Javadoc)
> * @see
org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFi gure()
> */
> protected IFigure createFigure() {
> FreeformLayer f = new FreeformLayer();
> f.setLayoutManager(new FreeformLayout());
> f.setOpaque(true);
> f.setBackgroundColor(ColorConstants.red);
> return f;
> }
>
>
>
> private ShapesDiagram getCastedModel() {
> return (ShapesDiagram) getModel();
> }
>
> /* (non-Javadoc)
> * @see org.eclipse.gef.editparts.AbstractEditPart#getModelChildren( )
> */
> protected List getModelChildren() {
> return getCastedModel().getChildren(); // return a list of shapes
> }
>
> /* (non-Javadoc)
> * @see
>
java.beans.PropertyChangeListener#propertyChange(java.beans. PropertyChangeEv
ent)
> */
> public void propertyChange(PropertyChangeEvent evt) {
> String prop = evt.getPropertyName();
> // 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 (ShapesDiagram.CHILD_ADDED_PROP.equals(prop)
> || ShapesDiagram.CHILD_REMOVED_PROP.equals(prop)) {
> refreshChildren();
> }
> }
>
> /**
> * EditPolicy for the Figure used by this edit part.
> * Children of XYLayoutEditPolicy can be used in Figures with XYLayout.
> * @author Elias Volanakis
> */
> private class BeanBoxXYLayoutEditPolicy 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 DiagramEditPart#createEditPolicies()
> */
> BeanBoxXYLayoutEditPolicy(XYLayout layout) {
> if (layout == null) {
> throw new IllegalArgumentException();
> }
> setXyLayout(layout);
> }
>
> /* (non-Javadoc)
> * @see
>
org.eclipse.gef.editpolicies.ConstrainedLayoutEditPolicy#cre ateAddCommand(or
g.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 ateChangeConstra
intCommand(org.eclipse.gef.requests.ChangeBoundsRequest,
> org.eclipse.gef.EditPart, java.lang.Object)
> */
> protected Command createChangeConstraintCommand(ChangeBoundsRequest
request,
> EditPart child, Object constraint) {
> if (child instanceof BeanBoxEditPart && constraint instanceof Rectangle) {
> // return a command that can move and/or resize a Shape
> return new BeanBoxSetConstraintCommand(
> (BeanBox) child.getModel(), request, (Rectangle) constraint);
> }
> return super.createChangeConstraintCommand(request, child, constraint);
> }
>
> /* (non-Javadoc)
> * @see
>
org.eclipse.gef.editpolicies.ConstrainedLayoutEditPolicy#cre ateChangeConstra
intCommand(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.g
ef.requests.CreateRequest)
> */
> protected Command getCreateCommand(CreateRequest request) {
> Object childClass = request.getNewObjectType();
> if (childClass == BeanBox.class) {
> // return a command that can add a Shape to a ShapesDiagram
> return new BeanBoxCreateCommand(DiagramEditPart.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;
> }
> }
>
> }
>
> ------------------------------------------------------------ --------------
> class BeanBoxEditPart extends AbstractGraphicalEditPart
> implements PropertyChangeListener {
>
> /**
> * 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();
> f.setLayoutManager(new FreeformLayout());
> f.setOpaque(true);
> f.setBackgroundColor(ColorConstants.lightBlue);
> return f;
> }
>
>
> private BeanBox getCastedModel() {
> return (BeanBox) getModel();
> }
>
> /* (non-Javadoc)
> * @see
>
java.beans.PropertyChangeListener#propertyChange(java.beans. PropertyChangeEv
ent)
> */
> public void propertyChange(PropertyChangeEvent evt) {
> String prop = evt.getPropertyName();
> if (BeanBox.SIZE_PROP.equals(prop) || BeanBox.LOCATION_PROP.equals(prop))
{
> refreshVisuals();
> }
> }
>
> 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(or
g.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 ateChangeConstra
intCommand(org.eclipse.gef.requests.ChangeBoundsRequest,
> 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 ateChangeConstra
intCommand(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.g
ef.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;
> }
> }
>
> }
>
> ------------------------------------------------------------ --------------
> class ShapeEditPart extends AbstractGraphicalEditPart
> implements PropertyChangeListener {
>
> /**
> * 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());
> }
>
> /*(non-Javadoc)
> * @see
org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFi gure()
> */
> protected IFigure createFigure() {
> IFigure f = new RectangleFigure();
> f.setOpaque(true);
> f.setBackgroundColor(ColorConstants.lightGreen);
> return f;
> }
>
>
> private Shape getCastedModel() {
> return (Shape) getModel();
> }
>
> /* (non-Javadoc)
> * @see
>
java.beans.PropertyChangeListener#propertyChange(java.beans. PropertyChangeEv
ent)
> */
> public void propertyChange(PropertyChangeEvent evt) {
> String prop = evt.getPropertyName();
> if (Shape.SIZE_PROP.equals(prop) || Shape.LOCATION_PROP.equals(prop)) {
> refreshVisuals();
> }
> }
>
> 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);
> }
> }
> ------------------------------------------------------------ --------------
> ------------------------------------------------------------ --------------
>
> 3) Others
> ------------------------------------------------------------ --------------
> /**
> * Factory that maps model elements to edit parts.
> */
> public class ShapesEditPartFactory implements EditPartFactory {
>
> /*
> * (non-Javadoc)
> * @see
> org.eclipse.gef.EditPartFactory#createEditPart(org.eclipse.g ef.EditPart,
> java.lang.Object)
> */
> public EditPart createEditPart(EditPart context, Object modelElement) {
> // get EditPart for model element
> EditPart part = getPartForElement(modelElement);
> // store model element in EditPart
> part.setModel(modelElement);
> return part;
> }
>
> /**
> * Maps an object to an EditPart.
> * @throws RuntimeException if no match was found (programming error)
> */
> private EditPart getPartForElement(Object modelElement) {
> if (modelElement instanceof ShapesDiagram) {
> return new DiagramEditPart();
> }
> if (modelElement instanceof BeanBox) {
> return new BeanBoxEditPart();
> }
> if (modelElement instanceof Shape) {
> return new ShapeEditPart();
> }
> throw new RuntimeException(
> "Can't create part for model element: "
> + ((modelElement != null) ? modelElement.getClass().getName() : "null"));
> }
>
> }
> ------------------------------------------------------------ --------------
> /**
> * This edit policy enables the removal of a Shapes instance from its
> container.
> */
> class ShapeComponentEditPolicy extends ComponentEditPolicy {
>
> /* (non-Javadoc)
> * @see
>
org.eclipse.gef.editpolicies.ComponentEditPolicy#createDelet eCommand(org.ecl
ipse.gef.requests.GroupRequest)
> */
> protected Command createDeleteCommand(GroupRequest deleteRequest) {
> Object parent = getHost().getParent().getModel();
> Object child = getHost().getModel();
> if (parent instanceof BeanBox && child instanceof Shape) {
> return new ShapeDeleteCommand((BeanBox) parent, (Shape) child);
> }
> return super.createDeleteCommand(deleteRequest);
> }
> }
> ------------------------------------------------------------ --------------
> /**
> * A command to add a BeanBox to a ShapeDiagram.
> * The command can be undone or redone.
> */
> public class BeanBoxCreateCommand extends Command {
> /** The new beanbox. */
> private BeanBox newBeanBox;
>
> private final ShapesDiagram parent;
>
> /** A request to create a new BeanBox. */
> private final CreateRequest request;
> /** True, if newBeanBox was added to parent. */
> private boolean beanboxAdded;
>
> /**
> * Create a command that will add a new Beanbox to a ShapesDiagram.
> * @param parent the ShapesDiagram that will hold the new element
> * @param req a request to create a new BeanBox
> * @throws IllegalArgumentException if any parameter is null, or the
> request
> * does not provide a new BeanBox instance
> */
> public BeanBoxCreateCommand(ShapesDiagram parent, CreateRequest req) {
> if (parent == null || req == null || !(req.getNewObject() instanceof
> BeanBox)) {
> throw new IllegalArgumentException();
> }
> this.parent = parent;
> this.request = req;
> setLabel("beanbox creation");
> }
>
> /* (non-Javadoc)
> * @see org.eclipse.gef.commands.Command#canUndo()
> */
> public boolean canUndo() {
> return beanboxAdded;
> }
>
> /* (non-Javadoc)
> * @see org.eclipse.gef.commands.Command#execute()
> */
> public void execute() {
> // Obtain the new BeanBox instance from the request.
> // This causes the factory stored in the request to create a new instance.
> // The factory is supplied in the palette-tool-entry, see
> // TestGUIEditorPaletteFactory#createComponentsGroup()
> newBeanBox = (BeanBox) request.getNewObject();
> // Get desired location and size from the request
> newBeanBox.setSize(request.getSize()); // might be null!
> newBeanBox.setLocation(request.getLocation());
> redo();
> }
>
> /* (non-Javadoc)
> * @see org.eclipse.gef.commands.Command#redo()
> */
> public void redo() {
> beanboxAdded = parent.addChild(newBeanBox);
> }
>
> /* (non-Javadoc)
> * @see org.eclipse.gef.commands.Command#undo()
> */
> public void undo() {
> parent.removeChild(newBeanBox);
> }
>
> }
> ------------------------------------------------------------ --------------
> /**
> * A command to resize and/or move a beanbox.
> * The command can be undone or redone.
> */
> public class BeanBoxSetConstraintCommand extends Command {
> /** Stores the new size and location. */
> private final Rectangle newBounds;
> /** Stores the old size and location. */
> private Rectangle oldBounds;
> /** A request to move/resize an edit part. */
> private final ChangeBoundsRequest request;
>
> /** BeanBox to manipulate. */
> private final BeanBox beanbox;
>
> /**
> * Create a command that can resize and/or move a BeanBox.
> * @param beanbox the BeanBox to manipulate
> * @param req the move and resize request
> * @param newBounds the new size and location
> * @throws IllegalArgumentException if any of the parameters is null
> */
> public BeanBoxSetConstraintCommand(BeanBox beanbox, ChangeBoundsRequest
> req,
> Rectangle newBounds) {
> if (beanbox == null || req == null || newBounds == null) {
> throw new IllegalArgumentException();
> }
> this.beanbox = beanbox;
> this.request = req;
> this.newBounds = newBounds.getCopy();
> setLabel("move / resize");
> }
>
> /* (non-Javadoc)
> * @see org.eclipse.gef.commands.Command#canExecute()
> */
> public boolean canExecute() {
> Object type = request.getType();
> // make sure the Request is of a type we support:
> return (RequestConstants.REQ_MOVE.equals(type)
> || RequestConstants.REQ_MOVE_CHILDREN.equals(type)
> || RequestConstants.REQ_RESIZE.equals(type)
> || RequestConstants.REQ_RESIZE_CHILDREN.equals(type));
> }
>
> /* (non-Javadoc)
> * @see org.eclipse.gef.commands.Command#execute()
> */
> public void execute() {
> oldBounds = new Rectangle(beanbox.getLocation(), beanbox.getSize());
> redo();
> }
>
> /* (non-Javadoc)
> * @see org.eclipse.gef.commands.Command#redo()
> */
> public void redo() {
> beanbox.setSize(newBounds.getSize());
> beanbox.setLocation(newBounds.getLocation());
> }
>
> /* (non-Javadoc)
> * @see org.eclipse.gef.commands.Command#undo()
> */
> public void undo() {
> beanbox.setSize(oldBounds.getSize());
> beanbox.setLocation(oldBounds.getLocation());
> }
> }
>
> ------------------------------------------------------------ --------------
> /**
> * A command to add a Shape to a BeanBox.
> * The command can be undone or redone.
> */
> public class ShapeCreateCommand extends Command {
> /** The new shape. */
> private Shape newShape;
>
> /** ShapeDiagram to add to. */
> private final BeanBox parent;
> /** A request to create a new Shape. */
> private final CreateRequest request;
> /** True, if newShape was added to parent. */
> private boolean shapeAdded;
>
> /**
> * Create a command that will add a new Shape to a BeanBox.
> * @param parent the ShapesDiagram that will hold the new element
> * @param req a request to create a new Shape
> * @throws IllegalArgumentException if any parameter is null, or the
> request
> * does not provide a new Shape instance
> */
> public ShapeCreateCommand(BeanBox parent, CreateRequest req) {
> if (parent == null || req == null || !(req.getNewObject() instanceof
> Shape)) {
> throw new IllegalArgumentException();
> }
> this.parent = parent;
> this.request = req;
> setLabel("shape creation");
> }
>
> /* (non-Javadoc)
> * @see org.eclipse.gef.commands.Command#canUndo()
> */
> public boolean canUndo() {
> return shapeAdded;
> }
>
> /* (non-Javadoc)
> * @see org.eclipse.gef.commands.Command#execute()
> */
> public void execute() {
> // Obtain the new Shape instance from the request.
> // This causes the factory stored in the request to create a new instance.
> // The factory is supplied in the palette-tool-entry, see
> // ShapesEditorPaletteFactory#createComponentsGroup()
> newShape = (Shape) request.getNewObject();
> // Get desired location and size from the request
> newShape.setSize(request.getSize()); // might be null!
> newShape.setLocation(request.getLocation());
> redo();
> }
>
> /* (non-Javadoc)
> * @see org.eclipse.gef.commands.Command#redo()
> */
> public void redo() {
> shapeAdded = parent.addChild(newShape);
> }
>
> /* (non-Javadoc)
> * @see org.eclipse.gef.commands.Command#undo()
> */
> public void undo() {
> parent.removeChild(newShape);
> }
>
> }
>
> ------------------------------------------------------------ --------------
> /**
> * A command to remove a shape from its parent.
> * The command can be undone or redone.
> */
> public class ShapeDeleteCommand extends Command {
> /** Shape to remove. */
> private final Shape child;
>
> /** ShapeDiagram to remove from. */
> private final BeanBox parent;
>
> /** True, if child was removed from its parent. */
> private boolean wasRemoved;
>
> /**
> * Create a command that will remove the shape from its parent.
> * @param parent the ShapesDiagram containing the child
> * @param child the Shape to remove
> * @throws IllegalArgumentException if any parameter is null
> */
> public ShapeDeleteCommand(BeanBox parent, Shape child) {
> if (parent == null || child == null) {
> throw new IllegalArgumentException();
> }
> setLabel("shape deletion");
> this.parent = parent;
> this.child = child;
> }
>
>
> /* (non-Javadoc)
> * @see org.eclipse.gef.commands.Command#canUndo()
> */
> public boolean canUndo() {
> return wasRemoved;
> }
>
> /* (non-Javadoc)
> * @see org.eclipse.gef.commands.Command#execute()
> */
> public void execute() {
>
> redo();
> }
>
> /* (non-Javadoc)
> * @see org.eclipse.gef.commands.Command#redo()
> */
> public void redo() {
> // remove the child and disconnect its connections
> wasRemoved = parent.removeChild(child);
>
> }
>
>
>
> /* (non-Javadoc)
> * @see org.eclipse.gef.commands.Command#undo()
> */
> public void undo() {
> // add the child and reconnect its connections
> parent.addChild(child);
> }
> }
> ------------------------------------------------------------ --------------
> /**
> * A command to resize and/or move a shape.
> * The command can be undone or redone.
> */
> public class ShapeSetConstraintCommand extends Command {
> /** Stores the new size and location. */
> private final Rectangle newBounds;
> /** Stores the old size and location. */
> private Rectangle oldBounds;
> /** A request to move/resize an edit part. */
> private final ChangeBoundsRequest request;
>
> /** Shape to manipulate. */
> private final Shape shape;
>
> /**
> * Create a command that can resize and/or move a shape.
> * @param shape the shape to manipulate
> * @param req the move and resize request
> * @param newBounds the new size and location
> * @throws IllegalArgumentException if any of the parameters is null
> */
> public ShapeSetConstraintCommand(Shape shape, ChangeBoundsRequest req,
> Rectangle newBounds) {
> if (shape == null || req == null || newBounds == null) {
> throw new IllegalArgumentException();
> }
> this.shape = shape;
> this.request = req;
> this.newBounds = newBounds.getCopy();
> setLabel("move / resize");
> }
>
> /* (non-Javadoc)
> * @see org.eclipse.gef.commands.Command#canExecute()
> */
> public boolean canExecute() {
> Object type = request.getType();
> // make sure the Request is of a type we support:
> return (RequestConstants.REQ_MOVE.equals(type)
> || RequestConstants.REQ_MOVE_CHILDREN.equals(type)
> || RequestConstants.REQ_RESIZE.equals(type)
> || RequestConstants.REQ_RESIZE_CHILDREN.equals(type));
> }
>
> /* (non-Javadoc)
> * @see org.eclipse.gef.commands.Command#execute()
> */
> public void execute() {
> oldBounds = new Rectangle(shape.getLocation(), shape.getSize());
> redo();
> }
>
> /* (non-Javadoc)
> * @see org.eclipse.gef.commands.Command#redo()
> */
> public void redo() {
> shape.setSize(newBounds.getSize());
> shape.setLocation(newBounds.getLocation());
> }
>
> /* (non-Javadoc)
> * @see org.eclipse.gef.commands.Command#undo()
> */
> public void undo() {
> shape.setSize(oldBounds.getSize());
> shape.setLocation(oldBounds.getLocation());
> }
> }
>
> ------------------------------------------------------------ --------------
|
|
|
Re: Figure not displaying in viewer ? [message #166816 is a reply to message #166749] |
Thu, 03 February 2005 09:58  |
Eclipse User |
|
|
|
Originally posted by: no.mail.please
Perfect !!
I indeed forgot to override getModelChildren() and I also forgot to
catch CHILD_ADDED property in propertyChange :
if (BeanBox.CHILD_ADDED_PROP.equals(prop)
|| BeanBox.CHILD_REMOVED_PROP.equals(prop)) {
refreshChildren();
}
That works OK
Thanks again
--
Arnaud
|
|
|
Goto Forum:
Current Time: Thu Jun 12 22:05:38 EDT 2025
Powered by FUDForum. Page generated in 0.07169 seconds
|