📄 tab.java
字号:
package org.eclipse.swt.examples.controlexample;
/*
* (c) Copyright IBM Corp. 2000, 2002. * This file is 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
*/
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", "Display" 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, displayGroup, 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;
/* Common controls for the "Colors" group */
Button backgroundButton, foregroundButton, fontButton;
Color backgroundColor, foregroundColor;
Font font;
/**
* 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
* left half of each example tab. It consists of the
* style group, the display group and the size group.
*/
controlGroup = new Group (tabFolderPage, SWT.NONE);
GridLayout gridLayout= new GridLayout ();
controlGroup.setLayout (gridLayout);
gridLayout.numColumns = 2;
gridLayout.makeColumnsEqualWidth = true;
controlGroup.setLayoutData (new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
controlGroup.setText (ControlExample.getResourceString("Parameters"));
/* Create individual groups inside the "Control" group */
createStyleGroup ();
createDisplayGroup ();
createSizeGroup ();
createColorGroup ();
/*
* 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);
}
}
}
/**
* Creates the "Control" widget children.
* Subclasses override this method to augment
* the standard controls created in the "Style",
* "Display" and "Size" groups.
*/
void createControlWidgets () {
}
/**
* Creates the "Color" 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.NULL);
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"));
Shell shell = backgroundButton.getShell ();
final ColorDialog backgroundDialog = new ColorDialog (shell);
final ColorDialog foregroundDialog = new ColorDialog (shell);
final FontDialog fontDialog = new FontDialog (shell);
/* Create images to display current colors */
int imageSize = 12;
Display display = shell.getDisplay ();
final Image backgroundImage = new Image (display, imageSize, imageSize);
final Image foregroundImage = new Image (display, imageSize, imageSize);
/* Add listeners to set the colors and font */
backgroundButton.setImage(backgroundImage);
backgroundButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
RGB rgb = backgroundDialog.open();
if (rgb == null) return;
Color oldColor = backgroundColor;
backgroundColor = new Color (backgroundButton.getDisplay(), rgb);
setExampleWidgetBackground ();
if (oldColor != null) oldColor.dispose ();
}
});
foregroundButton.setImage(foregroundImage);
foregroundButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
RGB rgb = foregroundDialog.open();
if (rgb == null) return;
Color oldColor = foregroundColor;
foregroundColor = new Color (foregroundButton.getDisplay(), rgb);
setExampleWidgetForeground ();
if (oldColor != null) oldColor.dispose ();
}
});
fontButton.addSelectionListener(new SelectionAdapter () {
public void widgetSelected (SelectionEvent e) {
FontData fontData = fontDialog.open ();
if (fontData == null) return;
Font oldFont = font;
font = new Font (fontButton.getDisplay(), fontData);
setExampleWidgetFont ();
setExampleWidgetSize ();
if (oldFont != null) oldFont.dispose ();
}
});
backgroundButton.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent event) {
if (backgroundImage != null) backgroundImage.dispose();
if (foregroundImage != null) foregroundImage.dispose();
if (backgroundColor != null) backgroundColor.dispose();
if (foregroundColor != null) foregroundColor.dispose();
if (font != null) font.dispose();
backgroundColor = null;
foregroundColor = null;
font = null;
}
});
}
/**
* Creates the "Display" group. This is typically
* a child of the "Control" group.
*/
void createDisplayGroup () {
/* Create the group */
displayGroup = new Group (controlGroup, SWT.NONE);
displayGroup.setLayout (new GridLayout ());
displayGroup.setLayoutData (new GridData (GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
displayGroup.setText (ControlExample.getResourceString("State"));
/* Create the controls */
enabledButton = new Button(displayGroup, SWT.CHECK);
enabledButton.setText(ControlExample.getResourceString("Enabled"));
visibleButton = new Button(displayGroup, 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);
}
/**
* Creates the "Example" group. The "Example" group
* is typically the left hand column in the tab.
*/
void createExampleGroup () {
exampleGroup = new Group (tabFolderPage, SWT.NONE);
GridLayout gridLayout = new GridLayout ();
exampleGroup.setLayout (gridLayout);
exampleGroup.setLayoutData (new GridData (GridData.FILL_BOTH));
}
/**
* Creates the "Example" widget children of the "Example" group.
* Subclasses override this method to create the particular
* example control.
*/
void createExampleWidgets () {
/* Do nothing */
}
/**
* Creates the "Size" group. The "Size" group contains
* controls that allow the user to change the size of
* the example widgets.
*/
void createSizeGroup () {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -