?? internalshell.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中顯示的內(nèi)部窗口。
*
* 它支持MIN、NO_TRIM、BORDER、TITLE風(fēng)格; 允許取消和配置InternalShell;
* 當(dāng)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,在其中添加標(biāo)題欄、按鈕并且設(shè)置布局和樣式。
*/
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;
// 創(chuàng)建菜單欄
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;
// 根據(jù)style創(chuàng)建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;
}
}
// 對控件的布局進(jìn)行控制
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);
// 設(shè)置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);
}
/**
* 在原有樣式的基礎(chǔ)加上了NO_RADIO_GROUP樣式; 返回表示樣式的整型。
*/
private static int checkStyle(int style) {
int mask = SWT.NO_RADIO_GROUP;
style &= mask;
return style;
}
/**
* 獲得當(dāng)前窗口的樣式;
*/
public int getStyle() {
return style;
}
/**
* 返回InternalShell的顯示內(nèi)容的容器;
*/
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();
}
/**
* 創(chuàng)建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;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -