/*******************************************************************************
* Copyright (c) 2004 Berthold Daum. All rights reserved. This program and the
* accompanying materials are 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
*
* Contributors: Berthold Daum
******************************************************************************/
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.TreeAdapter;
import org.eclipse.swt.events.TreeEvent;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Slider;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
public class WidgetTest2 {
public WidgetTest2() {
super();
}
/** * MessageBox ** */
public static void main(String[] args) {
// Create Display instance
final Display display = new Display();
// Create top level shell (pass display as parent)
final Shell toplevelShell = new Shell(display);
// Set title line
toplevelShell.setText("TopLevel.titleLine");
// Show shell
toplevelShell.open();
while (true) {
// Create message box
MessageBox box = new MessageBox(toplevelShell, SWT.RETRY
| SWT.CANCEL | SWT.APPLICATION_MODAL | SWT.ICON_QUESTION);
// Set title
box.setText("Test");
// Set message
box.setMessage("Do you want to try again?");
// Open message box
if (box.open() == SWT.CANCEL)
break;
}
}
/** * Composite ** */
private static final int X = 0;
private static final int Y = 0;
private static final int WIDTH = 300;
private static final int HEIGHT = 200;
public static Composite createComposite(Composite parent) {
/** * Composite ** */
// Create new Composite instance
final Composite composite = new Composite(parent, 0);
// Get properties from the containing composite
composite.setBackground(parent.getBackground());
composite.setForeground(parent.getForeground());
composite.setFont(parent.getFont());
// Set position and size
composite.setBounds(X, Y, WIDTH, HEIGHT);
return composite;
}
/** * Buttons ** */
public static Button createButton(Composite composite) {
final Button button = new Button(composite, SWT.PUSH);
button.setText("Press me!");
// React to click events
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
System.out.println("Key was pressed");
}
});
return button;
}
public static Button createImageButton(Composite composite) {
final Button button = new Button(composite, SWT.PUSH);
Display display = composite.getDisplay();
final Image image = new Image(display, "images/button1.gif");
button.setImage(image);
// React to click events
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
System.out.println("Key was pressed");
}
});
// Dispose image when button is disposed
button.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
image.dispose();
}
});
return button;
}
/** * Sliders ** */
public static Slider createSlider(Composite composite) {
final Slider slider = new Slider(composite, SWT.HORIZONTAL);
// Minimalwert setzen
slider.setMinimum(0);
// Maximalwert setzen
slider.setMaximum(1000);
// Inkrement fur Pfeiltasten setzen
slider.setIncrement(50);
// Inkrement fur Klick auf Schiebeflache setzen
slider.setPageIncrement(200);
// aktuelle Position setzen
slider.setSelection(500);
// GroBe des Handgriffs setzen
slider.setThumb(200);
// Auf Schiebeereignisse reagieren
slider.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
System.out.println("Schieberegler verstellt: "
+ slider.getSelection());
}
});
return slider;
}
/** * Text fields ** */
public static Text createText(Composite composite) {
final Text text = new Text(composite, SWT.SINGLE);
text.setText("Input text");
text.addSelectionListener(new SelectionAdapter() {
public void widgetDefaultSelected(SelectionEvent e) {
System.out.println("Enter was pressed: " + text.getSelection());
}
});
text.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
System.out
.println("Text after modification: " + text.getText());
}
});
text.addVerifyListener(new VerifyListener() {
public void verifyText(VerifyEvent e) {
String s = text.getText();
System.out.println("Text before modification: " + s);
// Veto: Text longer than 10 characters is prohibited
if (s.length() >= 10)
e.doit = false;
}
});
return text;
}
/** * Tables ** */
public static Table createTable(Composite composite) {
final Table table = new Table(composite, SWT.SINGLE | SWT.H_SCROLL
| SWT.V_SCROLL | SWT.BORDER | SWT.FULL_SELECTION);
// Create three table columns
final TableColumn col1 = new TableColumn(table, SWT.LEFT);
col1.setText("Column 1");
col1.setWidth(80);
final TableColumn col2 = new TableColumn(table, SWT.LEFT);
col2.setText("Column 2");
col2.setWidth(80);
final TableColumn col3 = new TableColumn(table, SWT.LEFT);
col3.setText("Column 3");
col3.setWidth(80);
// Make column headers and grid lines visible
table.setHeaderVisible(true);
table.setLinesVisible(true);
// Create table rows
final TableItem item1 = new TableItem(table, 0);
item1.setText(new String[] { "a", "b", "c" });
final TableItem item2 = new TableItem(table, 0);
item2.setText(new String[] { "d", "c", "e" });
// Add selection listeners
table.addSelectionListener(new SelectionAdapter() {
public void widgetDefaultSelected(SelectionEvent e) {
processSelection("Enter was pressed: ");
}
public void widgetSelected(SelectionEvent e) {
processSelection("Table element was selected: ");
}
private void processSelection(String message) {
// Get selected table row
TableItem[] selection = table.getSelection();
// Because of SWT.SINGLE only one row was selected
TableItem selectedRow = selection[0];
// Format the table elements for output
String s = selectedRow.getText(0) + ", "
+ selectedRow.getText(1) + ", "
+ selectedRow.getText(2);
System.out.println(message + s);
}
});
return table;
}
/** * Lists ** */
public static List createList(Composite composite) {
final List list = new List(composite, SWT.MULTI);
list.add("Element1");
list.add("Element2");
list.add("Element3");
list.addSelectionListener(new SelectionAdapter() {
public void widgetDefaultSelected(SelectionEvent e) {
processSelection("Enter was pressed: ");
}
public void widgetSelected(SelectionEvent e) {
processSelection("List entry was selected: ");
}
private void processSelection(String message) {
// Get selected entries
String[] selection = list.getSelection();
// Format entries for output
StringBuffer sb = new StringBuffer();
for (int i = 0; i < selection.length; i++) {
sb.append(selection[i] + " ");
}
System.out.println(message + sb);
}
});
return list;
}
/** * Combos ** */
public static Combo createCombo(Composite composite) {
final Combo combo = new Combo(composite, SWT.DROP_DOWN);
// Listenelement erzeugen
combo.add("Element1");
combo.add("Element2");
combo.add("Element3");
// Vorbelegung des Textfeldes
combo.setText("Select");
// SelectionListener hinzufugen
combo.addSelectionListener(new SelectionAdapter() {
public void widgetDefaultSelected(SelectionEvent e) {
System.out.println("Enter gedruckt: " + combo.getText());
}
public void widgetSelected(SelectionEvent e) {
System.out.println("Listeneintrag ausgewahlt: "
+ combo.getText());
}
});
// ModifyListener hinzufugen
combo.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
System.out
.println("Text wurde modifiziert: " + combo.getText());
}
});
return combo;
}
/** * Trees ** */
public static Tree createTree(Composite composite) {
final Tree tree = new Tree(composite, SWT.SINGLE);
// Oberste Knotenebene erzeugen
final TreeItem node1 = new TreeItem(tree, SWT.NULL);
node1.setText("Knoten 1");
final TreeItem node2 = new TreeItem(tree, SWT.NULL);
node2.setText("Knoten 2");
final TreeItem node3 = new TreeItem(tree, SWT.NULL);
node3.setText("Knoten 3");
// Zweite Knotenebene erzeugen
final TreeItem node11 = new TreeItem(node1, SWT.NULL);
node11.setText("Knoten 1.1");
final TreeItem node12 = new TreeItem(node1, SWT.NULL);
node12.setText("Knoten 1.2");
// SelectionListener hinzufugen
tree.addSelectionListener(new SelectionAdapter() {
public void widgetDefaultSelected(SelectionEvent e) {
System.out.println("Enter gedruckt: "
+ tree.getSelection()[0].getText());
}
public void widgetSelected(SelectionEvent e) {
System.out.println("Baumknoteneintrag ausgewahlt: "
+ tree.getSelection()[0].getText());
}
});
// TreeListener hinzufugen
tree.addTreeListener(new TreeAdapter() {
public void treeCollapsed(TreeEvent e) {
System.out.println("Knoten kollabiert: "
+ ((TreeItem) e.item).getText());
}
public void treeExpanded(TreeEvent e) {
System.out.println("Knoten expandiert: "
+ ((TreeItem) e.item).getText());
}
});
return tree;
}
}