1e3f6868dac3b4c4714637d12b93d97823011a35cshowardpackage autotest.common.ui;
21c8c2215e525de8813c375e796354f8ffb811a08showard
3acdbe3535529a24555d874a8038993c62fd690c2showardimport autotest.common.CustomHistory;
46f1593c98487664a6c330a638e6645dc39b4aca3showardimport autotest.common.CustomHistory.CustomHistoryListener;
5acdbe3535529a24555d874a8038993c62fd690c2showard
6a5e4d84c9616b0a987e904d3b1d1d3fd9ca1be86showardimport com.google.gwt.event.dom.client.ClickEvent;
7a5e4d84c9616b0a987e904d3b1d1d3fd9ca1be86showardimport com.google.gwt.event.dom.client.ClickHandler;
879a7b0d387aac103fc1d125353eefa361030452ashowardimport com.google.gwt.event.logical.shared.BeforeSelectionEvent;
979a7b0d387aac103fc1d125353eefa361030452ashowardimport com.google.gwt.event.logical.shared.BeforeSelectionHandler;
1079a7b0d387aac103fc1d125353eefa361030452ashowardimport com.google.gwt.event.logical.shared.SelectionEvent;
1179a7b0d387aac103fc1d125353eefa361030452ashowardimport com.google.gwt.event.logical.shared.SelectionHandler;
129e049285fcdf1a964708774f46b84aae5c09837aJiaxi Luoimport com.google.gwt.event.logical.shared.ValueChangeEvent;
139e049285fcdf1a964708774f46b84aae5c09837aJiaxi Luoimport com.google.gwt.event.logical.shared.ValueChangeHandler;
149e049285fcdf1a964708774f46b84aae5c09837aJiaxi Luoimport com.google.gwt.user.client.Timer;
15acdbe3535529a24555d874a8038993c62fd690c2showardimport com.google.gwt.user.client.ui.Button;
169e049285fcdf1a964708774f46b84aae5c09837aJiaxi Luoimport com.google.gwt.user.client.ui.CheckBox;
171c8c2215e525de8813c375e796354f8ffb811a08showardimport com.google.gwt.user.client.ui.Composite;
181c8c2215e525de8813c375e796354f8ffb811a08showardimport com.google.gwt.user.client.ui.DeckPanel;
191c8c2215e525de8813c375e796354f8ffb811a08showardimport com.google.gwt.user.client.ui.HorizontalPanel;
201c8c2215e525de8813c375e796354f8ffb811a08showardimport com.google.gwt.user.client.ui.Panel;
211c8c2215e525de8813c375e796354f8ffb811a08showardimport com.google.gwt.user.client.ui.TabPanel;
221c8c2215e525de8813c375e796354f8ffb811a08showardimport com.google.gwt.user.client.ui.VerticalPanel;
23acdbe3535529a24555d874a8038993c62fd690c2showard
24acdbe3535529a24555d874a8038993c62fd690c2showardimport java.util.ArrayList;
25acdbe3535529a24555d874a8038993c62fd690c2showardimport java.util.Collections;
26acdbe3535529a24555d874a8038993c62fd690c2showardimport java.util.List;
27ca67229c5d9d3d6d5a94164338cedb1db6fc49cashowardimport java.util.Map;
281c8c2215e525de8813c375e796354f8ffb811a08showard
2979a7b0d387aac103fc1d125353eefa361030452ashowardpublic class CustomTabPanel extends Composite implements CustomHistoryListener,
3079a7b0d387aac103fc1d125353eefa361030452ashoward                                                         BeforeSelectionHandler<Integer>,
3179a7b0d387aac103fc1d125353eefa361030452ashoward                                                         SelectionHandler<Integer> {
32920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang    private static final int AUTOREFRESH_INTERVAL = 5000; // millisecond
33920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang
341c8c2215e525de8813c375e796354f8ffb811a08showard    protected TabPanel tabPanel = new TabPanel();
351c8c2215e525de8813c375e796354f8ffb811a08showard    protected Panel otherWidgetsPanel = new HorizontalPanel();
36a334a8b203268d4a53dd39130add56874eabd012showard    private Panel commonAreaPanel = new VerticalPanel();
37acdbe3535529a24555d874a8038993c62fd690c2showard    protected Button refreshButton = new Button("Refresh");
38920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang    protected boolean autorefreshEnabled = false;
39920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang    protected CheckBox autorefreshCheckbox = new CheckBox("Auto Refresh");
401c8c2215e525de8813c375e796354f8ffb811a08showard    protected int topBarHeight = 0;
41acdbe3535529a24555d874a8038993c62fd690c2showard    protected List<TabView> tabViews = new ArrayList<TabView>();
426f1593c98487664a6c330a638e6645dc39b4aca3showard    private boolean doUpdateHistory = true;
43920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang    private Timer autorefreshTimer;
44920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang
451c8c2215e525de8813c375e796354f8ffb811a08showard    public CustomTabPanel() {
46920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang      this(false);
47920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang    }
48920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang
49920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang    public CustomTabPanel(boolean enableAutorefresh) {
501c8c2215e525de8813c375e796354f8ffb811a08showard        VerticalPanel container = new VerticalPanel();
511c8c2215e525de8813c375e796354f8ffb811a08showard        HorizontalPanel top = new HorizontalPanel();
52a334a8b203268d4a53dd39130add56874eabd012showard        VerticalPanel bottom = new VerticalPanel();
531c8c2215e525de8813c375e796354f8ffb811a08showard        container.add(top);
54a334a8b203268d4a53dd39130add56874eabd012showard        container.add(bottom);
55920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang
561c8c2215e525de8813c375e796354f8ffb811a08showard        // put the TabBar at the top left
571c8c2215e525de8813c375e796354f8ffb811a08showard        top.add(tabPanel.getTabBar());
581c8c2215e525de8813c375e796354f8ffb811a08showard        top.setCellHeight(tabPanel.getTabBar(), "100%");
591c8c2215e525de8813c375e796354f8ffb811a08showard        tabPanel.getTabBar().setHeight("100%");
60920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang
611c8c2215e525de8813c375e796354f8ffb811a08showard        // make a place for other widgets next to the tab bar
621c8c2215e525de8813c375e796354f8ffb811a08showard        top.add(otherWidgetsPanel);
63920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang
64a334a8b203268d4a53dd39130add56874eabd012showard        // put a common area above the tab deck
650c31bc5ef2ecdf8edf19468e1a373520110f5bc6showard        commonAreaPanel.setWidth("100%");
66a334a8b203268d4a53dd39130add56874eabd012showard        bottom.add(commonAreaPanel);
67920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang
68acdbe3535529a24555d874a8038993c62fd690c2showard        // put the TabPanel's DeckPanel below
691c8c2215e525de8813c375e796354f8ffb811a08showard        DeckPanel tabDeck = tabPanel.getDeckPanel();
70a334a8b203268d4a53dd39130add56874eabd012showard        bottom.add(tabDeck);
71a334a8b203268d4a53dd39130add56874eabd012showard        bottom.setCellHeight(tabDeck, "100%");
72920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang
7379a7b0d387aac103fc1d125353eefa361030452ashoward        tabPanel.addBeforeSelectionHandler(this);
7479a7b0d387aac103fc1d125353eefa361030452ashoward        tabPanel.addSelectionHandler(this);
75920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang
76a334a8b203268d4a53dd39130add56874eabd012showard        // transfer the DeckPanel's class to the entire bottom panel
77a334a8b203268d4a53dd39130add56874eabd012showard        String tabDeckClass = tabDeck.getStyleName();
78a334a8b203268d4a53dd39130add56874eabd012showard        tabDeck.setStyleName("");
79a334a8b203268d4a53dd39130add56874eabd012showard        bottom.setStyleName(tabDeckClass);
8002ed4bd47471fe77270e5507a7f9759955b3753cshoward        bottom.setWidth("100%");
81920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang
82a5e4d84c9616b0a987e904d3b1d1d3fd9ca1be86showard        refreshButton.addClickHandler(new ClickHandler() {
83a5e4d84c9616b0a987e904d3b1d1d3fd9ca1be86showard            public void onClick(ClickEvent event) {
84acdbe3535529a24555d874a8038993c62fd690c2showard                getSelectedTabView().refresh();
85920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang            }
86acdbe3535529a24555d874a8038993c62fd690c2showard        });
87acdbe3535529a24555d874a8038993c62fd690c2showard        otherWidgetsPanel.add(refreshButton);
889e049285fcdf1a964708774f46b84aae5c09837aJiaxi Luo
89920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang        this.autorefreshEnabled = enableAutorefresh;
90920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang        if (enableAutorefresh) {
91920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang            autorefreshCheckbox.addValueChangeHandler(new ValueChangeHandler() {
92920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang              public void onValueChange(ValueChangeEvent event) {
93920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang                  // Ensure Initialization.
94920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang                  if (autorefreshTimer == null)
95920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang                      initialize();
96920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang
97920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang                  Boolean value = autorefreshCheckbox.getValue();
98920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang                  TabView selectedTabView = tabViews.get(
99920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang                          tabPanel.getTabBar().getSelectedTab());
100920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang                  if (selectedTabView != null)
101920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang                      selectedTabView.setAutorefresh(value);
102920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang                  if (value) {
103920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang                      autorefreshTimer.scheduleRepeating(AUTOREFRESH_INTERVAL);
104920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang                  } else {
105920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang                      autorefreshTimer.cancel();
106920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang                  }
107920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang              }
108920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang          });
109920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang          otherWidgetsPanel.add(autorefreshCheckbox);
110920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang        }
111920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang
112acdbe3535529a24555d874a8038993c62fd690c2showard        CustomHistory.addHistoryListener(this);
113d58f008bca8c2d70c3f36d93a9fc1f76f25b4d3fJiaxi Luo
114d58f008bca8c2d70c3f36d93a9fc1f76f25b4d3fJiaxi Luo        top.setStyleName("custom-tab-top");
115acdbe3535529a24555d874a8038993c62fd690c2showard        container.setStyleName("custom-tab-panel");
1161c8c2215e525de8813c375e796354f8ffb811a08showard        initWidget(container);
1171c8c2215e525de8813c375e796354f8ffb811a08showard    }
118920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang
119acdbe3535529a24555d874a8038993c62fd690c2showard    /**
120acdbe3535529a24555d874a8038993c62fd690c2showard     * This must be called after this widget has been added to the page.
121acdbe3535529a24555d874a8038993c62fd690c2showard     */
122acdbe3535529a24555d874a8038993c62fd690c2showard    public void initialize() {
123920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang        // if the history token didn't provide a selected tab, default to the
124acdbe3535529a24555d874a8038993c62fd690c2showard        // first tab
125acdbe3535529a24555d874a8038993c62fd690c2showard        if (getSelectedTabView() == null)
126acdbe3535529a24555d874a8038993c62fd690c2showard            tabPanel.selectTab(0);
127920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang        autorefreshTimer = new Timer() {
128920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang            @Override
129920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang            public void run() {
130920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang                getSelectedTabView().refresh();
131920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang            }
132920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang        };
133acdbe3535529a24555d874a8038993c62fd690c2showard    }
134920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang
135acdbe3535529a24555d874a8038993c62fd690c2showard    public void addTabView(TabView tabView) {
1369f4500a294eea35dce003cee41c558fcde3eb09fshoward        tabView.attachToDocument();
137acdbe3535529a24555d874a8038993c62fd690c2showard        tabViews.add(tabView);
1389f4500a294eea35dce003cee41c558fcde3eb09fshoward        tabPanel.add(tabView.getWidget(), tabView.getTitle());
139acdbe3535529a24555d874a8038993c62fd690c2showard    }
140920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang
141acdbe3535529a24555d874a8038993c62fd690c2showard    public List<TabView> getTabViews() {
142acdbe3535529a24555d874a8038993c62fd690c2showard        return Collections.unmodifiableList(tabViews);
143acdbe3535529a24555d874a8038993c62fd690c2showard    }
144920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang
145acdbe3535529a24555d874a8038993c62fd690c2showard    public TabView getSelectedTabView() {
146acdbe3535529a24555d874a8038993c62fd690c2showard        int selectedTab = tabPanel.getTabBar().getSelectedTab();
147acdbe3535529a24555d874a8038993c62fd690c2showard        if (selectedTab == -1)
148acdbe3535529a24555d874a8038993c62fd690c2showard            return null;
149acdbe3535529a24555d874a8038993c62fd690c2showard        return tabViews.get(selectedTab);
150acdbe3535529a24555d874a8038993c62fd690c2showard    }
151920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang
152acdbe3535529a24555d874a8038993c62fd690c2showard    public void selectTabView(TabView tabView) {
153acdbe3535529a24555d874a8038993c62fd690c2showard        for (int i = 0; i < tabViews.size(); i++) {
154acdbe3535529a24555d874a8038993c62fd690c2showard            if (tabViews.get(i) == tabView) {
155acdbe3535529a24555d874a8038993c62fd690c2showard                tabPanel.selectTab(i);
156acdbe3535529a24555d874a8038993c62fd690c2showard                return;
157acdbe3535529a24555d874a8038993c62fd690c2showard            }
158acdbe3535529a24555d874a8038993c62fd690c2showard        }
159920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang
160acdbe3535529a24555d874a8038993c62fd690c2showard        throw new IllegalArgumentException("Tab not found");
161acdbe3535529a24555d874a8038993c62fd690c2showard    }
162e3f6868dac3b4c4714637d12b93d97823011a35cshoward
163e3f6868dac3b4c4714637d12b93d97823011a35cshoward    public TabPanel getTabPanel() {
164e3f6868dac3b4c4714637d12b93d97823011a35cshoward        return tabPanel;
165e3f6868dac3b4c4714637d12b93d97823011a35cshoward    }
166e3f6868dac3b4c4714637d12b93d97823011a35cshoward
167e3f6868dac3b4c4714637d12b93d97823011a35cshoward    public Panel getOtherWidgetsPanel() {
168e3f6868dac3b4c4714637d12b93d97823011a35cshoward        return otherWidgetsPanel;
169e3f6868dac3b4c4714637d12b93d97823011a35cshoward    }
170920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang
171a334a8b203268d4a53dd39130add56874eabd012showard    public Panel getCommonAreaPanel() {
172a334a8b203268d4a53dd39130add56874eabd012showard        return commonAreaPanel;
173a334a8b203268d4a53dd39130add56874eabd012showard    }
174acdbe3535529a24555d874a8038993c62fd690c2showard
1756f1593c98487664a6c330a638e6645dc39b4aca3showard    public void onHistoryChanged(Map<String, String> arguments) {
176ca67229c5d9d3d6d5a94164338cedb1db6fc49cashoward        String tabId = arguments.get("tab_id");
177ca67229c5d9d3d6d5a94164338cedb1db6fc49cashoward        if (tabId == null) {
178ca67229c5d9d3d6d5a94164338cedb1db6fc49cashoward            return;
179ca67229c5d9d3d6d5a94164338cedb1db6fc49cashoward        }
180920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang
181acdbe3535529a24555d874a8038993c62fd690c2showard        for (TabView tabView : tabViews) {
182ca67229c5d9d3d6d5a94164338cedb1db6fc49cashoward            if (tabId.equals(tabView.getElementId())) {
183acdbe3535529a24555d874a8038993c62fd690c2showard                tabView.ensureInitialized();
184353923be210e4c452ecd4e23ccd4e2d1fbebb490showard                if (arguments.size() > 1) {
185353923be210e4c452ecd4e23ccd4e2d1fbebb490showard                    // only pass along arguments if there's more than just tab_id
186353923be210e4c452ecd4e23ccd4e2d1fbebb490showard                    tabView.handleHistoryArguments(arguments);
187353923be210e4c452ecd4e23ccd4e2d1fbebb490showard                }
188920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang
1896f1593c98487664a6c330a638e6645dc39b4aca3showard                if (getSelectedTabView() != tabView) {
1906f1593c98487664a6c330a638e6645dc39b4aca3showard                    doUpdateHistory = false;
191acdbe3535529a24555d874a8038993c62fd690c2showard                    selectTabView(tabView);
1926f1593c98487664a6c330a638e6645dc39b4aca3showard                    doUpdateHistory = true;
1936f1593c98487664a6c330a638e6645dc39b4aca3showard                } else {
194353923be210e4c452ecd4e23ccd4e2d1fbebb490showard                    tabView.display();
1956f1593c98487664a6c330a638e6645dc39b4aca3showard                }
196920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang
197acdbe3535529a24555d874a8038993c62fd690c2showard                return;
198acdbe3535529a24555d874a8038993c62fd690c2showard            }
199acdbe3535529a24555d874a8038993c62fd690c2showard        }
200acdbe3535529a24555d874a8038993c62fd690c2showard    }
201920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang
20279a7b0d387aac103fc1d125353eefa361030452ashoward    @Override
20379a7b0d387aac103fc1d125353eefa361030452ashoward    public void onBeforeSelection(BeforeSelectionEvent<Integer> event) {
2046f1593c98487664a6c330a638e6645dc39b4aca3showard        // do nothing if the user clicks the selected tab
20579a7b0d387aac103fc1d125353eefa361030452ashoward        if (tabPanel.getTabBar().getSelectedTab() == event.getItem())
20679a7b0d387aac103fc1d125353eefa361030452ashoward            event.cancel();
2070c31bc5ef2ecdf8edf19468e1a373520110f5bc6showard        TabView selectedTabView = getSelectedTabView();
2080c31bc5ef2ecdf8edf19468e1a373520110f5bc6showard        if (selectedTabView != null) {
2090c31bc5ef2ecdf8edf19468e1a373520110f5bc6showard            selectedTabView.hide();
2100c31bc5ef2ecdf8edf19468e1a373520110f5bc6showard        }
21179a7b0d387aac103fc1d125353eefa361030452ashoward        tabViews.get(event.getItem()).ensureInitialized();
21279a7b0d387aac103fc1d125353eefa361030452ashoward        tabViews.get(event.getItem()).display();
2136f1593c98487664a6c330a638e6645dc39b4aca3showard    }
214920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang
21579a7b0d387aac103fc1d125353eefa361030452ashoward    @Override
21679a7b0d387aac103fc1d125353eefa361030452ashoward    public void onSelection(SelectionEvent<Integer> event) {
217d94d8dc25b6c3f7af965075474d1583cbd7e471eJiaxi Luo        TabView selectedTabView = tabViews.get(event.getSelectedItem());
2186f1593c98487664a6c330a638e6645dc39b4aca3showard        if (doUpdateHistory)
219d94d8dc25b6c3f7af965075474d1583cbd7e471eJiaxi Luo            selectedTabView.updateHistory();
220920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang        // The second parameter is to fire a valueChange event if value changed.
221920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang        if (autorefreshEnabled) {
222920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang          autorefreshCheckbox.setValue(selectedTabView.isAutorefreshOn(),
223920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang             true);
224920a2d1fb62a56abdfd58f90df1d7d457abbc55cMichael Tang        }
2256f1593c98487664a6c330a638e6645dc39b4aca3showard    }
2261c8c2215e525de8813c375e796354f8ffb811a08showard}
227