View Javadoc

1   /**********************************************
2    * Copyright (C) 2009 Lukas Laag
3    * This file is part of lib-gwt-svg-samples.
4    * 
5    * libgwtsvg-samples is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    * 
10   * libgwtsvg-samples is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License
16   * along with libgwtsvg-samples.  If not, see http://www.gnu.org/licenses/
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   * Base class for lib-gwt-svg samples. All samples are
35   * stored in a tab panel with three tabs.
36   * <ol>
37   * <li>The first tab contains the result of the sample code execution</li>
38   * <li>The second tab contains the source code for the sample</li>
39   * <li>The third tab contains the UiBinder code for the sample</li>
40   * <ol>
41   * @author laaglu
42   */
43  public abstract class SampleBase {
44  	/**
45  	 * Directory where HTML-ified source code is generated
46  	 */
47  	public static final String HTML_SRC_DIR = "src/";
48  	/**
49  	 * Directory where HTML-ified UiBinder code is generated
50  	 */
51  	public static final String UIBINDER_SRC_DIR = "binder/";
52  	/**
53  	 * The HTML-ified source code
54  	 */
55  	public HTML sourceHtml;
56  	/**
57  	 * The HTML-ified UiBinder code
58  	 */
59  	public HTML uiBinderHtml;
60  	/**
61  	 * The tab panel containing this sample
62  	 */
63  	@UiField
64  	public TabLayoutPanel tabPanel;
65  
66  	/**
67  	 * Instantiates the tab panel which contains the sample
68  	 * @return
69  	 */
70  	public abstract TabLayoutPanel getPanel();
71  	
72  	/**
73  	 * Dynamically create the source code tab and UiBinder code tab
74  	 * @param sampleName The name of the sample
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  		// Create the tab container structure
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  		// Create the tab item
98  		Label label = new Label(tabName);
99  		label.setStyleName(Main.mainBundle.css().tab());
100 		
101 		// Add the tab
102 		tabPanel.add(tabContainer, label);
103 		return html;
104 	}
105 
106 	/**
107 	 * Load the sample HTML source code
108 	 */
109 	protected void requestCodeContents(String partialPath, final HTML html) {
110 
111 		// Request the contents of the file
112 		// Add a bogus query to bypass the browser cache as advised by:
113 		// https://developer.mozilla.org/En/Using_XMLHttpRequest#Bypassing_the_cache
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 		// Send the request
126 		try {
127 			builder.send();
128 		} catch (RequestException e) {
129 			GWT.log("Cannot fetch HTML source for " + partialPath, e);
130 		}
131 	}
132 	/**
133 	 * Resizes the sample
134 	 */
135 	protected void resize(int width, int height) {
136 		GWT.log("resize: " + width + " " + height);
137 	}
138 }