在定义模型时我们提到了SWT模型,在这里我们将如何解释执行SWT模型。
SWT模型的create方法
在定义SWT模型时约定了任何一个SWT模型都有一个名为create的行为,目的是根据模型的属性创建对应的Java对象。
在每个SWT控件的create方法里主要做以下事:
下面我们以Shell控件的create方法的实现代码为例。
先列出遍历执行子事物的代码:
//创建子节点,把当前创建的控件以parent名称放入到变量上下文
actionContext.peek().put("parent", shell);
for(Thing child : self.getAllChilds()){
child.doAction("create", actionContext);
}
actionContext.peek().remove("parent");
//子事物,比如一个Button的创建代码会用到parent变量
Composite parent = (Composite) actionContext.get("parent");
Button button = new Button(parent, style);
Shell控件的create方法实现的完整代码。
public static Object create(ActionContext actionContext){
World world = World.getInstance();
//self是事物模型本身
Thing self = (Thing) actionContext.get("self");
//初始化style
Action styleAction = world.getAction("xworker.swt.widgets.Composite/@scripts/@getStyles");
int style = (Integer) styleAction.run(actionContext);
String selfStyle = self.getString("style");
if(self.getBoolean("NO_TRIM"))
style |= SWT.NO_TRIM;
if(self.getBoolean("CLOSE"))
style |= SWT.CLOSE;
if(self.getBoolean("TITLE"))
style |= SWT.TITLE;
if(self.getBoolean("MIN"))
style |= SWT.MIN;
if(self.getBoolean("MAX"))
style |= SWT.MAX;
if(self.getBoolean("BORDER"))
style |= SWT.BORDER;
if(self.getBoolean("RESIZE"))
style |= SWT.RESIZE;
if(self.getBoolean("ON_TOP"))
style |= SWT.ON_TOP;
if(self.getBoolean("TOOL"))
style |= SWT.TOOL;
if("MODELESS".equals(selfStyle)){
style |= SWT.MODELESS;
}else if("PRIMARY_MODAL".equals(selfStyle)){
style |= SWT.PRIMARY_MODAL;
}else if("APPLICATION_MODAL".equals(selfStyle)){
style |= SWT.APPLICATION_MODAL;
}else if("SYSTEM_MODAL".equals(selfStyle)){
style |= SWT.SYSTEM_MODAL;
}
if(self.getBoolean("NO_FOCUS"))
style |= SWT.NO_FOCUS;
//创建shell对象
Shell shell = null;
try{
Object parent = actionContext.get("parent");
if(parent instanceof Display){
shell = new Shell((Display) parent, style);
}else if(parent instanceof Shell){
shell = new Shell((Shell) parent, style);
}else{
shell = new Shell(style);
}
}catch(Exception e){
shell = new Shell(style);
}
//初始化公共属性
Bindings bindings = actionContext.push(null);
bindings.put("control", shell);
bindings.put("self", self);
try{
Action action = world.getAction("xworker.swt.widgets.Composite/@scripts/@init");
action.run(actionContext);
}finally{
actionContext.pop();
}
//初始化控件的自有或附加属性
if(!"-1".equals(self.getString("width"))){
shell.setSize(self.getInt("width", -1), self.getInt("height", -1));
}
String text = self.getString("text");
if(text != null && !"".equals(text))
shell.setText(UtilString.getString(text, actionContext));
int alpha = self.getInt("alpha", -1);
if(alpha != -1) shell.setAlpha(alpha);
shell.setFullScreen(self.getBoolean("fullScreen"));
String imeInputMode = self.getString("imeInputMode");
if("NONE".equals(imeInputMode)){
shell.setImeInputMode(SWT.NONE);
}else if("ROMAN".equals(imeInputMode)){
shell.setImeInputMode(SWT.ROMAN);
}else if("DBCS".equals(imeInputMode)){
shell.setImeInputMode(SWT.DBCS);
}else if("PHONETIC".equals(imeInputMode)){
shell.setImeInputMode(SWT.PHONETIC);
}else if("NATIVE".equals(imeInputMode)){
shell.setImeInputMode(SWT.NATIVE);
}else if("ALPHA".equals(imeInputMode)){
shell.setImeInputMode(SWT.ALPHA);
}
//保存变量
actionContext.getScope(0).put(self.getString("name"), shell);
//创建子节点
actionContext.peek().put("parent", shell);
for(Thing child : self.getAllChilds()){
child.doAction("create", actionContext);
}
actionContext.peek().remove("parent");
//其他功能
if(self.getBoolean("pack")){
shell.pack();
}
if(self.getBoolean("centerScreen")){
try{
SwtUtils.centerShell(shell);
}catch(Throwable t){
}
}
if(self.getBoolean("maximized")){
shell.setMaximized(true);
}
//绑定到designer,使用鼠标中键双击可以打开菜单,可以通过菜单编辑模型等
if(!shell.isDisposed()){
Designer.attach(shell, self.getMetadata().getPath(), actionContext);
}
return shell;
}
Copyright © 2007-2019 XWorker.org 版权所有 沪ICP备08000575号