aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/itmill/toolkit/demo/TableDemo.java
blob: 71e255e323f01c2d67fd03794f4163e102bc1780 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package com.itmill.toolkit.demo;

import java.sql.SQLException;

import com.itmill.toolkit.data.util.QueryContainer;
import com.itmill.toolkit.event.Action;
import com.itmill.toolkit.demo.util.SampleDatabase;
import com.itmill.toolkit.terminal.ExternalResource;
import com.itmill.toolkit.ui.Button;
import com.itmill.toolkit.ui.Label;
import com.itmill.toolkit.ui.Link;
import com.itmill.toolkit.ui.OrderedLayout;
import com.itmill.toolkit.ui.Table;
import com.itmill.toolkit.ui.Window;

/**
 * Similar to QueryContainerDemo
 * 
 * @author IT Mill Ltd.
 * @since 4.0.0
 * 
 */
public class TableDemo extends com.itmill.toolkit.Application implements
		Action.Handler {

	private static final String ACTION_DESCRIPTION = "Try right mouse button to initiate "
			+ "actions menu.<br />Note: on Opera you use meta key "
			+ "and left mouse button.";

	private static final String TABLE_CAPTION = "All rows from employee table";

	private Link menuLink = new Link("Go back to menu", new ExternalResource(
			"index.html"));

	// Table component where SQL rows are attached (using QueryContainer)
	private Table table = new Table();

	// Label which displays last performed action against table row
	private Label tableLastAction = new Label("No action selected for table.");

	// Database provided with sample data
	private SampleDatabase sampleDatabase;

	// Example Actions for table
	private Action ACTION1 = new Action("Upload");

	private Action ACTION2 = new Action("Download");

	private Action ACTION3 = new Action("Show history");

	private Action[] actions = new Action[] { ACTION1, ACTION2, ACTION3 };

	// Button which is used to disable or enable table
	// note: when button click event occurs, tableEnabler() method is called
	private Button tableEnabler = new Button("Disable table", this,
			"tableEnabler");

	// Button which is used to hide or show table
	// note: when button click event occurs, tableVisibility() method is called
	private Button tableVisibility = new Button("Hide table", this,
			"tableVisibility");

	// Button which is used to hide or show table
	// note: when button click event occurs, tableVisibility() method is called
	private Button tableCaption = new Button("Hide caption", this,
			"tableCaption");

	// Links for switching between TableDemo and EmbeddedToolkit.jsp page
	private Link embeddedToolkitLink = new Link(
			"See same application instance through ToolkitMashup.jsp",
			new ExternalResource("ToolkitMashup.jsp"));

	private Link tableDemoLink = new Link(
			"See same application instance through TableDemo",
			new ExternalResource("TableDemo?renderingMode=ajax"));

	/**
	 * Initialize Application. Demo components are added to main window.
	 */
	public void init() {
		Window main = new Window("Table demo");
		setMainWindow(main);

		// set the application to use Corporate -theme
		setTheme("corporate");

		// Add link back to index.html
		main.addComponent(menuLink);

		// create demo database
		sampleDatabase = new SampleDatabase();

		// Main window contains heading, two buttons, table and label
		main.addComponent(new Label("<h2>Table demo</h2>" + ACTION_DESCRIPTION,
				Label.CONTENT_XHTML));
		OrderedLayout layout = new OrderedLayout(
				OrderedLayout.ORIENTATION_HORIZONTAL);
		layout.addComponent(tableVisibility);
		layout.addComponent(tableEnabler);
		layout.addComponent(tableCaption);
		main.addComponent(layout);
		main.addComponent(table);
		main.addComponent(tableLastAction);
		main.addComponent(embeddedToolkitLink);
		main.addComponent(tableDemoLink);

		// initialize demo components
		initTable();
	}

	/**
	 * Populates table component with all rows from employee table.
	 * 
	 */
	private void initTable() {
		// init table
		table.setCaption(TABLE_CAPTION);
		table.setPageLength(10);
		table.setSelectable(true);
		table.setRowHeaderMode(Table.ROW_HEADER_MODE_INDEX);
		table.setColumnCollapsingAllowed(true);
		table.setColumnReorderingAllowed(true);
		table.setSelectable(true);
		// this class handles table actions (see handleActions method below)
		table.addActionHandler(this);
		table.setDescription(ACTION_DESCRIPTION);

		// populate Toolkit table component with test SQL table rows
		try {
			QueryContainer qc = new QueryContainer("SELECT * FROM employee",
					sampleDatabase.getConnection());
			table.setContainerDataSource(qc);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		// define which columns should be visible on Table component
		table.setVisibleColumns(new Object[] { "FIRSTNAME", "LASTNAME",
				"TITLE", "UNIT" });
		table.setItemCaptionPropertyId("ID");
	}

	public void tableVisibility() {
		if (table.isVisible()) {
			tableVisibility.setCaption("Show table");
			table.setVisible(false);
			tableEnabler.setEnabled(false);
			tableCaption.setEnabled(false);
		} else {
			tableVisibility.setCaption("Hide table");
			table.setVisible(true);
			tableEnabler.setEnabled(true);
			tableCaption.setEnabled(true);
		}
	}

	public void tableEnabler() {
		if (table.isEnabled()) {
			tableEnabler.setCaption("Enable table");
			table.setEnabled(false);
		} else {
			tableEnabler.setCaption("Disable table");
			table.setEnabled(true);
		}
	}

	public void tableCaption() {
		if (table.getCaption().equals("")) {
			table.setCaption(TABLE_CAPTION);
			tableCaption.setCaption("Hide caption");
		} else {
			table.setCaption("");
			tableCaption.setCaption("Show caption");
		}
	}

	/**
	 * URIHandler Return example actions
	 */
	public Action[] getActions(Object target, Object sender) {
		return actions;
	}

	/**
	 * Executed by right mouse button on table or tree component.
	 */
	public void handleAction(Action action, Object sender, Object target) {
		if (sender == table) {
			tableLastAction.setValue("Last action clicked was '"
					+ action.getCaption() + "' on item " + target);
		}
	}

}