2 * Copyright (C) 2011 Openismus GmbH
4 * This file is part of GWT-Glom.
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.
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
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/>.
20 package org.glom.web.client.ui;
22 import java.util.ArrayList;
24 import org.glom.web.client.StringUtils;
25 import org.glom.web.client.place.ListPlace;
26 import org.glom.web.shared.Reports;
28 import com.google.gwt.core.client.GWT;
29 import com.google.gwt.event.dom.client.ClickEvent;
30 import com.google.gwt.event.dom.client.ClickHandler;
31 import com.google.gwt.event.dom.client.HasChangeHandlers;
32 import com.google.gwt.event.shared.HandlerRegistration;
33 import com.google.gwt.user.client.ui.Anchor;
34 import com.google.gwt.user.client.ui.Composite;
35 import com.google.gwt.user.client.ui.FlowPanel;
36 import com.google.gwt.user.client.ui.Label;
37 import com.google.gwt.user.client.ui.ListBox;
38 import com.google.gwt.user.client.ui.TextBox;
43 public class TableSelectionViewImpl extends Composite implements TableSelectionView {
45 // OnlineGlomConstants.java is generated in the target/ directory,
46 // from OnlineGlomConstants.properties
47 // by the gwt-maven-plugin's i18n (mvn:i18n) goal.
48 private final OnlineGlomConstants constants = GWT.create(OnlineGlomConstants.class);
50 Label documentTitleLabel = new Label();
51 ListBox tablesChooser = new ListBox();
53 Label searchLabel = new Label(constants.search());
54 TextBox searchTextBox = new TextBox();
56 Label reportsLabel = new Label(constants.reports());
57 ListBox reportsChooser = new ListBox();
59 ListBox localesChooser = new ListBox();
61 Anchor backLink = new Anchor(constants.backToList());
62 private Presenter presenter;
63 private HandlerRegistration backLinkHandlerReg;
65 public TableSelectionViewImpl() {
66 tablesChooser.setStyleName("tableschooser");
67 searchLabel.setStyleName("searchlabel"); // TODO: This is tedious.
68 searchTextBox.setStyleName("searchtextbox"); // TODO: This is tedious.
69 backLink.setStyleName("backlink");
71 // empty click handler to avoid having to check for if the HandlerRegistration is null in setBackLink()
72 backLinkHandlerReg = backLink.addClickHandler(new ClickHandler() {
74 public void onClick(final ClickEvent event) {
78 final FlowPanel titlebox = new FlowPanel();
79 titlebox.getElement().setAttribute("id", "titlebox");
80 titlebox.add(documentTitleLabel);
82 titlebox.add(localesChooser);
83 titlebox.add(reportsChooser);
84 titlebox.add(reportsLabel);
87 // Set a default value for the document title label with the opacity set to 0. The headbox will bounce up and
88 // down when retrieving the document title from the server if an empty string is used.
89 documentTitleLabel.getElement().getStyle().setOpacity(0);
90 documentTitleLabel.setText("A");
91 documentTitleLabel.addStyleName("document-title");
92 documentTitleLabel.getElement().setAttribute("id", "document-title");
94 reportsLabel.setStyleName("reportslabel"); // TODO: This is tedious.
95 reportsChooser.setStyleName("reportschooser"); // TODO: This is tedious.
97 localesChooser.setStyleName("localeschooser"); // TODO: This is tedious.
99 // headbox with the table selector
100 final FlowPanel headbox = new FlowPanel();
101 headbox.getElement().setAttribute("id", "headbox");
102 headbox.add(tablesChooser);
104 final FlowPanel searchbox = new FlowPanel();
105 searchbox.getElement().setAttribute("id", "searchbox");
106 searchbox.add(searchLabel);
107 searchbox.add(searchTextBox);
108 headbox.add(searchbox);
110 headbox.add(backLink);
112 // the main container widget
113 final FlowPanel mainPanel = new FlowPanel();
114 mainPanel.add(titlebox);
115 mainPanel.add(headbox);
117 initWidget(mainPanel);
121 public void setTableSelection(final ArrayList<String> names, final ArrayList<String> titles) {
122 tablesChooser.clear();
128 for (int i = 0; i < names.size(); i++) {
129 tablesChooser.addItem(titles.get(i), names.get(i));
134 public void setSelectedTableName(final String tableName) {
135 if (StringUtils.isEmpty(tableName)) {
136 // Make sure none are selected:
137 // TODO: Find a better way to do this.
138 for (int i = 0; i < tablesChooser.getItemCount(); i++) {
139 tablesChooser.setItemSelected(i, false);
145 for (int i = 0; i < tablesChooser.getItemCount(); i++) {
146 if (tableName.equals(tablesChooser.getValue(i))) {
147 tablesChooser.setSelectedIndex(i);
155 public HasChangeHandlers getTableSelector() {
156 return tablesChooser;
160 public String getSelectedTableName() {
161 final int selectedIndex = tablesChooser.getSelectedIndex();
162 return selectedIndex < 0 ? "" : tablesChooser.getValue(selectedIndex);
166 public void setBackLink(final String documentID, final String tableName, final String quickFind) {
167 backLinkHandlerReg.removeHandler();
168 backLinkHandlerReg = backLink.addClickHandler(new ClickHandler() {
170 public void onClick(final ClickEvent event) {
171 presenter.goTo(new ListPlace(documentID, tableName, ""));
177 public void setDocumentTitle(final String documentTitle) {
178 documentTitleLabel.setText(documentTitle);
179 documentTitleLabel.getElement().getStyle().setOpacity(100);
183 public void clear() {
184 tablesChooser.clear();
185 // Set a default value for the document title label with the opacity set to 0. The headbox will bounce up and
186 // down when retrieving the document title from the server if an empty string is used.
187 documentTitleLabel.getElement().getStyle().setOpacity(0);
188 documentTitleLabel.setText("A");
192 public void setBackLinkVisible(final boolean visible) {
193 backLink.setVisible(visible);
197 public void setPresenter(final Presenter presenter) {
198 this.presenter = presenter;
202 public String getSelectedTableTitle() {
203 final int selectedIndex = tablesChooser.getSelectedIndex();
204 return selectedIndex < 0 ? "" : tablesChooser.getItemText(selectedIndex);
208 public HasChangeHandlers getQuickFindBox() {
209 return searchTextBox;
213 public String getQuickFindText() {
214 return searchTextBox.getText();
218 public void setQuickFindText(final String quickFind) {
219 searchTextBox.setText(quickFind);
223 public HasChangeHandlers getLocaleSelector() {
224 return localesChooser;
228 public void setLocaleList(final ArrayList<String> ids, final ArrayList<String> titles) {
229 localesChooser.clear();
235 for (int i = 0; i < ids.size(); i++) {
236 localesChooser.addItem(titles.get(i), ids.get(i));
241 public String getSelectedLocale() {
242 final int selectedIndex = localesChooser.getSelectedIndex();
243 return selectedIndex < 0 ? "" : localesChooser.getValue(selectedIndex);
247 public void setSelectedLocale(final String localeID) {
248 if (StringUtils.isEmpty(localeID)) {
249 // Make sure none are selected:
250 // TODO: Find a better way to do this.
251 for (int i = 0; i < localesChooser.getItemCount(); i++) {
252 localesChooser.setItemSelected(i, false);
258 for (int i = 0; i < localesChooser.getItemCount(); i++) {
259 if (localeID.equals(localesChooser.getValue(i))) {
260 localesChooser.setSelectedIndex(i);
268 public HasChangeHandlers getReportSelector() {
269 return reportsChooser;
273 public String getSelectedReport() {
274 final int selectedIndex = reportsChooser.getSelectedIndex();
275 return selectedIndex < 0 ? "" : reportsChooser.getValue(selectedIndex);
279 public void setSelectedReport(final String reportName) {
280 if (StringUtils.isEmpty(reportName)) {
281 // Make sure none are selected:
282 // TODO: Find a better way to do this.
283 for (int i = 0; i < reportsChooser.getItemCount(); i++) {
284 reportsChooser.setItemSelected(i, false);
290 for (int i = 0; i < reportsChooser.getItemCount(); i++) {
291 if (reportName.equals(reportsChooser.getValue(i))) {
292 reportsChooser.setSelectedIndex(i);
300 public void setReportList(final Reports reports) {
301 reportsChooser.clear();
303 // Add a first item as a default for the combobox.
304 // Otherwise a report will be chosen already,
305 // so the user will not be able to choose that report to go to that page.
306 // TODO: Think of a better UI for this.
307 reportsChooser.addItem("-", "");
312 for (int i = 0; i < reports.getCount(); i++) {
313 reportsChooser.addItem(reports.getTitle(i), reports.getName(i));