Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

aboutsummaryrefslogtreecommitdiffstats
blob: 03fd467fb5d15c1aa6da7052b6cbb873014ba267 (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
/*******************************************************************************
 * Copyright (c) 2004, 2005 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.internal.layout;

import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Control;

/**
 * Caches the preferred sizes of an array of controls
 * 
 * @since 3.0
 */
public class LayoutCache {
    private SizeCache[] caches = new SizeCache[0];

    /**
     * Creates an empty layout cache
     */
    public LayoutCache() {
    }

    /**
     * Creates a cache for the given array of controls
     * 
     * @param controls
     */
    public LayoutCache(Control[] controls) {
        rebuildCache(controls);
    }

    /**
     * Returns the size cache for the given control
     * 
     * @param idx
     * @return
     */
    public SizeCache getCache(int idx) {
        return caches[idx];
    }

    /**
     * Sets the controls that are being cached here. If these are the same
     * controls that were used last time, this method does nothing. Otherwise,
     * the cache is flushed and a new cache is created for the new controls.
     * 
     * @param controls
     */
    public void setControls(Control[] controls) {
        // If the number of controls has changed, discard the entire cache
        if (controls.length != caches.length) {
            rebuildCache(controls);
            return;
        }

        for (int idx = 0; idx < controls.length; idx++) {
            caches[idx].setControl(controls[idx]);
        }
    }

    /**
     * Creates a new size cache for the given set of controls, discarding any
     * existing cache.
     * 
     * @param controls the controls whose size is being cached
     */
    private void rebuildCache(Control[] controls) {
        SizeCache[] newCache = new SizeCache[controls.length];

        for (int idx = 0; idx < controls.length; idx++) {
            // Try to reuse existing caches if possible
            if (idx < caches.length) {
                newCache[idx] = caches[idx];
                newCache[idx].setControl(controls[idx]);
            } else {
                newCache[idx] = new SizeCache(controls[idx]);
            }
        }

        caches = newCache;
    }

    /**
     * Computes the preferred size of the nth control
     * 
     * @param controlIndex index of the control whose size will be computed
     * @param widthHint width of the control (or SWT.DEFAULT if unknown)
     * @param heightHint height of the control (or SWT.DEFAULT if unknown)
     * @return the preferred size of the control
     */
    public Point computeSize(int controlIndex, int widthHint, int heightHint) {
        return caches[controlIndex].computeSize(widthHint, heightHint);
    }

    /**
     * Flushes the cache for the given control. This should be called if exactly
     * one of the controls has changed but the remaining controls remain unmodified
     * 
     * @param controlIndex
     */
    public void flush(int controlIndex) {
        caches[controlIndex].flush();
    }

    /**
     * Flushes the cache.
     */
    public void flush() {
        for (int idx = 0; idx < caches.length; idx++) {
            caches[idx].flush();
        }
    }
}

Back to the top