ログ日記

作業ログと日記とメモ

UiBinderで独自クラスを使う場合のコンストラクタ

例えば、SimplePagerでfastForwardRowsの行数を変えたい場合*1

package myapp.client.widget;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiConstructor;
import com.google.gwt.user.cellview.client.SimplePager;

//see: http://code.google.com/p/google-web-toolkit/issues/detail?id=5471
public class SimplePager2  extends SimplePager {
    @UiConstructor
    public SimplePager2(int fastForwardPage){
        this(TextLocation.CENTER, fastForwardPage);
    }
    public SimplePager2(SimplePager.TextLocation location, int fastForwardPage){

        super(location,
                (SimplePager.Resources)GWT.create(SimplePager.Resources.class),
                true, fastForwardPage, true);
    }
}

WidgetでUiConstructorアノテーションを使う。
それからUiBinderでSimplePager2を読み込んで

<!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'
    xmlns:p1="urn:import:com.google.gwt.user.cellview.client"
    xmlns:p2="urn:import:myapp.client.widget">

...

<p2:SimplePager2  ui:field='pager' fastForwardPage="100"/>

...

こんな感じでコンストラクタの引数を指定できる。


ちなみに元々のSimplePagerはGWT Designerで配置した場合にエラーになったが、それはSimplePagerのコンストラクタが

  @UiConstructor
  // Hack for Google I/O demo
  public SimplePager(TextLocation location){
    this(location, getDefaultResources(), true, DEFAULT_FAST_FORWARD_ROWS,
         false);
  }

となっているためで、locationを(たとえば location="CENTER" と)指定する必要がある。
なんかコメントが気になるが…。
ここで使われているgetDefaultResources()とDEFAULT_FAST_FORWARD_ROWSはprivateで宣言されているのでちょっとめんどくさいことになっていて、そのせいでここの値を変更するには上のようなsnippetを使わないといけない。


DEFAULT_FAST_FORWARD_ROWSは1000で定義されているので、テスト中は10などの動きが分かる値にした方がいいと思う。