📄 tab.java
字号:
/*******************************************************************************
* Copyright (c) 2000, 2005 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.swt.examples.controlexample;
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.events.*;
/**
* <code>Tab</code> is the abstract superclass of every page
* in the example's tab folder. Each page in the tab folder
* describes a control.
*
* A Tab itself is not a control but instead provides a
* hierarchy with which to share code that is common to
* every page in the folder.
*
* A typical page in a Tab contains a two column composite.
* The left column contains the "Example" group. The right
* column contains "Control" group. The "Control" group
* contains controls that allow the user to interact with
* the example control. The "Control" group typically
* contains a "Style", "Other" and "Size" group. Subclasses
* can override these defaults to augment a group or stop
* a group from being created.
*/
abstract class Tab {
/* Common control buttons */
Button borderButton, enabledButton, visibleButton;
Button preferredButton, tooSmallButton, smallButton, largeButton, fillButton;
/* Common groups and composites */
Composite tabFolderPage;
Group exampleGroup, controlGroup, listenersGroup, otherGroup, sizeGroup, styleGroup, colorGroup;
/* Controlling instance */
final ControlExample instance;
/* Sizing constants for the "Size" group */
static final int TOO_SMALL_SIZE = 10;
static final int SMALL_SIZE = 50;
static final int LARGE_SIZE = 100;
/* Right-to-left support */
static final boolean RTL_SUPPORT_ENABLE = false;
Group orientationGroup;
Button rtlButton, ltrButton, defaultOrietationButton;
/* Controls and resources for the "Colors" group */
Button foregroundButton, backgroundButton, fontButton;
Image foregroundImage, backgroundImage;
Color foregroundColor, backgroundColor;
Font font;
/* Event logging variables and controls */
Text eventConsole;
boolean logging = false;
boolean [] eventsFilter;
/* Set/Get API controls */
Combo nameCombo;
Label returnTypeLabel;
Button getButton, setButton;
Text setText, getText;
static final String [] EVENT_NAMES = {
"None",
"KeyDown", "KeyUp",
"MouseDown", "MouseUp", "MouseMove", "MouseEnter", "MouseExit", "MouseDoubleClick",
"Paint", "Move", "Resize", "Dispose",
"Selection", "DefaultSelection",
"FocusIn", "FocusOut",
"Expand", "Collapse",
"Iconify", "Deiconify", "Close",
"Show", "Hide",
"Modify", "Verify",
"Activate", "Deactivate",
"Help", "DragDetect", "Arm", "Traverse", "MouseHover",
"HardKeyDown", "HardKeyUp",
"MenuDetect",
"SetData",
"MouseWheel",
};
/**
* Creates the Tab within a given instance of ControlExample.
*/
Tab(ControlExample instance) {
this.instance = instance;
}
/**
* Creates the "Control" group. The "Control" group
* is typically the right hand column in the tab.
*/
void createControlGroup () {
/*
* Create the "Control" group. This is the group on the
* right half of each example tab. It consists of the
* "Style" group, the "Other" group and the "Size" group.
*/
controlGroup = new Group (tabFolderPage, SWT.NONE);
controlGroup.setLayout (new GridLayout (2, true));
controlGroup.setLayoutData (new GridData(SWT.FILL, SWT.FILL, false, false));
controlGroup.setText (ControlExample.getResourceString("Parameters"));
/* Create individual groups inside the "Control" group */
createStyleGroup ();
createOtherGroup ();
createSetGetGroup();
createSizeGroup ();
createColorGroup ();
if (RTL_SUPPORT_ENABLE) {
createOrientationGroup ();
}
/*
* For each Button child in the style group, add a selection
* listener that will recreate the example controls. If the
* style group button is a RADIO button, ensure that the radio
* button is selected before recreating the example controls.
* When the user selects a RADIO button, the current RADIO
* button in the group is deselected and the new RADIO button
* is selected automatically. The listeners are notified for
* both these operations but typically only do work when a RADIO
* button is selected.
*/
SelectionListener selectionListener = new SelectionAdapter () {
public void widgetSelected (SelectionEvent event) {
if ((event.widget.getStyle () & SWT.RADIO) != 0) {
if (!((Button) event.widget).getSelection ()) return;
}
recreateExampleWidgets ();
}
};
Control [] children = styleGroup.getChildren ();
for (int i=0; i<children.length; i++) {
if (children [i] instanceof Button) {
Button button = (Button) children [i];
button.addSelectionListener (selectionListener);
}
}
if (RTL_SUPPORT_ENABLE) {
rtlButton.addSelectionListener (selectionListener);
ltrButton.addSelectionListener (selectionListener);
defaultOrietationButton.addSelectionListener (selectionListener);
}
}
/**
* Append the Set/Get API controls to the "Other" group.
*/
void createSetGetGroup() {
/*
* Create the button to access set/get API functionality.
*/
final String [] methodNames = getMethodNames ();
if (methodNames != null) {
Button setGetButton = new Button (otherGroup, SWT.PUSH);
setGetButton.setText (ControlExample.getResourceString ("Set_Get"));
setGetButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
setGetButton.addSelectionListener (new SelectionAdapter() {
public void widgetSelected (SelectionEvent e) {
Button button = (Button)e.widget;
Point pt = button.getLocation();
pt = e.display.map(button, null, pt);
createSetGetDialog(pt.x, pt.y, methodNames);
}
});
}
}
/**
* Creates the "Control" widget children.
* Subclasses override this method to augment
* the standard controls created in the "Style",
* "Other" and "Size" groups.
*/
void createControlWidgets () {
}
/**
* Creates the "Colors" group. This is typically
* a child of the "Control" group. Subclasses override
* this method to customize and set system colors.
*/
void createColorGroup () {
/* Create the group */
colorGroup = new Group(controlGroup, SWT.NONE);
colorGroup.setLayout (new GridLayout (2, false));
colorGroup.setLayoutData (new GridData (GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
colorGroup.setText (ControlExample.getResourceString ("Colors"));
new Label (colorGroup, SWT.NONE).setText (ControlExample.getResourceString ("Foreground_Color"));
foregroundButton = new Button (colorGroup, SWT.PUSH);
new Label (colorGroup, SWT.NONE).setText (ControlExample.getResourceString ("Background_Color"));
backgroundButton = new Button (colorGroup, SWT.PUSH);
fontButton = new Button (colorGroup, SWT.PUSH);
fontButton.setText(ControlExample.getResourceString("Font"));
fontButton.setLayoutData(new GridData (GridData.HORIZONTAL_ALIGN_FILL));
Button defaultsButton = new Button (colorGroup, SWT.PUSH);
defaultsButton.setText(ControlExample.getResourceString("Defaults"));
Shell shell = controlGroup.getShell ();
final ColorDialog foregroundDialog = new ColorDialog (shell);
final ColorDialog backgroundDialog = new ColorDialog (shell);
final FontDialog fontDialog = new FontDialog (shell);
/* Create images to display current colors */
int imageSize = 12;
Display display = shell.getDisplay ();
foregroundImage = new Image (display, imageSize, imageSize);
backgroundImage = new Image (display, imageSize, imageSize);
/* Add listeners to set the colors and font */
foregroundButton.setImage(foregroundImage); // sets the size of the button
foregroundButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
Color oldColor = foregroundColor;
if (oldColor == null) {
Control [] controls = getExampleWidgets ();
if (controls.length > 0) oldColor = controls [0].getForeground ();
}
if (oldColor != null) foregroundDialog.setRGB(oldColor.getRGB()); // seed dialog with current color
RGB rgb = foregroundDialog.open();
if (rgb == null) return;
oldColor = foregroundColor; // save old foreground color to dispose when done
foregroundColor = new Color (event.display, rgb);
setExampleWidgetForeground ();
if (oldColor != null) oldColor.dispose ();
}
});
backgroundButton.setImage(backgroundImage); // sets the size of the button
backgroundButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
Color oldColor = backgroundColor;
if (oldColor == null) {
Control [] controls = getExampleWidgets ();
if (controls.length > 0) oldColor = controls [0].getBackground (); // seed dialog with current color
}
if (oldColor != null) backgroundDialog.setRGB(oldColor.getRGB());
RGB rgb = backgroundDialog.open();
if (rgb == null) return;
oldColor = backgroundColor; // save old background color to dispose when done
backgroundColor = new Color (event.display, rgb);
setExampleWidgetBackground ();
if (oldColor != null) oldColor.dispose ();
}
});
fontButton.addSelectionListener(new SelectionAdapter () {
public void widgetSelected (SelectionEvent event) {
Font oldFont = font;
if (oldFont == null) {
Control [] controls = getExampleWidgets ();
if (controls.length > 0) oldFont = controls [0].getFont ();
}
if (oldFont != null) fontDialog.setFontList(oldFont.getFontData()); // seed dialog with current font
FontData fontData = fontDialog.open ();
if (fontData == null) return;
oldFont = font; // dispose old font when done
font = new Font (event.display, fontData);
setExampleWidgetFont ();
setExampleWidgetSize ();
if (oldFont != null) oldFont.dispose ();
}
});
defaultsButton.addSelectionListener(new SelectionAdapter () {
public void widgetSelected (SelectionEvent e) {
resetColorsAndFonts ();
}
});
shell.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent event) {
if (foregroundImage != null) foregroundImage.dispose();
if (backgroundImage != null) backgroundImage.dispose();
if (foregroundColor != null) foregroundColor.dispose();
if (backgroundColor != null) backgroundColor.dispose();
if (font != null) font.dispose();
foregroundColor = null;
backgroundColor = null;
font = null;
}
});
}
/**
* Creates the "Other" group. This is typically
* a child of the "Control" group.
*/
void createOtherGroup () {
/* Create the group */
otherGroup = new Group (controlGroup, SWT.NONE);
otherGroup.setLayout (new GridLayout ());
otherGroup.setLayoutData (new GridData (SWT.FILL, SWT.FILL, false, false));
otherGroup.setText (ControlExample.getResourceString("Other"));
/* Create the controls */
enabledButton = new Button(otherGroup, SWT.CHECK);
enabledButton.setText(ControlExample.getResourceString("Enabled"));
visibleButton = new Button(otherGroup, SWT.CHECK);
visibleButton.setText(ControlExample.getResourceString("Visible"));
/* Add the listeners */
enabledButton.addSelectionListener (new SelectionAdapter () {
public void widgetSelected (SelectionEvent event) {
setExampleWidgetEnabled ();
}
});
visibleButton.addSelectionListener (new SelectionAdapter () {
public void widgetSelected (SelectionEvent event) {
setExampleWidgetVisibility ();
}
});
/* Set the default state */
enabledButton.setSelection(true);
visibleButton.setSelection(true);
}
/**
* Create the event console popup menu.
*/
void createEventConsolePopup () {
Menu popup = new Menu (eventConsole.getShell (), SWT.POP_UP);
eventConsole.setMenu (popup);
MenuItem cut = new MenuItem (popup, SWT.PUSH);
cut.setText (ControlExample.getResourceString("MenuItem_Cut"));
cut.addListener (SWT.Selection, new Listener () {
public void handleEvent (Event event) {
eventConsole.cut ();
}
});
MenuItem copy = new MenuItem (popup, SWT.PUSH);
copy.setText (ControlExample.getResourceString("MenuItem_Copy"));
copy.addListener (SWT.Selection, new Listener () {
public void handleEvent (Event event) {
eventConsole.copy ();
}
});
MenuItem paste = new MenuItem (popup, SWT.PUSH);
paste.setText (ControlExample.getResourceString("MenuItem_Paste"));
paste.addListener (SWT.Selection, new Listener () {
public void handleEvent (Event event) {
eventConsole.paste ();
}
});
new MenuItem (popup, SWT.SEPARATOR);
MenuItem selectAll = new MenuItem (popup, SWT.PUSH);
selectAll.setText(ControlExample.getResourceString("MenuItem_SelectAll"));
selectAll.addListener (SWT.Selection, new Listener () {
public void handleEvent (Event event) {
eventConsole.selectAll ();
}
});
}
/**
* Creates the "Example" group. The "Example" group
* is typically the left hand column in the tab.
*/
void createExampleGroup () {
exampleGroup = new Group (tabFolderPage, SWT.NONE);
exampleGroup.setLayout (new GridLayout ());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -