分类:
2009-10-13 00:25:17
-----------------------------------------------------------
本文系本站原创,欢迎转载!
转载请注明出处:http://sjj0412.cublog.cn/
-----------------------------------------------------------
查找指定的figure:
public final IFigure findFigureAt(Point pt) {
return findFigureAtExcluding(pt.x, pt.y, Collections.EMPTY_LIST);
}
public final IFigure findFigureAtExcluding(int x, int y, Collection c) {
return findFigureAt(x, y, new ExclusionSearch(c));
}
public IFigure findFigureAt(int x, int y, TreeSearch search) {
if (!containsPoint(x, y))
return null;
if (search.prune(this))
return null;
IFigure child = findDescendantAtExcluding(x, y, search);
if (child != null)
return child;
if (search.accept(this))
return this;
return null;
}
可以看得出findFigureAt是个递归函数,满足条件的figure有以下这些条件:
1. 这个figure要包含这个点,这个体现在containsPoint。
2. ExclusionSearch的collection不包含这个figure,search.prune(this).
3. ExclusionSearch的accept要返回true,search.accept(this).
class ExclusionSearch
public boolean accept(IFigure figure) {
//Prune is called before accept, so there is no reason to check the collection again.
return true;
}
//修剪不符合条件的figure
public boolean prune(IFigure f) {
return c.contains(f);
}
我们在将seletionTool找dragTracer的时候,遇到过这个函数的应用:
Handle handle = ((GraphicalViewer) viewer).findHandleAt(p);
public Handle findHandleAt(Point p) {
LayerManager layermanager = (LayerManager)getEditPartRegistry().get(LayerManager.ID);
if (layermanager == null)
return null;
List list = new ArrayList(3);
list.add(layermanager.getLayer(LayerConstants.PRIMARY_LAYER));
list.add(layermanager.getLayer(LayerConstants.CONNECTION_LAYER));
list.add(layermanager.getLayer(LayerConstants.FEEDBACK_LAYER));
//这个是排除条件,也就是说在PRIMARY_LAYER,CONNECTION_LAYER, FEEDBACK_LAYER层上的都不符合条件,因为handle是在handle层上,所以找的就是hanlde类型的figure。
IFigure handle = getLightweightSystem().getRootFigure()
.findFigureAtExcluding(p.x, p.y, list);
//执行查找任务。
if (handle instanceof Handle)
return (Handle)handle;
return null;
}
还有是在updateTargetUnderMouse函数中的findObjectAtExcluding:
protected boolean updateTargetUnderMouse() {
if (!isTargetLocked()) {
EditPart editPart = getCurrentViewer().findObjectAtExcluding(
getLocation(),
getExclusionSet(),
getTargetingConditional());
if (editPart != null)
editPart = editPart.getTargetEditPart(getTargetRequest());
boolean changed = getTargetEditPart() != editPart;
setTargetEditPart(editPart);
return changed;
} else
return false;
}
public EditPart findObjectAtExcluding(
Point pt,
Collection exclude,
final Conditional condition) {
class ConditionalTreeSearch extends ExclusionSearch {
ConditionalTreeSearch (Collection coll) {
super(coll);
}
public boolean accept(IFigure figure) {
EditPart editpart = null;
while (editpart == null && figure != null) {
editpart = (EditPart)getVisualPartMap().get(figure);
figure = figure.getParent();
}
return editpart != null
&& (condition == null || condition.evaluate(editpart));
}
}
IFigure figure = getLightweightSystem()
.getRootFigure()
.findFigureAt(pt.x, pt.y, new ConditionalTreeSearch(exclude));
EditPart part = null;
while (part == null && figure != null) {
part = (EditPart)getVisualPartMap().get(figure);
figure = figure.getParent();
}
if (part == null)
return getContents();
return part;
}
可见这种查找方法多了一个条件conditon参数:
条件就是:
Figure对应的editpart要满足condition.evaluate.
condition.evaluate(editpart)