summaryrefslogtreecommitdiffstats
path: root/src/com/itmill/toolkit/tests/book/DefaultButtonExample.java
blob: 1f340ce4d52eedcb087c12839ac030518fee0e22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* 
@ITMillApache2LicenseForJavaFiles@
 */

package com.itmill.toolkit.tests.book;

import com.itmill.toolkit.event.Action;
import com.itmill.toolkit.event.ShortcutAction;
import com.itmill.toolkit.event.Action.Handler;
import com.itmill.toolkit.ui.Button;
import com.itmill.toolkit.ui.CustomComponent;
import com.itmill.toolkit.ui.FormLayout;
import com.itmill.toolkit.ui.HorizontalLayout;
import com.itmill.toolkit.ui.Label;
import com.itmill.toolkit.ui.Panel;
import com.itmill.toolkit.ui.TextField;

public class DefaultButtonExample extends CustomComponent implements Handler {
	// Define and create user interface components
	Panel panel = new Panel("Login");
	FormLayout formlayout = new FormLayout();
	TextField username = new TextField("Username");
	TextField password = new TextField("Password");
	HorizontalLayout buttons = new HorizontalLayout();

	// Create buttons and define their listener methods.
	Button ok = new Button("OK", this, "okHandler");
	Button cancel = new Button("Cancel", this, "cancelHandler");

	// Have the unmodified Enter key cause an event
	Action action_ok = new ShortcutAction("Default key",
			ShortcutAction.KeyCode.ENTER, null);

	// Have the C key modified with Alt cause an event
	Action action_cancel = new ShortcutAction("Alt+C",
			ShortcutAction.KeyCode.C,
			new int[] { ShortcutAction.ModifierKey.ALT });

	public DefaultButtonExample() {
		// Set up the user interface
		setCompositionRoot(panel);
		panel.addComponent(formlayout);
		formlayout.addComponent(username);
		formlayout.addComponent(password);
		formlayout.addComponent(buttons);
		buttons.addComponent(ok);
		buttons.addComponent(cancel);

		// Set focus to username
		username.focus();

		// Set this object as the action handler
		System.out.println("adding ah");
		panel.addActionHandler(this);

		System.out.println("start done.");
	}

	/**
	 * Retrieve actions for a specific component. This method will be called for
	 * each object that has a handler; in this example just for login panel. The
	 * returned action list might as well be static list.
	 */
	public Action[] getActions(Object target, Object sender) {
		System.out.println("getActions()");
		return new Action[] { action_ok, action_cancel };
	}

	/**
	 * Handle actions received from keyboard. This simply directs the actions to
	 * the same listener methods that are called with ButtonClick events.
	 */
	public void handleAction(Action action, Object sender, Object target) {
		if (action == action_ok) {
			okHandler();
		}
		if (action == action_cancel) {
			cancelHandler();
		}
	}

	public void okHandler() {
		// Do something: report the click
		formlayout.addComponent(new Label("OK clicked. " + "User="
				+ username.getValue() + ", password=" + password.getValue()));
		//  
	}

	public void cancelHandler() {
		// Do something: report the click
		formlayout.addComponent(new Label("Cancel clicked. User="
				+ username.getValue() + ", password=" + password.getValue()));
	}
}