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