1 package autotest.common.ui;
2 
3 import autotest.common.CustomHistory;
4 import autotest.common.CustomHistory.CustomHistoryListener;
5 
6 import com.google.gwt.event.dom.client.ClickEvent;
7 import com.google.gwt.event.dom.client.ClickHandler;
8 import com.google.gwt.event.logical.shared.BeforeSelectionEvent;
9 import com.google.gwt.event.logical.shared.BeforeSelectionHandler;
10 import com.google.gwt.event.logical.shared.SelectionEvent;
11 import com.google.gwt.event.logical.shared.SelectionHandler;
12 import com.google.gwt.event.logical.shared.ValueChangeEvent;
13 import com.google.gwt.event.logical.shared.ValueChangeHandler;
14 import com.google.gwt.user.client.Timer;
15 import com.google.gwt.user.client.ui.Button;
16 import com.google.gwt.user.client.ui.CheckBox;
17 import com.google.gwt.user.client.ui.Composite;
18 import com.google.gwt.user.client.ui.DeckPanel;
19 import com.google.gwt.user.client.ui.HorizontalPanel;
20 import com.google.gwt.user.client.ui.Panel;
21 import com.google.gwt.user.client.ui.TabPanel;
22 import com.google.gwt.user.client.ui.VerticalPanel;
23 
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.Map;
28 
29 public class CustomTabPanel extends Composite implements CustomHistoryListener,
30                                                          BeforeSelectionHandler<Integer>,
31                                                          SelectionHandler<Integer> {
32     protected TabPanel tabPanel = new TabPanel();
33     protected Panel otherWidgetsPanel = new HorizontalPanel();
34     private Panel commonAreaPanel = new VerticalPanel();
35     protected Button refreshButton = new Button("Refresh");
36     protected int topBarHeight = 0;
37     protected List<TabView> tabViews = new ArrayList<TabView>();
38     private boolean doUpdateHistory = true;
39 
CustomTabPanel()40     public CustomTabPanel() {
41         VerticalPanel container = new VerticalPanel();
42         HorizontalPanel top = new HorizontalPanel();
43         VerticalPanel bottom = new VerticalPanel();
44         container.add(top);
45         container.add(bottom);
46 
47         // put the TabBar at the top left
48         top.add(tabPanel.getTabBar());
49         top.setCellHeight(tabPanel.getTabBar(), "100%");
50         tabPanel.getTabBar().setHeight("100%");
51 
52         // make a place for other widgets next to the tab bar
53         top.add(otherWidgetsPanel);
54 
55         // put a common area above the tab deck
56         commonAreaPanel.setWidth("100%");
57         bottom.add(commonAreaPanel);
58 
59         // put the TabPanel's DeckPanel below
60         DeckPanel tabDeck = tabPanel.getDeckPanel();
61         bottom.add(tabDeck);
62         bottom.setCellHeight(tabDeck, "100%");
63 
64         tabPanel.addBeforeSelectionHandler(this);
65         tabPanel.addSelectionHandler(this);
66 
67         // transfer the DeckPanel's class to the entire bottom panel
68         String tabDeckClass = tabDeck.getStyleName();
69         tabDeck.setStyleName("");
70         bottom.setStyleName(tabDeckClass);
71         bottom.setWidth("100%");
72 
73         refreshButton.addClickHandler(new ClickHandler() {
74             public void onClick(ClickEvent event) {
75                 getSelectedTabView().refresh();
76             }
77         });
78         otherWidgetsPanel.add(refreshButton);
79 
80         CustomHistory.addHistoryListener(this);
81 
82         top.setStyleName("custom-tab-top");
83         container.setStyleName("custom-tab-panel");
84         initWidget(container);
85     }
86 
87     /**
88      * This must be called after this widget has been added to the page.
89      */
initialize()90     public void initialize() {
91         // if the history token didn't provide a selected tab, default to the
92         // first tab
93         if (getSelectedTabView() == null)
94             tabPanel.selectTab(0);
95     }
96 
addTabView(TabView tabView)97     public void addTabView(TabView tabView) {
98         tabView.attachToDocument();
99         tabViews.add(tabView);
100         tabPanel.add(tabView.getWidget(), tabView.getTitle());
101     }
102 
getTabViews()103     public List<TabView> getTabViews() {
104         return Collections.unmodifiableList(tabViews);
105     }
106 
getSelectedTabView()107     public TabView getSelectedTabView() {
108         int selectedTab = tabPanel.getTabBar().getSelectedTab();
109         if (selectedTab == -1)
110             return null;
111         return tabViews.get(selectedTab);
112     }
113 
selectTabView(TabView tabView)114     public void selectTabView(TabView tabView) {
115         for (int i = 0; i < tabViews.size(); i++) {
116             if (tabViews.get(i) == tabView) {
117                 tabPanel.selectTab(i);
118                 return;
119             }
120         }
121 
122         throw new IllegalArgumentException("Tab not found");
123     }
124 
getTabPanel()125     public TabPanel getTabPanel() {
126         return tabPanel;
127     }
128 
getOtherWidgetsPanel()129     public Panel getOtherWidgetsPanel() {
130         return otherWidgetsPanel;
131     }
132 
getCommonAreaPanel()133     public Panel getCommonAreaPanel() {
134         return commonAreaPanel;
135     }
136 
onHistoryChanged(Map<String, String> arguments)137     public void onHistoryChanged(Map<String, String> arguments) {
138         String tabId = arguments.get("tab_id");
139         if (tabId == null) {
140             return;
141         }
142 
143         for (TabView tabView : tabViews) {
144             if (tabId.equals(tabView.getElementId())) {
145                 tabView.ensureInitialized();
146                 if (arguments.size() > 1) {
147                     // only pass along arguments if there's more than just tab_id
148                     tabView.handleHistoryArguments(arguments);
149                 }
150 
151                 if (getSelectedTabView() != tabView) {
152                     doUpdateHistory = false;
153                     selectTabView(tabView);
154                     doUpdateHistory = true;
155                 } else {
156                     tabView.display();
157                 }
158 
159                 return;
160             }
161         }
162     }
163 
164     @Override
onBeforeSelection(BeforeSelectionEvent<Integer> event)165     public void onBeforeSelection(BeforeSelectionEvent<Integer> event) {
166         // do nothing if the user clicks the selected tab
167         if (tabPanel.getTabBar().getSelectedTab() == event.getItem())
168             event.cancel();
169         TabView selectedTabView = getSelectedTabView();
170         if (selectedTabView != null) {
171             selectedTabView.hide();
172         }
173         tabViews.get(event.getItem()).ensureInitialized();
174         tabViews.get(event.getItem()).display();
175     }
176 
177     @Override
onSelection(SelectionEvent<Integer> event)178     public void onSelection(SelectionEvent<Integer> event) {
179         TabView selectedTabView = tabViews.get(event.getSelectedItem());
180         if (doUpdateHistory)
181             selectedTabView.updateHistory();
182     }
183 }
184