DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Simple Wizard In SWT

08.31.2006
| 16515 views |
  • submit to reddit
        // This code is an example for StackLayout

import java.util.Vector;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class Wizard {

	private Display display;
	private Shell shell;
	private int index;
	private Button prev;
	private Button next;
	private Vector<Control> PAGES;
	private Composite wizardPanel;
	private StackLayout wizardLayout;
	private Vector<String> TITLES;

	public Wizard() {
		display = new Display();
		shell = new Shell(display);

		shell.setText("Wizard");
		
		init();
		createGUI();
		
		fillPages();
		setSize(500, 400);
		
		shell.open();
		
		while(!shell.isDisposed()) {
			if(!display.readAndDispatch()) {
				display.sleep();
			}
		}
		display.dispose();
	}
	
	private void fillPages() {
		Button label = new Button(shell, SWT.BORDER);
		label.setText("Hello, World 0!");
		addPage(label, "Page 1");
		
		label = new Button(shell, SWT.BORDER);
		label.setText("Hello, World 1!");
		addPage(label, "This is page #2!");
		
		Group group = new Group(shell, SWT.NONE);
		group.setLayout(new GridLayout(2, false));
		
		Label imgLabel = new Label(group, SWT.BORDER);
		GridData gd = new GridData(GridData.FILL_VERTICAL);
		gd.verticalSpan = 2;
		imgLabel.setLayoutData(gd);
		imgLabel.setImage(new Image(display, "C:\\Programme\\eclipse\\workspace\\TestSwt\\src\\icons\\simwizard.jpg"));
		
		Text text = new Text(group, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);
		text.setText("Hier steht ein Hinweis.\nAuch über mehrere\n\tZeilen, mit Sonderzeichen!");
		text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		Group ng = new Group(group, SWT.NONE);
		ng.setLayout(new GridLayout(2, false));
		ng.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.GRAB_VERTICAL | GridData.GRAB_HORIZONTAL));
		
		Label l = new Label(ng, SWT.NONE);
		l.setLayoutData(new GridData());
		l.setText("Combo:");
		Combo combo = new Combo(ng, SWT.BORDER);
		combo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		combo.setItems(new String[] {"foo", "bar", "baz"});
		
		l = new Label(ng, SWT.NONE);
		l.setLayoutData(new GridData());
		l.setText("Text:");
		Text t = new Text(ng, SWT.BORDER);
		t.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		t.setText("foo");

		l = new Label(ng, SWT.NONE);
		l.setLayoutData(new GridData());
		l.setText("Radio:");
		
		Group g = new Group(ng, SWT.NONE);
		g.setLayout(new FillLayout());
		Button r = new Button(g, SWT.RADIO);
		r.setText("Radio #1");
		r = new Button(g, SWT.RADIO);
		r.setText("Radio #2");
		r = new Button(g, SWT.RADIO);
		r.setText("Radio #3");

		l = new Label(ng, SWT.NONE);
		l.setLayoutData(new GridData());
		l.setText("Check:");
		
		g = new Group(ng, SWT.NONE);
		g.setLayout(new FillLayout());
		r = new Button(g, SWT.CHECK);
		r.setText("Check #1");
		r = new Button(g, SWT.CHECK);
		r.setText("Check #2");
		r = new Button(g, SWT.CHECK);
		r.setText("Check #3");

		addPage(group, "This is filled with controls.");
	}

	private void init() {
		index = 0; // index for current page
		PAGES = new Vector<Control>();
		TITLES = new Vector<String>();
	}
	
	private void createGUI() {
		shell.setLayout(new GridLayout(2, false));
		
		wizardPanel = new Composite(shell, SWT.NONE);
		GridData gd = new GridData(GridData.FILL_BOTH);
		gd.horizontalSpan = 2;
		wizardPanel.setLayoutData(gd);
		
		wizardLayout = new StackLayout();
		wizardPanel.setLayout(wizardLayout);
			
		prev = new Button(shell, SWT.PUSH);
		next = new Button(shell, SWT.PUSH);
		next.setText("Next >");
		next.setLayoutData(new GridData());
		next.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				next();
			}
		});
		next.setEnabled(false);
		
		prev.setText("< Previous");
		prev.setLayoutData(new GridData());
		prev.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				previous();
			}
		});
		prev.setEnabled(false);
	}
	
	private void next() {
		if(index == PAGES.size()-1 && "finish".equalsIgnoreCase(next.getText())) {
			System.out.println("finished!");
			shell.close();
			display.dispose();
			return;
		}
		
		if(index < PAGES.size()-1) {
			index += 1;
			wizardLayout.topControl = PAGES.get(index);
			shell.setText("Wizard - " + TITLES.get(index));
			wizardPanel.layout();
		}
		
		if(index-1 >= 0) {
			prev.setEnabled(true);
		}
		if(index+1 >= PAGES.size()) {
//			next.setEnabled(false);
			next.setText("Finish");
		}
	}
	
	private void previous() {
		if(index > 0) {
			index -= 1;
			wizardLayout.topControl = PAGES.get(index);
			shell.setText("Wizard - " + TITLES.get(index));
			wizardPanel.layout();
		}
		
		if(index+1 < PAGES.size()) {
//			next.setEnabled(true);
			next.setText("Next >");
		}
		if(index-1 < 0) {
			prev.setEnabled(false);
		}
	}
	
	public void addPage(Control c) {
		addPage(c, "Page");
	}
	
	public void addPage(Control c, String title) {
		// auf Wizardpanel umsetzen
		c.setParent(wizardPanel);
		
		PAGES.addElement(c);
		TITLES.addElement(title);
		wizardLayout.topControl = PAGES.get(0);
		shell.setText("Wizard - " + TITLES.get(0));
		if(PAGES.size() > 0) {
			next.setEnabled(true);
		}
	}
	
	public int getPageCount() {
		return PAGES.size();
	}
	
	public void setSize(int width, int height) {
		shell.setSize(width, height);
	}
	
	public static void main(String[] args) {
		new Wizard();
	}

}