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

UiDocumentEditPart

public class UiDocumentEditPart extends UiElementEditPart
Graphical edit part for the root document.

It acts as a simple container.

Fields Summary
private org.eclipse.swt.widgets.Display
mDisplay
private org.eclipse.draw2d.FreeformLayer
mLayer
private ImageBackground
mImage
private org.eclipse.draw2d.Label
mChild
Constructors Summary
public UiDocumentEditPart(com.android.ide.eclipse.editors.uimodel.UiDocumentNode uiDocumentNode, org.eclipse.swt.widgets.Display display)

        super(uiDocumentNode);
        mDisplay = display;
    
Methods Summary
protected voidcreateEditPolicies()

        super.createEditPolicies();

        // This policy indicates this a root component that cannot be removed
        installEditPolicy(EditPolicy.COMPONENT_ROLE, new RootComponentEditPolicy());

        installLayoutEditPolicy(this);
    
protected org.eclipse.draw2d.IFigurecreateFigure()

        mLayer = new FreeformLayer();
        mLayer.setLayoutManager(new FreeformLayout());
        
        mLayer.setOpaque(true);
        mLayer.setBackgroundColor(ColorConstants.lightGray);
        
        return mLayer;
    
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.

For the root document, we want the first child edit part to the be the target since an XML document can have only one root element. {@inheritDoc}

        if (request != null && request.getType() == RequestConstants.REQ_CREATE) {
            // We refuse the drop if it's not in the bounds of the document.
            if (request instanceof DropRequest) {
                Point where = ((DropRequest) request).getLocation().getCopy();
                UiElementNode uiNode = getUiNode();
                if (uiNode instanceof UiDocumentNode) {
                    // Take the bounds of the background image as the valid drop zone
                    Object editData = uiNode.getEditData();
                    if (editData instanceof BufferedImage) {
                        BufferedImage image = (BufferedImage)editData;
                        int w = image.getWidth();
                        int h = image.getHeight();
                        if (where.x > w || where.y > h) {
                            return null;
                        }
                    }
                    
                }
            }

            // For the root document, we want the first child edit part to the be the target
            // since an XML document can have only one root element.
            if (getChildren().size() > 0) {
                Object o = getChildren().get(0);
                if (o instanceof EditPart) {
                    return ((EditPart) o).getTargetEditPart(request);
                }
            }
        }
        return super.getTargetEditPart(request);
    
protected voidhideSelection()

        // no selection at this level.
    
protected voidrefreshVisuals()

        UiElementNode model = (UiElementNode)getModel();
        
        Object editData = model.getEditData();
        if (editData instanceof BufferedImage) {
            BufferedImage image = (BufferedImage)editData;
            
            if (mImage == null || image != mImage.getBufferedImage()) {
                mImage = new ImageBackground(image, mDisplay);
            }
            
            mLayer.setBorder(mImage);
            
            if (mChild != null && mChild.getParent() == mLayer) {
                mLayer.remove(mChild);
            }
        } else if (editData instanceof String) {
            mLayer.setBorder(null);
            if (mChild == null) {
                mChild = new Label();
            }
            mChild.setText((String)editData);

            if (mChild != null && mChild.getParent() != mLayer) {
                mLayer.add(mChild);
            }
            Rectangle bounds = mChild.getTextBounds();
            bounds.x = bounds.y = 0;
            mLayer.setConstraint(mChild, bounds);
        }

        // refresh the children as well
        refreshChildrenVisuals();
    
protected voidshowSelection()

        // no selection at this level.