📄 tab.java
字号:
exampleGroup.setLayoutData (new GridData (SWT.FILL, SWT.FILL, true, false));
}
/**
* Creates the "Example" widget children of the "Example" group.
* Subclasses override this method to create the particular
* example control.
*/
void createExampleWidgets () {
/* Do nothing */
}
/**
* Creates and opens the "Listener selection" dialog.
*/
void createListenerSelectionDialog () {
final Shell dialog = new Shell (tabFolderPage.getShell (), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
dialog.setText (ControlExample.getResourceString ("Select_Listeners"));
dialog.setLayout (new GridLayout (2, false));
final Table table = new Table (dialog, SWT.BORDER | SWT.V_SCROLL | SWT.CHECK);
GridData data = new GridData(GridData.FILL_BOTH);
data.verticalSpan = 2;
table.setLayoutData(data);
for (int i = 0; i < EVENT_NAMES.length; i++) {
TableItem item = new TableItem (table, SWT.NONE);
item.setText (EVENT_NAMES[i]);
item.setChecked (eventsFilter[i]);
}
final String [] customNames = getCustomEventNames ();
for (int i = 0; i < customNames.length; i++) {
TableItem item = new TableItem (table, SWT.NONE);
item.setText (customNames[i]);
item.setChecked (eventsFilter[EVENT_NAMES.length + i]);
}
Button selectAll = new Button (dialog, SWT.PUSH);
selectAll.setText(ControlExample.getResourceString ("Select_All"));
selectAll.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
selectAll.addSelectionListener (new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
TableItem [] items = table.getItems();
for (int i = 0; i < EVENT_NAMES.length; i++) {
items[i].setChecked(true);
}
for (int i = 0; i < customNames.length; i++) {
items[EVENT_NAMES.length + i].setChecked(true);
}
}
});
Button deselectAll = new Button (dialog, SWT.PUSH);
deselectAll.setText(ControlExample.getResourceString ("Deselect_All"));
deselectAll.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_BEGINNING));
deselectAll.addSelectionListener (new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
TableItem [] items = table.getItems();
for (int i = 0; i < EVENT_NAMES.length; i++) {
items[i].setChecked(false);
}
for (int i = 0; i < customNames.length; i++) {
items[EVENT_NAMES.length + i].setChecked(false);
}
}
});
new Label(dialog, SWT.NONE); /* Filler */
Button ok = new Button (dialog, SWT.PUSH);
ok.setText(ControlExample.getResourceString ("OK"));
dialog.setDefaultButton(ok);
ok.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
ok.addSelectionListener (new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
TableItem [] items = table.getItems();
for (int i = 0; i < EVENT_NAMES.length; i++) {
eventsFilter[i] = items[i].getChecked();
}
for (int i = 0; i < customNames.length; i++) {
eventsFilter[EVENT_NAMES.length + i] = items[EVENT_NAMES.length + i].getChecked();
}
dialog.dispose();
}
});
dialog.pack ();
dialog.open ();
while (! dialog.isDisposed()) {
if (! dialog.getDisplay().readAndDispatch()) dialog.getDisplay().sleep();
}
}
/**
* Creates the "Listeners" group. The "Listeners" group
* goes below the "Example" and "Control" groups.
*/
void createListenersGroup () {
listenersGroup = new Group (tabFolderPage, SWT.NONE);
listenersGroup.setLayout (new GridLayout (3, false));
listenersGroup.setLayoutData (new GridData (SWT.FILL, SWT.FILL, false, true, 2, 1));
listenersGroup.setText (ControlExample.getResourceString ("Listeners"));
/*
* Create the button to access the 'Listeners' dialog.
*/
Button listenersButton = new Button (listenersGroup, SWT.PUSH);
listenersButton.setText (ControlExample.getResourceString ("Select_Listeners"));
listenersButton.addSelectionListener (new SelectionAdapter() {
public void widgetSelected (SelectionEvent e) {
createListenerSelectionDialog ();
recreateExampleWidgets ();
}
});
/*
* Create the checkbox to add/remove listeners to/from the example widgets.
*/
final Button listenCheckbox = new Button (listenersGroup, SWT.CHECK);
listenCheckbox.setText (ControlExample.getResourceString ("Listen"));
listenCheckbox.addSelectionListener (new SelectionAdapter () {
public void widgetSelected(SelectionEvent e) {
logging = listenCheckbox.getSelection ();
recreateExampleWidgets ();
}
});
/*
* Create the button to clear the text.
*/
Button clearButton = new Button (listenersGroup, SWT.PUSH);
clearButton.setText (ControlExample.getResourceString ("Clear"));
clearButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
clearButton.addSelectionListener (new SelectionAdapter() {
public void widgetSelected (SelectionEvent e) {
eventConsole.setText ("");
}
});
/* Initialize the eventsFilter to log all events. */
int customEventCount = getCustomEventNames ().length;
eventsFilter = new boolean [EVENT_NAMES.length + customEventCount];
for (int i = 0; i < EVENT_NAMES.length + customEventCount; i++) {
eventsFilter [i] = true;
}
/* Create the event console Text. */
eventConsole = new Text (listenersGroup, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
GridData data = new GridData (GridData.FILL_BOTH);
data.horizontalSpan = 3;
data.heightHint = 80;
eventConsole.setLayoutData (data);
createEventConsolePopup ();
eventConsole.addKeyListener (new KeyAdapter () {
public void keyPressed (KeyEvent e) {
if ((e.keyCode == 'A' || e.keyCode == 'a') && (e.stateMask & SWT.MOD1) != 0) {
eventConsole.selectAll ();
e.doit = false;
}
}
});
}
/**
* Returns a list of set/get API method names (without the set/get prefix)
* that can be used to set/get values in the example control(s).
*/
String[] getMethodNames() {
return null;
}
void createSetGetDialog(int x, int y, String[] methodNames) {
final Shell dialog = new Shell(eventConsole.getShell (), SWT.DIALOG_TRIM | SWT.RESIZE | SWT.MODELESS);
dialog.setLayout(new GridLayout(2, false));
dialog.setText(getTabText() + " " + ControlExample.getResourceString ("Set_Get"));
nameCombo = new Combo(dialog, SWT.NONE);
nameCombo.setItems(methodNames);
nameCombo.setText(methodNames[0]);
nameCombo.setVisibleItemCount(methodNames.length);
nameCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
nameCombo.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
resetLabels();
}
});
returnTypeLabel = new Label(dialog, SWT.NONE);
returnTypeLabel.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, false, false));
setButton = new Button(dialog, SWT.PUSH);
setButton.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, false, false));
setButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
setValue();
}
});
setText = new Text(dialog, SWT.SINGLE | SWT.BORDER);
setText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
getButton = new Button(dialog, SWT.PUSH);
getButton.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, false, false));
getButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
getValue();
}
});
getText = new Text(dialog, SWT.MULTI | SWT.BORDER | SWT.READ_ONLY | SWT.H_SCROLL | SWT.V_SCROLL);
GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
data.widthHint = 240;
data.heightHint = 200;
getText.setLayoutData(data);
resetLabels();
dialog.setDefaultButton(setButton);
dialog.pack();
dialog.setLocation(x, y);
dialog.open();
}
void resetLabels() {
String methodRoot = nameCombo.getText();
returnTypeLabel.setText(parameterInfo(methodRoot));
setButton.setText(setMethodName(methodRoot));
getButton.setText("get" + methodRoot);
setText.setText("");
getText.setText("");
getValue();
setText.setFocus();
}
String setMethodName(String methodRoot) {
return "set" + methodRoot;
}
String parameterInfo(String methodRoot) {
String typeName = null;
Class returnType = getReturnType(methodRoot);
boolean isArray = returnType.isArray();
if (isArray) {
typeName = returnType.getComponentType().getName();
} else {
typeName = returnType.getName();
}
String typeNameString = typeName;
int index = typeName.lastIndexOf('.');
if (index != -1 && index+1 < typeName.length()) typeNameString = typeName.substring(index+1);
String info = ControlExample.getResourceString("Info_" + typeNameString + (isArray ? "A" : ""));
if (isArray) {
typeNameString += "[]";
}
return ControlExample.getResourceString("Parameter_Info", new Object[] {typeNameString, info});
}
void getValue() {
String methodName = "get" + nameCombo.getText();
getText.setText("");
Control[] controls = getExampleWidgets();
for (int i = 0; i < controls.length; i++) {
try {
java.lang.reflect.Method method = controls[i].getClass().getMethod(methodName, null);
Object result = method.invoke(controls[i], null);
if (result == null) {
getText.append("null");
} else if (result.getClass().isArray()) {
Object [] arrayResult = (Object[]) result;
for (int j = 0; j < arrayResult.length; j++) {
getText.append(arrayResult[j].toString() + "\n");
}
} else {
getText.append(result.toString());
}
} catch (Exception e) {
getText.append(e.toString());
}
if (i + 1 < controls.length) {
getText.append("\n\n");
}
}
}
Class getReturnType(String methodRoot) {
Class returnType = null;
String methodName = "get" + methodRoot;
Control[] controls = getExampleWidgets();
try {
java.lang.reflect.Method method = controls[0].getClass().getMethod(methodName, null);
returnType = method.getReturnType();
} catch (Exception e) {
}
return returnType;
}
void setValue() {
/* The parameter type must be the same as the get method's return type */
String methodRoot = nameCombo.getText();
Class returnType = getReturnType(methodRoot);
String methodName = setMethodName(methodRoot);
String value = setText.getText();
Control[] controls = getExampleWidgets();
for (int i = 0; i < controls.length; i++) {
try {
java.lang.reflect.Method method = controls[i].getClass().getMethod(methodName, new Class[] {returnType});
String typeName = returnType.getName();
Object[] parameter = null;
if (typeName.equals("int")) {
parameter = new Object[] {new Integer(value)};
} else if (typeName.equals("long")) {
parameter = new Object[] {new Long(value)};
} else if (typeName.equals("char")) {
parameter = new Object[] {value.length() == 1 ? new Character(value.charAt(0)) : new Character('\0')};
} else if (typeName.equals("boolean")) {
parameter = new Object[] {new Boolean(value)};
} else if (typeName.equals("java.lang.String")) {
parameter = new Object[] {value};
} else if (typeName.equals("org.eclipse.swt.graphics.Point")) {
String xy[] = value.split(",");
parameter = new Object[] {new Point(new Integer(xy[0]).intValue(),new Integer(xy[1]).intValue())};
} else if (typeName.equals("[Ljava.lang.String;")) {
parameter = new Object[] {value.split(",")};
} else {
parameter = parameterForType(typeName, value, controls[i]);
}
method.invoke(controls[i], parameter);
} catch (Exception e) {
getText.setText(e.toString());
}
}
}
Object[] parameterForType(String typeName, String value, Control control) {
return new Object[] {value};
}
void createOrientationGroup () {
/* Create Orientation group*/
orientationGroup = new Group (controlGroup, SWT.NONE);
orientationGroup.setLayout (new GridLayout());
orientationGroup.setLayoutData (new GridData (GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
orientationGroup.setText (ControlExample.getResourceString("Orientation"));
defaultOrietationButton = new Button (orientationGroup, SWT.RADIO);
defaultOrietationButton.setText (ControlExample.getResourceString("Default"));
defaultOrietationButton.setSelection (true);
ltrButton = new Button (orientationGroup, SWT.RADIO);
ltrButton.setText ("SWT.LEFT_TO_RIGHT");
rtlButton = new Button (orientationGroup, SWT.RADIO);
rtlButton.setText ("SWT.RIGHT_TO_LEFT");
}
/**
* Creates the "Size" group. The "Size" group contains
* controls that allow the user to change the size of
* the example widgets.
*/
void createSizeGroup () {
/* Create the group */
sizeGroup = new Group (controlGroup, SWT.NONE);
sizeGroup.setLayout (new GridLayout());
sizeGroup.setLayoutData (new GridData (GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
sizeGroup.setText (ControlExample.getResourceString("Size"));
/* Create the controls */
/*
* The preferred size of a widget is the size returned
* by widget.computeSize (SWT.DEFAULT, SWT.DEFAULT).
* This size is defined on a widget by widget basis.
* Many widgets will attempt to display their contents.
*/
preferredButton = new Button (sizeGroup, SWT.RADIO);
preferredButton.setText (ControlExample.getResourceString("Preferred"));
tooSmallButton = new Button (sizeGroup, SWT.RADIO);
tooSmallButton.setText (TOO_SMALL_SIZE + " X " + TOO_SMALL_SIZE);
smallButton = new Button(sizeGroup, SWT.RADIO);
smallButton.setText (SMALL_SIZE + " X " + SMALL_SIZE);
largeButton = new Button (sizeGroup, SWT.RADIO);
largeButton.setText (LARGE_SIZE + " X " + LARGE_SIZE);
fillButton = new Button (sizeGroup, SWT.RADIO);
fillButton.setText (ControlExample.getResourceString("Fill"));
/* Add the listeners */
SelectionAdapter selectionListener = new SelectionAdapter () {
public void widgetSelected (SelectionEvent event) {
if (!((Button) event.widget).getSelection ()) return;
setExampleWidgetSize ();
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -