Skip to main content
summaryrefslogtreecommitdiffstats
blob: 70bf749b02bd5e677a7c2d1471f3709cbe0a52f4 (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
/*******************************************************************************
 * 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.validation.caret;

import org.eclipse.gef.EditPart;
import org.eclipse.jst.pagedesigner.dom.DOMRefPosition;
import org.eclipse.jst.pagedesigner.dom.IDOMPosition;
import org.eclipse.jst.pagedesigner.parts.NodeEditPart;

/**
 * @author mengbo
 */
public class DefaultPositionRule implements IPositionRule {

	protected IPositionMediator _mediator;

	protected ActionData _actionData;

	/**
	 * 
	 */
	public DefaultPositionRule(IPositionMediator mediator, ActionData actionData) {
		super();
		_mediator = mediator;
		if (_actionData != null) {
			_actionData = actionData;
		} else {
			_actionData = new ActionData(ActionData.UNKNOWN, new Object());
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.pagedesigner.caret.IPositionRule#hasEditableArea(org.w3c.dom.Node)
	 */
	public boolean hasEditableArea(Target target) {
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.pagedesigner.caret.IPositionRule#isEditable(org.eclipse.gef.EditPart)
	 */
	public boolean isEditable(Target target) {
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.pagedesigner.caret.IPositionRule#isTransparent(org.eclipse.gef.EditPart,
	 *      org.eclipse.draw2d.geometry.Point)
	 */
	public boolean canReference(Target target, boolean atRight) {
		return true;
	}

	/**
	 * If container is inEditable and can be referenced, the position is
	 * invalid, otherwise as default the position is valid. (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.pagedesigner.caret.IPositionRule#isValidPosition(org.eclipse.jst.pagedesigner.dom.IDOMPosition)
	 */
	public boolean isValidPosition(IDOMPosition position) {
		boolean result = isEditable(new Target(position.getContainerNode()));
		if (result) {
			if (position.getOffset() == 0
					|| position.getOffset() == position.getContainerNode()
							.getChildNodes().getLength()) {
				result = true;
			} else {
				boolean dir;
				Target target = null;
				if (position instanceof DOMRefPosition) {
					target = new Target(((DOMRefPosition) position)
							.getReferenceNode());
					dir = ((DOMRefPosition) position).isForward();
					result = canReference(target, dir);
				}
			}
		}
		return result;
	}

	/**
	 * @param host
	 * @param tagName
	 * @return
	 */
	public static boolean isWidget(EditPart host) {
		if (host instanceof NodeEditPart) {
			return ((NodeEditPart) host).isWidget();
		} else {
			return false;
		}
	}
}

Back to the top