惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
C
Cybersecurity and Infrastructure Security Agency CISA
S
Secure Thoughts
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Hacker News: Ask HN
Hacker News: Ask HN
Attack and Defense Labs
Attack and Defense Labs
N
News and Events Feed by Topic
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
A
Arctic Wolf
www.infosecurity-magazine.com
www.infosecurity-magazine.com
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The GitHub Blog
The GitHub Blog
W
WeLiveSecurity
Simon Willison's Weblog
Simon Willison's Weblog
WordPress大学
WordPress大学
Y
Y Combinator Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Forbes - Security
Forbes - Security
NISL@THU
NISL@THU
博客园 - 聂微东
G
Google Developers Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Schneier on Security
Schneier on Security
V
Vulnerabilities – Threatpost
V
V2EX
I
Intezer
S
Schneier on Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
雷峰网
雷峰网
L
LangChain Blog
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
T
The Blog of Author Tim Ferriss
L
LINUX DO - 热门话题
P
Proofpoint News Feed
Microsoft Security Blog
Microsoft Security Blog
Project Zero
Project Zero
V
Visual Studio Blog
Engineering at Meta
Engineering at Meta
爱范儿
爱范儿
H
Hacker News: Front Page
B
Blog
T
Threatpost
Spread Privacy
Spread Privacy
H
Heimdal Security Blog
F
Fortinet All Blogs
TaoSecurity Blog
TaoSecurity Blog
T
Threat Research - Cisco Blogs
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
The Cloudflare Blog

博客园 - 刘刚

Google Map [导入]Editor中嵌入Editor [导入]GEF中修改默认的FeedBack [导入](接上篇)关于GEF Feedback的补充 [导入]GEF中导视图的使用 [导入]Eclipse中Job API使用总结如下三 [导入]Eclipse中Job API使用总结如下二 [导入]Eclipse中Job API使用总结如下一 [导入]Eclipse调试框架的学习与理解二 [导入]Eclipse调试框架的学习与理解一 [导入]使用JET自动生成代码 [导入]定义自己的Common Navigator三 [导入]定义自己的Common Navigator二 [导入]定义自己的Common Navigator一 [导入]Eclipse运行(launcher)框架三 [导入]Eclipse运行(launcher)框架二 [导入]Eclipse运行(launcher)框架一 [导入]Eclipse属性页的支持 [导入]GEF理解系列九
[导入]修改GEF中Connection的端点
刘刚 · 2008-07-14 · via 博客园 - 刘刚

作者: liugang594  链接:http://liugang594.javaeye.com/blog/214606  发表时间: 2008年07月14日

声明:本文系JavaEye网站发布的原创博客文章,未经作者书面许可,严禁任何网站转载本文,否则必将追究法律责任!

GEF中,选中一条连接线,默认的端点是两个小黑框,如下:

Figure 1

这个可用,不过终究是不太美观。这里介绍一下怎么修改这个选中的端点外观。修改后的效果如下:

Figure 2

首先有一点我们知道:通常要使得连线能够被选中,我们要在连接线对应的EditPart上安装以下Policy:

installEditPolicy(EditPolicy.CONNECTION_ENDPOINTS_ROLE, new ConnectionEndpointEditPolicy());

所以,我们知道,要想修改连线端点,我们需要从ConnectionEndpointEditPolicy下手。

ConnectionEndpointEditPolicy里有一个方法:createSelectionHandles()。这个方法就是提供端点选择的方法。其实我们在选中某个结点的时候,结点周围出现在带8个点的边框也是某个Policy提供的一些SelectionHandles。

所以下面我们的任务就是继承ConnectionEndpointEditPolicy,例如我们新写一个类:HomunculeConnectionEndpointPolicy,重写createSelectionHandles()方法。

我们看一下源码:

protected List createSelectionHandles() {
	List list = new ArrayList();
	list.add(new ConnectionEndHandle((ConnectionEditPart)getHost()));
	list.add(new ConnectionStartHandle((ConnectionEditPart)getHost()));
 	return list;
}

可以看到,为了修改端点的显示,我们需要提供自己的ConnectionEndHandle和ConnectionStartHandle。最简单的就是分别写两个继承这两个类的类。然后重写paintFigure(Graphics)方法。

不过,有点可惜的是:ConnectionEndHandle和ConnectionStartHandle都是final的,所以没法继承。所以我们需要转而继承他们的父类了。还好,这个很简单,最后的结果如下:

HomunculeConnectionEndpointPolicy类:

public class HomunculeConnectionEndpointPolicy extends
		ConnectionEndpointEditPolicy {
	@Override
	protected List<ConnectionHandle> createSelectionHandles() {
		List<ConnectionHandle> list = new ArrayList<ConnectionHandle>();
		list.add(new HomunculeConnectionEndHandle((ConnectionEditPart) getHost()));
		list.add(new HomunculeConnectionStartHandle((ConnectionEditPart) getHost()));
		return list;
	}
}

 HomunculeConnectionStartHandle类:

public class HomunculeConnectionStartHandle extends ConnectionHandle{

	public HomunculeConnectionStartHandle(ConnectionEditPart host) {
		setOwner(host);
		setLocator(new ConnectionLocator(getConnection(), ConnectionLocator.SOURCE));
	}
	
	/**
	 * Creates and returns a new {@link ConnectionEndpointTracker}.
	 * @return the new ConnectionEndpointTracker
	 */
	protected DragTracker createDragTracker() {
		if (isFixed()) 
			return null;
		ConnectionEndpointTracker tracker;
		tracker = new ConnectionEndpointTracker((ConnectionEditPart)getOwner());
		tracker.setCommandName(RequestConstants.REQ_RECONNECT_SOURCE);
		tracker.setDefaultCursor(getCursor());
		return tracker;
	}
	
	@Override
	public void paintFigure(Graphics g) {
		Rectangle r = getBounds();
		r.shrink(1, 1);
		try {
			g.setBackgroundColor(ColorConstants.green);
			g.fillOval(r);
			g.setForegroundColor(ColorConstants.darkBlue);
			g.drawOval(r);
		} finally {
			// We don't really own rect 'r', so fix it.
			r.expand(1, 1);
		}
	}
}

HomunculeConnectionEndHandle类:

public class HomunculeConnectionEndHandle extends ConnectionHandle {

	public HomunculeConnectionEndHandle(ConnectionEditPart host) {
		setOwner(host);
		setLocator(new ConnectionLocator(getConnection(), ConnectionLocator.TARGET));
	}
	
	/**
	 * Creates and returns a new {@link ConnectionEndpointTracker}.
	 * 
	 * @return the new ConnectionEndpointTracker
	 */
	protected DragTracker createDragTracker() {
		if (isFixed())
			return null;
		ConnectionEndpointTracker tracker;
		tracker = new ConnectionEndpointTracker((ConnectionEditPart) getOwner());
		tracker.setCommandName(RequestConstants.REQ_RECONNECT_TARGET);
		tracker.setDefaultCursor(getCursor());
		return tracker;
	}

	@Override
	public void paintFigure(Graphics g) {
		Rectangle r = getBounds();
		r.shrink(1, 1);
		try {
			g.setBackgroundColor(ColorConstants.green);
			g.fillOval(r);
			g.setForegroundColor(ColorConstants.darkBlue);
			g.drawOval(r);
		} finally {
			// We don't really own rect 'r', so fix it.
			r.expand(1, 1);
		}
	}

}

 最后就可以得到我们想要的结果。

多说一句:createDragTracker() 方法是用来重定向连接的!

本文的讨论也很精彩,浏览讨论>>

JavaEye推荐

文章来源:http://liugang594.javaeye.com/blog/214606