1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.vectomatic.svg.samples.client;
19
20 import com.google.gwt.core.client.GWT;
21 import com.google.gwt.http.client.Request;
22 import com.google.gwt.http.client.RequestBuilder;
23 import com.google.gwt.http.client.RequestCallback;
24 import com.google.gwt.http.client.RequestException;
25 import com.google.gwt.http.client.Response;
26 import com.google.gwt.uibinder.client.UiField;
27 import com.google.gwt.user.client.ui.FlowPanel;
28 import com.google.gwt.user.client.ui.HTML;
29 import com.google.gwt.user.client.ui.Label;
30 import com.google.gwt.user.client.ui.SimplePanel;
31 import com.google.gwt.user.client.ui.TabLayoutPanel;
32
33
34
35
36
37
38
39
40
41
42
43 public abstract class SampleBase {
44
45
46
47 public static final String HTML_SRC_DIR = "src/";
48
49
50
51 public static final String UIBINDER_SRC_DIR = "binder/";
52
53
54
55 public HTML sourceHtml;
56
57
58
59 public HTML uiBinderHtml;
60
61
62
63 @UiField
64 public TabLayoutPanel tabPanel;
65
66
67
68
69
70 public abstract TabLayoutPanel getPanel();
71
72
73
74
75
76 protected void createCodeTabs(String sampleName) {
77 sourceHtml = createCodeTab("HTML");
78 requestCodeContents(HTML_SRC_DIR + sampleName + ".html", sourceHtml);
79
80 uiBinderHtml = createCodeTab("UIBinder");
81 requestCodeContents(UIBINDER_SRC_DIR + sampleName + ".html", uiBinderHtml);
82
83 tabPanel.selectTab(0);
84 }
85
86 private HTML createCodeTab(String tabName) {
87
88 FlowPanel tabContainer = new FlowPanel();
89 SimplePanel simplePanel = new SimplePanel();
90 FlowPanel container = new FlowPanel();
91 container.setStyleName(Main.mainBundle.css().sample());
92 HTML html = new HTML();
93 tabContainer.add(simplePanel);
94 simplePanel.setWidget(container);
95 container.add(html);
96
97
98 Label label = new Label(tabName);
99 label.setStyleName(Main.mainBundle.css().tab());
100
101
102 tabPanel.add(tabContainer, label);
103 return html;
104 }
105
106
107
108
109 protected void requestCodeContents(String partialPath, final HTML html) {
110
111
112
113
114 RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, GWT.getModuleBaseURL() + partialPath + "?ts=" + System.currentTimeMillis());
115 builder.setCallback(new RequestCallback() {
116 public void onError(Request request, Throwable exception) {
117 html.setHTML("Cannot find resource");
118 }
119
120 public void onResponseReceived(Request request, Response response) {
121 html.setHTML(response.getText());
122 }
123 });
124
125
126 try {
127 builder.send();
128 } catch (RequestException e) {
129 GWT.log("Cannot fetch HTML source for " + partialPath, e);
130 }
131 }
132
133
134
135 protected void resize(int width, int height) {
136 GWT.log("resize: " + width + " " + height);
137 }
138 }