Make the TableSelector header match the mockup.
[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.ListBox;
35
36 /**
37  * @author Ben Konrath <ben@bagu.org>
38  */
39 public class TableSelectionViewImpl extends Composite implements TableSelectionView {
40
41         ListBox tableChooser = new ListBox();
42         Anchor backLink = new Anchor("Back to List");
43         private Presenter presenter;
44         private HandlerRegistration backLinkHandlerReg;
45
46         public TableSelectionViewImpl() {
47                 tableChooser.setStyleName("tablechooser");
48                 backLink.setStyleName("backlink");
49
50                 // empty click handler to avoid having to check for if the HandlerRegistration is null in setBackLink()
51                 backLinkHandlerReg = backLink.addClickHandler(new ClickHandler() {
52                         @Override
53                         public void onClick(ClickEvent event) {
54                         }
55                 });
56
57                 // the main container widget
58                 FlowPanel mainPanel = new FlowPanel();
59                 DOM.setElementAttribute(mainPanel.getElement(), "id", "headbox");
60
61                 mainPanel.add(backLink);
62                 mainPanel.add(tableChooser);
63
64                 initWidget(mainPanel);
65         }
66
67         @Override
68         public void setTableSelection(ArrayList<String> names, ArrayList<String> titles) {
69                 tableChooser.clear();
70                 for (int i = 0; i < names.size(); i++) {
71                         tableChooser.addItem(titles.get(i), names.get(i));
72                 }
73         }
74
75         @Override
76         public void setSelectedTableName(String tableName) {
77                 for (int i = 0; i < tableChooser.getItemCount(); i++) {
78                         if (tableName.equals(tableChooser.getValue(i))) {
79                                 tableChooser.setSelectedIndex(i);
80                                 break;
81                         }
82                 }
83
84         }
85
86         @Override
87         public HasChangeHandlers getTableSelector() {
88                 return tableChooser;
89         }
90
91         @Override
92         public String getSelectedTable() {
93                 int selectedIndex = tableChooser.getSelectedIndex();
94                 return selectedIndex < 0 ? "" : tableChooser.getValue(selectedIndex);
95         }
96
97         public void setBackLink(final String documentID, final String tableName) {
98                 backLinkHandlerReg.removeHandler();
99                 backLinkHandlerReg = backLink.addClickHandler(new ClickHandler() {
100                         @Override
101                         public void onClick(ClickEvent event) {
102                                 presenter.goTo(new ListPlace(documentID, tableName));
103                         }
104                 });
105         }
106
107         @Override
108         public void clear() {
109                 tableChooser.clear();
110         }
111
112         @Override
113         public void setBackLinkVisible(boolean visible) {
114                 backLink.setVisible(visible);
115         }
116
117         @Override
118         public void setPresenter(Presenter presenter) {
119                 this.presenter = presenter;
120         }
121 }