Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4535c584b70a06efe7d76012146b09ea0704e09e (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
183
184
185
186
/*******************************************************************************
 * 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.parts;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.SharedCursors;
import org.eclipse.jst.pagedesigner.css2.ICSSStyle;
import org.eclipse.jst.pagedesigner.css2.layout.CSSTextFigure;
import org.eclipse.jst.pagedesigner.css2.layout.ICSSFigure;
import org.eclipse.jst.pagedesigner.css2.property.ICSSPropertyID;
import org.eclipse.jst.pagedesigner.css2.provider.ICSSTextProvider;
import org.eclipse.jst.pagedesigner.css2.style.DefaultStyle;
import org.eclipse.jst.pagedesigner.editpolicies.LinkEditPolicy;
import org.eclipse.jst.pagedesigner.range.RangeUtil;
import org.eclipse.jst.pagedesigner.utils.HTMLUtil;
import org.eclipse.jst.pagedesigner.viewer.DesignRange;
import org.eclipse.jst.pagedesigner.viewer.IHTMLGraphicalViewer;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.wst.sse.core.internal.provisional.INodeNotifier;
import org.w3c.dom.Node;
import org.w3c.dom.Text;

/**
 * @author mengbo
 */
public class TextEditPart extends SubNodeEditPart implements ICSSTextProvider {
	private String _cachedData;

	private Text _textNode;

	private Text _textNodeForFigure;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.EditPart#setModel(java.lang.Object)
	 */
	public void setModel(Object model) {
		super.setModel(model);
		_textNode = (Text) model;
		_cachedData = _textNode.getData();
		_textNodeForFigure = getDestDocumentForDesign().createTextNode(
				_cachedData);
	}

	protected IFigure createFigure() {
		// XXX: currently creating of CSSTextFigure is distributed both here
		// and FigureFactory. We may want to unify them later.
		return new CSSTextFigure(this);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
	 */
	protected void createEditPolicies() {
		super.createEditPolicies();
		this.installEditPolicy("link editpolicy", new LinkEditPolicy()); //$NON-NLS-1$
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.pagedesigner.parts.SubNodeEditPart#getNodeForFigure()
	 */
	public Node getNodeForFigure() {
		return _textNodeForFigure;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ibm.sse.model.INodeAdapter#notifyChanged(com.ibm.sse.model.INodeNotifier,
	 *      int, java.lang.Object, java.lang.Object, java.lang.Object, int)
	 */
	public void notifyChanged(INodeNotifier notifier, int eventType,
			Object changedFeature, Object oldValue, Object newValue, int pos) {
		_cachedData = _textNode.getData();
		_textNodeForFigure.setData(_cachedData);
		if (eventType == INodeNotifier.CHANGE) {
			getFigure().revalidate();
		} else {
			// XXX: been removed? parent EditPart should have handled it.
		}
	}


	/**
	 * @return the associated css style for this text node
	 */
	public ICSSStyle getCSSStyle() {
		IFigure figure1 = this.getFigure();
		if (figure1 instanceof ICSSFigure) {
			ICSSStyle style = ((ICSSFigure) figure1).getCSSStyle();
			if (style != null) {
				return style;
			}
		}
		return DefaultStyle.getInstance();
	}

	/**
	 * As when text are displayed in HTML, they are "normalized". For example,
	 * leading whitespace may be removed dure to previous node as trailing
	 * whitespace. Entity reference may have be resolved. Sequence whitespace
	 * been merged.
	 * 
	 * It is also possible that the text node is in "PRE" mode, in that case the
	 * above things are not done.
	 * 
	 * This method return the really value that is going to be presented to
	 * user. EditPartPosition's offset is referencing this value.
	 * 
	 * @return the text data
	 * @see org.eclipse.jst.pagedesigner.viewer.DesignPosition
	 */
	public String getTextData() {
		ICSSStyle style = getCSSStyle();
		String data = _cachedData;
		if (style.getStyleProperty(ICSSPropertyID.ATTR_WHITESPACE) != ICSSPropertyID.VAL_PRE) {
			return HTMLUtil.compactWhitespaces(_textNode, data);
		}
        return data;
	}

	/**
	 * check what part of this text node is in the range selection.
	 * 
	 * @return text node ranges
	 */
	public int[] getSelectedRange() {
		IHTMLGraphicalViewer viewer = (IHTMLGraphicalViewer) this.getViewer();
		if (viewer == null || !viewer.isInRangeMode()) {
			return null;
		}
		DesignRange range = viewer.getRangeSelection();
		if (range == null || !range.isValid()) {
			return null;
		}
		if (!RangeUtil.intersect(range, this)) {
			return null;
		}
		// ok, we intersect with the range.
		range = RangeUtil.normalize(range);
		EditPart startContainer = range.getStartPosition().getContainerPart();
		EditPart endContainer = range.getEndPosition().getContainerPart();
		int[] ret = new int[2];
		if (startContainer != this) {
			ret[0] = 0;
		} else {
			ret[0] = range.getStartPosition().getOffset();
		}
		if (endContainer != this) {
			ret[1] = this.getTextData().length();
		} else {
			{
				ret[1] = range.getEndPosition().getOffset();
			}
		}
		return ret;
	}

	public boolean isSelectable() {
        // controls, amongst other things, whether or not a standard 
        // hit test in SelectionTool for mouse over will find this edit part 
		return true;
	}

    public Cursor getCursor(Point mouseLocation) {
        return SharedCursors.IBEAM;
    }
    
    
}

Back to the top