1 11 12 package org.eclipse.ui.internal.layout; 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.ListIterator ; 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 29 public class TrimArea { 30 private static final IWindowTrim defaultData = new WindowTrimProxy(null, null, null, 0, true); 34 35 48 static void filterResizable(List input, List resizable, List nonResizable, 49 boolean width) { 50 Iterator 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 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 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 136 private static int maxDimension(List caches, int hint, boolean width) { 137 138 if (hint == SWT.DEFAULT) { 139 int result = 0; 140 Iterator 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 resizable = new ArrayList (caches.size()); 152 List nonResizable = new ArrayList (caches.size()); 153 154 filterResizable(caches, resizable, nonResizable, width); 155 156 int result = 0; 157 int usedHeight = 0; 158 159 Iterator 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 193 private int fId; 194 195 198 private String fDisplayName; 199 200 203 private ArrayList fTrim; 204 205 209 private int fControlModifiers; 210 211 214 private int fTrimSize; 215 216 223 public TrimArea(int id, String displayName) { 224 fTrim = new ArrayList (); 225 fId = id; 226 fDisplayName = displayName; 227 fControlModifiers = SWT.HORIZONTAL; 228 } 229 230 235 public boolean isEmpty() { 236 return fTrim.isEmpty(); 237 } 238 239 244 public List getTrims() { 245 List trim = new ArrayList (fTrim.size()); 246 Iterator 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 260 public List getDescriptors() { 261 return (List ) fTrim.clone(); 262 } 263 264 270 public void setTrimSize(int size) { 271 fTrimSize = size; 272 } 273 274 279 public int getTrimSize() { 280 return fTrimSize; 281 } 282 283 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 311 public boolean isVertical() { 312 return (fControlModifiers & SWT.VERTICAL) == SWT.VERTICAL 313 || fControlModifiers == SWT.LEFT 314 || fControlModifiers == SWT.RIGHT; 315 } 316 317 322 public int getId() { 323 return fId; 324 } 325 326 331 public String getDisplayName() { 332 return fDisplayName; 333 } 334 335 341 public void addTrim(TrimDescriptor desc) { 342 fTrim.add(desc); 343 } 344 345 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 i = fTrim.listIterator(idx); 360 i.add(desc); 361 } 362 } 363 364 370 public void removeTrim(TrimDescriptor desc) { 371 fTrim.remove(desc); 372 } 373 374 381 public boolean contains(TrimDescriptor desc) { 382 return fTrim.contains(desc); 383 } 384 385 392 public List getCaches() { 393 ArrayList result = new ArrayList (fTrim.size()); 394 Iterator 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 412 public int getControlModifiers() { 413 return fControlModifiers; 414 } 415 416 423 public void setControlModifiers(int mod) { 424 fControlModifiers = mod; 425 } 426 } 427 | Popular Tags |