Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 48dd34aef6b12dcab9bab5c70d4cf898974affbc (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
/*******************************************************************************
 * Copyright (c) 2004, 2008 John Krasnay 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:
 *     John Krasnay - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.xml.vex.core.internal.layout;

import org.eclipse.wst.xml.vex.core.internal.core.Caret;
import org.eclipse.wst.xml.vex.core.internal.core.FontMetrics;
import org.eclipse.wst.xml.vex.core.internal.core.FontResource;
import org.eclipse.wst.xml.vex.core.internal.core.Graphics;
import org.eclipse.wst.xml.vex.core.internal.css.Styles;
import org.eclipse.wst.xml.vex.core.internal.dom.Element;

/**
 * A zero-width box that represents a single offset in the document.
 */
public class PlaceholderBox extends AbstractInlineBox {

	private Element element;
	private int relOffset;
	private int textTop;
	private int baseline;

	/**
	 * Class constructor.
	 * 
	 * @param context
	 *            LayoutContext in effect.
	 * @param element2
	 *            Element containing this placeholder. the element is used both
	 *            to determine the size of the box and its caret, but also as a
	 *            base point for relOffset.
	 * @param relOffset
	 *            Offset of the placeholder, relative to the start of the
	 *            element.
	 */
	public PlaceholderBox(LayoutContext context, Element element2, int relOffset) {

		this.element = element2;
		this.relOffset = relOffset;

		this.setWidth(0);

		Graphics g = context.getGraphics();
		Styles styles = context.getStyleSheet().getStyles(element2);
		FontResource font = g.createFont(styles.getFont());
		FontResource oldFont = g.setFont(font);
		FontMetrics fm = g.getFontMetrics();
		int height = fm.getAscent() + fm.getDescent();

		int lineHeight = styles.getLineHeight();
		this.textTop = (lineHeight - height) / 2;

		this.baseline = this.textTop + fm.getAscent();
		this.setHeight(lineHeight);
		g.setFont(oldFont);
		font.dispose();
	}

	/**
	 * @see org.eclipse.wst.xml.vex.core.internal.layout.InlineBox#getBaseline()
	 */
	public int getBaseline() {
		return this.baseline;
	}

	/**
	 * @see org.eclipse.wst.xml.vex.core.internal.layout.InlineBox#split(org.eclipse.wst.xml.vex.core.internal.layout.LayoutContext,
	 *      int, boolean)
	 */
	public Pair split(LayoutContext context, int maxWidth, boolean force) {
		return new Pair(null, this);
	}

	/**
	 * @see org.eclipse.wst.xml.vex.core.internal.layout.Box#getCaret(org.eclipse.wst.xml.vex.core.internal.layout.LayoutContext,
	 *      int)
	 */
	public Caret getCaret(LayoutContext context, int offset) {
		return new TextCaret(0, this.textTop, this.baseline - this.textTop);
	}

	/**
	 * @see org.eclipse.wst.xml.vex.core.internal.layout.Box#getElement()
	 */
	public Element getElement() {
		return this.element;
	}

	/**
	 * @see org.eclipse.wst.xml.vex.core.internal.layout.Box#getEndOffset()
	 */
	public int getEndOffset() {
		return this.element.getStartOffset() + this.relOffset;
	}

	/**
	 * @see org.eclipse.wst.xml.vex.core.internal.layout.Box#getStartOffset()
	 */
	public int getStartOffset() {
		return this.element.getStartOffset() + this.relOffset;
	}

	/**
	 * @see org.eclipse.wst.xml.vex.core.internal.layout.Box#hasContent()
	 */
	public boolean hasContent() {
		return true;
	}

	public boolean isEOL() {
		return false;
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return "[placeholder(" + this.getStartOffset() + ")]";
	}

	/**
	 * @see org.eclipse.wst.xml.vex.core.internal.layout.Box#viewToModel(org.eclipse.wst.xml.vex.core.internal.layout.LayoutContext,
	 *      int, int)
	 */
	public int viewToModel(LayoutContext context, int x, int y) {
		return this.getStartOffset();
	}

}

Back to the top