comparison org.eclipse.draw2d/src/org/eclipse/draw2d/text/FlowPage.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children dbfb303e8fb0
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
1 /*******************************************************************************
2 * Copyright (c) 2000, 2005 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 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module org.eclipse.draw2d.text.FlowPage;
14
15 import java.lang.all;
16
17 import org.eclipse.draw2d.geometry.Dimension;
18 import org.eclipse.draw2d.geometry.Rectangle;
19 import org.eclipse.draw2d.text.BlockFlow;
20 import org.eclipse.draw2d.text.FlowFigureLayout;
21 import org.eclipse.draw2d.text.PageFlowLayout;
22 import org.eclipse.draw2d.text.FlowFigure;
23
24 /**
25 * The root of a Flow hierarchy. A flow page can be treated as a normal figure, but
26 * contains FlowFigures.
27 * <P>
28 * A FlowPage will not have a defined width unless it is inside a figure whose layout
29 * provides width hints when calling
30 * {@link org.eclipse.draw2d.IFigure#getPreferredSize(int, int)}.
31 *
32 * <P>WARNING: This class is not intended to be subclassed by clients.
33 */
34 public class FlowPage
35 : BlockFlow
36 {
37 alias BlockFlow.add add;
38
39 private Dimension pageSize;
40 private int recommendedWidth;
41 private int[] pageSizeCacheKeys;
42 private Dimension[] pageSizeCacheValues;
43
44 public this(){
45 pageSize = new Dimension();
46 pageSizeCacheKeys = new int[3];
47 pageSizeCacheValues = new Dimension[3];
48 }
49 /**
50 * @see org.eclipse.draw2d.Figure#addNotify()
51 */
52 public void addNotify() {
53 super.addNotify();
54 setValid(false);
55 }
56
57 /**
58 * @see org.eclipse.draw2d.text.BlockFlow#createDefaultFlowLayout()
59 */
60 protected FlowFigureLayout createDefaultFlowLayout() {
61 return new PageFlowLayout(this);
62 }
63
64 /**
65 * @see org.eclipse.draw2d.Figure#getMinimumSize(int, int)
66 */
67 public Dimension getMinimumSize(int w, int h) {
68 return getPreferredSize(w, h);
69 }
70
71 /**
72 * @see org.eclipse.draw2d.Figure#invalidate()
73 */
74 public void invalidate() {
75 pageSizeCacheValues = new Dimension[3];
76 super.invalidate();
77 }
78
79 /**
80 * @see org.eclipse.draw2d.Figure#getPreferredSize(int, int)
81 */
82 public Dimension getPreferredSize(int width, int h) {
83 for (int i = 0; i < 3; i++) {
84 if (pageSizeCacheKeys[i] is width && pageSizeCacheValues[i] !is null)
85 return pageSizeCacheValues[i];
86 }
87
88 pageSizeCacheKeys[2] = pageSizeCacheKeys[1];
89 pageSizeCacheKeys[1] = pageSizeCacheKeys[0];
90 pageSizeCacheKeys[0] = width;
91
92 pageSizeCacheValues[2] = pageSizeCacheValues[1];
93 pageSizeCacheValues[1] = pageSizeCacheValues[0];
94
95 //Flowpage must temporarily layout to determine its preferred size
96 int oldWidth = getPageWidth();
97 setPageWidth(width);
98 validate();
99 pageSizeCacheValues[0] = pageSize.getCopy();
100
101 if (width !is oldWidth) {
102 setPageWidth(oldWidth);
103 getUpdateManager().addInvalidFigure(this);
104 }
105 return pageSizeCacheValues[0];
106 }
107
108 int getPageWidth() {
109 return recommendedWidth;
110 }
111
112 /**
113 * @see BlockFlow#postValidate()
114 */
115 public void postValidate() {
116 Rectangle r = getBlockBox().toRectangle();
117 pageSize.width = r.width;
118 pageSize.height = r.height;
119 List v = getChildren();
120 for (int i = 0; i < v.size(); i++)
121 (cast(FlowFigure)v.get(i)).postValidate();
122 }
123
124 /**
125 * Overridden to set valid.
126 * @see org.eclipse.draw2d.IFigure#removeNotify()
127 */
128 public void removeNotify() {
129 super.removeNotify();
130 setValid(true);
131 }
132
133 /**
134 * @see FlowFigure#setBounds(Rectangle)
135 */
136 public void setBounds(Rectangle r) {
137 if (getBounds().opEquals(r))
138 return;
139 bool invalidate = getBounds().width !is r.width || getBounds().height !is r.height;
140 super.setBounds(r);
141 int newWidth = r.width;
142 if (invalidate || getPageWidth() !is newWidth) {
143 setPageWidth(newWidth);
144 getUpdateManager().addInvalidFigure(this);
145 }
146 }
147
148 private void setPageWidth(int width) {
149 if (recommendedWidth is width)
150 return;
151 recommendedWidth = width;
152 super.invalidate();
153 }
154
155 /**
156 * @see org.eclipse.draw2d.Figure#validate()
157 */
158 public void validate() {
159 if (isValid())
160 return;
161 super.validate();
162 postValidate();
163 }
164
165 }