FileDocCategorySizeDatePackage
UiElementEditPart.javaAPI DocAndroid 1.5 API12415Wed May 06 22:41:10 BST 2009com.android.ide.eclipse.editors.layout.parts

UiElementEditPart

public abstract class UiElementEditPart extends org.eclipse.gef.editparts.AbstractGraphicalEditPart implements com.android.ide.eclipse.editors.uimodel.IUiUpdateListener
An {@link EditPart} for a {@link UiElementNode}.

Fields Summary
Constructors Summary
public UiElementEditPart(com.android.ide.eclipse.editors.uimodel.UiElementNode uiElementNode)

        setModel(uiElementNode);
    
Methods Summary
public voidactivate()

        super.activate();
        getUiNode().addUpdateListener(this);
    
protected voidcreateEditPolicies()

        /*
         * This is no longer needed, as a selection edit policy is set by the parent layout.
         * Leave this code commented out right now, I'll want to play with this later.
         * 
        installEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE,
                new NonResizableSelectionEditPolicy(this));
         */
    
public voiddeactivate()

        super.deactivate();
        getUiNode().removeUpdateListener(this);
    
protected final org.eclipse.draw2d.geometry.RectanglegetBounds()

        UiElementNode model = (UiElementNode)getModel();
        
        Object editData = model.getEditData();

        if (editData != null) {
            // assert with fully qualified class name to prevent import changes to another
            // Rectangle class.
            assert (editData instanceof org.eclipse.draw2d.geometry.Rectangle);
    
            return (Rectangle)editData;
        }

        // return a dummy rect
        return new Rectangle(0, 0, 0, 0);
    
protected final com.android.ide.eclipse.editors.descriptors.ElementDescriptorgetDescriptor()

        return getUiNode().getDescriptor();
    
public org.eclipse.gef.DragTrackergetDragTracker(org.eclipse.gef.Request request)

        return new SelectEditPartTracker(this);
    
protected final com.android.ide.eclipse.editors.layout.parts.UiElementEditPartgetEditPartParent()

        EditPart parent = getParent();
        if (parent instanceof UiElementEditPart) {
            return (UiElementEditPart)parent; 
        }
        return null;
    
protected java.util.ListgetModelChildren()

        return getUiNode().getUiChildren();
    
protected final java.lang.StringgetStringAttr(java.lang.String attrName)
Returns a given XML attribute.

param
attrName The local name of the attribute.
return
the attribute as a {@link String}, if it exists, or null

        UiElementNode uiNode = getUiNode();
        if (uiNode.getXmlNode() != null) {
            Node xmlNode = uiNode.getXmlNode();
            if (xmlNode != null) {
                NamedNodeMap nodeAttributes = xmlNode.getAttributes();
                if (nodeAttributes != null) {
                    Node attr = nodeAttributes.getNamedItemNS(
                            SdkConstants.NS_RESOURCES, attrName);
                    if (attr != null) {
                        return attr.getNodeValue();
                    }
                }
            }
        }
        return null;
    
public org.eclipse.gef.EditPartgetTargetEditPart(org.eclipse.gef.Request request)
Returns the EditPart that should be used as the target for the specified Request.

For instance this is called during drag'n'drop with a CreateRequest.

Reject being a target for elements which descriptor does not allow children. {@inheritDoc}

        if (request != null && request.getType() == RequestConstants.REQ_CREATE) {
            // Reject being a target for elements which descriptor does not allow children.
            if (!getUiNode().getDescriptor().hasChildren()) {
                return null;
            }
        }
        return super.getTargetEditPart(request);
    
public final com.android.ide.eclipse.editors.uimodel.UiElementNodegetUiNode()

return
The object model casted to an {@link UiElementNode}

        return (UiElementNode) getModel();
    
protected abstract voidhideSelection()

protected voidinstallLayoutEditPolicy(com.android.ide.eclipse.editors.layout.parts.UiElementEditPart layoutEditPart)
Used by derived classes {@link UiDocumentEditPart} and {@link UiLayoutEditPart} to accept drag'n'drop of new items from the palette.

param
layoutEditPart The layout edit part where this policy is installed. It can be either a {@link UiDocumentEditPart} or a {@link UiLayoutEditPart}.

        // This policy indicates how elements can be constrained by the layout.
        // TODO Right now we use the XY layout policy since our constraints are
        // handled by the android rendering engine rather than GEF. Tweak as
        // appropriate.
        installEditPolicy(EditPolicy.LAYOUT_ROLE,  new LayoutEditPolicy() {

            /**
             * We don't allow layout children to be resized yet.
             * <p/>
             * Typical choices would be:
             * <ul>
             * <li> ResizableEditPolicy, to allow for selection, move and resize.
             * <li> NonResizableEditPolicy, to allow for selection, move but not resize.
             * <li> SelectionEditPolicy to allow for only selection.
             * </ul>
             * <p/>
             * TODO: make this depend on the part layout. For an AbsoluteLayout we should
             * probably use a NonResizableEditPolicy and SelectionEditPolicy for the rest.
             * Whether to use ResizableEditPolicy or NonResizableEditPolicy should depend
             * on the child in an AbsoluteLayout.
             */
            @Override
            protected EditPolicy createChildEditPolicy(EditPart child) {
                if (child instanceof UiElementEditPart) {
                    return new NonResizableSelectionEditPolicy((UiElementEditPart) child);
                }
                return null;
            }

            @Override
            protected Command getCreateCommand(CreateRequest request) {
                // We store the ElementDescriptor in the request.factory.type
                Object newType = request.getNewObjectType();
                if (newType instanceof ElementDescriptor) {
                    Point where = request.getLocation().getCopy();
                    Point origin = getLayoutContainer().getClientArea().getLocation();
                    where.translate(origin.getNegated());
                    
                    // The host is the EditPart where this policy is installed,
                    // e.g. this UiElementEditPart.
                    EditPart host = getHost();
                    if (host instanceof UiElementEditPart) {
                        
                        return new ElementCreateCommand((ElementDescriptor) newType,
                                (UiElementEditPart) host,
                                where);
                    }
                }
                
                return null;
            }

            @Override
            protected Command getMoveChildrenCommand(Request request) {
                // TODO Auto-generated method stub
                return null;
            }
            
            @Override
            public void showLayoutTargetFeedback(Request request) {
                super.showLayoutTargetFeedback(request);
                
                // for debugging
                // System.out.println("target: " + request.toString() + " -- " + layoutEditPart.getUiNode().getBreadcrumbTrailDescription(false));
                
                if (layoutEditPart instanceof UiLayoutEditPart &&
                        request instanceof DropRequest) {
                    Point where = ((DropRequest) request).getLocation().getCopy();
                    Point origin = getLayoutContainer().getClientArea().getLocation();
                    where.translate(origin.getNegated());

                    ((UiLayoutEditPart) layoutEditPart).showDropTarget(where);
                }
            }

            @Override
            protected void eraseLayoutTargetFeedback(Request request) {
                super.eraseLayoutTargetFeedback(request);
                if (layoutEditPart instanceof UiLayoutEditPart) {
                    ((UiLayoutEditPart) layoutEditPart).hideDropTarget();
                }
            }
            
            @Override
            protected IFigure createSizeOnDropFeedback(CreateRequest createRequest) {
                // TODO understand if this is useful for us or remove
                return super.createSizeOnDropFeedback(createRequest);
            }
            
        });
    
protected voidrefreshChildrenVisuals()

        if (children != null) {
            for (Object child : children) {
                if (child instanceof UiElementEditPart) {
                    UiElementEditPart childPart = (UiElementEditPart)child;
                    childPart.refreshVisuals();
                }
            }
        }
    
protected voidrefreshVisuals()

        if (getFigure().getParent() != null) {
            ((GraphicalEditPart) getParent()).setLayoutConstraint(this, getFigure(), getBounds());
        }
        
        // update the visuals of the children as well
        refreshChildrenVisuals();
    
protected abstract voidshowSelection()

public voiduiElementNodeUpdated(com.android.ide.eclipse.editors.uimodel.UiElementNode ui_node, UiUpdateState state)

        // TODO: optimize by refreshing only when needed
        switch(state) {
        case ATTR_UPDATED:
            refreshVisuals();
            break;
        case CHILDREN_CHANGED:
            refreshChildren();
            
            // new children list, need to update the layout
            refreshVisuals();
            break;
        case CREATED:
            refreshVisuals();
            break;
        case DELETED:
            // pass
            break;
        }