GEF 框架中的设计模式
2010-06-07 00:00:00 来源:WEB开发网清单 3. ConnectionEditPart 类中部分代码
class ConnectionEditPart extends AbstractConnectionEditPart
implements PropertyChangeListener {
...
protected void createEditPolicies() {
installEditPolicy(EditPolicy.CONNECTION_ROLE, new ConnectionEditPolicy() {
protected Command getDeleteCommand(GroupRequest request) {
return new ConnectionDeleteCommand(getCastedModel());
}
});
installEditPolicy(EditPolicy.CONNECTION_ENDPOINTS_ROLE,
new ConnectionEndpointEditPolicy());
} ...
}
请求提交到选定的 editpart 后,通过 EditPolicy 的职责链进行处理。从第一个 EditPolicy 开始,链中收到请求的 EditPolicy 确定是否可以处理它,否则转发给链中的下一个 editpolicy。EditPolicy 的声明顺序决定了请求被传递的顺序。多个编辑策略可以收到请求,返回 Commands 作为响应,这些 Commands 以链的方式组织在一起。示例代码描述了 AbstractEditPart 中的职责链工作方式。
清单 4. AbstractEditPart 类中部分代码
package org.eclipse.gef.editparts;
public abstract class AbstractEditPart
implements EditPart, RequestConstants, IAdaptable
{
......
public Command getCommand(Request request) {
Command command = null;
EditPolicyIterator i = getEditPolicyIterator();
while (i.hasNext()) {
if (command != null)
command = command.chain(i.next().getCommand(request));
else
command = i.next().getCommand(request);
}
return command;
}
}
更多精彩
赞助商链接