KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > forms > widgets > LayoutCache


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  *******************************************************************************/

11 package org.eclipse.ui.forms.widgets;
12
13 import org.eclipse.swt.graphics.Point;
14 import org.eclipse.swt.widgets.Control;
15
16 /**
17  * Caches the preferred sizes of an array of controls
18  *
19  * @since 3.0
20  */

21 public class LayoutCache {
22     private SizeCache[] caches = new SizeCache[0];
23
24     /**
25      * Creates an empty layout cache
26      */

27     public LayoutCache() {
28     }
29
30     /**
31      * Creates a cache for the given array of controls
32      *
33      * @param controls
34      */

35     public LayoutCache(Control[] controls) {
36         rebuildCache(controls);
37     }
38
39     /**
40      * Returns the size cache for the given control
41      *
42      * @param idx
43      * @return the size cache for the given control
44      */

45     public SizeCache getCache(int idx) {
46         return caches[idx];
47     }
48
49     /**
50      * Sets the controls that are being cached here. If these are the same
51      * controls that were used last time, this method does nothing. Otherwise,
52      * the cache is flushed and a new cache is created for the new controls.
53      *
54      * @param controls
55      */

56     public void setControls(Control[] controls) {
57         // If the number of controls has changed, discard the entire cache
58
if (controls.length != caches.length) {
59             rebuildCache(controls);
60             return;
61         }
62
63         for (int idx = 0; idx < controls.length; idx++) {
64             caches[idx].setControl(controls[idx]);
65         }
66     }
67
68     /**
69      * Creates a new size cache for the given set of controls, discarding any
70      * existing cache.
71      *
72      * @param controls the controls whose size is being cached
73      */

74     private void rebuildCache(Control[] controls) {
75         SizeCache[] newCache = new SizeCache[controls.length];
76
77         for (int idx = 0; idx < controls.length; idx++) {
78             // Try to reuse existing caches if possible
79
if (idx < caches.length) {
80                 newCache[idx] = caches[idx];
81                 newCache[idx].setControl(controls[idx]);
82             } else {
83                 newCache[idx] = new SizeCache(controls[idx]);
84             }
85         }
86
87         caches = newCache;
88     }
89
90     /**
91      * Computes the preferred size of the nth control
92      *
93      * @param controlIndex index of the control whose size will be computed
94      * @param widthHint width of the control (or SWT.DEFAULT if unknown)
95      * @param heightHint height of the control (or SWT.DEFAULT if unknown)
96      * @return the preferred size of the control
97      */

98     public Point computeSize(int controlIndex, int widthHint, int heightHint) {
99         return caches[controlIndex].computeSize(widthHint, heightHint);
100     }
101
102     /**
103      * Flushes the cache for the given control. This should be called if exactly
104      * one of the controls has changed but the remaining controls remain unmodified
105      *
106      * @param controlIndex
107      */

108     public void flush(int controlIndex) {
109         caches[controlIndex].flush();
110     }
111
112     /**
113      * Flushes the cache.
114      */

115     public void flush() {
116         for (int idx = 0; idx < caches.length; idx++) {
117             caches[idx].flush();
118         }
119     }
120 }
121
Popular Tags