Skip to main content
summaryrefslogtreecommitdiffstats
blob: e19bb0a09db31e0b2ad7ed445dcfe865b50daee3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.pagedesigner.commands;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jst.pagedesigner.dom.IDOMPosition;
import org.eclipse.jst.pagedesigner.editors.palette.IDropSourceData;
import org.eclipse.jst.pagedesigner.utils.CommandUtil;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;
import org.w3c.dom.Element;

/**
 * @author mengbo
 */
public class CreateItemCommand extends DesignerCommand implements ICustomizableCommand 
{
    private final IDOMPosition _position;
    private final IDropSourceData _creationProvider;
    private Element _ele;
    private IAdaptable _customizationData;

    /**
     * @param label
     * @param model
     * @param position
     * @param creationProvider
     */
    public CreateItemCommand(String label, IDOMModel model,
            IDOMPosition position, IDropSourceData creationProvider) {
        super(label, model.getDocument());
        this._position = position;
        this._creationProvider = creationProvider;
    }

    /**
     * @return the dom position for the drop
     */
    public IDOMPosition getPosition()
    {
        return _position;
    }


    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jst.pagedesigner.commands.DesignerCommand#doExecute()
     */
    protected void doExecute() 
    {
        Element element = CommandUtil.executeInsertion(
                _creationProvider,
                getModel(), this._position, this._customizationData);
        if (element != null) 
        {
            formatNode(element);
        }
        this._ele = element;
    }

    @Override
    protected void postPostExecute() 
    {
        // during JUnit testing, we may not have viewer.
        // this will cause us not to have undo support,
        // but should not effect testing for this command
        if (getViewer() != null)
        {
            super.postPostExecute();
        }
    }

    @Override
    protected boolean prePreExecute() 
    {
        // during JUnit testing, we may not have viewer.
        // this will cause us not to have undo support,
        // but should not effect testing for this command
        if (getViewer() != null)
        {
            return super.prePreExecute();
        }
        
        return true;
    }

    /*
     * (non-Javadoc)
     * 
     * @seeorg.eclipse.jst.pagedesigner.commands.DesignerCommand#
     * getAfterCommandDesignerSelection()
     */
    protected ISelection getAfterCommandDesignerSelection() {
        return toDesignSelection(_ele);
    }

    /**
     * @param customizationData
     */
    public void setCustomizationData(IAdaptable customizationData) 
    {
        _customizationData = customizationData;
    }
    
    /**
     * This method is for test purposes and should generally not be 
     * used by clients.
     * 
     * @return the customization data
     */
    protected final IAdaptable getCustomizationData()
    {
        return _customizationData;
    }
    
    /**
     * @return the result of the command execution
     * TODO: add Object getResult() method to DesignerCommand
     */
    protected Element getResult()
    {
        return this._ele;
    }
}

Back to the top