網(wǎng)站:
JavaEye
作者:
iwinyeah
鏈接:
http://iwinyeah.javaeye.com/blog/172974
發(fā)表時(shí)間: 2008年03月17日
聲明:本文系JavaEye網(wǎng)站發(fā)布的原創(chuàng)博客文章,未經(jīng)作者書(shū)面許可,嚴(yán)禁任何網(wǎng)站轉(zhuǎn)載本文,否則必將追究法律責(zé)任!
Action: 規(guī)定了與用戶(hù)交互的View可以觸發(fā)的動(dòng)作,在某個(gè)View新建之后顯示之前,應(yīng)先為其指定具體的Action,當(dāng)用戶(hù)按下了相應(yīng)的Command按鈕之后,View將該Command對(duì)應(yīng)的Action發(fā)送到該View的Controller進(jìn)行處理。
//
public class Action{
String name; // 名稱(chēng)
Command command; // 命令
int code; // 代碼 (將由該View的傳遞到其Controller使用)
Item item; // 數(shù)據(jù)項(xiàng)
boolean defaultAction; // 是否是默認(rèn)的Action
//...省略
}
請(qǐng)看View的基類(lèi)的代碼節(jié)選
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是屏幕相關(guān)的命令
}
else
{
item.addCommand( command ); // 該Action是數(shù)據(jù)項(xiàng)相關(guān)的命令
if( action.isDefaultAction() )
{
item.setDefaultCommand( command );
}
}
}
//...省略
// 用戶(hù)按下相應(yīng)的命令鍵后,觸發(fā)執(zhí)行與其關(guān)聯(lián)的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 );
}
}
}
// 用戶(hù)在某個(gè)指定了命令的Item按下了命令按鈕時(shí)觸發(fā)執(zhí)行與其關(guān)聯(lián)的Action
public void commandAction( final Command command, final Item item )
{
if( !handleAction( command ) )
{
AbstractController.commandAction( this, command );
}
}
// 根據(jù)所觸發(fā)的命令查找關(guān)聯(lián)的Action,并新它發(fā)送到Controller進(jìn)行處理
public boolean handleAction( final Command command )
{
if( activeActions.containsKey( command ) )
{
final Action action = (Action) activeActions.get( command );
// 以Action代碼為參數(shù)生成ControllerEvent并傳遞到controller處理
final ControllerEvent event = new ControllerEvent(
action.getCode(),
this
);
controller.handle( event );
return true;
}
else
{
return false;
}
}
//...省略
}
本文的討論也很精彩,瀏覽討論>>
JavaEye推薦
文章來(lái)源:
http://iwinyeah.javaeye.com/blog/172974
View <--> Controller"
trackback:ping="http://m.tkk7.com/iwinyeah/services/trackbacks/187974.aspx" />
-->