| 1 | /* |
| 2 | ----------------------------------------------------------------------------- |
| 3 | (c) Copyright IBM Corp. 2003 All rights reserved. |
| 4 | |
| 5 | The sample program(s) is/are owned by International Business Machines |
| 6 | Corporation or one of its subsidiaries ("IBM") and is/are copyrighted and |
| 7 | licensed, not sold. |
| 8 | |
| 9 | You may copy, modify, and distribute this/these sample program(s) in any form |
| 10 | without payment to IBM, for any purpose including developing, using, marketing |
| 11 | or distributing programs that include or are derivative works of the sample |
| 12 | program(s). |
| 13 | |
| 14 | The sample program(s) is/are provided to you on an "AS IS" basis, without |
| 15 | warranty of any kind. IBM HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER |
| 16 | EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 17 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Some jurisdictions do |
| 18 | not allow for the exclusion or limitation of implied warranties, so the above |
| 19 | limitations or exclusions may not apply to you. IBM shall not be liable for |
| 20 | any damages you suffer as a result of using, modifying or distributing the |
| 21 | sample program(s) or its/their derivatives. |
| 22 | |
| 23 | Each copy of any portion of this/these sample program(s) or any derivative |
| 24 | work, must include the above copyright notice and disclaimer of warranty. |
| 25 | |
| 26 | ----------------------------------------------------------------------------- |
| 27 | */ |
| 28 | |
| 29 | package swing2swt.layout; |
| 30 | |
| 31 | import org.eclipse.swt.SWT; |
| 32 | import org.eclipse.swt.graphics.Point; |
| 33 | import org.eclipse.swt.graphics.Rectangle; |
| 34 | import org.eclipse.swt.widgets.Composite; |
| 35 | import org.eclipse.swt.widgets.Control; |
| 36 | |
| 37 | /** |
| 38 | * Port of AWT BorderLayout to SWT. |
| 39 | * @author Yannick Saillet |
| 40 | */ |
| 41 | public class BorderLayout extends AWTLayout { |
| 42 | public final static String CENTER = "Center"; |
| 43 | public final static String EAST = "East"; |
| 44 | public final static String NORTH = "North"; |
| 45 | public final static String SOUTH = "South"; |
| 46 | public final static String WEST = "West"; |
| 47 | |
| 48 | //----------------------- |
| 49 | |
| 50 | private int hgap, vgap; |
| 51 | private Control centerChild, eastChild, northChild, southChild, westChild; |
| 52 | |
| 53 | public BorderLayout() { |
| 54 | super(); |
| 55 | } |
| 56 | |
| 57 | public BorderLayout(int hgap, int vgap) { |
| 58 | this.hgap = hgap; |
| 59 | this.vgap = vgap; |
| 60 | } |
| 61 | |
| 62 | protected Point computeSize( |
| 63 | Composite composite, |
| 64 | int wHint, |
| 65 | int hHint, |
| 66 | boolean flushCache) { |
| 67 | readLayoutData(composite); |
| 68 | Point size = new Point(0, 0); |
| 69 | |
| 70 | Point preferredSize; |
| 71 | if (northChild != null) { |
| 72 | preferredSize = |
| 73 | getPreferredSize(northChild, wHint, SWT.DEFAULT, flushCache); |
| 74 | size.y += preferredSize.y + vgap; |
| 75 | } |
| 76 | if (southChild != null) { |
| 77 | preferredSize = |
| 78 | getPreferredSize(southChild, wHint, SWT.DEFAULT, flushCache); |
| 79 | size.y += preferredSize.y + vgap; |
| 80 | } |
| 81 | if (westChild != null) { |
| 82 | preferredSize = |
| 83 | getPreferredSize(westChild, SWT.DEFAULT, hHint, flushCache); |
| 84 | size.x += preferredSize.x + hgap; |
| 85 | } |
| 86 | if (eastChild != null) { |
| 87 | preferredSize = |
| 88 | getPreferredSize(eastChild, SWT.DEFAULT, hHint, flushCache); |
| 89 | size.x += preferredSize.x + hgap; |
| 90 | } |
| 91 | if (centerChild != null) { |
| 92 | preferredSize = getPreferredSize(centerChild, wHint, hHint, flushCache); |
| 93 | size.x += preferredSize.x; |
| 94 | size.y += preferredSize.y; |
| 95 | } |
| 96 | return size; |
| 97 | } |
| 98 | |
| 99 | protected void layout(Composite composite, boolean flushCache) { |
| 100 | readLayoutData(composite); |
| 101 | Rectangle clientArea = composite.getClientArea(); |
| 102 | int top = clientArea.y; |
| 103 | int bottom = clientArea.y + clientArea.height; |
| 104 | int left = clientArea.x; |
| 105 | int right = clientArea.x + clientArea.width; |
| 106 | |
| 107 | Point preferredSize; |
| 108 | if (northChild != null) { |
| 109 | preferredSize = |
| 110 | getPreferredSize(northChild, clientArea.width, SWT.DEFAULT, flushCache); |
| 111 | northChild.setBounds(left, top, right - left, preferredSize.y); |
| 112 | top += preferredSize.y + vgap; |
| 113 | } |
| 114 | if (southChild != null) { |
| 115 | preferredSize = |
| 116 | getPreferredSize(southChild, clientArea.width, SWT.DEFAULT, flushCache); |
| 117 | southChild.setBounds( |
| 118 | left, |
| 119 | bottom - preferredSize.y, |
| 120 | right - left, |
| 121 | preferredSize.y); |
| 122 | bottom -= preferredSize.y + vgap; |
| 123 | } |
| 124 | if (westChild != null) { |
| 125 | preferredSize = |
| 126 | getPreferredSize(westChild, SWT.DEFAULT, bottom - top, flushCache); |
| 127 | westChild.setBounds(left, top, preferredSize.x, bottom - top); |
| 128 | left += preferredSize.x + hgap; |
| 129 | } |
| 130 | if (eastChild != null) { |
| 131 | preferredSize = |
| 132 | getPreferredSize(eastChild, SWT.DEFAULT, bottom - top, flushCache); |
| 133 | eastChild.setBounds( |
| 134 | right - preferredSize.x, |
| 135 | top, |
| 136 | preferredSize.x, |
| 137 | bottom - top); |
| 138 | right -= preferredSize.x + hgap; |
| 139 | } |
| 140 | if (centerChild != null) { |
| 141 | centerChild.setBounds(left, top, right - left, bottom - top); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Read the layout data of the children of a composite. |
| 147 | * @param composite the parent composite |
| 148 | */ |
| 149 | private void readLayoutData(Composite composite) { |
| 150 | northChild = southChild = eastChild = westChild = centerChild = null; |
| 151 | Control[] children = composite.getChildren(); |
| 152 | for (int i = 0; i < children.length; i++) { |
| 153 | //if (!children[i].isVisible()) |
| 154 | // continue; |
| 155 | Object layoutData = children[i].getLayoutData(); |
| 156 | if (NORTH.equals(layoutData)) |
| 157 | northChild = children[i]; |
| 158 | else if (SOUTH.equals(layoutData)) |
| 159 | southChild = children[i]; |
| 160 | else if (EAST.equals(layoutData)) |
| 161 | eastChild = children[i]; |
| 162 | else if (WEST.equals(layoutData)) |
| 163 | westChild = children[i]; |
| 164 | else |
| 165 | centerChild = children[i]; |
| 166 | } |
| 167 | } |
| 168 | /** |
| 169 | * @return Returns the hgap. |
| 170 | */ |
| 171 | public int getHgap() { |
| 172 | return hgap; |
| 173 | } |
| 174 | /** |
| 175 | * @param hgap The hgap to set. |
| 176 | */ |
| 177 | public void setHgap(int hgap) { |
| 178 | this.hgap = hgap; |
| 179 | } |
| 180 | /** |
| 181 | * @return Returns the vgap. |
| 182 | */ |
| 183 | public int getVgap() { |
| 184 | return vgap; |
| 185 | } |
| 186 | /** |
| 187 | * @param vgap The vgap to set. |
| 188 | */ |
| 189 | public void setVgap(int vgap) { |
| 190 | this.vgap = vgap; |
| 191 | } |
| 192 | } |