Add navigation buttons to related list tables.
[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.place.ListPlace;
25
26 import com.google.gwt.event.dom.client.ClickEvent;
27 import com.google.gwt.event.dom.client.ClickHandler;
28 import com.google.gwt.event.dom.client.HasChangeHandlers;
29 import com.google.gwt.event.shared.HandlerRegistration;
30 import com.google.gwt.user.client.DOM;
31 import com.google.gwt.user.client.ui.Anchor;
32 import com.google.gwt.user.client.ui.Composite;
33 import com.google.gwt.user.client.ui.FlowPanel;
34 import com.google.gwt.user.client.ui.Label;
35 import com.google.gwt.user.client.ui.ListBox;
36
37 /**
38  * @author Ben Konrath <ben@bagu.org>
39  */
40 public class TableSelectionViewImpl extends Composite implements TableSelectionView {
41
42         Label documentTitleLabel = new Label();
43         ListBox tableChooser = new ListBox();
44         Anchor backLink = new Anchor("Back to List");
45         private Presenter presenter;
46         private HandlerRegistration backLinkHandlerReg;
47
48         public TableSelectionViewImpl() {
49                 tableChooser.setStyleName("tablechooser");
50                 backLink.setStyleName("backlink");
51
52                 // empty click handler to avoid having to check for if the HandlerRegistration is null in setBackLink()
53                 backLinkHandlerReg = backLink.addClickHandler(new ClickHandler() {
54                         @Override
55                         public void onClick(ClickEvent event) {
56                         }
57                 });
58
59                 // headbox with the table selector
60                 FlowPanel headbox = new FlowPanel();
61                 DOM.setElementAttribute(headbox.getElement(), "id", "headbox");
62                 headbox.add(backLink);
63                 headbox.add(tableChooser);
64
65                 // document title
66                 // Set a default value for the document title label with the opacity set to 0. The headbox will bounce up and
67                 // down when retrieving the document title from the server if an empty string is used.
68                 documentTitleLabel.getElement().getStyle().setOpacity(0);
69                 documentTitleLabel.setText("A");
70                 documentTitleLabel.addStyleName("document-title");
71                 DOM.setElementAttribute(documentTitleLabel.getElement(), "id", "document-title");
72
73                 // the main container widget
74                 FlowPanel mainPanel = new FlowPanel();
75                 mainPanel.add(documentTitleLabel);
76                 mainPanel.add(headbox);
77
78                 initWidget(mainPanel);
79         }
80
81         @Override
82         public void setTableSelection(ArrayList<String> names, ArrayList<String> titles) {
83                 tableChooser.clear();
84                 for (int i = 0; i < names.size(); i++) {
85                         tableChooser.addItem(titles.get(i), names.get(i));
86                 }
87         }
88
89         @Override
90         public void setSelectedTableName(String tableName) {
91                 for (int i = 0; i < tableChooser.getItemCount(); i++) {
92                         if (tableName.equals(tableChooser.getValue(i))) {
93                                 tableChooser.setSelectedIndex(i);
94                                 break;
95                         }
96                 }
97
98         }
99
100         @Override
101         public HasChangeHandlers getTableSelector() {
102                 return tableChooser;
103         }
104
105         @Override
106         public String getSelectedTableName() {
107                 int selectedIndex = tableChooser.getSelectedIndex();
108                 return selectedIndex < 0 ? "" : tableChooser.getValue(selectedIndex);
109         }
110
111         public void setBackLink(final String documentID, final String tableName) {
112                 backLinkHandlerReg.removeHandler();
113                 backLinkHandlerReg = backLink.addClickHandler(new ClickHandler() {
114                         @Override
115                         public void onClick(ClickEvent event) {
116                                 presenter.goTo(new ListPlace(documentID, tableName));
117                         }
118                 });
119         }
120
121         public void setDocumentTitle(String documentTitle) {
122                 documentTitleLabel.setText(documentTitle);
123                 documentTitleLabel.getElement().getStyle().setOpacity(100);
124         }
125
126         @Override
127         public void clear() {
128                 tableChooser.clear();
129                 // Set a default value for the document title label with the opacity set to 0. The headbox will bounce up and
130                 // down when retrieving the document title from the server if an empty string is used.
131                 documentTitleLabel.getElement().getStyle().setOpacity(0);
132                 documentTitleLabel.setText("A");
133         }
134
135         @Override
136         public void setBackLinkVisible(boolean visible) {
137                 backLink.setVisible(visible);
138         }
139
140         @Override
141         public void setPresenter(Presenter presenter) {
142                 this.presenter = presenter;
143         }
144
145         @Override
146         public String getSelectedTableTitle() {
147                 int selectedIndex = tableChooser.getSelectedIndex();
148                 return selectedIndex < 0 ? "" : tableChooser.getItemText(selectedIndex);
149         }
150 }