通过查看EditDomain的源码可知。
/**
* Listens to the PaletteViewer for changes in selection, and sets the Domain's Tool
* accordingly.
* 监听工具箱里面工具的选中
*/
private PaletteListener paletteListener = new PaletteListener() {
public void activeToolChanged(PaletteViewer viewer, ToolEntry tool) {
handlePaletteToolChanged();
}
};
/*
* 当选中了一个Tool时,就会创建一个Tool实例,并将这个Tool设置为EditDomain上的Active Tool
*/
private void handlePaletteToolChanged() {
PaletteViewer paletteViewer = getPaletteViewer();
if (paletteViewer != null) {
ToolEntry entry = paletteViewer.getActiveTool();
if (entry != null)
setActiveTool(entry.createTool());
else
setActiveTool(getDefaultTool());
}
}
/**
* Sets the active Tool for this EditDomain. If a current Tool is active, it is
* deactivated. The new Tool is told its EditDomain, and is activated.
* @param tool the Tool
*/
public void setActiveTool(Tool tool) {
if (activeTool != null)
activeTool.deactivate();//使之前的activeTool不可用
activeTool = tool;//替换activeTool
if (activeTool != null) {
activeTool.setEditDomain(this);
activeTool.activate();//使之激活
}
}
当然,也应该注意到,在EditDomain的构造方法处,载入了一个默认的Tool。
/**
* Constructs an EditDomain and loads the default tool.
*/
public EditDomain() {
loadDefaultTool();
}
/**
* Loads the default Tool. If a palette has been provided and that palette has a default,
* then that tool is loaded. If not, the EditDomain's default tool is loaded. By default,
* this is the {@link org.eclipse.gef.tools.SelectionTool}.
*/
public void loadDefaultTool() {
setActiveTool(null);
PaletteViewer paletteViewer = getPaletteViewer();
if (paletteRoot != null && paletteViewer != null) {
if (paletteRoot.getDefaultEntry() != null) {
paletteViewer.setActiveTool(paletteRoot.getDefaultEntry());
return;
} else
paletteViewer.setActiveTool(null);
}
setActiveTool(getDefaultTool());
}
/**
* Returns the default tool for this edit domain. This will be a {@link
* org.eclipse.gef.tools.SelectionTool} unless specifically replaced using {@link
* #setDefaultTool(Tool)}.
* 默认的Tool为SelectionTool
* @return The default Tool for this domain
*/
public Tool getDefaultTool() {
if (defaultTool == null)
defaultTool = new SelectionTool();
return defaultTool;
}