001: /*******************************************************************************
002: * Copyright (c) 2004, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.layout;
011:
012: import org.eclipse.swt.graphics.Point;
013: import org.eclipse.swt.widgets.Control;
014:
015: /**
016: * Caches the preferred sizes of an array of controls
017: *
018: * @since 3.0
019: */
020: public class LayoutCache {
021: private SizeCache[] caches = new SizeCache[0];
022:
023: /**
024: * Creates an empty layout cache
025: */
026: public LayoutCache() {
027: }
028:
029: /**
030: * Creates a cache for the given array of controls
031: *
032: * @param controls
033: */
034: public LayoutCache(Control[] controls) {
035: rebuildCache(controls);
036: }
037:
038: /**
039: * Returns the size cache for the given control
040: *
041: * @param idx
042: * @return
043: */
044: public SizeCache getCache(int idx) {
045: return caches[idx];
046: }
047:
048: /**
049: * Sets the controls that are being cached here. If these are the same
050: * controls that were used last time, this method does nothing. Otherwise,
051: * the cache is flushed and a new cache is created for the new controls.
052: *
053: * @param controls
054: */
055: public void setControls(Control[] controls) {
056: // If the number of controls has changed, discard the entire cache
057: if (controls.length != caches.length) {
058: rebuildCache(controls);
059: return;
060: }
061:
062: for (int idx = 0; idx < controls.length; idx++) {
063: caches[idx].setControl(controls[idx]);
064: }
065: }
066:
067: /**
068: * Creates a new size cache for the given set of controls, discarding any
069: * existing cache.
070: *
071: * @param controls the controls whose size is being cached
072: */
073: private void rebuildCache(Control[] controls) {
074: SizeCache[] newCache = new SizeCache[controls.length];
075:
076: for (int idx = 0; idx < controls.length; idx++) {
077: // Try to reuse existing caches if possible
078: if (idx < caches.length) {
079: newCache[idx] = caches[idx];
080: newCache[idx].setControl(controls[idx]);
081: } else {
082: newCache[idx] = new SizeCache(controls[idx]);
083: }
084: }
085:
086: caches = newCache;
087: }
088:
089: /**
090: * Computes the preferred size of the nth control
091: *
092: * @param controlIndex index of the control whose size will be computed
093: * @param widthHint width of the control (or SWT.DEFAULT if unknown)
094: * @param heightHint height of the control (or SWT.DEFAULT if unknown)
095: * @return the preferred size of the control
096: */
097: public Point computeSize(int controlIndex, int widthHint,
098: int heightHint) {
099: 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: }
|