⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 internalshell.java

📁 Eclipse编程技术与实例一书的附CD-ROM光盘
💻 JAVA
字号:
package org.test.custom.shell;

import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;

import org.test.custom.form.SizeBorder;
import org.test.custom.form.SizeGrip;
import org.test.custom.internal.TitleBar;
import org.test.custom.internal.TitleBarButton;

/**
 * 在DesktopForm中显示的内部窗口。
 * 
 * 它支持MIN、NO_TRIM、BORDER、TITLE风格; 允许取消和配置InternalShell;
 * 当DesktopForm大小改变时,保持InternalShell可见。
 */

public class InternalShell extends Composite {
	private static final int BORDER_SIZE = 4;

	private Composite contentPane;

	private TitleBar titleBar;

	private SizeGrip sizeGrip;

	private SizeBorder sizeBorder;

	private int minWidth = 112;

	private int minHeight;

	private DesktopForm desktop;

	private boolean maximized;

	private Rectangle pluralizedBounds;

	private final int titleHeight;

	private final int style;

	private TitleBarButton closeButton, maxButton, minButton;

	Control focusControl;

	/**
	 * 初始化InternalShell,在其中添加标题栏、按钮并且设置布局和样式。
	 */
	public InternalShell(DesktopForm parent, int style) {
		super(parent, checkStyle(style));
		this.desktop = parent;
		this.style = style;
		setBackground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
		FormLayout layout = new FormLayout();
		setLayout(layout);
		FormData fd;

		// 创建菜单栏
		titleBar = new TitleBar(this, style
				& (SWT.CLOSE | SWT.RESIZE | SWT.MAX | SWT.TOOL | SWT.MIN));
		titleHeight = titleBar.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).y;

		Control leftButton = null;

		// 根据style创建InternalShell上的菜单项,并为这些菜单项注册侦听器;注意,这里没有为最小化按钮做任何处理,留给读者完成。
		if ((style & (SWT.CLOSE | SWT.MAX | SWT.MIN)) != 0) {
			closeButton = new TitleBarButton(this, SWT.CLOSE);
			if ((style & SWT.CLOSE) == 0)
				closeButton.setEnabled(false);
			closeButton.addListener(SWT.Selection, new Listener() {
				public void handleEvent(Event event) {
					close();
				}
			});
			fd = new FormData(titleHeight, titleHeight);
			if (leftButton != null)
				fd.right = new FormAttachment(leftButton);
			else
				fd.right = new FormAttachment(100, -BORDER_SIZE);
			fd.top = new FormAttachment(0, BORDER_SIZE);
			closeButton.setLayoutData(fd);
			leftButton = closeButton;

			if ((style & (SWT.MAX | SWT.MIN)) != 0) {
				maxButton = new TitleBarButton(this, SWT.MAX);
				if ((style & SWT.MAX) == 0)
					maxButton.setEnabled(false);
				maxButton.addListener(SWT.Selection, new Listener() {
					public void handleEvent(Event event) {
						setMaximized(!maximized);
					}
				});
				fd = new FormData(titleHeight, titleHeight);
				if (leftButton != null)
					fd.right = new FormAttachment(leftButton);
				else
					fd.right = new FormAttachment(100, -BORDER_SIZE);
				fd.top = new FormAttachment(0, BORDER_SIZE);
				maxButton.setLayoutData(fd);
				leftButton = maxButton;

				minButton = new TitleBarButton(this, SWT.MIN);
				if ((style & SWT.MIN) == 0)
					minButton.setEnabled(false);
				/*
				 * minButton.addListener(SWT.Selection, new Listener() { public
				 * void handleEvent(Event event) { } });
				 */
				fd = new FormData(titleHeight, titleHeight);
				if (leftButton != null)
					fd.right = new FormAttachment(leftButton);
				else
					fd.right = new FormAttachment(100, -BORDER_SIZE);
				fd.top = new FormAttachment(0, BORDER_SIZE);
				minButton.setLayoutData(fd);
				leftButton = minButton;
			}
		}

		// 对控件的布局进行控制
		fd = new FormData();
		fd.left = new FormAttachment(0, BORDER_SIZE);
		if (leftButton != null)
			fd.right = new FormAttachment(leftButton);
		else
			fd.right = new FormAttachment(100, -BORDER_SIZE);
		fd.top = new FormAttachment(0, BORDER_SIZE);
		titleBar.setLayoutData(fd);

		contentPane = new Composite(this, SWT.NONE);
		fd = new FormData();
		fd.left = new FormAttachment(0, BORDER_SIZE);
		fd.right = new FormAttachment(100, -BORDER_SIZE);
		fd.top = new FormAttachment(titleBar, 1);
		fd.bottom = new FormAttachment(100, -BORDER_SIZE);
		contentPane.setLayoutData(fd);

		// 设置InternalShell边框的宽度
		sizeBorder = new SizeBorder(this, this, SWT.BORDER);
		sizeBorder.setBorderWidth(BORDER_SIZE);
		fd = new FormData();
		fd.left = new FormAttachment(0);
		fd.right = new FormAttachment(100);
		fd.top = new FormAttachment(0);
		fd.bottom = new FormAttachment(100);
		sizeBorder.setLayoutData(fd);

		minHeight = titleHeight + 2 * BORDER_SIZE;
		sizeBorder.setMinimumShellSize(minWidth, minHeight);
		sizeBorder.setCornerSize(titleHeight + BORDER_SIZE);
		if ((style & SWT.RESIZE) == 0)
			sizeBorder.setEnabled(false);

		setSize(320, 240);
		setVisible(false);

		desktop.manage(this);
	}

	/**
	 * 在原有样式的基础加上了NO_RADIO_GROUP样式; 返回表示样式的整型。
	 */
	private static int checkStyle(int style) {
		int mask = SWT.NO_RADIO_GROUP;
		style &= mask;
		return style;
	}

	/**
	 * 获得当前窗口的样式;
	 */
	public int getStyle() {
		return style;
	}

	/**
	 * 返回InternalShell的显示内容的容器;
	 */
	public Composite getContentPane() {
		return contentPane;
	}

	public void setText(String s) {
		titleBar.setText(s);
	}

	public String getText() {
		return titleBar.getText();
	}

	public void setImage(Image image) {
		titleBar.setImage(image);
	}

	public Image getImage() {
		return titleBar.getImage();
	}

	/**
	 * 创建SizeGrip.
	 */
	public void createSizeGrip(int style) {
		checkWidget();
		if (sizeGrip != null)
			throw new SWTException("SizeGrip was already created");
		if ((this.style & SWT.RESIZE) == 0)
			throw new SWTException(
					"Cannot create SizeGrip for InternalShell without style RESIZE");
		sizeGrip = new SizeGrip(this, this, style);
		sizeGrip.setBackground(contentPane.getBackground());
		sizeGrip.moveAbove(contentPane);
		FormData fd = new FormData();
		fd.right = new FormAttachment(100, -BORDER_SIZE);
		fd.bottom = new FormAttachment(100, -BORDER_SIZE);
		sizeGrip.setLayoutData(fd);
		sizeGrip.setMinimumShellSize(minWidth, minHeight);
		if (isVisible())
			layout(true);
	}

	public Point computeSize(int wHint, int hHint, boolean changed) {
		Point p = super.computeSize(wHint, hHint, changed);
		if (p.x < minWidth)
			p.x = minWidth;
		if (p.y < minHeight)
			p.y = minHeight;
		return p;
	}

	public void setSize(int width, int height) {
		if (width < minWidth)
			width = minWidth;
		if (height < minHeight)
			height = minHeight;
		super.setSize(width, height);
	}

	public void setBounds(int x, int y, int width, int height) {
		if (width < minWidth)
			width = minWidth;
		if (height < minHeight)
			height = minHeight;
		super.setBounds(x, y, width, height);
	}

	public void setMinimumSize(int width, int height) {
		checkWidget();
		minWidth = width;
		minHeight = height;
		sizeGrip.setMinimumShellSize(minWidth, minHeight);
		sizeBorder.setMinimumShellSize(minWidth, minHeight);
		Point size = getSize();
		if (size.x < minWidth || size.y < minHeight)
			setSize(Math.max(minWidth, size.x), Math.max(minHeight, size.y));
	}

	public void close() {
		Event event = new Event();
		notifyListeners(SWT.Close, event);
		if (event.doit && !isDisposed())
			dispose();
	}

	public void open() {
		desktop.activate(this);
		setVisible(true);
		setFocus();
	}

	public void setVisible(boolean visible) {
		if (!visible)
			desktop.shellVisibilityChanged(this, false);
		super.setVisible(visible);
		if (visible)
			desktop.shellVisibilityChanged(this, true);
	}

	public void setActive() {
		desktop.activate(this);
	}

	public void setMaximized(boolean maximized) {
		checkWidget();
		if (this.maximized == maximized)
			return;
		setMaximizedWithoutNotification(maximized);
		desktop.shellMaximizedOrRestored(this, maximized);
	}

	void setMaximizedWithoutNotification(boolean maximized) {
		if (this.maximized == maximized)
			return;
		this.maximized = maximized;
		if (maximized) {
			pluralizedBounds = getBounds();
			desktopResized(desktop.getClientArea());
		} else {
			setBounds(pluralizedBounds);
		}
		// Note: This method may be called in a Dispose event for this object
		if (sizeGrip != null && !sizeGrip.isDisposed())
			sizeGrip.setVisible(!maximized);
		if (!sizeBorder.isDisposed())
			sizeBorder.setEnabled(!maximized && (style & SWT.RESIZE) != 0);
		if (maxButton != null && !maxButton.isDisposed())
			maxButton.redraw();
	}

	public boolean getMaximized() {
		checkWidget();
		return maximized;
	}

	void redrawDecorationsAfterActivityChange() {
		// Note: This method may be called in a Dispose event for this object
		if (!titleBar.isDisposed())
			titleBar.redraw();
		if (closeButton != null && !closeButton.isDisposed())
			closeButton.redraw();
		if (maxButton != null && !maxButton.isDisposed())
			maxButton.redraw();
		if (minButton != null && !minButton.isDisposed())
			minButton.redraw();
	}

	void desktopResized(Rectangle ca) {
		if (maximized) {
			int hideTitle = desktop.getShowMaximizedTitle() ? 0
					: (titleHeight + 1);
			setBounds(ca.x - BORDER_SIZE, ca.y - BORDER_SIZE - hideTitle,
					ca.width + 2 * BORDER_SIZE, ca.height + 2 * BORDER_SIZE
							+ hideTitle);
		}
	}

	public boolean setFocus() {
		if (focusControl != null && focusControl != this
				&& !focusControl.isDisposed())
			return focusControl.setFocus();
		return super.setFocus();
	}

	public boolean isActiveShell() {
		return desktop.getActiveShell() == this;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -