GAE + GWT プログラミングメモ

Google App Engine とGoogle Web Toolkit のメモ

GWTでブラウザバックを実行する

GWTでブラウザバックを行うためには、HyperlinkとHistoryを使う。
まずは適当にUiBuilderでUIを作る。

<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
	xmlns:g="urn:import:com.google.gwt.user.client.ui">
	<ui:style>
		
	</ui:style>
	<g:HTMLPanel>
		<g:VerticalPanel>
			<g:HorizontalPanel>
				<g:SimplePanel ui:field="hyperlink1Panel"/>
				<g:SimplePanel ui:field="hyperlink2Panel"/>
			</g:HorizontalPanel>
			<g:Label text="New Label" ui:field="resultLabel"/>
		</g:VerticalPanel>
	</g:HTMLPanel>
</ui:UiBinder> 

Hyperlinkのインスタンス化で第1引数に表示文字、第2引数にトークン(URLの#以降に追加される文字列)を指定する。
ブラウザバックを有効化させるためにはHistory.addValueChangeHandlerにハンドラを登録すればよい。
このとき、引数のevent.getValue()でHyperlinkのインスタンス化で第2引数に指定した文字列を取得することができる。

public class MainComposite extends Composite {

	private static MainCompositeUiBinder uiBinder = GWT
			.create(MainCompositeUiBinder.class);
	@UiField SimplePanel hyperlink1Panel;
	@UiField SimplePanel hyperlink2Panel;
	@UiField Label resultLabel;

	interface MainCompositeUiBinder extends UiBinder<Widget, MainComposite> {
	}

	public MainComposite() {
		initWidget(uiBinder.createAndBindUi(this));
		Hyperlink link1 = new Hyperlink("link1", "link1Token");
		hyperlink1Panel.add(link1);
		Hyperlink link2 = new Hyperlink("link2", "link2Token");
		hyperlink2Panel.add(link2);
		History.addValueChangeHandler(new ValueChangeHandler<String>() {
			@Override
			public void onValueChange(ValueChangeEvent<String> event) {
				resultLabel.setText(event.getValue());
			}
		});
	}
}


作成中のアニメ特化アンテナサイト
http://kumo2ji.appspot.com/