`

让连线上的Label动起来吧

    博客分类:
  • GEF
阅读更多
大家都知道GEF中连线上的label默认是在线的中间,一旦要连很多线的时候显示label就是棘手的问题了,所以啊就要求可以移动label避免重叠啊
三个步骤
第一步:写个类继承MidpointLocator
             public class MidpointOffsetLocator extends MidpointLocator {
   private Point offset;
   
   public MidpointOffsetLocator(Connection c, int i) {
      super(c, i);
      offset = new Point(0, 0);
   }
   
   @Override
   protected Point getReferencePoint() {
      Point point = super.getReferencePoint();
      return point.getTranslated(offset);
   }

   public Point getOffset() {
      return offset;
   }
   
   public void setOffset(Point offset) {
      this.offset = offset;
   }
}

第二步:添加Label到connection
connection.add(conditionLabel, new MidpointOffsetLocator(connection, 0));

第三步:给Label添加事件
conditionLabel.addMouseListener(new MouseListener(){
			public void mouseDoubleClicked(MouseEvent me){
			}

			public void mousePressed(MouseEvent me){
				anchorX = me.x;
				anchorY = me.y;
				me.consume();
			}

			public void mouseReleased(MouseEvent me){
				me.consume();
			}
		});
		conditionLabel.addMouseMotionListener(new MouseMotionListener(){
			public void mouseDragged(MouseEvent me){
				dx += me.x - anchorX;
				dy += me.y - anchorY;
				anchorX = me.x;
				anchorY = me.y;
				Object constraint = connection.getLayoutManager().getConstraint(conditionLabel);
				if(constraint instanceof MidpointOffsetLocator){
					((MidpointOffsetLocator) constraint).setOffset(new Point(dx,dy));
					conditionLabel.revalidate();
				}
				me.consume();
			}

			@Override
			public void mouseEntered(MouseEvent me) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseExited(MouseEvent me) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseHover(MouseEvent me) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseMoved(MouseEvent me) {
				// TODO Auto-generated method stub
				
			}
		});

经过上面三个步骤就大功告成了
1
0
分享到:
评论
3 楼 扬手就是一长鞭 2015-04-06  
移动label也无法使得save可以执行。
2 楼 扬手就是一长鞭 2015-04-06  
我的save是正常的,我添加删除node或者connection时,都可以点击save
1 楼 扬手就是一长鞭 2015-04-06  
我也是这扥写的,可是我双击label改变他的文字时,发现save按钮是灰色的,对editor来说,并没有发生变化一样。请问你知道是什么原因吗?

相关推荐

Global site tag (gtag.js) - Google Analytics