1package autotest.afe;
2
3import autotest.afe.HostDetailView.HostDetailListener;
4import autotest.afe.HostListView.HostListListener;
5import autotest.afe.JobDetailView.JobDetailListener;
6import autotest.afe.JobListView.JobSelectListener;
7import autotest.afe.create.CreateJobViewPresenter.JobCreateListener;
8import autotest.afe.create.CreateJobViewTab;
9import autotest.common.CustomHistory;
10import autotest.common.JsonRpcProxy;
11import autotest.common.SiteCommonClassFactory;
12import autotest.common.StaticDataRepository;
13import autotest.common.ui.CustomTabPanel;
14import autotest.common.ui.NotifyManager;
15import autotest.common.ui.TabView;
16
17import com.google.gwt.core.client.EntryPoint;
18import com.google.gwt.dom.client.Document;
19import com.google.gwt.json.client.JSONValue;
20import com.google.gwt.user.client.ui.RootPanel;
21import com.google.gwt.user.client.Window.Location;
22
23
24public class AfeClient implements EntryPoint {
25    private JobListView jobList;
26    private JobDetailView jobDetail;
27    private CreateJobViewTab createJob;
28    private HostListView hostListView;
29    private HostDetailView hostDetailView;
30
31    public CustomTabPanel mainTabPanel = new CustomTabPanel();
32
33    /**
34     * Application entry point.
35     */
36    public void onModuleLoad() {
37        JsonRpcProxy.setDefaultBaseUrl(JsonRpcProxy.AFE_BASE_URL);
38        NotifyManager.getInstance().initialize();
39
40        // initialize static data, and don't show main UI until that's done
41        StaticDataRepository.getRepository().refresh(
42                                 new StaticDataRepository.FinishedCallback() {
43            public void onFinished() {
44                finishLoading();
45            }
46        });
47    }
48
49    private JobCreateListener jobCreateListener = new JobCreateListener() {
50        public void onJobCreated(int jobId) {
51            showJob(jobId);
52        }
53    };
54
55    protected void finishLoading() {
56        SiteCommonClassFactory.globalInitialize();
57
58        String wmatrixUrl = StaticDataRepository.getRepository().getData(
59            "wmatrix_url").isString().stringValue();
60        if (!wmatrixUrl.equals("")) {
61            Document.get().getElementById("wmatrix-link").setAttribute(
62                "href", wmatrixUrl);
63            Document.get().getElementById("wmatrix").removeClassName("hidden");
64        }
65        boolean is_moblab = StaticDataRepository.getRepository().getData(
66            "is_moblab").isBoolean().booleanValue();
67        if (is_moblab) {
68            Document.get().getElementById("moblab_setup").removeClassName("hidden");
69            Document.get().getElementById("mobmonitor_link").setAttribute("href",
70                "http://" + Location.getHostName() + ":9991");
71        }
72
73        jobList = new JobListView(new JobSelectListener() {
74            public void onJobSelected(int jobId) {
75                showJob(jobId);
76            }
77        });
78        jobDetail = new JobDetailView(new JobDetailListener() {
79            public void onHostSelected(String hostId) {
80                showHost(hostId);
81            }
82
83            public void onCloneJob(JSONValue cloneInfo) {
84                createJob.ensureInitialized();
85                mainTabPanel.selectTabView(createJob);
86                // Makes sure we set cloning mode after the tab is selected. Otherwise,
87                // the mode will be reset.
88                createJob.cloneJob(cloneInfo);
89            }
90        });
91
92        createJob = AfeUtils.factory.getCreateJobView(jobCreateListener);
93
94        hostListView = new HostListView(new HostListListener() {
95            public void onHostSelected(String hostId) {
96                showHost(hostId);
97            }
98        }, jobCreateListener);
99
100        hostDetailView = new HostDetailView(new HostDetailListener() {
101            public void onJobSelected(int jobId) {
102                showJob(jobId);
103            }
104        }, jobCreateListener);
105
106        TabView[] tabViews = new TabView[] {jobList, jobDetail, createJob,
107                                            hostListView, hostDetailView};
108        for (TabView tabView : tabViews) {
109            mainTabPanel.addTabView(tabView);
110        }
111
112        final RootPanel tabsRoot = RootPanel.get("tabs");
113        tabsRoot.add(mainTabPanel);
114        CustomHistory.processInitialToken();
115        mainTabPanel.initialize();
116        tabsRoot.setStyleName("");
117    }
118
119    protected void showJob(int jobId) {
120        jobDetail.ensureInitialized();
121        jobDetail.updateObjectId(Integer.toString(jobId));
122        mainTabPanel.selectTabView(jobDetail);
123    }
124
125    protected void showHost(String hostId) {
126        hostDetailView.ensureInitialized();
127        hostDetailView.updateObjectId(hostId);
128        mainTabPanel.selectTabView(hostDetailView);
129    }
130}
131