Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0a805da702b7b77d6fff65c317c4fb5c6de02a25 (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
143
144
145
146
147
148
/*******************************************************************************
 * 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.editpolicies;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.UnexecutableCommand;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gef.requests.DropRequest;
import org.eclipse.jst.pagedesigner.commands.CloneNodeCommand;
import org.eclipse.jst.pagedesigner.commands.MoveNodeCommand;
import org.eclipse.jst.pagedesigner.dom.DOMPositionHelper;
import org.eclipse.jst.pagedesigner.dom.DOMUtil;
import org.eclipse.jst.pagedesigner.dom.IDOMPosition;
import org.eclipse.jst.pagedesigner.parts.NodeEditPart;
import org.eclipse.jst.pagedesigner.validation.caret.ActionData;
import org.eclipse.jst.pagedesigner.validation.caret.DnDPositionValidator;
import org.eclipse.jst.pagedesigner.validation.caret.DropActionData;
import org.eclipse.jst.pagedesigner.validation.caret.IPositionMediator;
import org.eclipse.jst.pagedesigner.validation.caret.DropActionData.DropData;
import org.eclipse.jst.pagedesigner.viewer.DesignPosition;
import org.eclipse.jst.pagedesigner.viewer.IDropLocationStrategy;
import org.eclipse.jst.pagedesigner.viewer.IHTMLGraphicalViewer;
import org.w3c.dom.Node;

/**
 * @author mengbo
 * @version 1.5
 */
public class DragMoveEditPolicy extends DropEditPolicy
{
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editpolicies.AbstractEditPolicy#getCommand(org.eclipse.gef.Request)
	 */
	public final Command getCommand(Request request) {
		if (!(request instanceof ChangeBoundsRequest)) {
			return null;
		}

		ChangeBoundsRequest r = (ChangeBoundsRequest) request;

		// we only support move/copy a single node.
		if (!MoveSupport.isSingleNode(r)) {
			return UnexecutableCommand.INSTANCE;
		}

		// the edit policy only handle at the target part, so only care about
		// the
		// target part request.
		final Object type = r.getType();
		if (type != REQ_ADD && type != REQ_CLONE && type != REQ_MOVE_CHILDREN) {

			return null;
		}

		Node draggedNode = MoveSupport.getDraggedNode(r);
		Node hostNode = ((NodeEditPart) getHost()).getIDOMNode();

		if (DOMUtil.isAncester(draggedNode, hostNode)) {
			return UnexecutableCommand.INSTANCE;
		}

		DesignPosition position = findPosition(r);
		if (position == null || !position.isValid()) {
			return null;
		}

		// can't move/copy into self.
		Node node = position.getContainerNode();
		if (DOMUtil.isAncester(draggedNode, node)) {
			return UnexecutableCommand.INSTANCE;
		}

		// ok, we are about to move/copy into the specified position.
		IDOMPosition domposition = DOMPositionHelper.toDOMPosition(position);

		if (REQ_CLONE.equals(type)) {
			return new CloneNodeCommand((IHTMLGraphicalViewer) getHost()
					.getViewer(), domposition, draggedNode);
		}
        return new MoveNodeCommand((IHTMLGraphicalViewer) getHost()
        		.getViewer(), domposition, draggedNode);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editpolicies.AbstractEditPolicy#getTargetEditPart(org.eclipse.gef.Request)
	 */
	public EditPart getTargetEditPart(Request request) {
		if (request instanceof ChangeBoundsRequest) {
			return this.getHost();
		}
		return super.getTargetEditPart(request);
	}

	protected final DesignPosition findPosition(DropRequest r) {
		final IPositionMediator mediator = getDropChildValidator(r);
        if (mediator == null)
        {
            return null;
        }
        final IDropLocationStrategy dropStrategy = createDropLocationStrategy(r);
		final DesignPosition position = 
            dropStrategy.calculateDesignPosition(getHost(), r.getLocation(), mediator);
        
        // verify that the drop strategy has honoured it's contract that our
        // mediator be respected
        if (position != null)
        {
            if (!mediator.isValidPosition(position))
            {
                // if our mediator says no go, then veto the requestor
                // there is no drop location
                return null;
            }
        }
		return position;
	}

    public void showTargetFeedback(Request request) 
    {
        Object type = request.getType();
        // only show feedback for these request types
        if (type == REQ_ADD || type == REQ_CLONE
                || type == REQ_MOVE_CHILDREN || type == REQ_MOVE) {
            super.showTargetFeedback(request);
        }
    }

    protected final IPositionMediator createDefaultDropChildValidator(DropData r)
    {
        return new DnDPositionValidator(new DropActionData(
                ActionData.COMPONENT_MOVE, r));
    }
}

Back to the top