CopyCutActionpublic class CopyCutAction extends org.eclipse.jface.action.Action Provides Cut and Copy actions for the tree nodes. |
Fields Summary |
---|
private List | mUiNodes | private boolean | mPerformCut | private final com.android.ide.eclipse.editors.AndroidEditor | mEditor | private final org.eclipse.swt.dnd.Clipboard | mClipboard | private final ICommitXml | mXmlCommit |
Constructors Summary |
---|
public CopyCutAction(com.android.ide.eclipse.editors.AndroidEditor editor, org.eclipse.swt.dnd.Clipboard clipboard, ICommitXml xmlCommit, com.android.ide.eclipse.editors.uimodel.UiElementNode selected, boolean perform_cut)Creates a new Copy or Cut action.
this(editor, clipboard, xmlCommit, toList(selected), perform_cut);
| public CopyCutAction(com.android.ide.eclipse.editors.AndroidEditor editor, org.eclipse.swt.dnd.Clipboard clipboard, ICommitXml xmlCommit, List selected, boolean perform_cut)Creates a new Copy or Cut action.
super(perform_cut ? "Cut" : "Copy");
mEditor = editor;
mClipboard = clipboard;
mXmlCommit = xmlCommit;
ISharedImages images = PlatformUI.getWorkbench().getSharedImages();
if (perform_cut) {
setImageDescriptor(images.getImageDescriptor(ISharedImages.IMG_TOOL_CUT));
setHoverImageDescriptor(images.getImageDescriptor(ISharedImages.IMG_TOOL_CUT));
setDisabledImageDescriptor(
images.getImageDescriptor(ISharedImages.IMG_TOOL_CUT_DISABLED));
} else {
setImageDescriptor(images.getImageDescriptor(ISharedImages.IMG_TOOL_COPY));
setHoverImageDescriptor(images.getImageDescriptor(ISharedImages.IMG_TOOL_COPY));
setDisabledImageDescriptor(
images.getImageDescriptor(ISharedImages.IMG_TOOL_COPY_DISABLED));
}
mUiNodes = selected;
mPerformCut = perform_cut;
|
Methods Summary |
---|
private java.lang.String | getXmlTextFromEditor(org.w3c.dom.Node xml_node)Get the data directly from the editor.
String data = null;
IStructuredModel model = mEditor.getModelForRead();
try {
IStructuredDocument sse_doc = mEditor.getStructuredDocument();
if (xml_node instanceof NodeContainer) {
// The easy way to get the source of an SSE XML node.
data = ((NodeContainer) xml_node).getSource();
} else if (xml_node instanceof IndexedRegion && sse_doc != null) {
// Try harder.
IndexedRegion region = (IndexedRegion) xml_node;
int start = region.getStartOffset();
int end = region.getEndOffset();
if (end > start) {
data = sse_doc.get(start, end - start);
}
}
} catch (BadLocationException e) {
// the region offset was invalid. ignore.
} finally {
model.releaseFromRead();
}
return data;
| private java.lang.String | getXmlTextFromSerialization(org.w3c.dom.Node xml_node)Direct XML serialization of the XML node.
This uses the generic Node interface with no SSE tricks. It's however slower
and doesn't respect formatting (since serialization is involved instead of reading
the actual text buffer.)
String data;
StringWriter sw = new StringWriter();
XMLSerializer serializer = new XMLSerializer(sw,
new OutputFormat(Method.XML,
OutputFormat.Defaults.Encoding /* utf-8 */,
true /* indent */));
// Serialize will throw an IOException if it fails.
serializer.serialize((Element) xml_node);
data = sw.toString();
return data;
| public void | run()Performs the cut or copy action.
First an XML serializer is used to turn the existing XML node into a valid
XML fragment, which is added as text to the clipboard.
super.run();
if (mUiNodes == null || mUiNodes.size() < 1) {
return;
}
// Commit the current pages first, to make sure the XML is in sync.
// Committing may change the XML structure.
if (mXmlCommit != null) {
mXmlCommit.commitPendingXmlChanges();
}
StringBuilder allText = new StringBuilder();
ArrayList<UiElementNode> nodesToCut = mPerformCut ? new ArrayList<UiElementNode>() : null;
for (UiElementNode uiNode : mUiNodes) {
try {
Node xml_node = uiNode.getXmlNode();
if (xml_node == null) {
return;
}
String data = getXmlTextFromEditor(xml_node);
// In the unlikely event that IStructuredDocument failed to extract the text
// directly from the editor, try to fall back on a direct XML serialization
// of the XML node. This uses the generic Node interface with no SSE tricks.
if (data == null) {
data = getXmlTextFromSerialization(xml_node);
}
if (data != null) {
allText.append(data);
if (mPerformCut) {
// only remove notes to cut if we actually got some XML text from them
nodesToCut.add(uiNode);
}
}
} catch (Exception e) {
AdtPlugin.log(e, "CopyCutAction failed for UI node %1$s", //$NON-NLS-1$
uiNode.getBreadcrumbTrailDescription(true));
}
} // for uiNode
if (allText != null && allText.length() > 0) {
mClipboard.setContents(
new Object[] { allText.toString() },
new Transfer[] { TextTransfer.getInstance() });
if (mPerformCut) {
for (UiElementNode uiNode : nodesToCut) {
uiNode.deleteXmlNode();
}
}
}
| private static java.util.ArrayList | toList(com.android.ide.eclipse.editors.uimodel.UiElementNode selected)Static helper class to wrap on node into a list for the constructors.
ArrayList<UiElementNode> list = null;
if (selected != null) {
list = new ArrayList<UiElementNode>(1);
list.add(selected);
}
return list;
|
|