Skip to main content
summaryrefslogtreecommitdiffstats
blob: 41fe7f1045da1e72f6a6d9d2b7d74782bea71b72 (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
135
136
137
138
139
140
141
142
/*******************************************************************************
 * Copyright (c) 2006, 2008 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.core.runtime.IStatus;
import org.eclipse.jst.jsf.common.ui.internal.logging.Logger;
import org.eclipse.jst.pagedesigner.PDPlugin;
import org.eclipse.jst.pagedesigner.dnd.internal.SourceViewerDragDropHelper;
import org.eclipse.jst.pagedesigner.dom.DOMPosition;
import org.eclipse.jst.pagedesigner.dom.EditModelQuery;
import org.eclipse.jst.pagedesigner.dom.IDOMPosition;
import org.eclipse.jst.pagedesigner.editors.palette.IDropSourceData;
import org.eclipse.jst.pagedesigner.itemcreation.customizer.DropCustomizationController;
import org.eclipse.jst.pagedesigner.utils.CommandUtil;
import org.eclipse.wst.sse.ui.StructuredTextEditor;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * Handles tag creation when dropped onto the WPE source view
 * 
 * @author mengbo
 */
public class PaletteDropInsertCommand extends SourceViewerCommand implements ICustomizableCommand
{

	private final Logger _log = PDPlugin
			.getLogger(PaletteDropInsertCommand.class);

	private IDropSourceData _creationProvider;

	private int _location;

	private Element _element;

	private IAdaptable _customizationData;

	/**
	 * @param label
	 * @param editor
	 * @param creationProvider
	 * @param location
	 */
	public PaletteDropInsertCommand(String label, StructuredTextEditor editor,
			IDropSourceData creationProvider, int location) {
		super(label, editor);
		_creationProvider = creationProvider;
		_location = location;
	}

	public void doExecute() {
		Node node;
		try {
			node = getSourceEditingTextTools().getNode(_location);
		} catch (Exception e) {
			_log.error("Bad text insertion location", e);		 //$NON-NLS-1$
			return;
		}
		IDOMPosition position = null;
		if (node != null) {
			position = SourceViewerDragDropHelper.getInstance()
					.findPosition(_location, node);
		} else {
			if (getModel().getDocument().getFirstChild() != null) {
				//Fix for 224541 - When palette item is dropped to end of file in source view of Web Page Editor, the item is inserted at the top of file
				//add inside at end of necessary container
				position = SourceViewerDragDropHelper.getInstance()
					.findPosition(getModel().getDocument().getEndOffset(), getModel().getDocument().getFirstChild());
			} 
			else {
				// empty file
				position = new DOMPosition(getModel().getDocument(), 0);
			}
		}

        // essentially copied from ItemCreationTool so that DesignView drop and
        // SourceViewDrop are same.
        // Note that SourceView does NO drop validation checking. This is
        // handled by ItemCreationPolicy in DesignView
        final IStatus status = performCustomization(getModel().getDocument(),
                position);

        if (status.isOK()) 
        {
            final Element element = CommandUtil.executeInsertion(
                    _creationProvider, getModel()
                    , position, getCustomizationData());
            if (element != null) {
            	//Bug 330412 - [WPE] Drag 'n' Drop of tag from palette doesn't notify team system of edit
            	notifyTeamFrameworkOfEdit();
                formatNode(element);
            }
            this._element = element;
        }
    }

	/**
     * @param domDoc 
	 * @param position 
     * @return status
	 */
	protected IStatus performCustomization(final IDOMDocument domDoc, final IDOMPosition position) {
		return new DropCustomizationController(this, _creationProvider, domDoc, position).performCustomization();
	}

    /**
	 * @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;
	}
	
	public void setSelection() {
		if (_element != null) {
			int offset = EditModelQuery.getNodeStartIndex(_element);
			int length = EditModelQuery.getNodeEndIndex(_element) - offset;
			_editor.getTextViewer().setSelectedRange(offset, length);
		}
	}
	
}

Back to the top