Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3e10f32134671a4a261cd7a266d6c28d6586e32a (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*******************************************************************************
 * 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.viewer;

import java.util.List;

import org.eclipse.gef.EditPart;
import org.w3c.dom.Node;
import org.w3c.dom.Text;

/**
 * @author mengbo
 */
public class DesignPosition {
	public static final DesignPosition INVALID = new DesignPosition(null, -1);

	private EditPart _containerPart;

	int _offset;

	//private Node _containerNode;

	/**
	 * @param part
	 * @param offset
	 */
	public DesignPosition(EditPart part, int offset) {
		_containerPart = part;
		_offset = offset;
	}

	/**
	 * if _containerPart is null, means it is invalid
	 * 
	 * @return
	 */
	public EditPart getContainerPart() {
		return _containerPart;
	}

	public Node getContainerNode() {
		if (_containerPart != null) {
			return (Node) _containerPart.getModel();
		}
        return null;
	}

	/**
	 * if offset < 0, means it is invalid.
	 * 
	 * @return
	 */
	public int getOffset() {
		return _offset;
	}

	public boolean isValid() {
		return (_containerPart != null) && (_offset >= 0);
	}

	/**
	 * This method should not be called when is text node.
	 * 
	 * @param forward
	 * @return
	 */
	public EditPart getSiblingEditPart(boolean forward) {
		if (!isValid()) {
			return null;
		}

		int index = forward ? (_offset) : (_offset - 1);
		List children = _containerPart.getChildren();

		if ((index >= children.size()) || (index < 0)) {
			return null;
		}

		return (EditPart) children.get(index);
	}

	/**
	 * factory method
	 * 
	 * @param part
	 * @return
	 */
	public static DesignPosition createPositionBeforePart(EditPart part) {
		EditPart parent = part.getParent();

		if (parent == null) {
			return new DesignPosition(part, 0);
		}
        return new DesignPosition(parent, parent.getChildren()
        		.indexOf(part));
	}

	/**
	 * factory method
	 * 
	 * @param part
	 * @return
	 */
	public static DesignPosition createPositionAfterPart(EditPart part) {
		EditPart parent = part.getParent();

		if (parent == null) {
			return new DesignPosition(part, part.getChildren().size());
		}
        return new DesignPosition(parent, parent.getChildren()
        		.indexOf(part) + 1);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object obj) {
		if (obj instanceof DesignPosition) {
			DesignPosition p = (DesignPosition) obj;

			return (p.getContainerPart() == this._containerPart)
					&& (p.getOffset() == this._offset);
		}

		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	public StringBuffer debugDump(StringBuffer buffer) {
//		try {
			buffer.append("DesignPosition: ").append(this._containerPart)
					.append(": ").append(this._offset).append("\n");

			if (this._containerPart.getModel() instanceof Text) {
				// skip
			} else {
				if (this._offset > 0) {
					buffer.append("after: ").append(
							this._containerPart.getChildren().get(
									this._offset - 1)).append("\n");
				}

				if (this._offset < (this._containerPart.getChildren().size() - 1)) {
					buffer.append("before: ")
							.append(
									this._containerPart.getChildren().get(
											this._offset)).append("\n");
				}
			}
            //TODO: what was being caught here?
//		} catch (Exception e) {
//		}

		return buffer;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		StringBuffer buffer = new StringBuffer();

		return debugDump(buffer).toString();
	}
}

Back to the top