緗戠珯: JavaEye
浣滆? iwinyeah
閾炬帴錛?a style="color:red;">http://iwinyeah.javaeye.com/blog/172974
鍙戣〃鏃墮棿: 2008騫?3鏈?7鏃?
澹版槑錛氭湰鏂囩郴JavaEye緗戠珯鍙戝竷鐨勫師鍒涘崥瀹㈡枃绔狅紝鏈粡浣滆呬功闈㈣鍙紝涓ョ浠諱綍緗戠珯杞澆鏈枃錛屽惁鍒欏繀灝嗚拷絀舵硶寰嬭矗浠伙紒
Action: 瑙勫畾浜嗕笌鐢ㄦ埛浜や簰鐨刅iew鍙互瑙﹀彂鐨勫姩浣?鍦ㄦ煇涓猇iew鏂板緩涔嬪悗鏄劇ず涔嬪墠,搴斿厛涓哄叾鎸囧畾鍏蜂綋鐨凙ction,褰撶敤鎴鋒寜涓嬩簡鐩稿簲鐨凜ommand鎸夐挳涔嬪悗錛孷iew灝嗚Command瀵瑰簲鐨凙ction鍙戦佸埌璇iew鐨凜ontroller榪涜澶勭悊銆?
//
public class Action{
String name; // 鍚嶇О
Command command; // 鍛戒護(hù)
int code; // 浠g爜 (灝嗙敱璇iew鐨勪紶閫掑埌鍏禖ontroller浣跨敤)
Item item; // 鏁版嵁欏?
boolean defaultAction; // 鏄惁鏄粯璁ょ殑Action
//...鐪佺暐
}
璇風(fēng)湅View鐨勫熀綾葷殑浠g爜鑺傞?
public abstract class AbstractView{
//...鐪佺暐
// 涓鴻View澧炲姞Action
public void addAction( final Action action, final boolean active )
{
if( !actions.containsKey( action.getName() ) )
{
// 灝咥ction瀛樺叆Actions琛ㄤ腑
actions.put( action.getName(), action );
if( active )
{
activateAction( action );
}
}
}
// 浣緼ction鐢熸晥鍙敤
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 ); // 璇ction鏄睆騫曠浉鍏崇殑鍛戒護(hù)
}
else
{
item.addCommand( command ); // 璇ction鏄暟鎹」鐩稿叧鐨勫懡浠?
if( action.isDefaultAction() )
{
item.setDefaultCommand( command );
}
}
}
//...鐪佺暐
// 鐢ㄦ埛鎸変笅鐩稿簲鐨勫懡浠ら敭鍚?瑙﹀彂鎵ц涓庡叾鍏寵仈鐨凙ction
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ù)鐨処tem鎸変笅浜嗗懡浠ゆ寜閽椂瑙﹀彂鎵ц涓庡叾鍏寵仈鐨凙ction
public void commandAction( final Command command, final Item item )
{
if( !handleAction( command ) )
{
AbstractController.commandAction( this, command );
}
}
// 鏍規(guī)嵁鎵瑙﹀彂鐨勫懡浠ゆ煡鎵懼叧鑱旂殑Action,騫舵柊瀹冨彂閫佸埌Controller榪涜澶勭悊
public boolean handleAction( final Command command )
{
if( activeActions.containsKey( command ) )
{
final Action action = (Action) activeActions.get( command );
// 浠ction浠g爜涓哄弬鏁扮敓鎴怌ontrollerEvent騫朵紶閫掑埌controller澶勭悊
final ControllerEvent event = new ControllerEvent(
action.getCode(),
this
);
controller.handle( event );
return true;
}
else
{
return false;
}
}
//...鐪佺暐
}
鏈枃鐨勮璁轟篃寰堢簿褰╋紝嫻忚璁ㄨ>>
JavaEye鎺ㄨ崘
鏂囩珷鏉ユ簮:http://iwinyeah.javaeye.com/blog/172974 
]]>