網站:
JavaEye
作者:
iwinyeah
鏈接:
http://iwinyeah.javaeye.com/blog/172974
發表時間: 2008年03月17日
聲明:本文系JavaEye網站發布的原創博客文章,未經作者書面許可,嚴禁任何網站轉載本文,否則必將追究法律責任!
Action: 規定了與用戶交互的View可以觸發的動作,在某個View新建之后顯示之前,應先為其指定具體的Action,當用戶按下了相應的Command按鈕之后,View將該Command對應的Action發送到該View的Controller進行處理。
//
public class Action{
String name; // 名稱
Command command; // 命令
int code; // 代碼 (將由該View的傳遞到其Controller使用)
Item item; // 數據項
boolean defaultAction; // 是否是默認的Action
//...省略
}
請看View的基類的代碼節選
public abstract class AbstractView{
//...省略
// 為該View增加Action
public void addAction( final Action action, final boolean active )
{
if( !actions.containsKey( action.getName() ) )
{
// 將Action存入Actions表中
actions.put( action.getName(), action );
if( active )
{
activateAction( action );
}
}
}
// 使Action生效可用
private void activateAction( final Action action )
{
final Command command = action.getCommand();
activeActions.put( command, action );
final Item item = action.getItem();
if( item == null )
{
addCommand( command ); // 該Action是屏幕相關的命令
}
else
{
item.addCommand( command ); // 該Action是數據項相關的命令
if( action.isDefaultAction() )
{
item.setDefaultCommand( command );
}
}
}
//...省略
// 用戶按下相應的命令鍵后,觸發執行與其關聯的Action
public void commandAction(
final Command command,
final Displayable displayable
)
{
if( !handleAction( command ) )
{
if( displayable instanceof Choice )
{
AbstractController.commandAction(
this,
command,
(Choice) displayable
);
}
else
{
AbstractController.commandAction( this, command );
}
}
}
// 用戶在某個指定了命令的Item按下了命令按鈕時觸發執行與其關聯的Action
public void commandAction( final Command command, final Item item )
{
if( !handleAction( command ) )
{
AbstractController.commandAction( this, command );
}
}
// 根據所觸發的命令查找關聯的Action,并新它發送到Controller進行處理
public boolean handleAction( final Command command )
{
if( activeActions.containsKey( command ) )
{
final Action action = (Action) activeActions.get( command );
// 以Action代碼為參數生成ControllerEvent并傳遞到controller處理
final ControllerEvent event = new ControllerEvent(
action.getCode(),
this
);
controller.handle( event );
return true;
}
else
{
return false;
}
}
//...省略
}
本文的討論也很精彩,瀏覽討論>>
JavaEye推薦
文章來源:
http://iwinyeah.javaeye.com/blog/172974
View <--> Controller"
trackback:ping="http://m.tkk7.com/iwinyeah/services/trackbacks/187974.aspx" />
-->