TabView.java revision c674d3ea684f75f3e05f5834b598050eb1c8856d
1package autotest.common.ui;
2
3import autotest.common.CustomHistory;
4import autotest.common.CustomHistory.HistoryToken;
5
6import com.google.gwt.http.client.URL;
7import com.google.gwt.user.client.Event;
8import com.google.gwt.user.client.History;
9import com.google.gwt.user.client.Window;
10import com.google.gwt.user.client.ui.Composite;
11
12import java.util.Map;
13
14/**
15 * A widget to facilitate building a tab panel from elements present in the
16 * static HTML document.  Each <code>TabView</code> grabs a certain HTML
17 * element, removes it from the document, and wraps it.  It can then be added
18 * to a TabPanel.  The <code>getTitle()</code> method retrieves a title for the
19 * tab from the "title" attribute of the HTML element.  This class also supports
20 * lazy initialization of the tab by waiting until the tab is first displayed.
21 */
22public abstract class TabView extends Composite {
23    protected boolean initialized = false;
24    protected String title;
25    protected boolean visible;
26    private Map<String, String> savedState;
27
28    public TabView() {
29        ElementWidget thisTab = new ElementWidget(getElementId());
30        initWidget(thisTab);
31        title = thisTab.getElement().getAttribute("title");
32    }
33
34    public void ensureInitialized() {
35        if (!initialized) {
36            initialize();
37            initialized = true;
38        }
39    }
40
41    // primarily for subclasses to override
42    public void refresh() {}
43
44    public void display() {
45        ensureInitialized();
46        refresh();
47        visible = true;
48    }
49
50    public void hide() {
51        visible = false;
52    }
53
54    protected boolean isTabVisible() {
55        return visible;
56    }
57
58    @Override
59    public String getTitle() {
60        return title;
61    }
62
63    public void updateHistory() {
64        CustomHistory.newItem(getHistoryArguments());
65    }
66
67    /**
68     * Subclasses should override this to store any additional history information.
69     */
70    public HistoryToken getHistoryArguments() {
71        HistoryToken arguments = new HistoryToken();
72        arguments.put("tab_id", getElementId());
73        return arguments;
74    }
75
76    /**
77     * Subclasses should override this to actually handle the tokens.
78     * Should *not* trigger a refresh.  refresh() will be called separately.
79     */
80    public void handleHistoryArguments(Map<String, String> arguments) {}
81
82    public abstract void initialize();
83    public abstract String getElementId();
84
85    protected void saveHistoryState() {
86        savedState = getHistoryArguments();
87    }
88
89    protected void restoreHistoryState() {
90        handleHistoryArguments(savedState);
91    }
92
93    protected void openHistoryToken(HistoryToken historyToken) {
94        if (isOpenInNewWindowEvent()) {
95            String newUrl = Window.Location.getPath() + "#" + historyToken;
96            Window.open(URL.encode(newUrl), "_blank", "");
97        } else {
98            History.newItem(historyToken.toString());
99        }
100    }
101
102    private static boolean isOpenInNewWindowEvent() {
103        Event event = Event.getCurrentEvent();
104        boolean middleMouseButton = (event.getButton() & Event.BUTTON_MIDDLE) != 0;
105        return event.getCtrlKey() || middleMouseButton;
106    }
107}
108