FileDocCategorySizeDatePackage
PasteAction.javaAPI DocAndroid 1.5 API5677Wed May 06 22:41:10 BST 2009com.android.ide.eclipse.editors.ui.tree

PasteAction

public class PasteAction extends org.eclipse.jface.action.Action
Provides Paste operation for the tree nodes

Fields Summary
private com.android.ide.eclipse.editors.uimodel.UiElementNode
mUiNode
private final com.android.ide.eclipse.editors.AndroidEditor
mEditor
private final org.eclipse.swt.dnd.Clipboard
mClipboard
Constructors Summary
public PasteAction(com.android.ide.eclipse.editors.AndroidEditor editor, org.eclipse.swt.dnd.Clipboard clipboard, com.android.ide.eclipse.editors.uimodel.UiElementNode ui_node)

        super("Paste");
        mEditor = editor;
        mClipboard = clipboard;

        ISharedImages images = PlatformUI.getWorkbench().getSharedImages();
        setImageDescriptor(images.getImageDescriptor(ISharedImages.IMG_TOOL_PASTE));
        setHoverImageDescriptor(images.getImageDescriptor(ISharedImages.IMG_TOOL_PASTE));
        setDisabledImageDescriptor(
                images.getImageDescriptor(ISharedImages.IMG_TOOL_PASTE_DISABLED));

        mUiNode = ui_node;
    
Methods Summary
public voidrun()
Performs the paste operation.

        super.run();
        
        final String data = (String) mClipboard.getContents(TextTransfer.getInstance());
        if (data != null) {
            IStructuredModel model = mEditor.getModelForEdit();
            try {
                IStructuredDocument sse_doc = mEditor.getStructuredDocument();
                if (sse_doc != null) {
                    if (mUiNode.getDescriptor().hasChildren()) {
                        // This UI Node can have children. The new XML is
                        // inserted as the first child.
                        
                        if (mUiNode.getUiChildren().size() > 0) {
                            // There's already at least one child, so insert right before it.
                            Node xml_node = mUiNode.getUiChildren().get(0).getXmlNode();
                            if (xml_node instanceof IndexedRegion) { // implies xml_node != null
                                IndexedRegion region = (IndexedRegion) xml_node;
                                sse_doc.replace(region.getStartOffset(), 0, data);
                                return; // we're done, no need to try the other cases
                            }                                
                        }
                        
                        // If there's no first XML node child. Create one by
                        // inserting at the end of the *start* tag.
                        Node xml_node = mUiNode.getXmlNode();
                        if (xml_node instanceof NodeContainer) {
                            NodeContainer container = (NodeContainer) xml_node;
                            IStructuredDocumentRegion start_tag =
                                container.getStartStructuredDocumentRegion();
                            if (start_tag != null) {
                                sse_doc.replace(start_tag.getEndOffset(), 0, data);
                                return; // we're done, no need to try the other case
                            }
                        }
                    }
                    
                    // This UI Node doesn't accept children. The new XML is inserted as the
                    // next sibling. This also serves as a fallback if all the previous
                    // attempts failed. However, this is not possible if the current node
                    // has for parent a document -- an XML document can only have one root,
                    // with no siblings.
                    if (!(mUiNode.getUiParent() instanceof UiDocumentNode)) {
                        Node xml_node = mUiNode.getXmlNode();
                        if (xml_node instanceof IndexedRegion) {
                            IndexedRegion region = (IndexedRegion) xml_node;
                            sse_doc.replace(region.getEndOffset(), 0, data);
                        }
                    }
                }

            } catch (BadLocationException e) {
                AdtPlugin.log(e, "ParseAction failed for UI Node %2$s, content '%1$s'", //$NON-NLS-1$
                        mUiNode.getBreadcrumbTrailDescription(true), data);
            } finally {
                model.releaseFromEdit();
            }
        }