aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/itmill/toolkit/demo/KeyboardShortcut.java
blob: e5170b7758c1a05bfaa2242a1116d722e7616be2 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package com.itmill.toolkit.demo;

import com.itmill.toolkit.event.Action;
import com.itmill.toolkit.event.ShortcutAction;
import com.itmill.toolkit.event.Action.Handler;
import com.itmill.toolkit.ui.*;

/**
 * Note: This feature is under development and is considered as beta
 * 
 * @author IT Mill Ltd.
 * 
 */
public class KeyboardShortcut extends com.itmill.toolkit.Application implements
		Handler {
	private Window main;

	private Button a;

	private Button z;

	private Button x;

	private Button close;

	private AbstractField f;

	Action[] actions = new Action[] {
			new ShortcutAction("Button a action", ShortcutAction.KeyCode.A,
					new int[] { ShortcutAction.ModifierKey.CTRL,
							ShortcutAction.ModifierKey.SHIFT }),
			new ShortcutAction("Button z action", ShortcutAction.KeyCode.Z,
					new int[] { ShortcutAction.ModifierKey.CTRL,
							ShortcutAction.ModifierKey.SHIFT }),
			new ShortcutAction("Button x action", ShortcutAction.KeyCode.X,
					new int[] { ShortcutAction.ModifierKey.CTRL,
							ShortcutAction.ModifierKey.SHIFT }),
			new ShortcutAction("Restart ", ShortcutAction.KeyCode.ESCAPE, null) };

	public void init() {

		main = new Window("Keyboard shortcuts demo");
		setMainWindow(main);

		main
				.addComponent(new Label(
						"<h3>Test application for shortcut actions</h3>"
								+ "<p><b>Notes:</b><br />"
								+ "<b>This feature is under development and it's API may still change.</b><br />"
								+ "<b>If events do not work, <b>set focus to Textfield first.</b><br />"
								+ "<b>Browsers may have reserved the keyboard combinations used in "
								+ "this demo for other purposes.</b><br /></p>",
						Label.CONTENT_XHTML));
		main
				.addComponent(new Label(
						"ESC restarts program, ctrl-shift-a clicks A button, "
								+ "ctrl-shift-z clicks Z button, ctrl-shift-x clicks X button"));

		// Restart button
		close = new Button("restart", this, "close");

		main.addComponent(close);

		a = new Button("Button A", this, "buttonAHandler");

		z = new Button("Button Z", this, "buttonZHandler");

		x = new Button("Button X", this, "buttonXHandler");

		f = new TextField("Textfield");

		main.addComponent(a);
		main.addComponent(z);
		main.addComponent(x);
		main.addComponent(f);

		main.addActionHandler(this);

		f.focus();
	}

	public Action[] getActions(Object target, Object sender) {
		return actions;
	}

	public void handleAction(Action action, Object sender, Object target) {
		if (action == actions[0])
			this.buttonAHandler();
		if (action == actions[1])
			this.buttonZHandler();
		if (action == actions[2])
			this.buttonXHandler();
		if (action == actions[3])
			this.close();
	}

	public void buttonAHandler() {
		main.addComponent(new Label("Button A handler fired"));
	}

	public void buttonZHandler() {
		main.addComponent(new Label("Button Z handler fired"));
	}

	public void buttonXHandler() {
		main.addComponent(new Label("Button X handler fired"));
	}

}