Project

General

Profile

Download (6.13 KB) Statistics
| Branch: | Tag: | Revision:
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.draw2d.text;
12

    
13
import java.util.Iterator;
14

    
15
import org.eclipse.draw2d.Figure;
16
import org.eclipse.draw2d.IFigure;
17
import org.eclipse.draw2d.geometry.Rectangle;
18

    
19
/**
20
 * The base implementation for text flow figures. A flow figure is used to
21
 * render a document in which elements are laid out horizontally within a "line"
22
 * until that line is filled. Layout continues on the next line.
23
 * 
24
 * <p>
25
 * WARNING: This class is not intended to be subclassed by clients. Future
26
 * versions may contain additional abstract methods.
27
 * 
28
 * @author hudsonr
29
 * @since 2.1
30
 */
31
public abstract class FlowFigure extends Figure {
32

    
33
	/**
34
	 * integer indicating whether selection should be displayed.
35
	 */
36
	protected int selectionStart = -1;
37

    
38
	/**
39
	 * Constructs a new FlowFigure.
40
	 */
41
	public FlowFigure() {
42
		setLayoutManager(createDefaultFlowLayout());
43
	}
44

    
45
	/**
46
	 * If the child is a <code>FlowFigure</code>, its FlowContext is passed to
47
	 * it.
48
	 * 
49
	 * @see org.eclipse.draw2d.IFigure#add(IFigure, Object, int)
50
	 */
51
	public void add(IFigure child, Object constraint, int index) {
52
		super.add(child, constraint, index);
53
		// If this layout manager is a FlowContext, then the child *must* be a
54
		// FlowFigure
55
		if (getLayoutManager() instanceof FlowContext)
56
			((FlowFigure) child)
57
					.setFlowContext((FlowContext) getLayoutManager());
58
		revalidateBidi(this);
59
	}
60

    
61
	/**
62
	 * Calculates the width of text before the next line-break is encountered.
63
	 * <p>
64
	 * Default implementation treats each FlowFigure as a line-break. It adds no
65
	 * width and returns <code>true</code>. Sub-classes should override as
66
	 * needed.
67
	 * 
68
	 * @param width
69
	 *            the width before the next line-break (if one's found; all the
70
	 *            width, otherwise) will be added on to the first int in the
71
	 *            given array
72
	 * @return boolean indicating whether or not a line-break was found
73
	 * @since 3.1
74
	 */
75
	public boolean addLeadingWordRequirements(int[] width) {
76
		return true;
77
	}
78

    
79
	/**
80
	 * FlowFigures can contribute text for their block to the given
81
	 * {@link BidiProcessor}, which will process the contributions to determine
82
	 * Bidi levels and shaping requirements.
83
	 * <p>
84
	 * This method is invoked as part of validating Bidi.
85
	 * <p>
86
	 * Sub-classes that cache the BidiInfo and/or the bidi level in ContentBoxes
87
	 * should clear the cached values when this method is invoked.
88
	 * 
89
	 * @param proc
90
	 *            the BidiProcessor to which contributions should be made
91
	 * @see BidiProcessor#add(FlowFigure, String)
92
	 * @since 3.1
93
	 */
94
	protected void contributeBidi(BidiProcessor proc) {
95
		for (Iterator iter = getChildren().iterator(); iter.hasNext();)
96
			((FlowFigure) iter.next()).contributeBidi(proc);
97
	}
98

    
99
	/**
100
	 * Creates the default layout manager
101
	 * 
102
	 * @return The default layout
103
	 */
104
	protected abstract FlowFigureLayout createDefaultFlowLayout();
105

    
106
	/**
107
	 * Called after validate has occurred. This is used to update the bounds of
108
	 * the FlowFigure to encompass its new flow boxed created during validate.
109
	 */
110
	public abstract void postValidate();
111

    
112
	/**
113
	 * Overridden to revalidateBidi when fragments are removed.
114
	 * 
115
	 * @see org.eclipse.draw2d.IFigure#remove(org.eclipse.draw2d.IFigure)
116
	 */
117
	public void remove(IFigure figure) {
118
		super.remove(figure);
119
		revalidateBidi(this);
120
	}
121

    
122
	/**
123
	 * This method should be invoked whenever a change that can potentially
124
	 * affect the Bidi evaluation is made (eg., adding or removing children,
125
	 * changing text, etc.).
126
	 * <p>
127
	 * The default implementation delegates the revalidation task to the parent.
128
	 * Only {@link BlockFlow#revalidateBidi(IFigure) blocks} perform the actual
129
	 * revalidation.
130
	 * <p>
131
	 * The given IFigure is the one that triggered the revalidation. This can be
132
	 * used to optimize bidi evaluation.
133
	 * 
134
	 * @param origin
135
	 *            the figure that was revalidated
136
	 * @since 3.1
137
	 */
138
	protected void revalidateBidi(IFigure origin) {
139
		if (getParent() != null)
140
			((FlowFigure) getParent()).revalidateBidi(origin);
141
	}
142

    
143
	/**
144
	 * Sets the bidi information for this figure. A flow figure contributes bidi
145
	 * text in {@link #contributeBidi(BidiProcessor)}. If the figure contributes
146
	 * text associated with it, this method is called back to indicate the bidi
147
	 * properties for that text within its block.
148
	 * 
149
	 * @param info
150
	 *            the BidiInfo for this figure
151
	 * @since 3.1
152
	 */
153
	public void setBidiInfo(BidiInfo info) {
154
	}
155

    
156
	/**
157
	 * FlowFigures override setBounds() to prevent translation of children.
158
	 * "bounds" is a derived property for FlowFigures, calculated from the
159
	 * fragments that make up the FlowFigure.
160
	 * 
161
	 * @see Figure#setBounds(Rectangle)
162
	 */
163
	public void setBounds(Rectangle r) {
164
		if (bounds.equals(r))
165
			return;
166
		if (!r.contains(bounds))
167
			erase();
168
		bounds.x = r.x;
169
		bounds.y = r.y;
170
		bounds.width = r.width;
171
		bounds.height = r.height;
172
		fireFigureMoved();
173
		if (isCoordinateSystem())
174
			fireCoordinateSystemChanged();
175
		repaint();
176
	}
177

    
178
	/**
179
	 * Sets the flow context.
180
	 * 
181
	 * @param flowContext
182
	 *            the flow context for this flow figure
183
	 */
184
	public void setFlowContext(FlowContext flowContext) {
185
		((FlowFigureLayout) getLayoutManager()).setFlowContext(flowContext);
186
	}
187

    
188
	/**
189
	 * Sets the selection or a range of selection. A start value of -1 is used
190
	 * to indicate no selection. A start value >=0 indicates show selection. A
191
	 * start and end value can be used to represent a range of offsets which
192
	 * should render selection.
193
	 * 
194
	 * @param start
195
	 *            the start offset
196
	 * @param end
197
	 *            the end offset
198
	 * @since 3.1
199
	 */
200
	public void setSelection(int start, int end) {
201
		if (selectionStart == start)
202
			return;
203
		selectionStart = start;
204
		repaint();
205
	}
206

    
207
}
(16-16/31)