14b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye/******************************************************************************* 24b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * Copyright (c) 2011 Google, Inc. 34b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * All rights reserved. This program and the accompanying materials 44b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * are made available under the terms of the Eclipse Public License v1.0 54b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * which accompanies this distribution, and is available at 64b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * http://www.eclipse.org/legal/epl-v10.html 74b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * 84b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * Contributors: 94b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * Google, Inc. - initial API and implementation 104b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye *******************************************************************************/ 114b1a9d7baa71df312585022c22e8025fafaba95aTor Norbyepackage org.eclipse.wb.core.controls; 124b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye 134b1a9d7baa71df312585022c22e8025fafaba95aTor Norbyeimport org.eclipse.swt.SWT; 144b1a9d7baa71df312585022c22e8025fafaba95aTor Norbyeimport org.eclipse.swt.SWTException; 154b1a9d7baa71df312585022c22e8025fafaba95aTor Norbyeimport org.eclipse.swt.custom.SashForm; 164b1a9d7baa71df312585022c22e8025fafaba95aTor Norbyeimport org.eclipse.swt.graphics.Rectangle; 174b1a9d7baa71df312585022c22e8025fafaba95aTor Norbyeimport org.eclipse.swt.widgets.Composite; 184b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye 194b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye/** 204b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * Instances of the class <code>SelfOrientingSashForm</code> implement a sash form that will 214b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * automatically reset its orientation based on the relationship between the width and height of the 224b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * client area. This is done so that the sash form can be placed in a view that will sometimes be 234b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * tall and narrow and sometimes be short and wide and still lay out its children in a pleasing way. 244b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * <p> 254b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * 264b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * @author unknown 274b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * @author Brian Wilkerson 284b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * @version $Revision: 1.2 $ 294b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * @coverage core.control 304b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye */ 314b1a9d7baa71df312585022c22e8025fafaba95aTor Norbyepublic class SelfOrientingSashForm extends SashForm { 324b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye //////////////////////////////////////////////////////////////////////////// 334b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye // 344b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye // Constructors 354b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye // 364b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye //////////////////////////////////////////////////////////////////////////// 374b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye /** 384b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * Initialize a newly created control to have the given parent and style. The style describes the 394b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * behavior and appearance of this control. 404b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * <p> 414b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * The style value is either one of the style constants defined in the class <code>SWT</code> 424b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * which is applicable to instances of this class, or must be built by <em>bitwise OR</em>'ing 434b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * together (that is, using the <code>int</code> "|" operator) two or more of those 444b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * <code>SWT</code> style constants. The class description for all SWT widget classes should 454b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * include a comment which describes the style constants which are applicable to the class. 464b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * </p> 474b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * 484b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * @param parent 494b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * a widget which will be the parent of the new instance (not null) 504b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * @param style 514b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * the style of widget to construct 524b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * 534b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * @exception IllegalArgumentException 544b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * <ul> 554b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> 564b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * </ul> 574b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * @exception SWTException 584b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * <ul> 594b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the 604b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * parent</li> 614b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> 624b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * </ul> 634b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye */ 644b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye public SelfOrientingSashForm(Composite parent, int style) { 654b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye super(parent, style); 664b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye } 674b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye 684b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye //////////////////////////////////////////////////////////////////////////// 694b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye // 704b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye // Layout 714b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye // 724b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye //////////////////////////////////////////////////////////////////////////// 734b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye /** 744b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * Returns SWT.HORIZONTAL if the controls in the SashForm are laid out side by side or 754b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * SWT.VERTICAL if the controls in the SashForm are laid out top to bottom. 764b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * 774b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * @return SWT.HORIZONTAL or SWT.VERTICAL 784b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye */ 794b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye @Override 804b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye public int getOrientation() { 814b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye int currentOrientation = super.getOrientation(); 824b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye if (inSetOrientation) { 834b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye return currentOrientation; 844b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye } 854b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye int preferredOrientation = isDisposed() ? currentOrientation : getPreferredOrientation(); 864b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye if (currentOrientation != preferredOrientation) { 874b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye setOrientation(preferredOrientation); 884b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye } 894b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye return preferredOrientation; 904b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye } 914b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye 924b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye boolean inSetOrientation = false; 934b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye 944b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye @Override 954b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye public void setOrientation(int orientation) { 964b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye if (inSetOrientation) { 974b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye return; 984b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye } 994b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye inSetOrientation = true; 1004b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye super.setOrientation(orientation); 1014b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye inSetOrientation = false; 1024b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye } 1034b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye 1044b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye /** 1054b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * If the receiver has a layout, ask the layout to <em>lay out</em> (that is, set the size and 1064b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * location of) the receiver's children. If the argument is <code>true</code> the layout must not 1074b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * rely on any cached information it is keeping about the children. If it is <code>false</code> 1084b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * the layout may (potentially) simplify the work it is doing by assuming that the state of the 1094b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * none of the receiver's children has changed since the last layout. If the receiver does not 1104b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * have a layout, do nothing. 1114b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * 1124b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * @param changed 1134b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * <code>true</code> if the layout must flush its caches, and <code>false</code> 1144b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * otherwise 1154b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * 1164b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * @exception SWTException 1174b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * <ul> 1184b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 1194b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the 1204b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * receiver</li> 1214b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye * </ul> 1224b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye */ 1234b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye @Override 1244b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye public void layout(boolean changed) { 1254b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye Rectangle area; 1264b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye int oldOrientation, newOrientation; 1274b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye area = getClientArea(); 1284b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye if (area.width > 0 && area.height > 0) { 1294b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye oldOrientation = super.getOrientation(); 1304b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye newOrientation = SWT.HORIZONTAL; 1314b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye if (area.width < area.height) { 1324b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye newOrientation = SWT.VERTICAL; 1334b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye } 1344b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye if (newOrientation != oldOrientation) { 1354b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye setOrientation(newOrientation); 1364b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye changed = true; 1374b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye } 1384b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye } 1394b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye super.layout(changed); 1404b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye } 1414b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye 1424b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye private int getPreferredOrientation() { 1434b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye Rectangle area = getClientArea(); 1444b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye if (area.width > 0 && area.height > 0 && area.width < area.height) { 1454b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye return SWT.VERTICAL; 1464b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye } 1474b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye return SWT.HORIZONTAL; 1484b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye } 1494b1a9d7baa71df312585022c22e8025fafaba95aTor Norbye}