*LightWeightSystem概述 ---功能 draw2d的核心功能,在draw2d和SWT之间的桥梁.内容包括figure的绘制,事件转发,图形更新. ---由3个主要组成成分, root ...
*LightweightSystem概述
---功能
draw2d的核心功能,在draw2d和SWT之间的桥梁.内容包括figure的绘制,事件转发,图形更新.
---由3个主要组成成分,
root figure,event dispatcher,update manager
下面通过源码分析功能
*root figure
---在构造器初始化时被自动创建,作为自定义figure的容器,
rootfigure源码 protected RootFigure createRootFigure() {
RootFigure f = new RootFigure();
f.addNotify();
f.setOpaque(true);
f.setLayoutManager(new StackLayout()); //
return f;
}
---自定义的figure通过setContents(IFigure figure)设置
setConstents() public void setContents(IFigure figure) {
if (contents != null)
root.remove(contents); //root figure only contains a child
contents = figure;
root.add(contents);
}
figure的层次结构,root figure ->自定义figure->孩子们
*event dispatcher
---默认的事件调度器是SWTEventDispatcher
---功能,将Canvas上的注册SWT事件,通过dispatcher传给对应的figure(获取焦点的figure)
---实现原理
1.LightweightSystem.addListeners() //实现canvas添加各种SWT事件监听器,这个监听器为内部类LightweightSystem.EventHandler
2.EventHandler调用dispatcher处理
EventHandler监听方法 /** @see MouseTrackListener#mouseHover(MouseEvent) */
public void mouseHover(MouseEvent e) {
getEventDispatcher().dispatchMouseHover(e);
}
3.dispatcher调用对应的figure完成处理
对应上面方法的后续处理实现
SWTEventDispatcher中处理SWT事件 /**
* @see EventDispatcher#dispatchMouseHover(org.eclipse.swt.events.MouseEvent)
*/
public void dispatchMouseHover(org.eclipse.swt.events.MouseEvent me) {
receive(me);
if (mouseTarget != null)
mouseTarget.handleMouseHover(currentEvent);
/*
* Check Tooltip source. Get Tooltip source's Figure. Set that tooltip
* as the lws contents on the helper.
*/
if (hoverSource != null) {
toolTipHelper = getToolTipHelper();
IFigure tip = hoverSource.getToolTip();
Control control = (Control) me.getSource();
org.eclipse.swt.graphics.Point absolute;
absolute = control.toDisplay(new org.eclipse.swt.graphics.Point(
me.x, me.y));
toolTipHelper.displayToolTipNear(hoverSource, tip, absolute.x,
absolute.y);
}
}
*update manager
---功能
绘制和更新figure的内容
---默认的实现private UpdateManager manager = new DeferredUpdateManager();
---实现原理
1.LightweightSystem初始化过程中,
在初始化canvas时,配置update manager
初始化updateManager /**
* Sets the LightweightSystem's control to the passed Canvas.
*
* @param c
* the canvas
* @since 2.0
*/
public void setControl(Canvas c) {
if (canvas == c)
return;
canvas = c;
if ((c.getStyle() & SWT.DOUBLE_BUFFERED) != 0)
getUpdateManager().setGraphicsSource(
new NativeGraphicsSource(canvas));
else
getUpdateManager().setGraphicsSource(
new BufferedGraphicsSource(canvas)); //配置图形上下文,实质是GC
getEventDispatcher().setControl(c);
addListeners();
// Size the root figure and contents to the current control's size
Rectangle r = new Rectangle(canvas.getClientArea());
r.setLocation(0, 0);
root.setBounds(r);
root.revalidate();
}
初始化root时配置,配置root figure
设置rootFigure /**
* Sets this LightweightSystem's root figure.
*
* @param root
* the new root figure
*/
protected void setRootPaneFigure(RootFigure root) {
getUpdateManager().setRoot(root); //设置root
this.root = root;
}
2.在Canvas的ControlListener时触发update manager,就是在Canvas上有move和resize的情况时发生
ControlListener触发update manager /**
* Resizes and revalidates the root figure when the control is resized.
*/
protected void controlResized() {
if (ignoreResize > 0)
return;
Rectangle r = new Rectangle(canvas.getClientArea());
r.setLocation(0, 0);
root.setBounds(r);
root.revalidate();
getUpdateManager().performUpdate(); //执行update manager,完成更新
}
*总结
---draw2d的一切都是从Canvas开始的,将功能分成figure的图形展现,update manager的更新机制,和dispatcher的SWT事件转发机制.
所有这些功能在LightweightSystem中集成.