📄 sashform.java
字号:
/******************************************************************************* * Copyright (c) 2000, 2004 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/package org.eclipse.swt.custom;import org.eclipse.swt.*;import org.eclipse.swt.widgets.*;import org.eclipse.swt.graphics.*;/** * The SashForm lays out its children in a Row or Column arrangement (as specified * by the orientation) and places a Sash between the children. * One child may be maximized to occupy the entire size of the SashForm. * The relative sizes of the children may be specfied using weights. * * <p> * <dl> * <dt><b>Styles:</b><dd>HORIZONTAL, VERTICAL * </dl> */public class SashForm extends Composite { public int SASH_WIDTH = 3; private static final int DRAG_MINIMUM = 20; private int orientation = SWT.HORIZONTAL; private Sash[] sashes = new Sash[0]; // Remember background and foreground // colors to determine whether to set // sashes to the default color (null) or // a specific color private Color background = null; private Color foreground = null; private Control[] controls = new Control[0]; private Control maxControl = null; private Listener sashListener; private final static String LAYOUT_RATIO = "layout ratio"; //$NON-NLS-1$/** * Constructs a new instance of this class given its parent * and a style value describing its behavior and appearance. * <p> * The style value is either one of the style constants defined in * class <code>SWT</code> which is applicable to instances of this * class, or must be built by <em>bitwise OR</em>'ing together * (that is, using the <code>int</code> "|" operator) two or more * of those <code>SWT</code> style constants. The class description * lists the style constants that are applicable to the class. * Style bits are also inherited from superclasses. * </p> * * @param parent a widget which will be the parent of the new instance (cannot be null) * @param style the style of widget to construct * * @exception IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> * </ul> * @exception SWTException <ul> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> * </ul> * * @see SWT#HORIZONTAL * @see SWT#VERTICAL * @see #getStyle() */public SashForm(Composite parent, int style) { super(parent, checkStyle(style)); if ((style & SWT.VERTICAL) != 0){ orientation = SWT.VERTICAL; } this.addListener(SWT.Resize, new Listener() { public void handleEvent(Event e) { layout(true); } }); sashListener = new Listener() { public void handleEvent(Event e) { onDragSash(e); } };}private static int checkStyle (int style) { int mask = SWT.BORDER | SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT; return style & mask;}public Point computeSize (int wHint, int hHint, boolean changed) { checkWidget(); Control[] cArray = getControls(true); if (cArray.length == 0) return new Point(wHint, hHint); int sashwidth = sashes.length > 0 ? SASH_WIDTH + sashes [0].getBorderWidth() * 2 : SASH_WIDTH; int width = 0; int height = 0; boolean vertical = (orientation == SWT.VERTICAL); if (vertical) { height += (cArray.length - 1) * sashwidth; } else { width += (cArray.length - 1) * sashwidth; } for (int i = 0; i < cArray.length; i++) { if (vertical) { Point size = cArray[i].computeSize(wHint, SWT.DEFAULT); height += size.y; width = Math.max(width, size.x); } else { Point size = cArray[i].computeSize(SWT.DEFAULT, hHint); width += size.x; height = Math.max(height, size.y); } } if (wHint != SWT.DEFAULT) width = wHint; if (hHint != SWT.DEFAULT) height = hHint; return new Point(width, height);}/** * Returns SWT.HORIZONTAL if the controls in the SashForm are laid out side by side * or SWT.VERTICAL if the controls in the SashForm are laid out top to bottom. * * @return SWT.HORIZONTAL or SWT.VERTICAL */public int getOrientation() { //checkWidget(); return orientation;}/** * Answer the control that currently is maximized in the SashForm. * This value may be null. * * @return the control that currently is maximized or null */public Control getMaximizedControl(){ //checkWidget(); return this.maxControl;}/** * Answer the relative weight of each child in the SashForm. The weight represents the * percent of the total width (if SashForm has Horizontal orientation) or * total height (if SashForm has Vertical orientation) each control occupies. * The weights are returned in order of the creation of the widgets (weight[0] * corresponds to the weight of the first child created). * * @return the relative weight of each child * * @exception SWTException <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * </ul> */public int[] getWeights() { checkWidget(); Control[] cArray = getControls(false); int[] ratios = new int[cArray.length]; for (int i = 0; i < cArray.length; i++) { Long ratio = (Long)cArray[i].getData(LAYOUT_RATIO); if (ratio != null) { ratios[i] = (int)(ratio.longValue() * 1000 >> 16); } else { ratios[i] = 200; } } return ratios;}Control[] getControls(boolean onlyVisible) { Control[] children = getChildren(); Control[] result = new Control[0]; for (int i = 0; i < children.length; i++) { if (children[i] instanceof Sash) continue; if (onlyVisible && !children[i].getVisible()) continue; Control[] newResult = new Control[result.length + 1]; System.arraycopy(result, 0, newResult, 0, result.length); newResult[result.length] = children[i]; result = newResult; } return result;}public void layout(boolean changed) { checkWidget(); Rectangle area = getClientArea(); if (area.width == 0 || area.height == 0) return; Control[] newControls = getControls(true); if (controls.length == 0 && newControls.length == 0) return; controls = newControls; if (maxControl != null && !maxControl.isDisposed()) { for (int i= 0; i < controls.length; i++){ if (controls[i] != maxControl) { controls[i].setBounds(-200, -200, 0, 0); } else { controls[i].setBounds(area); } } return; } // keep just the right number of sashes if (sashes.length < controls.length - 1) { Sash[] newSashes = new Sash[controls.length - 1]; System.arraycopy(sashes, 0, newSashes, 0, sashes.length); int sashStyle = (orientation == SWT.HORIZONTAL) ? SWT.VERTICAL : SWT.HORIZONTAL; if ((getStyle() & SWT.BORDER) != 0) sashStyle |= SWT.BORDER; for (int i = sashes.length; i < newSashes.length; i++) { newSashes[i] = new Sash(this, sashStyle); newSashes[i].setBackground(background); newSashes[i].setForeground(foreground); newSashes[i].addListener(SWT.Selection, sashListener); } sashes = newSashes; } if (sashes.length > controls.length - 1) { if (controls.length == 0) { for (int i = 0; i < sashes.length; i++) { sashes[i].dispose(); } sashes = new Sash[0]; } else { Sash[] newSashes = new Sash[controls.length - 1]; System.arraycopy(sashes, 0, newSashes, 0, newSashes.length); for (int i = controls.length - 1; i < sashes.length; i++) { sashes[i].dispose(); } sashes = newSashes; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -