!!! Listings zu Artikel von Tobias Himstedt
!!! "Schoener fensterln", iX 10/2005, S. 153

!!! Listing 1: HelloWorld

package ix.swt;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class HelloWorld {
    public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    Label label = new Label(shell, SWT.NONE);
    label.setText("Hello");
    label.setBounds(10, 10, 100, 100);
    shell.pack();
    shell.open();
    while (!shell.isDisposed())
    	if (!display.readAndDispatch())
        display.sleep();
    display.dispose();
    shell.dispose();
	}
}

Listing 2: Event-Listener für einen Button 

Button button = new Button(shell, SWT.PUSH);
button.setText("Hier drücken für Kekse");
button.addSelectionListener(new SelectionAdapter() {
	public void widgetSelected(SelectionEvent e) {
		Button button = (Button) e.getSource();
		if (button.getText().equals("Hier drücken für Kekse"))
			button.setText("Kekse!");
		else
			button.setText("Hier drücken für Kekse");
	}
});

!!! Listing 3: Nutzung von GridLayout

Composite composite = new Composite(shell, SWT.NONE);
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 4;
composite.setLayout(gridLayout);
for (int i = 0; i < 15; i++) {
	Button button = new Button(composite, SWT.PUSH);
	button.setText("Button " + i);
	if (i %  4 == 0) {
		// Jeder vierte Button erhält doppelte Breite
		GridData gd = new GridData();
		gd.horizontalSpan = 2;
		gd.horizontalAlignment = GridData.FILL;
		button.setLayoutData(gd);
	}
}

!!! Listing 4: FormLayout

Button b1 = new Button(shell, SWT.PUSH);
b1.setText("Laaaaaanger Button");
FormData fd1 = new FormData();
fd1.top = new FormAttachment(50, 0);
fd1.left = new FormAttachment(20, 0);
b1.setLayoutData(fd1);
	
Button b2 = new Button(shell, SWT.PUSH);
b2.setText("Button 2");
FormData fd2 = new FormData();
fd2.left = new FormAttachment(b1, 0);
fd2.top = new FormAttachment(b1, 0);
b2.setLayoutData(fd2);
	
Button b3 = new Button(shell, SWT.PUSH);
b3.setText("Button 3");
FormData fd3 = new FormData();
fd3.right = new FormAttachment(b2, 0);
fd3.top= new FormAttachment(b2, 0);
b3.setLayoutData(fd3);

!!! Listing 5: Ein TreeViewer

...
TreeViewer tv = new TreeViewer(sShell);

!!! sShell ??

FileTreeContentProvider provider = new FileTreeContentProvider();
tv.setContentProvider(provider);
tv.setInput( File.listRoots()[0] );
...

class FileTreeContentProvider implements ITreeContentProvider {

    public Object[] getChildren(Object parentElement) {
        File file = (File) parentElement;
        if (file.isDirectory()) {
            return file.listFiles();
        }
        return null;
    }
    
!!! folgende Zeile richtig? kein Object[]?

    public Object getParent(Object element) {
        return ((File)element).getParent();
    }
    
    public boolean hasChildren(Object element) {
        File file = (File) element;
        if (file.isDirectory())
            return file.list().length > 0;
        return false;
    }
    
    public Object[] getElements(Object inputElement) {
        return getChildren(inputElement);
    }
    
    public void dispose() {}
    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}   
}

!!! Listing 6: JFace-Anwendung

public class JFaceApp extends ApplicationWindow {
    public JFaceApp(Shell shell) {
        super(shell);
        addMenuBar();
    }
    protected MenuManager createMenuManager() {
        MenuManager mainMenu = new MenuManager();
        MenuManager fileMenu = new MenuManager("File");
        mainMenu.add(fileMenu);
        fileMenu.add(new Action("Exit") {
            public void run() {
                System.exit(0);
            }
        });
        return mainMenu;

    }

    protected Control createContents(Composite parent) {
        Label hello = new Label(parent, SWT.CENTER);
        hello.setText("Moin moin.");
        parent.pack();
        return parent;
    }
   
    public static void main(String[] args) {
        JFaceApp window = new JFaceApp(null);
        window.setBlockOnOpen(true);
        window.open();
        Display.getCurrent().dispose();
    }
}
  
!!! Listing 7: Wizard und WizardPage

public class SimpleWizard extends Wizard {

    public void addPages(){
        addPage( new Page1() );
        // addPage( new Page2() ); 
	  // usw.
    }
    public boolean performFinish() {
        // Nichts weiter zu tun
        return true;
    }   
}


public class Page1 extends WizardPage {
    Text textField;    
    public Page1() {
        super("Wizard, Schritt 1");
        setMessage("Bitte geben Sie ihren Namen ein");
        setPageComplete(false); // Anfänglich next-Button inaktiv
    }
    public void createControl(Composite parent) {
        Composite container = new Composite(parent, SWT.NONE);
        setControl( container ); // <- nicht vergessen
        GridLayout layout = new GridLayout(2, false);
        container.setLayout( layout );

        Label label = new Label(container, SWT.NONE);
        label.setText("Name");
        
        textField = new Text(container, SWT.NONE);
        textField.addModifyListener(new ModifyListener() {
            public void modifyText(ModifyEvent e) {
                setPageComplete( !textField.getText().equals("") );
            }
        });
    }
}
