チュートリアル5(検索入力)

次は、SeekHtml の inputMain を実装します。
検索ボタン後の処理は outputMain で判定されているので、

  • ページリンク
  • 編集ボタン
  • 削除ボタン
  • 新規追加ボタン

が処理対象となります。

ページリンク

ページリンクには"page=1"のように別途クエリを設定しました。
これを判断材料にします。

tutorial2/logic/user/SeekHtml.java①
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
    public Forward inputMain( RequestParameter req, Forward fw ) throws PageException {
        // fwにはモックアップと同じ遷移先が設定されています。
        // 別の遷移先としたい場合は、新しくForwardインスタンスを生成して
        // リターンして下さい。
 
        RequestItem item = req.getItem( "page" );
        if ( item != null ) {
            page = Integer.parseInt( item.getValue( 0 ) );
        }
        return fw;
    }

page パラメータがあれば、page 変数に代入します。
後は outputMain が page 変数を参照して勝手に表示範囲を変更してくれます。

各ボタン

ボタン(コントロール)には任意のオブジェクトインスタンスを持たせる事が可能です。
ボタンに紐づけたインスタンスにより後々での処理方法を判断します。
まず、次のような enum とクラスをインナーとして宣言します。

tutorial2/logic/user/SeekHtml.java②
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
public class SeekHtml extends tutorial2.view.user.SeekHtml {
    /**
     * 処理種別。
     */
    public enum Type {
        EDIT,
        DELETE
    }
 
    /**
     * 処理対象。
     */
    public class Action {
        SeekHtml.Type   type;
        User                user;
        Action( SeekHtml.Type t, User u ) {
            type = t;
            user = u;
        }
    }

新規追加ボタンへは、initで割り当てておきます。

tutorial2/logic/user/SeekHtml.java③
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
    public void init(){
        super.init();
        // モックアップHTMLの内容と異なる内容で初期化したい場合は
        // super.init()の後に変更用の処理を記述して下さい。
        // このメソッドは上記コンストラクタの中から呼ばれます。
        // インスタンス発生はバックグラウンドで非同期に処理していますので
        // HTTPリクエスト発生よりずっと過去に初期化されます。
        // このため、ユーザーのトランザクションに応じた条件分岐を設けても
        // 意図した振る舞いになりません。
 
        getNotFoundP().setVisible( false );
        getResultDiv().setVisible( false );
        // テーブルのデータ行を複製する。
        org_line = getListTable().getLineReplica( 1 );
        // 新規追加へ指示の割り当て。
        getNewInput().setUserData( new Action( Type.EDIT, null ) );
    }

編集と削除は、一覧作成時に割り当てます。

tutorial2/logic/user/SeekHtml.java④
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
    private void makeTable( int start, int end ) {
        Table   table = getListTable();
        table.removeLine( 1, -1 );
        int count = end - start + 1;
        for ( int i = 0; i < count; i++ ) {
            User    user = User.db.get( start + i );
            Line    line = (Line)org_line.getReplica();
            line.setValueString(
                0,
                Integer.toString( i + 1 ),
                user.user_id,
                user.name,
                user.sex.equals( "m" )? "男性": "女性",
                user.address
            );
            table.addLine( line );
            Column[]    column = line.getColumnArray();
            Tag[]   input = column[column.length - 1].getAllTagByType( "input" );
            for ( int j = 0; j < input.length; j++ ) {
                Button  button = (Button)input[j];
                if ( button.isContain( getEditTags() ) ) {
                    button.setUserData( new Action( Type.EDIT, user ) );
                }
                else {
                    button.setUserData( new Action( Type.DELETE, user ) );
                }
            }
        }
    }

inputMain に押下ボタン判定を追加します。 ユーザーに押下されたボタンインスタンスを直接取得する事が可能です。 そのボタンインスタンスから、予め setUserData しておいたインスタンスを参照し、処理を判断します。

tutorial2/logic/user/SeekHtml.java⑤
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
    public Forward inputMain( RequestParameter req, Forward fw ) throws PageException {
        // fwにはモックアップと同じ遷移先が設定されています。
        // 別の遷移先としたい場合は、新しくForwardインスタンスを生成して
        // リターンして下さい。
 
        RequestItem item = req.getItem( "page" );
        if ( item != null ) {
            page = Integer.parseInt( item.getValue( 0 ) );
        }
        else {
            Button  button = getClickedButton();
            if ( button == null )   return fw;
            Action  action = (Action)button.getUserData();
            // 検索ボタンならnullが返るはず
            if ( action != null ) {
                switch( action.type ) {
                case EDIT:
                    fw = new Forward( PageType.USER_EDIT_HTML, true, false );
                    break;
                case DELETE:
                    User.db.remove( action.user );
                    break;
                }
            }
        }
        return fw;
    }

これで、削除の場合はデータが消去された後に outputMain が呼ばれ、新規と編集は編集画面へ遷移します。
getClickedButton が null を返す状況は、戻るがクリックされた時です。

チュートリアル6(編集出力)

最終更新: 2009/10/13
特に明示されていない限り、本Wikiの内容は次のライセンスに従います:CC Attribution 3.0 Unported
文書の先頭へ
SourceForge.JP
2009 © Akira Terasaki.  
Driven by DokuWiki The Apache Software Foundation