KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > layout > TrimArea


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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
12 package org.eclipse.ui.internal.layout;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.ListIterator JavaDoc;
18
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.graphics.Point;
21 import org.eclipse.swt.widgets.Control;
22 import org.eclipse.ui.internal.WindowTrimProxy;
23
24 /**
25  * Represents one Trim Area.
26  *
27  * @since 3.2
28  */

29 public class TrimArea {
30     // this is no longer necessary, since every piece of window trim defined
31
// itself as the trim layout data.
32
// private static final TrimLayoutData defaultData = new TrimLayoutData();
33
private static final IWindowTrim defaultData = new WindowTrimProxy(null, null, null, 0, true);
34
35     /**
36      * This method separates resizable controls from non-resizable controls.
37      *
38      * @param input
39      * the list of {@link SizeCache} to filter
40      * @param resizable
41      * will contain resizable controls from the input list
42      * @param nonResizable
43      * will contain non-resizable controls from the input list
44      * @param width
45      * if true, we're interested in horizontally-resizable controls.
46      * Else we're interested in vertically resizable controls
47      */

48     static void filterResizable(List JavaDoc input, List JavaDoc resizable, List JavaDoc nonResizable,
49             boolean width) {
50         Iterator JavaDoc iter = input.iterator();
51         while (iter.hasNext()) {
52             SizeCache next = (SizeCache) iter.next();
53
54             if (next.getControl().isVisible()) {
55                 if (isResizable(next.getControl(), width)) {
56                     resizable.add(next);
57                 } else {
58                     nonResizable.add(next);
59                 }
60             }
61         }
62     }
63
64     /**
65      * Helper function to check for resizeable controls.
66      * @param control the control to check
67      * @param horizontally the direction of resizeability.
68      * @return <code>true</code> if the control is resizeable.
69      */

70     static boolean isResizable(Control control, boolean horizontally) {
71         IWindowTrim data = getData(control);
72
73         if (!data.isResizeable()) {
74             return false;
75         }
76
77         if (horizontally) {
78             return data.getWidthHint() == SWT.DEFAULT;
79         }
80
81         return data.getHeightHint() == SWT.DEFAULT;
82     }
83
84     private static IWindowTrim getData(Control control) {
85         IWindowTrim data = (IWindowTrim) control.getLayoutData();
86         if (data == null) {
87             data = defaultData;
88         }
89
90         return data;
91     }
92
93     /**
94      * Cause the SizeCache to compute it's size.
95      * @param toCompute the cache to compute
96      * @param widthHint in pixels
97      * @param heightHint in pixels
98      * @return a point
99      */

100     private static Point computeSize(SizeCache toCompute, int widthHint,
101             int heightHint) {
102         IWindowTrim data = getData(toCompute.getControl());
103
104         if (widthHint == SWT.DEFAULT) {
105             widthHint = data.getWidthHint();
106         }
107
108         if (heightHint == SWT.DEFAULT) {
109             heightHint = data.getHeightHint();
110         }
111
112         if (widthHint == SWT.DEFAULT || heightHint == SWT.DEFAULT) {
113             return toCompute.computeSize(widthHint, heightHint);
114         }
115
116         return new Point(widthHint, heightHint);
117     }
118
119     static int getSize(SizeCache toCompute, int hint, boolean width) {
120         if (width) {
121             return computeSize(toCompute, SWT.DEFAULT, hint).x;
122         }
123
124         return computeSize(toCompute, hint, SWT.DEFAULT).y;
125     }
126
127     /**
128      * Computes the maximum dimensions of controls in the given list
129      *
130      * @param caches
131      * a list of {@link SizeCache}
132      * @param hint a size hint in pixels
133      * @param width are we interested in height or width
134      * @return pixel width
135      */

136     private static int maxDimension(List JavaDoc caches, int hint, boolean width) {
137
138         if (hint == SWT.DEFAULT) {
139             int result = 0;
140             Iterator JavaDoc iter = caches.iterator();
141
142             while (iter.hasNext()) {
143                 SizeCache next = (SizeCache) iter.next();
144
145                 result = Math.max(getSize(next, SWT.DEFAULT, width), result);
146             }
147
148             return result;
149         }
150
151         List JavaDoc resizable = new ArrayList JavaDoc(caches.size());
152         List JavaDoc nonResizable = new ArrayList JavaDoc(caches.size());
153
154         filterResizable(caches, resizable, nonResizable, width);
155
156         int result = 0;
157         int usedHeight = 0;
158
159         Iterator JavaDoc iter = nonResizable.iterator();
160
161         while (iter.hasNext()) {
162             SizeCache next = (SizeCache) iter.next();
163
164             Point nextSize = computeSize(next, SWT.DEFAULT, SWT.DEFAULT);
165
166             if (width) {
167                 result = Math.max(result, nextSize.x);
168                 usedHeight += nextSize.y;
169             } else {
170                 result = Math.max(result, nextSize.y);
171                 usedHeight += nextSize.x;
172             }
173         }
174
175         if (resizable.size() > 0) {
176             int individualHint = (hint - usedHeight) / resizable.size();
177
178             iter = resizable.iterator();
179
180             while (iter.hasNext()) {
181                 SizeCache next = (SizeCache) iter.next();
182
183                 result = Math.max(result, getSize(next, individualHint, width));
184             }
185         }
186
187         return result;
188     }
189
190     /**
191      * Our area ID.
192      */

193     private int fId;
194
195     /**
196      * An NLS display name.
197      */

198     private String JavaDoc fDisplayName;
199
200     /**
201      * Each trimArea is an ordered list of TrimDescriptors.
202      */

203     private ArrayList JavaDoc fTrim;
204
205     /**
206      * Relevant modifiers for a piece of trim to create it's control if it's
207      * interested in this area, like SWT.TOP, SWT.LEFT, etc.
208      */

209     private int fControlModifiers;
210
211     /**
212      * A default size for this trim area.
213      */

214     private int fTrimSize;
215
216     /**
217      * Create the trim area with its ID.
218      *
219      * @param id
220      * @param displayName
221      * the NLS display name
222      */

223     public TrimArea(int id, String JavaDoc displayName) {
224         fTrim = new ArrayList JavaDoc();
225         fId = id;
226         fDisplayName = displayName;
227         fControlModifiers = SWT.HORIZONTAL;
228     }
229
230     /**
231      * return true of the trim area is empty
232      *
233      * @return <code>true</code>
234      */

235     public boolean isEmpty() {
236         return fTrim.isEmpty();
237     }
238
239     /**
240      * Return the ordered list of trim for this area.
241      *
242      * @return a List containing IWindowTrim
243      */

244     public List JavaDoc getTrims() {
245         List JavaDoc trim = new ArrayList JavaDoc(fTrim.size());
246         Iterator JavaDoc d = fTrim.iterator();
247
248         while (d.hasNext()) {
249             TrimDescriptor desc = (TrimDescriptor) d.next();
250             trim.add(desc.getTrim());
251         }
252         return trim;
253     }
254
255     /**
256      * Return the ordered list of trim descriptors for this area.
257      *
258      * @return a List containing TrimDescriptor
259      */

260     public List JavaDoc getDescriptors() {
261         return (List JavaDoc) fTrim.clone();
262     }
263
264     /**
265      * Set the trim size for this area.
266      *
267      * @param size
268      * the size in pixels.
269      */

270     public void setTrimSize(int size) {
271         fTrimSize = size;
272     }
273
274     /**
275      * Return the trim size for this area.
276      *
277      * @return the size in pixels
278      */

279     public int getTrimSize() {
280         return fTrimSize;
281     }
282
283     /**
284      * Caculate a max dimension for this trim area. It uses a different hint
285      * depending on its orientation.
286      *
287      * @param wHint
288      * a width hint in pixels
289      * @param hHint
290      * a height hint in pixels
291      * @return the size in pixels
292      */

293     public int calculateTrimSize(int wHint, int hHint) {
294         int size = 0;
295         if (!fTrim.isEmpty()) {
296             size = getTrimSize();
297         }
298
299         if (size == SWT.DEFAULT) {
300             int hint = isVertical() ? hHint : wHint;
301             size = maxDimension(getCaches(), hint, isVertical());
302         }
303         return size;
304     }
305
306     /**
307      * return true if this area orientation is vertical.
308      *
309      * @return <code>true</code>
310      */

311     public boolean isVertical() {
312         return (fControlModifiers & SWT.VERTICAL) == SWT.VERTICAL
313                 || fControlModifiers == SWT.LEFT
314                 || fControlModifiers == SWT.RIGHT;
315     }
316
317     /**
318      * The ID for this area.
319      *
320      * @return the ID.
321      */

322     public int getId() {
323         return fId;
324     }
325
326     /**
327      * The NLS display name for this area.
328      *
329      * @return the String display name.
330      */

331     public String JavaDoc getDisplayName() {
332         return fDisplayName;
333     }
334
335     /**
336      * Add the descriptor representing a piece of trim to this trim area.
337      *
338      * @param desc
339      * the trim descriptor
340      */

341     public void addTrim(TrimDescriptor desc) {
342         fTrim.add(desc);
343     }
344
345     /**
346      * Insert this desc before the other desc. If beforeMe is not
347      * part of this area it just defaults to an add.
348      *
349      * @param desc
350      * the window trim
351      * @param beforeMe
352      * before this trim
353      */

354     public void addTrim(TrimDescriptor desc, TrimDescriptor beforeMe) {
355         int idx = fTrim.indexOf(beforeMe);
356         if (idx == -1) {
357             fTrim.add(desc);
358         } else {
359             ListIterator JavaDoc i = fTrim.listIterator(idx);
360             i.add(desc);
361         }
362     }
363
364     /**
365      * Remove the descriptor representing a piece of trim from this trim area.
366      *
367      * @param desc
368      * the trim descriptor
369      */

370     public void removeTrim(TrimDescriptor desc) {
371         fTrim.remove(desc);
372     }
373
374     /**
375      * Does this area contain a piece of trim.
376      *
377      * @param desc
378      * the trim
379      * @return <code>true</code> if we contain the trim.
380      */

381     public boolean contains(TrimDescriptor desc) {
382         return fTrim.contains(desc);
383     }
384
385     /**
386      * Takes the trim area and turns it into an List of {@link SizeCache}.
387      * There can be more items in the return list than there are trim
388      * descriptors in the area.
389      *
390      * @return a list of {@link SizeCache}
391      */

392     public List JavaDoc getCaches() {
393         ArrayList JavaDoc result = new ArrayList JavaDoc(fTrim.size());
394         Iterator JavaDoc d = fTrim.iterator();
395         while (d.hasNext()) {
396             TrimDescriptor desc = (TrimDescriptor) d.next();
397             if (desc.getDockingCache() != null) {
398                 result.add(desc.getDockingCache());
399             }
400             result.add(desc.getCache());
401         }
402         return result;
403     }
404
405     /**
406      * The bitwise SWT modifiers that this trim area suggests, like SWT.TOP
407      * or SWT.LEFT. The control modifiers determine the orientation,
408      * amongst other things.
409      *
410      * @return the bitwise OR of the SWT constants.
411      */

412     public int getControlModifiers() {
413         return fControlModifiers;
414     }
415
416     /**
417      * The bitwise SWT modifiers that this trim area suggests, like SWT.TOP
418      * or SWT.LEFT.
419      *
420      * @param mod
421      * the bitwise OR of the SWT constants.
422      */

423     public void setControlModifiers(int mod) {
424         fControlModifiers = mod;
425     }
426 }
427
Popular Tags