📄 tab.java
字号:
/*******************************************************************************
* Copyright (c) 2000, 2003 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.layoutexample;
import java.util.Vector;
import org.eclipse.swt.*;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
/**
* <code>Tab</code> is the abstract superclass of every page
* in the example's tab folder. Each page in the tab folder
* displays a layout, and allows the user to manipulate the
* layout.
*
* A typical page in a Tab contains a two column composite.
* The left column contains the layout group, which contains
* the "layout composite" (the one that has the example layout).
* The right column contains the "control" group. The "control"
* group allows the user to interact with the example. Typical
* operations are modifying layout parameters, adding children
* to the "layout composite", and modifying child layout data.
* The "Code" button in the "control" group opens a new window
* containing code that will regenerate the layout. This code
* (or parts of it) can be selected and copied to the clipboard.
*/
abstract class Tab {
/* Common groups and composites */
Composite tabFolderPage;
SashForm sash;
Group layoutGroup, controlGroup, childGroup;
/* The composite that contains the example layout */
Composite layoutComposite;
/* Common controls for modifying the example layout */
String [] names;
Control [] children;
Button size, add, delete, clear, code;
/* Common values for working with TableEditors */
Table table;
int index;
TableItem newItem, lastSelected;
Vector data = new Vector ();
/* Controlling instance */
final LayoutExample instance;
/* Listeners */
SelectionListener selectionListener = new SelectionAdapter () {
public void widgetSelected (SelectionEvent e) {
resetEditors ();
}
};
TraverseListener traverseListener = new TraverseListener () {
public void keyTraversed (TraverseEvent e) {
if (e.detail == SWT.TRAVERSE_RETURN) {
e.doit = false;
resetEditors ();
}
}
};
/**
* Creates the Tab within a given instance of LayoutExample.
*/
Tab(LayoutExample instance) {
this.instance = instance;
}
/**
* Creates the "child" group. This is the group that allows
* you to add children to the layout. It exists within the
* controlGroup.
*/
void createChildGroup () {
childGroup = new Group (controlGroup, SWT.NONE);
childGroup.setText (LayoutExample.getResourceString("Children"));
GridLayout layout = new GridLayout ();
layout.numColumns = 3;
childGroup.setLayout (layout);
GridData data = new GridData (GridData.FILL_BOTH);
data.horizontalSpan = 2;
childGroup.setLayoutData (data);
createChildWidgets ();
}
/**
* Creates the controls for modifying the "children"
* table, and the table itself.
* Subclasses override this method to augment the
* standard table.
*/
void createChildWidgets () {
/* Controls for adding and removing children */
add = new Button (childGroup, SWT.PUSH);
add.setText (LayoutExample.getResourceString ("Add"));
add.setLayoutData(new GridData (GridData.FILL_HORIZONTAL));
delete = new Button (childGroup, SWT.PUSH);
delete.setText (LayoutExample.getResourceString ("Delete"));
delete.setLayoutData(new GridData (GridData.FILL_HORIZONTAL));
delete.addSelectionListener (new SelectionAdapter () {
public void widgetSelected (SelectionEvent e) {
resetEditors ();
int [] selected = table.getSelectionIndices ();
table.remove (selected);
/* Refresh the control indices of the table */
for (int i = 0; i < table.getItemCount(); i++) {
table.getItem (i).setText (0, String.valueOf (i));
}
refreshLayoutComposite ();
layoutComposite.layout (true);
layoutGroup.layout (true);
}
});
clear = new Button (childGroup, SWT.PUSH);
clear.setText (LayoutExample.getResourceString ("Clear"));
clear.setLayoutData(new GridData (GridData.FILL_HORIZONTAL));
clear.addSelectionListener (new SelectionAdapter () {
public void widgetSelected (SelectionEvent e) {
resetEditors ();
children = layoutComposite.getChildren ();
for (int i = 0; i < children.length; i++) {
children [i].dispose ();
}
table.removeAll ();
data.clear ();
children = new Control [0];
layoutGroup.layout (true);
}
});
/* Create the "children" table */
table = new Table (childGroup, SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.HIDE_SELECTION);
table.setLinesVisible (true);
table.setHeaderVisible (true);
GridData gridData = new GridData (GridData.FILL_BOTH);
gridData.horizontalSpan = 3;
gridData.heightHint = 150;
table.setLayoutData (gridData);
table.addTraverseListener (traverseListener);
/* Add columns to the table */
String [] columnHeaders = getLayoutDataFieldNames ();
for (int i = 0; i < columnHeaders.length; i++) {
TableColumn column = new TableColumn(table, SWT.NONE);
column.setText (columnHeaders [i]);
if (i == 0) column.setWidth (20);
else if (i == 1) column.setWidth (80);
else column.pack ();
}
}
/**
* Creates the TableEditor with a CCombo in the first column
* of the table. This CCombo lists all the controls that
* the user can select to place on their layout.
*/
void createComboEditor (CCombo combo, TableEditor comboEditor) {
combo.setItems (new String [] {
"Button", "Canvas", "Combo", "Composite",
"CoolBar", "Group", "Label", "List",
"ProgressBar", "Scale", "Slider", "StyledText",
"Table", "Text", "ToolBar", "Tree"});
combo.setText (newItem.getText (1));
/* Set up editor */
comboEditor.horizontalAlignment = SWT.LEFT;
comboEditor.grabHorizontal = true;
comboEditor.minimumWidth = 50;
comboEditor.setEditor (combo, newItem, 1);
/* Add listener */
combo.addTraverseListener(new TraverseListener() {
public void keyTraversed(TraverseEvent e) {
if (e.detail == SWT.TRAVERSE_TAB_NEXT || e.detail == SWT.TRAVERSE_RETURN) {
resetEditors ();
}
if (e.detail == SWT.TRAVERSE_ESCAPE) {
disposeEditors ();
}
}
});
}
/**
* Creates the "control" group. This is the group on the
* right half of each example tab. It contains controls
* for adding new children to the layoutComposite, and
* for modifying the children's layout data.
*/
void createControlGroup () {
controlGroup = new Group (sash, SWT.NONE);
controlGroup.setText (LayoutExample.getResourceString("Parameters"));
GridLayout layout = new GridLayout ();
layout.numColumns = 2;
controlGroup.setLayout (layout);
size = new Button (controlGroup, SWT.CHECK);
size.setText (LayoutExample.getResourceString ("Preferred_Size"));
size.setSelection (false);
size.addSelectionListener (new SelectionAdapter () {
public void widgetSelected (SelectionEvent e) {
resetEditors ();
if (size.getSelection ()) {
layoutComposite.setLayoutData (new GridData ());
layoutGroup.layout (true);
} else {
layoutComposite.setLayoutData (new GridData (GridData.FILL_BOTH));
layoutGroup.layout (true);
}
}
});
GridData data = new GridData (GridData.FILL_HORIZONTAL);
data.horizontalSpan = 2;
size.setLayoutData (data);
createControlWidgets ();
}
/**
* Creates the "control" widget children.
* Subclasses override this method to augment
* the standard controls created.
*/
void createControlWidgets () {
createChildGroup ();
code = new Button (controlGroup, SWT.PUSH);
code.setText (LayoutExample.getResourceString ("Code"));
GridData gridData = new GridData (GridData.HORIZONTAL_ALIGN_CENTER | GridData.GRAB_HORIZONTAL);
gridData.horizontalSpan = 2;
code.setLayoutData (gridData);
code.addSelectionListener (new SelectionAdapter () {
public void widgetSelected (SelectionEvent e) {
final Shell shell = new Shell ();
shell.setText (LayoutExample.getResourceString ("Generated_Code"));
shell.setLayout (new FillLayout ());
final StyledText text = new StyledText (shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
String layoutCode = generateCode ().toString ();
if (layoutCode.length () == 0) return;
text.setText (layoutCode);
Menu bar = new Menu (shell, SWT.BAR);
shell.setMenuBar (bar);
MenuItem editItem = new MenuItem (bar, SWT.CASCADE);
editItem.setText (LayoutExample.getResourceString ("Edit"));
Menu menu = new Menu (bar);
MenuItem select = new MenuItem (menu, SWT.PUSH);
select.setText (LayoutExample.getResourceString ("Select_All"));
select.setAccelerator (SWT.MOD1 + 'A');
select.addSelectionListener (new SelectionAdapter () {
public void widgetSelected (SelectionEvent e) {
text.selectAll ();
}
});
MenuItem copy = new MenuItem (menu, SWT.PUSH);
copy.setText (LayoutExample.getResourceString ("Copy"));
copy.setAccelerator (SWT.MOD1 + 'C');
copy.addSelectionListener (new SelectionAdapter () {
public void widgetSelected (SelectionEvent e) {
text.copy ();
}
});
MenuItem exit = new MenuItem (menu, SWT.PUSH);
exit.setText (LayoutExample.getResourceString ("Exit"));
exit.addSelectionListener (new SelectionAdapter () {
public void widgetSelected (SelectionEvent e) {
shell.close ();
}
});
editItem.setMenu (menu);
shell.pack ();
shell.setSize (400, 500);
shell.open ();
Display display = shell.getDisplay ();
while (!shell.isDisposed ())
if (!display.readAndDispatch ()) display.sleep ();
}
});
}
/**
* Creates the example layout.
* Subclasses override this method.
*/
void createLayout () {
}
/**
* Creates the composite that contains the example layout.
*/
void createLayoutComposite () {
layoutComposite = new Composite (layoutGroup, SWT.BORDER);
layoutComposite.setLayoutData (new GridData (GridData.FILL_BOTH));
createLayout ();
}
/**
* Creates the layout group. This is the group on the
* left half of each example tab.
*/
void createLayoutGroup () {
layoutGroup = new Group (sash, SWT.NONE);
layoutGroup.setText (LayoutExample.getResourceString("Layout"));
layoutGroup.setLayout (new GridLayout ());
createLayoutComposite ();
}
/**
* Creates the tab folder page.
*
* @param tabFolder org.eclipse.swt.widgets.TabFolder
* @return the new page for the tab folder
*/
Composite createTabFolderPage (TabFolder tabFolder) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -