1
/*
2
 * Copyright (C) 2011 Openismus GmbH
3
 *
4
 * This file is part of GWT-Glom.
5
 *
6
 * GWT-Glom is free software: you can redistribute it and/or modify it
7
 * under the terms of the GNU Lesser General Public License as published by the
8
 * Free Software Foundation, either version 3 of the License, or (at your
9
 * option) any later version.
10
 *
11
 * GWT-Glom is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
14
 * for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with GWT-Glom.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
package org.glom.web.client.ui;
21
22
import java.util.ArrayList;
23
24
import org.glom.web.client.place.ListPlace;
25
26
import com.google.gwt.core.client.GWT;
27
import com.google.gwt.event.dom.client.ClickEvent;
28
import com.google.gwt.event.dom.client.ClickHandler;
29
import com.google.gwt.event.dom.client.HasChangeHandlers;
30
import com.google.gwt.event.shared.HandlerRegistration;
31
import com.google.gwt.user.client.DOM;
32
import com.google.gwt.user.client.ui.Anchor;
33
import com.google.gwt.user.client.ui.Composite;
34
import com.google.gwt.user.client.ui.FlowPanel;
35
import com.google.gwt.user.client.ui.Label;
36
import com.google.gwt.user.client.ui.ListBox;
37
import com.google.gwt.user.client.ui.TextBox;
38
39
/**
40
 *
41
 */
42
public class TableSelectionViewImpl extends Composite implements TableSelectionView {
43
44
	// OnlineGlomConstants.java is generated in the target/ directory,
45
	// from OnlineGlomConstants.properties
46
	// by the gwt-maven-plugin's i18n (mvn:i18n) goal.
47
	private OnlineGlomConstants constants = GWT.create(OnlineGlomConstants.class);
48
	
49
	Label documentTitleLabel = new Label();
50
	ListBox tablesChooser = new ListBox();
51
52
	Label searchLabel = new Label(constants.search());
53
	TextBox searchTextBox = new TextBox();
54
55
	ListBox localesChooser = new ListBox();
56
57
	Anchor backLink = new Anchor(constants.backToList());
58
	private Presenter presenter;
59
	private HandlerRegistration backLinkHandlerReg;
60
61
	public TableSelectionViewImpl() {
62
		tablesChooser.setStyleName("tableschooser");
63
		searchLabel.setStyleName("searchlabel"); // TODO: This is tedious.
64
		searchTextBox.setStyleName("searchtextbox"); // TODO: This is tedious.
65
		backLink.setStyleName("backlink");
66
67
		// empty click handler to avoid having to check for if the HandlerRegistration is null in setBackLink()
68
		backLinkHandlerReg = backLink.addClickHandler(new ClickHandler() {
69
			@Override
70
			public void onClick(final ClickEvent event) {
71
			}
72
		});
73
74
    final FlowPanel titlebox = new FlowPanel();
75
		DOM.setElementAttribute(titlebox.getElement(), "id", "titlebox");
76
		titlebox.add(documentTitleLabel);
77
		titlebox.add(localesChooser);
78
79
		// document title
80
		// Set a default value for the document title label with the opacity set to 0. The headbox will bounce up and
81
		// down when retrieving the document title from the server if an empty string is used.
82
		documentTitleLabel.getElement().getStyle().setOpacity(0);
83
		documentTitleLabel.setText("A");
84
		documentTitleLabel.addStyleName("document-title");
85
		DOM.setElementAttribute(documentTitleLabel.getElement(), "id", "document-title");
86
87
		localesChooser.setStyleName("localeschooser"); // TODO: This is tedious.
88
89
		// headbox with the table selector
90
		final FlowPanel headbox = new FlowPanel();
91
		DOM.setElementAttribute(headbox.getElement(), "id", "headbox");
92
		headbox.add(tablesChooser);
93
		headbox.add(searchLabel);
94
		headbox.add(searchTextBox);
95
		headbox.add(backLink);
96
		
97
		// the main container widget
98
		final FlowPanel mainPanel = new FlowPanel();
99
		mainPanel.add(titlebox);
100
		mainPanel.add(headbox);
101
102
		initWidget(mainPanel);
103
	}
104
105
	@Override
106
	public void setTableSelection(final ArrayList<String> names, final ArrayList<String> titles) {
107
		tablesChooser.clear();
108
		for (int i = 0; i < names.size(); i++) {
109
			tablesChooser.addItem(titles.get(i), names.get(i));
110
		}
111
	}
112
113
	@Override
114
	public void setSelectedTableName(final String tableName) {
115
		for (int i = 0; i < tablesChooser.getItemCount(); i++) {
116
			if (tableName.equals(tablesChooser.getValue(i))) {
117
				tablesChooser.setSelectedIndex(i);
118
				break;
119
			}
120
		}
121
122
	}
123
124
	@Override
125
	public HasChangeHandlers getTableSelector() {
126
		return tablesChooser;
127
	}
128
129
	@Override
130
	public String getSelectedTableName() {
131
		final int selectedIndex = tablesChooser.getSelectedIndex();
132
		return selectedIndex < 0 ? "" : tablesChooser.getValue(selectedIndex);
133
	}
134
135
	@Override
136
	public void setBackLink(final String documentID, final String tableName,
137
			final String quickFind) {
138
		backLinkHandlerReg.removeHandler();
139
		backLinkHandlerReg = backLink.addClickHandler(new ClickHandler() {
140
			@Override
141
			public void onClick(final ClickEvent event) {
142
				presenter.goTo(new ListPlace(documentID, tableName, ""));
143
			}
144
		});
145
	}
146
147
	@Override
148
	public void setDocumentTitle(final String documentTitle) {
149
		documentTitleLabel.setText(documentTitle);
150
		documentTitleLabel.getElement().getStyle().setOpacity(100);
151
	}
152
153
	@Override
154
	public void clear() {
155
		tablesChooser.clear();
156
		// Set a default value for the document title label with the opacity set to 0. The headbox will bounce up and
157
		// down when retrieving the document title from the server if an empty string is used.
158
		documentTitleLabel.getElement().getStyle().setOpacity(0);
159
		documentTitleLabel.setText("A");
160
	}
161
162
	@Override
163
	public void setBackLinkVisible(final boolean visible) {
164
		backLink.setVisible(visible);
165
	}
166
167
	@Override
168
	public void setPresenter(final Presenter presenter) {
169
		this.presenter = presenter;
170
	}
171
172
	@Override
173
	public String getSelectedTableTitle() {
174
		final int selectedIndex = tablesChooser.getSelectedIndex();
175
		return selectedIndex < 0 ? "" : tablesChooser.getItemText(selectedIndex);
176
	}
177
178
	@Override
179
	public HasChangeHandlers getQuickFindBox() {
180
		return searchTextBox;
181
	}
182
183
	@Override
184
	public String getQuickFindText() {
185
		return searchTextBox.getText();
186
	}
187
188
	@Override
189
	public void setQuickFindText(final String quickFind) {
190
		searchTextBox.setText(quickFind);
191
	}
192
193
	@Override
194
	public HasChangeHandlers getLocaleSelector() {
195
		return localesChooser;
196
	}
197
198
	@Override
199
	public void setLocaleList(final ArrayList<String> ids, final ArrayList<String> titles) {
200
		localesChooser.clear();
201
		for (int i = 0; i < ids.size(); i++) {
202
			localesChooser.addItem(titles.get(i), ids.get(i));
203
		}
204
	}
205
206
	@Override
207
	public String getSelectedLocale() {
208
		final int selectedIndex = localesChooser.getSelectedIndex();
209
		return selectedIndex < 0 ? "" : localesChooser.getValue(selectedIndex);
210
	}
211
212
	@Override
213
	public void setSelectedLocale(final String localeID) {
214
		for (int i = 0; i < localesChooser.getItemCount(); i++) {
215
			if (localeID.equals(localesChooser.getValue(i))) {
216
				localesChooser.setSelectedIndex(i);
217
				break;
218
			}
219
		}
220
221
	}
222
}