OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / sdk / eclipse / plugins / com.android.ide.eclipse.adt / src / com / android / ide / eclipse / adt / internal / editors / layout / parts / ElementCreateCommand.java
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
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
7  *
8  *      http://www.eclipse.org/org/documents/epl-v10.php
9  *
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.
15  */
16
17
18 package com.android.ide.eclipse.adt.internal.editors.layout.parts;
19
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;
25
26 import org.eclipse.draw2d.geometry.Point;
27 import org.eclipse.gef.commands.Command;
28
29 /**
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.
32  *
33  * @since GLE1
34  */
35 public class ElementCreateCommand extends Command {
36
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;
43
44     /**
45      * Creates a new {@link ElementCreateCommand}.
46      *
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
50      */
51     public ElementCreateCommand(ElementDescriptor descriptor,
52             UiElementEditPart targetPart, Point targetPoint) {
53                 mDescriptor = descriptor;
54                 mParentPart = targetPart;
55                 mTargetPoint = targetPoint;
56     }
57
58     // --- Methods inherited from Command ---
59
60     @Override
61     public boolean canExecute() {
62         return mDescriptor != null &&
63             mParentPart != null &&
64             mParentPart.getUiNode() != null &&
65             mParentPart.getUiNode().getEditor() instanceof LayoutEditor;
66     }
67
68     @Override
69     public void execute() {
70         super.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()),
77                         new Runnable() {
78                     public void run() {
79                         UiEditorActions actions = ((LayoutEditor) editor).getUiEditorActions();
80                         if (actions != null) {
81                             DropFeedback.addElementToXml(mParentPart, mDescriptor, mTargetPoint,
82                                     actions);
83                         }
84                     }
85                 });
86             }
87         }
88     }
89
90     @Override
91     public void redo() {
92         throw new UnsupportedOperationException("redo not supported by this command"); //$NON-NLS-1$
93     }
94
95     @Override
96     public void undo() {
97         throw new UnsupportedOperationException("undo not supported by this command"); //$NON-NLS-1$
98     }
99
100 }