Replace deprecated DOM.setElementAttribute().
[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.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;
39
40 /**
41  *
42  */
43 public class TableSelectionViewImpl extends Composite implements TableSelectionView {
44
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);
49
50         Label documentTitleLabel = new Label();
51         ListBox tablesChooser = new ListBox();
52
53         Label searchLabel = new Label(constants.search());
54         TextBox searchTextBox = new TextBox();
55
56         Label reportsLabel = new Label(constants.reports());
57         ListBox reportsChooser = new ListBox();
58
59         ListBox localesChooser = new ListBox();
60
61         Anchor backLink = new Anchor(constants.backToList());
62         private Presenter presenter;
63         private HandlerRegistration backLinkHandlerReg;
64
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");
70
71                 // empty click handler to avoid having to check for if the HandlerRegistration is null in setBackLink()
72                 backLinkHandlerReg = backLink.addClickHandler(new ClickHandler() {
73                         @Override
74                         public void onClick(final ClickEvent event) {
75                         }
76                 });
77
78                 final FlowPanel titlebox = new FlowPanel();
79                 titlebox.getElement().setAttribute("id", "titlebox");
80                 titlebox.add(documentTitleLabel);
81
82                 titlebox.add(localesChooser);
83                 titlebox.add(reportsChooser);
84                 titlebox.add(reportsLabel);
85
86                 // document title
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");
93
94                 reportsLabel.setStyleName("reportslabel"); // TODO: This is tedious.
95                 reportsChooser.setStyleName("reportschooser"); // TODO: This is tedious.
96
97                 localesChooser.setStyleName("localeschooser"); // TODO: This is tedious.
98
99                 // headbox with the table selector
100                 final FlowPanel headbox = new FlowPanel();
101                 headbox.getElement().setAttribute("id", "headbox");
102                 headbox.add(tablesChooser);
103
104                 final FlowPanel searchbox = new FlowPanel();
105                 searchbox.getElement().setAttribute("id", "searchbox");
106                 searchbox.add(searchLabel);
107                 searchbox.add(searchTextBox);
108                 headbox.add(searchbox);
109
110                 headbox.add(backLink);
111
112                 // the main container widget
113                 final FlowPanel mainPanel = new FlowPanel();
114                 mainPanel.add(titlebox);
115                 mainPanel.add(headbox);
116
117                 initWidget(mainPanel);
118         }
119
120         @Override
121         public void setTableSelection(final ArrayList<String> names, final ArrayList<String> titles) {
122                 tablesChooser.clear();
123
124                 if (names == null) {
125                         return;
126                 }
127
128                 for (int i = 0; i < names.size(); i++) {
129                         tablesChooser.addItem(titles.get(i), names.get(i));
130                 }
131         }
132
133         @Override
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);
140                         }
141
142                         return;
143                 }
144
145                 for (int i = 0; i < tablesChooser.getItemCount(); i++) {
146                         if (tableName.equals(tablesChooser.getValue(i))) {
147                                 tablesChooser.setSelectedIndex(i);
148                                 break;
149                         }
150                 }
151
152         }
153
154         @Override
155         public HasChangeHandlers getTableSelector() {
156                 return tablesChooser;
157         }
158
159         @Override
160         public String getSelectedTableName() {
161                 final int selectedIndex = tablesChooser.getSelectedIndex();
162                 return selectedIndex < 0 ? "" : tablesChooser.getValue(selectedIndex);
163         }
164
165         @Override
166         public void setBackLink(final String documentID, final String tableName, final String quickFind) {
167                 backLinkHandlerReg.removeHandler();
168                 backLinkHandlerReg = backLink.addClickHandler(new ClickHandler() {
169                         @Override
170                         public void onClick(final ClickEvent event) {
171                                 presenter.goTo(new ListPlace(documentID, tableName, ""));
172                         }
173                 });
174         }
175
176         @Override
177         public void setDocumentTitle(final String documentTitle) {
178                 documentTitleLabel.setText(documentTitle);
179                 documentTitleLabel.getElement().getStyle().setOpacity(100);
180         }
181
182         @Override
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");
189         }
190
191         @Override
192         public void setBackLinkVisible(final boolean visible) {
193                 backLink.setVisible(visible);
194         }
195
196         @Override
197         public void setPresenter(final Presenter presenter) {
198                 this.presenter = presenter;
199         }
200
201         @Override
202         public String getSelectedTableTitle() {
203                 final int selectedIndex = tablesChooser.getSelectedIndex();
204                 return selectedIndex < 0 ? "" : tablesChooser.getItemText(selectedIndex);
205         }
206
207         @Override
208         public HasChangeHandlers getQuickFindBox() {
209                 return searchTextBox;
210         }
211
212         @Override
213         public String getQuickFindText() {
214                 return searchTextBox.getText();
215         }
216
217         @Override
218         public void setQuickFindText(final String quickFind) {
219                 searchTextBox.setText(quickFind);
220         }
221
222         @Override
223         public HasChangeHandlers getLocaleSelector() {
224                 return localesChooser;
225         }
226
227         @Override
228         public void setLocaleList(final ArrayList<String> ids, final ArrayList<String> titles) {
229                 localesChooser.clear();
230
231                 if(ids == null) {
232                         return;
233                 }
234
235                 for (int i = 0; i < ids.size(); i++) {
236                         localesChooser.addItem(titles.get(i), ids.get(i));
237                 }
238         }
239
240         @Override
241         public String getSelectedLocale() {
242                 final int selectedIndex = localesChooser.getSelectedIndex();
243                 return selectedIndex < 0 ? "" : localesChooser.getValue(selectedIndex);
244         }
245
246         @Override
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);
253                         }
254
255                         return;
256                 }
257
258                 for (int i = 0; i < localesChooser.getItemCount(); i++) {
259                         if (localeID.equals(localesChooser.getValue(i))) {
260                                 localesChooser.setSelectedIndex(i);
261                                 break;
262                         }
263                 }
264
265         }
266
267         @Override
268         public HasChangeHandlers getReportSelector() {
269                 return reportsChooser;
270         }
271
272         @Override
273         public String getSelectedReport() {
274                 final int selectedIndex = reportsChooser.getSelectedIndex();
275                 return selectedIndex < 0 ? "" : reportsChooser.getValue(selectedIndex);
276         }
277
278         @Override
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);
285                         }
286
287                         return;
288                 }
289
290                 for (int i = 0; i < reportsChooser.getItemCount(); i++) {
291                         if (reportName.equals(reportsChooser.getValue(i))) {
292                                 reportsChooser.setSelectedIndex(i);
293                                 break;
294                         }
295                 }
296
297         }
298
299         @Override
300         public void setReportList(final Reports reports) {
301                 reportsChooser.clear();
302
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("-", "");
308
309                 if(reports == null)
310                         return;
311
312                 for (int i = 0; i < reports.getCount(); i++) {
313                         reportsChooser.addItem(reports.getTitle(i), reports.getName(i));
314                 }
315         }
316 }