75
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2004, 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 dwtx.ui.forms.widgets.LayoutCache;
|
|
14
|
|
15 import dwtx.ui.forms.widgets.SizeCache;
|
|
16
|
|
17 import dwt.graphics.Point;
|
|
18 import dwt.widgets.Control;
|
|
19
|
|
20 import dwt.dwthelper.utils;
|
|
21
|
|
22 /**
|
|
23 * Caches the preferred sizes of an array of controls
|
|
24 *
|
|
25 * @since 3.0
|
|
26 */
|
|
27 public class LayoutCache {
|
|
28 private SizeCache[] caches;
|
|
29
|
|
30 /**
|
|
31 * Creates an empty layout cache
|
|
32 */
|
|
33 public this() {
|
|
34 }
|
|
35
|
|
36 /**
|
|
37 * Creates a cache for the given array of controls
|
|
38 *
|
|
39 * @param controls
|
|
40 */
|
|
41 public this(Control[] controls) {
|
|
42 rebuildCache(controls);
|
|
43 }
|
|
44
|
|
45 /**
|
|
46 * Returns the size cache for the given control
|
|
47 *
|
|
48 * @param idx
|
|
49 * @return the size cache for the given control
|
|
50 */
|
|
51 public SizeCache getCache(int idx) {
|
|
52 return caches[idx];
|
|
53 }
|
|
54
|
|
55 /**
|
|
56 * Sets the controls that are being cached here. If these are the same
|
|
57 * controls that were used last time, this method does nothing. Otherwise,
|
|
58 * the cache is flushed and a new cache is created for the new controls.
|
|
59 *
|
|
60 * @param controls
|
|
61 */
|
|
62 public void setControls(Control[] controls) {
|
|
63 // If the number of controls has changed, discard the entire cache
|
|
64 if (controls.length !is caches.length) {
|
|
65 rebuildCache(controls);
|
|
66 return;
|
|
67 }
|
|
68
|
|
69 for (int idx = 0; idx < controls.length; idx++) {
|
|
70 caches[idx].setControl(controls[idx]);
|
|
71 }
|
|
72 }
|
|
73
|
|
74 /**
|
|
75 * Creates a new size cache for the given set of controls, discarding any
|
|
76 * existing cache.
|
|
77 *
|
|
78 * @param controls the controls whose size is being cached
|
|
79 */
|
|
80 private void rebuildCache(Control[] controls) {
|
|
81 SizeCache[] newCache = new SizeCache[controls.length];
|
|
82
|
|
83 for (int idx = 0; idx < controls.length; idx++) {
|
|
84 // Try to reuse existing caches if possible
|
|
85 if (idx < caches.length) {
|
|
86 newCache[idx] = caches[idx];
|
|
87 newCache[idx].setControl(controls[idx]);
|
|
88 } else {
|
|
89 newCache[idx] = new SizeCache(controls[idx]);
|
|
90 }
|
|
91 }
|
|
92
|
|
93 caches = newCache;
|
|
94 }
|
|
95
|
|
96 /**
|
|
97 * Computes the preferred size of the nth control
|
|
98 *
|
|
99 * @param controlIndex index of the control whose size will be computed
|
|
100 * @param widthHint width of the control (or DWT.DEFAULT if unknown)
|
|
101 * @param heightHint height of the control (or DWT.DEFAULT if unknown)
|
|
102 * @return the preferred size of the control
|
|
103 */
|
|
104 public Point computeSize(int controlIndex, int widthHint, int heightHint) {
|
|
105 return caches[controlIndex].computeSize(widthHint, heightHint);
|
|
106 }
|
|
107
|
|
108 /**
|
|
109 * Flushes the cache for the given control. This should be called if exactly
|
|
110 * one of the controls has changed but the remaining controls remain unmodified
|
|
111 *
|
|
112 * @param controlIndex
|
|
113 */
|
|
114 public void flush(int controlIndex) {
|
|
115 caches[controlIndex].flush();
|
|
116 }
|
|
117
|
|
118 /**
|
|
119 * Flushes the cache.
|
|
120 */
|
|
121 public void flush() {
|
|
122 for (int idx = 0; idx < caches.length; idx++) {
|
|
123 caches[idx].flush();
|
|
124 }
|
|
125 }
|
|
126 }
|