KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > WorkbenchLayout


1 /*******************************************************************************
2  * Copyright (c) 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 package org.eclipse.ui.internal;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.custom.CBanner;
19 import org.eclipse.swt.events.ControlEvent;
20 import org.eclipse.swt.events.ControlListener;
21 import org.eclipse.swt.graphics.Point;
22 import org.eclipse.swt.graphics.Rectangle;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.swt.widgets.CoolBar;
26 import org.eclipse.swt.widgets.CoolItem;
27 import org.eclipse.swt.widgets.Label;
28 import org.eclipse.swt.widgets.Layout;
29 import org.eclipse.ui.internal.layout.TrimCommonUIHandle;
30
31 /**
32  * This layout implements the handling necessary to support the positioning of
33  * all of the 'trim' elements defined for the workbench.
34  * <p>
35  * NOTE: This class is a part of a 'work in progress' and should not be used
36  * without consulting the Platform UI group. No guarantees are made as to the
37  * stability of the API.
38  * </p>
39  *
40  * @since 3.2
41  *
42  */

43 public class WorkbenchLayout extends Layout {
44     private static int defaultMargin = 5;
45
46     /**
47      * This is a convenience class that caches information for a single 'tiled'
48      * line of trim.
49      *
50      * @since 3.2
51      *
52      */

53     private class TrimLine {
54         /**
55          * Teh list of controls in this trim line
56          */

57         List JavaDoc controls = new ArrayList JavaDoc();
58
59         /**
60          * A cache of the previously computed sizes of each trim control
61          */

62         List JavaDoc computedSizes = new ArrayList JavaDoc();
63
64         /**
65          * In horizontal terms this is the 'height' of the tallest control.
66          */

67         int minorMax = 0;
68
69         /**
70          * The number of controls in the line that want to 'grab' extra space.
71          * Any unused space in a trim line is shared equally among these
72          * controls
73          */

74         int resizableCount = 0;
75
76         /**
77          * The amount of unused space in the line
78          */

79         int extraSpace = 0;
80     }
81
82     /**
83      * This layout is used to capture the CBanner's calls to 'computeSize' for
84      * the left trim (which is used to determine the height of the CBanner) so
85      * that it will compute its own height to be the max of either the left or
86      * the right control.
87      * <p>
88      * NOTE: This class is expected to be removed once the CBanner mods are in.
89      * </p>
90      *
91      * @since 3.2
92      *
93      */

94     private class LeftBannerLayout extends Layout {
95
96         /*
97          * (non-Javadoc)
98          *
99          * @see org.eclipse.swt.widgets.Layout#computeSize(org.eclipse.swt.widgets.Composite,
100          * int, int, boolean)
101          */

102         protected Point computeSize(Composite composite, int wHint, int hHint,
103                 boolean flushCache) {
104             // 'topMax' is the maximum height of both the left and
105
// the right side
106
return new Point(wHint, WorkbenchLayout.this.topMax);
107         }
108
109         /*
110          * (non-Javadoc)
111          *
112          * @see org.eclipse.swt.widgets.Layout#layout(org.eclipse.swt.widgets.Composite,
113          * boolean)
114          */

115         protected void layout(Composite composite, boolean flushCache) {
116         }
117
118     }
119
120     // Trim area 'ids'
121
public static final String JavaDoc TRIMID_CMD_PRIMARY = "Command Primary"; //$NON-NLS-1$
122

123     public static final String JavaDoc TRIMID_CMD_SECONDARY = "Command Secondary"; //$NON-NLS-1$
124

125     public static final String JavaDoc TRIMID_VERTICAL1 = "vertical1"; //$NON-NLS-1$
126

127     public static final String JavaDoc TRIMID_VERTICAL2 = "vertical2"; //$NON-NLS-1$
128

129     public static final String JavaDoc TRIMID_STATUS = "Status"; //$NON-NLS-1$
130

131     public static final String JavaDoc TRIMID_CENTER = "Center"; //$NON-NLS-1$
132

133     // 'CBanner' info
134
public CBanner banner;
135
136     private int topMax;
137
138     // 'Center' composite
139
public Composite centerComposite;
140
141     // inter-element spacing
142
private int spacing = 0;
143
144     // Trim Areas
145
private TrimArea cmdPrimaryTrimArea;
146
147     private TrimArea cmdSecondaryTrimArea;
148
149     private TrimArea leftTrimArea;
150
151     private TrimArea rightTrimArea;
152
153     private TrimArea bottomTrimArea;
154
155     // Drag handle info
156
private int horizontalHandleSize = -1;
157
158     private int verticalHandleSize = -1;
159
160     private List JavaDoc dragHandles;
161
162     // statics used in the layout
163
private static Composite layoutComposite;
164
165     private static Rectangle clientRect;
166
167     /**
168      * Construct a new layout. This defines the trim areas that trim can be
169      * placed into.
170      */

171     public WorkbenchLayout() {
172         super();
173
174         // Add the trim areas into the layout
175
cmdPrimaryTrimArea = new TrimArea(TRIMID_CMD_PRIMARY, SWT.TOP,
176                 defaultMargin);
177         cmdSecondaryTrimArea = new TrimArea(TRIMID_CMD_SECONDARY, SWT.TOP,
178                 defaultMargin);
179         leftTrimArea = new TrimArea(TRIMID_VERTICAL1, SWT.LEFT, defaultMargin);
180         rightTrimArea = new TrimArea(TRIMID_VERTICAL2, SWT.RIGHT, defaultMargin);
181         bottomTrimArea = new TrimArea(TRIMID_STATUS, SWT.BOTTOM, defaultMargin);
182
183         // init the list that has the drag handle cache
184
dragHandles = new ArrayList JavaDoc();
185     }
186
187     /**
188      * Create the CBanner control used to control the horizontal span of the
189      * primary and secondary command areas.
190      *
191      * @param workbenchComposite
192      * The workbench acting as the parent of the CBanner
193      */

194     public void createCBanner(Composite workbenchComposite) {
195         banner = new CBanner(workbenchComposite, SWT.NONE);
196         banner.setSimple(false);
197         banner.setRightWidth(175);
198         banner.setLocation(0, 0);
199
200         // Create the left composite and override its 'computeSize'
201
// to delegate to the 'primary' command trim area
202
Composite bannerLeft = new Composite(banner, SWT.NONE) {
203             /*
204              * (non-Javadoc)
205              *
206              * @see org.eclipse.swt.widgets.Composite#computeSize(int, int,
207              * boolean)
208              */

209             public Point computeSize(int wHint, int hHint, boolean changed) {
210                 // If we're doing a 'real' workbench layout then delegate to the
211
// appropriate trim area
212
if (WorkbenchLayout.layoutComposite != null) {
213                     return WorkbenchLayout.this.computeSize(TRIMID_CMD_PRIMARY,
214                             wHint);
215                 }
216
217                 return super.computeSize(wHint, hHint, changed);
218             }
219         };
220         bannerLeft.setLayout(new LeftBannerLayout());
221         bannerLeft.setBackground(workbenchComposite.getDisplay()
222                 .getSystemColor(SWT.COLOR_DARK_BLUE));
223         banner.setLeft(bannerLeft);
224
225         // Create the right hand part of the CBanner
226
Composite bannerRight = new Composite(banner, SWT.NONE) {
227             /*
228              * (non-Javadoc)
229              *
230              * @see org.eclipse.swt.widgets.Composite#computeSize(int, int,
231              * boolean)
232              */

233             public Point computeSize(int wHint, int hHint, boolean changed) {
234                 // If we're doing a 'real' workbench layout then delegate to the
235
// appropriate trim area
236
if (WorkbenchLayout.layoutComposite != null) {
237                     return WorkbenchLayout.this.computeSize(
238                             TRIMID_CMD_SECONDARY, wHint);
239                 }
240
241                 return super.computeSize(wHint, hHint, changed);
242             }
243         };
244         bannerRight.setBackground(workbenchComposite.getDisplay()
245                 .getSystemColor(SWT.COLOR_DARK_BLUE));
246         banner.setRight(bannerRight);
247
248         // If the right banner control changes size it's because
249
// the 'swoop' moved.
250
bannerRight.addControlListener(new ControlListener() {
251             public void controlMoved(ControlEvent e) {
252             }
253
254             public void controlResized(ControlEvent e) {
255                 Composite leftComp = (Composite) e.widget;
256                 leftComp.getShell().layout(true);
257             }
258         });
259
260         // Place the CBanner on the 'bottom' of the z-order
261
banner.moveBelow(null);
262     }
263
264     /*
265      * (non-Javadoc)
266      *
267      * @see org.eclipse.swt.widgets.Layout#computeSize(org.eclipse.swt.widgets.Composite,
268      * int, int, boolean)
269      *
270      * Note that this is arbitrary since the we're a top level shell (so
271      * computeSize won't be called.
272      */

273     protected Point computeSize(Composite composite, int wHint, int hHint,
274             boolean flushCache) {
275         Point size = new Point(wHint, hHint);
276         if (size.x == SWT.DEFAULT) {
277             size.x = 300;
278         }
279         if (size.y == SWT.DEFAULT) {
280             size.y = 300;
281         }
282         return size;
283     }
284
285     /*
286      * (non-Javadoc)
287      *
288      * @see org.eclipse.swt.widgets.Layout#layout(org.eclipse.swt.widgets.Composite,
289      * boolean)
290      *
291      * TODO: Supply a full description of the layout mechanicsS
292      */

293     protected void layout(Composite composite, boolean flushCache) {
294         layoutComposite = composite;
295         clientRect = composite.getClientArea();
296
297         // reset all the drag handles to be invisible
298
resetDragHandles();
299
300         // Compute the proper size for each trim area
301
// Do Top Right, Top Left, Left, Right then Bottom so the math works out
302
if (useCBanner()) {
303             banner.moveBelow(null);
304
305             // NOTE: calling 'computeSize' here will, in turn, call the
306
// 'computeSize' for the left/right areas, each with the
307
// 'correct' width hint...
308
Point bannerSize = banner
309                     .computeSize(clientRect.width, SWT.DEFAULT);
310
311             // set the amount of space used by the 'cmd' areas for use later
312
topMax = bannerSize.y;
313
314             banner.setSize(bannerSize);
315         } else {
316             Point c1Size = computeSize(TRIMID_CMD_PRIMARY, clientRect.width);
317             Point c2Size = computeSize(TRIMID_CMD_SECONDARY, clientRect.width);
318
319             // set the amount of space used by the 'cmd' areas for use later
320
topMax = c1Size.y + c2Size.y;
321         }
322
323         // Now do the vertical areas; their 'major' is whatever is left
324
// vertically once the top areas have been computed
325
Point v1Size = computeSize(TRIMID_VERTICAL1, clientRect.height - topMax);
326         Point v2Size = computeSize(TRIMID_VERTICAL2, clientRect.height - topMax);
327
328         // Finally, the status area's 'major' is whatever is left over
329
// horizontally once the vertical areas have been computed.
330
computeSize(TRIMID_STATUS, clientRect.width - (v1Size.x + v2Size.x));
331
332         // Now, layout the trim within each area
333
// Command primary area
334
if (useCBanner()) {
335             Point leftLoc = banner.getLeft().getLocation();
336             cmdPrimaryTrimArea.areaBounds.x = leftLoc.x;
337             cmdPrimaryTrimArea.areaBounds.y = leftLoc.y;
338         } else {
339             cmdPrimaryTrimArea.areaBounds.x = 0;
340             cmdPrimaryTrimArea.areaBounds.y = 0;
341         }
342         layoutTrim(cmdPrimaryTrimArea, cmdPrimaryTrimArea.areaBounds);
343
344         // Command secondary area
345
if (useCBanner()) {
346             Point rightLoc = banner.getRight().getLocation();
347             cmdSecondaryTrimArea.areaBounds.x = rightLoc.x;
348             cmdSecondaryTrimArea.areaBounds.y = rightLoc.y;
349         } else {
350             cmdSecondaryTrimArea.areaBounds.x = 0;
351             cmdSecondaryTrimArea.areaBounds.y = cmdPrimaryTrimArea.areaBounds.height;
352         }
353         layoutTrim(cmdSecondaryTrimArea, cmdSecondaryTrimArea.areaBounds);
354
355         leftTrimArea.areaBounds.x = 0;
356         leftTrimArea.areaBounds.y = topMax;
357         layoutTrim(leftTrimArea, leftTrimArea.areaBounds);
358
359         rightTrimArea.areaBounds.x = clientRect.width
360                 - rightTrimArea.areaBounds.width;
361         rightTrimArea.areaBounds.y = topMax;
362         layoutTrim(rightTrimArea, rightTrimArea.areaBounds);
363
364         bottomTrimArea.areaBounds.x = leftTrimArea.areaBounds.width;
365         bottomTrimArea.areaBounds.y = clientRect.height
366                 - bottomTrimArea.areaBounds.height;
367         layoutTrim(bottomTrimArea, bottomTrimArea.areaBounds);
368
369         // Set the center control's bounds to fill the space remaining
370
// after the trim has been arranged
371
layoutCenter();
372     }
373
374     /**
375      * Indicates whether or not the layout should use the CBanner or tile the
376      * primary and secondary trim areas one above the other.
377      *
378      * @return <code>true</code> iff the layout should use the CBanner.
379      */

380     private boolean useCBanner() {
381         return (banner != null && banner.getVisible());
382     }
383
384     /**
385      * @param areaId
386      * The identifier for the TrimArea to return
387      * @return The TrimArea that matches the given areaId
388      */

389     private TrimArea getTrimArea(String JavaDoc areaId) {
390         if (TRIMID_CMD_PRIMARY.equals(areaId)) {
391             return cmdPrimaryTrimArea;
392         }
393         if (TRIMID_CMD_SECONDARY.equals(areaId)) {
394             return cmdSecondaryTrimArea;
395         }
396         if (TRIMID_VERTICAL1.equals(areaId)) {
397             return leftTrimArea;
398         }
399         if (TRIMID_VERTICAL2.equals(areaId)) {
400             return rightTrimArea;
401         }
402         if (TRIMID_STATUS.equals(areaId)) {
403             return bottomTrimArea;
404         }
405
406         return null;
407     }
408
409     /**
410      * @param areaId
411      * The TrimArea that we want to get the controls for
412      * @return The list of controls whose TrimLayoutData's areaId matches the
413      * given areaId parameter
414      */

415     private List JavaDoc getTrimContents(String JavaDoc areaId) {
416         List JavaDoc trimContents = new ArrayList JavaDoc();
417         Control[] children = layoutComposite.getChildren();
418         for (int i = 0; i < children.length; i++) {
419             // Skip any disposed or invisible widgets
420
if (children[i].isDisposed() || !children[i].getVisible()) {
421                 continue;
422             }
423
424             // Only accept children that want to be layed out in a particular
425
// trim area
426
if (children[i].getLayoutData() instanceof TrimLayoutData) {
427                 TrimLayoutData tlData = (TrimLayoutData) children[i]
428                         .getLayoutData();
429                 if (tlData.areaId.equals(areaId)) {
430                     trimContents.add(children[i]);
431                 }
432             }
433         }
434
435         return trimContents;
436     }
437
438     /**
439      * Lays out the center composite once the outer trim areas have all been
440      * done.
441      */

442     private void layoutCenter() {
443         if (centerComposite != null) {
444             // The center is the 'inner' bounding box of the other trim areas
445
Rectangle areaBounds = new Rectangle(
446                     leftTrimArea.areaBounds.x + leftTrimArea.areaBounds.width,
447                     topMax,
448                     clientRect.width
449                             - (leftTrimArea.areaBounds.width + rightTrimArea.areaBounds.width),
450                     clientRect.height
451                             - (topMax + bottomTrimArea.areaBounds.height));
452
453             centerComposite.setBounds(areaBounds);
454         }
455     }
456
457     /**
458      * Computes the size of a trim area given the length of its major dimension.
459      * Depending on whether the trim area is horizontal or vertical one of the
460      * two value will always match the supplied 'majorHint' ('x' if it's
461      * horizontal).
462      * <p>
463      * Effectively, this computes the length of the minor dimension by tiling
464      * the trim area's controls into multiple lines based on the length of the
465      * major dimension.
466      * </p>
467      *
468      * @param areaId
469      * The area id to compute the size for
470      * @param majorHint
471      * The length of the major dimension
472      *
473      * @return The computed size
474      */

475     private Point computeSize(String JavaDoc areaId, int majorHint) {
476         TrimArea trimArea = getTrimArea(areaId);
477         boolean horizontal = trimArea.orientation == SWT.TOP
478                 || trimArea.orientation == SWT.BOTTOM;
479
480         // Gather up all the controls for this area and tile them out
481
trimArea.trimContents = getTrimContents(trimArea.areaId);
482
483         // Split the controls list into a list of TrimLines that each fit into
484
// the trim area
485
trimArea.trimLines = computeWrappedTrim(trimArea, majorHint);
486
487         // Now, calculate the max between the default and the contents
488
int minorMax = 0;
489         for (Iterator JavaDoc iter = trimArea.trimLines.iterator(); iter.hasNext();) {
490             TrimLine curLine = (TrimLine) iter.next();
491             minorMax += curLine.minorMax;
492         }
493         minorMax = Math.max(minorMax, trimArea.defaultMinor);
494
495         // Store the size in the area'a cache...
496
Point computedSize = new Point(0, 0);
497         if (horizontal) {
498             computedSize.x = trimArea.areaBounds.width = majorHint;
499             computedSize.y = trimArea.areaBounds.height = minorMax;
500         } else {
501             computedSize.x = trimArea.areaBounds.width = minorMax;
502             computedSize.y = trimArea.areaBounds.height = majorHint;
503         }
504
505         // ...and return it
506
return computedSize;
507     }
508
509     /**
510      * This is where the information required to lay the controls belonging to a
511      * particular trim area out.
512      * <p>
513      * Tile the controls in the trim area into one or more lines. Each line is
514      * guaranteed to take up less than or equal to the 'majorHint' in the major
515      * dimension. The result is a complete cache of the information needed to
516      * lay the controls in the trim area out.
517      * </p>
518      *
519      * @param trimArea The trim area to create the cache info for
520      * @param majorHint The length of the major dimension
521      *
522      * @return A List of <code>TrimLine</code> elements
523      */

524     private List JavaDoc computeWrappedTrim(TrimArea trimArea, int majorHint) {
525         boolean horizontal = trimArea.orientation == SWT.TOP
526                 || trimArea.orientation == SWT.BOTTOM;
527         // Return a list of 'lines' to tile the control into...
528
List JavaDoc lines = new ArrayList JavaDoc();
529         TrimLine curLine = new TrimLine();
530         lines.add(curLine);
531         curLine.minorMax = trimArea.defaultMinor;
532
533         // Init the tilePos to force a 'new' line
534
int tilePos = 0;
535         for (Iterator JavaDoc iter = trimArea.trimContents.iterator(); iter.hasNext();) {
536             Control control = (Control) iter.next();
537             TrimLayoutData td = (TrimLayoutData) control.getLayoutData();
538
539             Point prefSize;
540             if (horizontal) {
541                 //prefSize = control.computeSize(majorHint, SWT.DEFAULT);
542
prefSize = control.computeSize(SWT.DEFAULT, SWT.DEFAULT);
543             } else {
544                 prefSize = control.computeSize(SWT.DEFAULT, majorHint);
545             }
546
547             // Will this control fit onto the current line?
548
int curTileSize = horizontal ? prefSize.x : prefSize.y;
549
550             // every control except the first one needs to include the 'spacing'
551
// in the calc
552
if (curLine.controls.size() > 0) {
553                 curTileSize += spacing;
554             }
555
556             // If the control can be re-positioned then we have to add a drag
557
// handle to it
558
// we have to include the space that it'll occupy in the calcs
559
if (td.listener != null) {
560                 curTileSize += horizontal ? horizontalHandleSize
561                         : verticalHandleSize;
562             }
563
564             // Place the control into the 'current' line if it'll fit (or if
565
// it's the
566
// -first- control (this handles the case where a control is too
567
// large to
568
// fit into the current TrimArea's 'major' size.
569
if ((tilePos + curTileSize) <= majorHint
570                     || curLine.controls.size() == 0) {
571                 curLine.controls.add(control);
572
573                 // Cache the maximum amount of 'minor' space needed
574
int minorSize = horizontal ? prefSize.y : prefSize.x;
575                 if (minorSize > curLine.minorMax) {
576                     curLine.minorMax = minorSize;
577                 }
578
579                 tilePos += curTileSize;
580             } else {
581                 // Remember how much space was left on the current line
582
curLine.extraSpace = majorHint - tilePos;
583
584                 // We need a new line...
585
curLine = new TrimLine();
586                 lines.add(curLine);
587
588                 curLine.controls.add(control);
589
590                 // Cache the maximum amount of 'minor' space needed
591
int minorSize = horizontal ? prefSize.y : prefSize.x;
592                 if (minorSize > curLine.minorMax) {
593                     curLine.minorMax = minorSize;
594                 }
595
596                 tilePos = curTileSize;
597             }
598
599             // Count how many 'resizable' controls there are
600
if ((td.flags & TrimLayoutData.GROWABLE) != 0) {
601                 curLine.resizableCount++;
602             }
603         }
604
605         // Remember how much space was left on the current line
606
curLine.extraSpace = majorHint - tilePos;
607
608         return lines;
609     }
610
611     private void layoutTrim(TrimArea trimArea, Rectangle areaBounds) {
612         boolean horizontal = trimArea.orientation == SWT.TOP
613                 || trimArea.orientation == SWT.BOTTOM;
614
615         // How much space do we have to 'tile' into?
616
int areaMajor = horizontal ? areaBounds.width : areaBounds.height;
617
618         // Get the tiled 'List of Lists' for the trim
619
List JavaDoc tileAreas = trimArea.trimLines;
620
621         // Tile each 'line' into the trim
622
int tilePosMinor = horizontal ? areaBounds.y : areaBounds.x;
623         for (Iterator JavaDoc areaIter = tileAreas.iterator(); areaIter.hasNext();) {
624             TrimLine curLine = (TrimLine) areaIter.next();
625
626             // Compute how much space to give to 'resizable' controls. Note that
627
// we can end up computing -negative- 'extraSpace' values if the
628
// control's
629
// preferred 'major' size is actually > the 'major' size for the
630
// trim area
631
int resizePadding = 0;
632             if (curLine.resizableCount > 0 && curLine.extraSpace > 0) {
633                 resizePadding = curLine.extraSpace / curLine.resizableCount;
634             }
635
636             // Tile each line
637
int tilePosMajor = horizontal ? areaBounds.x : areaBounds.y;
638             for (Iterator JavaDoc iter = curLine.controls.iterator(); iter.hasNext();) {
639                 Control control = (Control) iter.next();
640                 TrimLayoutData td = (TrimLayoutData) control.getLayoutData();
641                 Point prefSize = control.computeSize(SWT.DEFAULT, SWT.DEFAULT);
642
643                 int major = horizontal ? prefSize.x : prefSize.y;
644                 int minor = horizontal ? prefSize.y : prefSize.x;
645
646                 // Ensure that controls that are too wide for the area get
647
// 'clipped'
648
if (major > areaMajor) {
649                     major = areaMajor;
650                 }
651
652                 // If desired, extend the 'minor' size of the control to fill
653
// the area
654
if ((td.flags & TrimLayoutData.GRAB_EXCESS_MINOR) != 0) {
655                     minor = curLine.minorMax;
656                 }
657
658                 // If desired, extend the 'major' size of the control to fill
659
// the area
660
if ((td.flags & TrimLayoutData.GROWABLE) != 0) {
661                     major += resizePadding;
662                 }
663
664                 // If we have to show a drag handle then do it here
665
if (td.listener != null && createHandles()) {
666                     TrimCommonUIHandle handle = getDragHandle(trimArea.orientation);
667                     // handle.setControl(control);
668

669                     if (horizontal) {
670                         handle.setBounds(tilePosMajor, tilePosMinor,
671                                 getHandleSize(true), curLine.minorMax);
672                     } else {
673                         handle.setBounds(tilePosMinor, tilePosMajor,
674                                 curLine.minorMax, getHandleSize(false));
675                     }
676
677                     tilePosMajor += horizontal ? getHandleSize(true)
678                             : getHandleSize(false);
679                 }
680
681                 // Now, lay out the actual control
682
switch (trimArea.orientation) {
683                 case SWT.TOP:
684                     control.setBounds(tilePosMajor, tilePosMinor, major, minor);
685                     break;
686                 case SWT.LEFT:
687                     control.setBounds(tilePosMinor, tilePosMajor, minor, major);
688                     break;
689                 case SWT.RIGHT:
690                     // Here we want to tile the control to the RIGHT edge
691
int rightEdge = tilePosMinor + curLine.minorMax;
692                     control.setBounds(rightEdge - minor, tilePosMajor, minor,
693                             major);
694                     break;
695                 case SWT.BOTTOM:
696                     // Here we want to tile the control to the RIGHT edge
697
int bottomEdge = tilePosMinor + curLine.minorMax;
698                     control.setBounds(tilePosMajor, bottomEdge - minor, major,
699                             minor);
700                     break;
701                 }
702
703                 // Ensure that the control is above the trim control
704
tilePosMajor += major + spacing;
705             }
706             tilePosMinor += curLine.minorMax;
707             tilePosMajor = horizontal ? areaBounds.x : areaBounds.y;
708         }
709     }
710
711     /**
712      * HACK>>>Remove if found in the wild...
713      * @return
714      */

715     private boolean createHandles() {
716         return false;
717     }
718
719     private void resetDragHandles() {
720         for (Iterator JavaDoc iter = dragHandles.iterator(); iter.hasNext();) {
721             // TrimCommonUIHandle handle = (TrimCommonUIHandle) iter.next();
722
// handle.setControl(null);
723
}
724     }
725
726     private TrimCommonUIHandle getDragHandle(int orientation) {
727         // boolean horizontal = orientation == SWT.TOP
728
// || orientation == SWT.BOTTOM;
729

730         for (Iterator JavaDoc iter = dragHandles.iterator(); iter.hasNext();) {
731             TrimCommonUIHandle handle = (TrimCommonUIHandle) iter.next();
732             // if (handle.toDrag == null && handle.horizontal == horizontal)
733
return handle;
734         }
735
736         // If we get here then we haven't found a new drag handle so create one
737
System.out.println("new Handle"); //$NON-NLS-1$
738
// TrimCommonUIHandle newHandle = new
739
// TrimCommonUIHandle(layoutComposite,
740
// horizontal);
741
// dragHandles.add(newHandle);
742
// return newHandle;
743
return null;
744     }
745
746     /**
747      * Return the SWT side that the trim area is on
748      *
749      * @param areaId The id of the area to get the orientation of
750      *
751      * @return The SWT side corresponding that the given area
752      */

753     public static int getOrientation(String JavaDoc areaId) {
754         if (TRIMID_CMD_PRIMARY.equals(areaId)) {
755             return SWT.TOP;
756         }
757         if (TRIMID_CMD_SECONDARY.equals(areaId)) {
758             return SWT.TOP;
759         }
760         if (TRIMID_VERTICAL1.equals(areaId)) {
761             return SWT.LEFT;
762         }
763         if (TRIMID_VERTICAL2.equals(areaId)) {
764             return SWT.RIGHT;
765         }
766         if (TRIMID_STATUS.equals(areaId)) {
767             return SWT.BOTTOM;
768         }
769
770         return SWT.NONE;
771     }
772
773     /**
774      * Calculate a size for the handle that will be large enough to show the
775      * CoolBar's drag affordance.
776      *
777      * @return The size that the handle has to be, based on the orientation
778      */

779     private int getHandleSize(boolean horizontal) {
780         // Do we already have a 'cached' value?
781
if (horizontal && horizontalHandleSize != -1) {
782             return horizontalHandleSize;
783         }
784
785         if (!horizontal && verticalHandleSize != -1) {
786             return verticalHandleSize;
787         }
788
789         // Must be the first time, calculate the value
790
CoolBar bar = new CoolBar(layoutComposite, horizontal ? SWT.HORIZONTAL
791                 : SWT.VERTICAL);
792
793         CoolItem item = new CoolItem(bar, SWT.NONE);
794
795         Label ctrl = new Label(bar, SWT.PUSH);
796         ctrl.setText("Button 1"); //$NON-NLS-1$
797
Point size = ctrl.computeSize(SWT.DEFAULT, SWT.DEFAULT);
798
799         Point ps = item.computeSize(size.x, size.y);
800         item.setPreferredSize(ps);
801         item.setControl(ctrl);
802
803         bar.pack();
804
805         // OK, now the difference between the location of the CB and the
806
// location of the
807
Point bl = ctrl.getLocation();
808         Point cl = bar.getLocation();
809
810         // Toss them now...
811
ctrl.dispose();
812         item.dispose();
813         bar.dispose();
814
815         // The 'size' is the difference between the start of teh CoolBar and
816
// start of its first control
817
int length;
818         if (horizontal) {
819             length = bl.x - cl.x;
820             horizontalHandleSize = length;
821         } else {
822             length = bl.y - cl.y;
823             verticalHandleSize = length;
824         }
825
826         return length;
827     }
828
829 }
830
Popular Tags