2 * Copyright (C) 2008 The Android Open Source Project
4 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.eclipse.org/org/documents/epl-v10.php
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 package com.android.ide.eclipse.adt.internal.editors.layout.parts;
20 import com.android.ide.eclipse.adt.internal.editors.AndroidXmlEditor;
21 import com.android.ide.eclipse.adt.internal.editors.descriptors.ElementDescriptor;
22 import com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditor;
23 import com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditor.UiEditorActions;
24 import com.android.ide.eclipse.adt.internal.editors.uimodel.UiElementNode;
26 import org.eclipse.draw2d.geometry.Point;
27 import org.eclipse.gef.commands.Command;
30 * A command that knows how to instantiate a new element based on a given {@link ElementDescriptor},
31 * the parent {@link UiElementEditPart} and an optional target location.
35 public class ElementCreateCommand extends Command {
37 /** Descriptor of the new element to create */
38 private final ElementDescriptor mDescriptor;
39 /** The edit part that hosts the new edit part */
40 private final UiElementEditPart mParentPart;
41 /** The drop location in parent coordinates */
42 private final Point mTargetPoint;
45 * Creates a new {@link ElementCreateCommand}.
47 * @param descriptor Descriptor of the new element to create
48 * @param targetPart The edit part that hosts the new edit part
49 * @param targetPoint The drop location in parent coordinates
51 public ElementCreateCommand(ElementDescriptor descriptor,
52 UiElementEditPart targetPart, Point targetPoint) {
53 mDescriptor = descriptor;
54 mParentPart = targetPart;
55 mTargetPoint = targetPoint;
58 // --- Methods inherited from Command ---
61 public boolean canExecute() {
62 return mDescriptor != null &&
63 mParentPart != null &&
64 mParentPart.getUiNode() != null &&
65 mParentPart.getUiNode().getEditor() instanceof LayoutEditor;
69 public void execute() {
71 UiElementNode uiParent = mParentPart.getUiNode();
72 if (uiParent != null) {
73 final AndroidXmlEditor editor = uiParent.getEditor();
74 if (editor instanceof LayoutEditor) {
75 ((LayoutEditor) editor).wrapUndoRecording(
76 String.format("Create %1$s", mDescriptor.getXmlLocalName()),
79 UiEditorActions actions = ((LayoutEditor) editor).getUiEditorActions();
80 if (actions != null) {
81 DropFeedback.addElementToXml(mParentPart, mDescriptor, mTargetPoint,
92 throw new UnsupportedOperationException("redo not supported by this command"); //$NON-NLS-1$
97 throw new UnsupportedOperationException("undo not supported by this command"); //$NON-NLS-1$