Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c610eb8f07b0969431c4dde1b43fd5c645223547 (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
187
188
189
190
/*******************************************************************************
 * 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.css2.layout;

import java.util.List;

import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.jst.pagedesigner.PDPlugin;

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

	// static final boolean SHOW_BASELINE = true;

	/**
	 * Constructs a new FlowFigure.
	 */
	public FlowFigure() {
		// setLayoutManager(createDefaultFlowLayout());
	}

	/**
	 * If the child is a <code>FlowFigure</code>, its FlowContext is passed
	 * to it.
	 * 
	 * @see org.eclipse.draw2d.IFigure#add(IFigure, Object, int)
	 */
	public void add(IFigure child, Object constraint, int index) {
		super.add(child, constraint, index);
		if (child instanceof FlowFigure) {
			FlowFigure ff = (FlowFigure) child;
			if (getLayoutManager() instanceof FlowContext) {
				ff.setOriginalFlowContext((FlowContext) getLayoutManager());
			} else {
				PDPlugin.getLogger(this.getClass()).error("layout is not FlowContext", new Throwable("This exception is artificial so  we can get a stack trace"));
			}
		}
	}

	/**
	 * Creates the default layout manager
	 * 
	 * @return The default layout
	 */
	// protected abstract FlowFigureLayout createDefaultFlowLayout();
	/**
	 * @see Figure#paintFigure(Graphics)
	 */
	protected void paintFigure(Graphics g) {
		super.paintFigure(g);
		// g.drawRectangle(getBounds().getResized(-1,-1));
	}

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

	/**
	 * FlowFigures override setBounds() to prevent translation of children.
	 * "bounds" is a derived property for FlowFigures, calculated from the
	 * fragments that make up the FlowFigure.
	 * 
	 * @see Figure#setBounds(Rectangle)
	 */
	public void setBounds(Rectangle r) {
		if (getBounds().equals(r))
			return;
		erase();
		bounds.x = r.x;
		bounds.y = r.y;
		bounds.width = r.width;
		bounds.height = r.height;
		fireFigureMoved();
		repaint();
	}

	/**
	 * Sets the flow context.
	 * 
	 * @param flowContext
	 *            the flow context for this flow figure
	 */
	public void setOriginalFlowContext(FlowContext flowContext) {
		((FlowFigureLayout) getLayoutManager())
				.setOriginalFlowContext(flowContext);
	}

	public void setDisplayString(String s) {
		_displayString = s;
	}

	public String toString() {
		if (_displayString == null)
        {
			return super.toString();
        }
        return _displayString + " " + getClass().getName();
	}

	String _displayString; // for debug

	/**
	 * @return
	 */
	public FlowContext getFlowContext() {
		return ((FlowFigureLayout) getLayoutManager()).getFlowContext();
	}

	// ----------------------------------------------------------------------
	// as absolute positioning and relative positioning may have children
	// out-side
	// of parent bounds, so we want to disable clipping when drawing figures
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.draw2d.Figure#paintChildren(org.eclipse.draw2d.Graphics)
	 */
	protected void paintChildren(Graphics graphics) {
		IFigure child;

		Rectangle clip = Rectangle.SINGLETON;
		List children = this.getChildren();
		for (int i = 0; i < children.size(); i++) {
			child = (IFigure) children.get(i);
			if (child.isVisible() && child.intersects(graphics.getClip(clip))) {
				// graphics.clipRect(child.getBounds());
				child.paint(graphics);
				graphics.restoreState();
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.draw2d.Figure#paintClientArea(org.eclipse.draw2d.Graphics)
	 */
	protected void paintClientArea(Graphics graphics) {
		if (this.getChildren().isEmpty())
			return;

		// boolean optimizeClip = getBorder() == null || getBorder().isOpaque();

		if (useLocalCoordinates()) {
			graphics.translate(getBounds().x + getInsets().left, getBounds().y
					+ getInsets().top);
			// if (!optimizeClip)
			// graphics.clipRect(getClientArea(PRIVATE_RECT));
			graphics.pushState();
			paintChildren(graphics);
			graphics.popState();
			graphics.restoreState();
		} else {
			// if (optimizeClip)
			paintChildren(graphics);
			// else {
			// graphics.clipRect(getClientArea(PRIVATE_RECT));
			// graphics.pushState();
			// paintChildren(graphics);
			// graphics.popState();
			// graphics.restoreState();
			// }
		}

	}
}

Back to the top