Chinaunix首页 | 论坛 | 博客
  • 博客访问: 512705
  • 博文数量: 95
  • 博客积分: 5168
  • 博客等级: 大校
  • 技术积分: 1271
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-28 23:31
文章分类

全部博文(95)

文章存档

2013年(2)

2012年(3)

2011年(1)

2010年(8)

2009年(81)

分类:

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.       ExclusionSearchcollection不包含这个figuresearch.prune(this).

3.       ExclusionSearchaccept要返回truesearch.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);

}

 

实例

我们在将seletionTooldragTracer的时候,遇到过这个函数的应用:

 

  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_LAYERCONNECTION_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)

 

阅读(1442) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~