Some checks for nulls.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / client / ui / TableSelectionViewImpl.java
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.StringUtils;
25 import org.glom.web.client.place.ListPlace;
26 import org.glom.web.shared.Reports;
27
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.DOM;
34 import com.google.gwt.user.client.ui.Anchor;
35 import com.google.gwt.user.client.ui.Composite;
36 import com.google.gwt.user.client.ui.FlowPanel;
37 import com.google.gwt.user.client.ui.Label;
38 import com.google.gwt.user.client.ui.ListBox;
39 import com.google.gwt.user.client.ui.TextBox;
40
41 /**
42  *
43  */
44 public class TableSelectionViewImpl extends Composite implements TableSelectionView {
45
46         // OnlineGlomConstants.java is generated in the target/ directory,
47         // from OnlineGlomConstants.properties
48         // by the gwt-maven-plugin's i18n (mvn:i18n) goal.
49         private final OnlineGlomConstants constants = GWT.create(OnlineGlomConstants.class);
50
51         Label documentTitleLabel = new Label();
52         ListBox tablesChooser = new ListBox();
53
54         Label searchLabel = new Label(constants.search());
55         TextBox searchTextBox = new TextBox();
56
57         Label reportsLabel = new Label(constants.reports());
58         ListBox reportsChooser = new ListBox();
59
60         ListBox localesChooser = new ListBox();
61
62         Anchor backLink = new Anchor(constants.backToList());
63         private Presenter presenter;
64         private HandlerRegistration backLinkHandlerReg;
65
66         public TableSelectionViewImpl() {
67                 tablesChooser.setStyleName("tableschooser");
68                 searchLabel.setStyleName("searchlabel"); // TODO: This is tedious.
69                 searchTextBox.setStyleName("searchtextbox"); // TODO: This is tedious.
70                 backLink.setStyleName("backlink");
71
72                 // empty click handler to avoid having to check for if the HandlerRegistration is null in setBackLink()
73                 backLinkHandlerReg = backLink.addClickHandler(new ClickHandler() {
74                         @Override
75                         public void onClick(final ClickEvent event) {
76                         }
77                 });
78
79                 final FlowPanel titlebox = new FlowPanel();
80                 DOM.setElementAttribute(titlebox.getElement(), "id", "titlebox");
81                 titlebox.add(documentTitleLabel);
82
83                 titlebox.add(localesChooser);
84                 titlebox.add(reportsChooser);
85                 titlebox.add(reportsLabel);
86
87                 // document title
88                 // Set a default value for the document title label with the opacity set to 0. The headbox will bounce up and
89                 // down when retrieving the document title from the server if an empty string is used.
90                 documentTitleLabel.getElement().getStyle().setOpacity(0);
91                 documentTitleLabel.setText("A");
92                 documentTitleLabel.addStyleName("document-title");
93                 DOM.setElementAttribute(documentTitleLabel.getElement(), "id", "document-title");
94
95                 reportsLabel.setStyleName("reportslabel"); // TODO: This is tedious.
96                 reportsChooser.setStyleName("reportschooser"); // TODO: This is tedious.
97
98                 localesChooser.setStyleName("localeschooser"); // TODO: This is tedious.
99
100                 // headbox with the table selector
101                 final FlowPanel headbox = new FlowPanel();
102                 DOM.setElementAttribute(headbox.getElement(), "id", "headbox");
103                 headbox.add(tablesChooser);
104
105                 final FlowPanel searchbox = new FlowPanel();
106                 DOM.setElementAttribute(searchbox.getElement(), "id", "searchbox");
107                 searchbox.add(searchLabel);
108                 searchbox.add(searchTextBox);
109                 headbox.add(searchbox);
110
111                 headbox.add(backLink);
112
113                 // the main container widget
114                 final FlowPanel mainPanel = new FlowPanel();
115                 mainPanel.add(titlebox);
116                 mainPanel.add(headbox);
117
118                 initWidget(mainPanel);
119         }
120
121         @Override
122         public void setTableSelection(final ArrayList<String> names, final ArrayList<String> titles) {
123                 tablesChooser.clear();
124
125                 if (names == null) {
126                         return;
127                 }
128
129                 for (int i = 0; i < names.size(); i++) {
130                         tablesChooser.addItem(titles.get(i), names.get(i));
131                 }
132         }
133
134         @Override
135         public void setSelectedTableName(final String tableName) {
136                 if (StringUtils.isEmpty(tableName)) {
137                         // Make sure none are selected:
138                         // TODO: Find a better way to do this.
139                         for (int i = 0; i < tablesChooser.getItemCount(); i++) {
140                                 tablesChooser.setItemSelected(i, false);
141                         }
142
143                         return;
144                 }
145
146                 for (int i = 0; i < tablesChooser.getItemCount(); i++) {
147                         if (tableName.equals(tablesChooser.getValue(i))) {
148                                 tablesChooser.setSelectedIndex(i);
149                                 break;
150                         }
151                 }
152
153         }
154
155         @Override
156         public HasChangeHandlers getTableSelector() {
157                 return tablesChooser;
158         }
159
160         @Override
161         public String getSelectedTableName() {
162                 final int selectedIndex = tablesChooser.getSelectedIndex();
163                 return selectedIndex < 0 ? "" : tablesChooser.getValue(selectedIndex);
164         }
165
166         @Override
167         public void setBackLink(final String documentID, final String tableName, final String quickFind) {
168                 backLinkHandlerReg.removeHandler();
169                 backLinkHandlerReg = backLink.addClickHandler(new ClickHandler() {
170                         @Override
171                         public void onClick(final ClickEvent event) {
172                                 presenter.goTo(new ListPlace(documentID, tableName, ""));
173                         }
174                 });
175         }
176
177         @Override
178         public void setDocumentTitle(final String documentTitle) {
179                 documentTitleLabel.setText(documentTitle);
180                 documentTitleLabel.getElement().getStyle().setOpacity(100);
181         }
182
183         @Override
184         public void clear() {
185                 tablesChooser.clear();
186                 // Set a default value for the document title label with the opacity set to 0. The headbox will bounce up and
187                 // down when retrieving the document title from the server if an empty string is used.
188                 documentTitleLabel.getElement().getStyle().setOpacity(0);
189                 documentTitleLabel.setText("A");
190         }
191
192         @Override
193         public void setBackLinkVisible(final boolean visible) {
194                 backLink.setVisible(visible);
195         }
196
197         @Override
198         public void setPresenter(final Presenter presenter) {
199                 this.presenter = presenter;
200         }
201
202         @Override
203         public String getSelectedTableTitle() {
204                 final int selectedIndex = tablesChooser.getSelectedIndex();
205                 return selectedIndex < 0 ? "" : tablesChooser.getItemText(selectedIndex);
206         }
207
208         @Override
209         public HasChangeHandlers getQuickFindBox() {
210                 return searchTextBox;
211         }
212
213         @Override
214         public String getQuickFindText() {
215                 return searchTextBox.getText();
216         }
217
218         @Override
219         public void setQuickFindText(final String quickFind) {
220                 searchTextBox.setText(quickFind);
221         }
222
223         @Override
224         public HasChangeHandlers getLocaleSelector() {
225                 return localesChooser;
226         }
227
228         @Override
229         public void setLocaleList(final ArrayList<String> ids, final ArrayList<String> titles) {
230                 localesChooser.clear();
231
232                 if(ids == null) {
233                         return;
234                 }
235
236                 for (int i = 0; i < ids.size(); i++) {
237                         localesChooser.addItem(titles.get(i), ids.get(i));
238                 }
239         }
240
241         @Override
242         public String getSelectedLocale() {
243                 final int selectedIndex = localesChooser.getSelectedIndex();
244                 return selectedIndex < 0 ? "" : localesChooser.getValue(selectedIndex);
245         }
246
247         @Override
248         public void setSelectedLocale(final String localeID) {
249                 if (StringUtils.isEmpty(localeID)) {
250                         // Make sure none are selected:
251                         // TODO: Find a better way to do this.
252                         for (int i = 0; i < localesChooser.getItemCount(); i++) {
253                                 localesChooser.setItemSelected(i, false);
254                         }
255
256                         return;
257                 }
258
259                 for (int i = 0; i < localesChooser.getItemCount(); i++) {
260                         if (localeID.equals(localesChooser.getValue(i))) {
261                                 localesChooser.setSelectedIndex(i);
262                                 break;
263                         }
264                 }
265
266         }
267
268         @Override
269         public HasChangeHandlers getReportSelector() {
270                 return reportsChooser;
271         }
272
273         @Override
274         public String getSelectedReport() {
275                 final int selectedIndex = reportsChooser.getSelectedIndex();
276                 return selectedIndex < 0 ? "" : reportsChooser.getValue(selectedIndex);
277         }
278
279         @Override
280         public void setSelectedReport(final String reportName) {
281                 if (StringUtils.isEmpty(reportName)) {
282                         // Make sure none are selected:
283                         // TODO: Find a better way to do this.
284                         for (int i = 0; i < reportsChooser.getItemCount(); i++) {
285                                 reportsChooser.setItemSelected(i, false);
286                         }
287
288                         return;
289                 }
290
291                 for (int i = 0; i < reportsChooser.getItemCount(); i++) {
292                         if (reportName.equals(reportsChooser.getValue(i))) {
293                                 reportsChooser.setSelectedIndex(i);
294                                 break;
295                         }
296                 }
297
298         }
299
300         @Override
301         public void setReportList(final Reports reports) {
302                 reportsChooser.clear();
303
304                 // Add a first item as a default for the combobox.
305                 // Otherwise a report will be chosen already,
306                 // so the user will not be able to choose that report to go to that page.
307                 // TODO: Think of a better UI for this.
308                 reportsChooser.addItem("-", "");
309
310                 if(reports == null)
311                         return;
312
313                 for (int i = 0; i < reports.getCount(); i++) {
314                         reportsChooser.addItem(reports.getTitle(i), reports.getName(i));
315                 }
316         }
317 }