一步一步实现GEF – 5

显示Diagram模型

到目前为止,仅仅显示了一个节点(Node)的图形,如何显示多个节点,也就是一个Diagram的模型呢?

我们修改NodeGefView类的showDiagram()方法,创建Diagram模型,并且创建两个Node模型,加入到Diagram中。

private void showDiagram() {
	Diagram diagram = new Diagram();
 
	Node node = new Node();
	node.setText("测试节点");
	node.setLocation(new Point(10, 10));
	diagram.addNode(node);
 
	Node node2 = new Node();
	node2.setText("测试节点");
	node2.setLocation(new Point(10, 70));
	diagram.addNode(node2);
 
	graphicalViewer.setContents(diagram);
}

但是运行时发现,现在视图上无显示,连上一步显示的Node模型对应的矩形框都没有了。
Continue reading

一步一步实现GEF – 4

创建View

打开META-INF/MANIFEST.MF,在Extensions页面添加org.eclipse.ui.views扩展点,并添加一个名为“Nodes GEF View”的扩展点。

创建View对应的Java类

自定义视图需要继承自ViewPart类型,通过覆盖createPartControl()方法自定义视图上显示的内容。此处视图上显示一个ScrollingGraphicalViewer,GraphicViewer需要设置rootEditPart和EditPartFactory,然后调用setContent()方法传入模型对象,该方法能根据模型的类型生成相应的EditPart,然后创建相应的Figure并加入显示队列中进行绘图。
Continue reading

一步一步实现GEF – 3

实现模型对应的Part

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.zhlwish.learngef.part;
 
import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
 
import com.zhlwish.learngef.figure.NodeFigure;
import com.zhlwish.learngef.model.Node;
 
public class NodePart extends AbstractGraphicalEditPart{
 
	@Override
	protected IFigure createFigure() {
		return new NodeFigure((Node) getModel());
	}
 
	@Override
	protected void createEditPolicies() {
	}
 
}

Continue reading

一步一步实现GEF – 2

创建模型对应的Figure

由于节点对应的Figure中需要使用Color,因此先在Activator类中加入ColorRegistry类型的属性,修改之后如下,其中第20行和第50行以后为新增加的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.zhlwish.learngef;
 
import org.eclipse.jface.resource.ColorRegistry;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
 
/**
 * The activator class controls the plug-in life cycle
 */
public class Activator extends AbstractUIPlugin {
 
	// The plug-in ID
	public static final String PLUGIN_ID = "com.zhlwish.learngef"; //$NON-NLS-1$
 
	// The shared instance
	private static Activator plugin;
 
	private ColorRegistry colorRegistry = new ColorRegistry();
 
	/**
	 * The constructor
	 */
	public Activator() {
	}
 
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
	}
 
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}
 
	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static Activator getDefault() {
		return plugin;
	}
 
	public Color getColor(RGB rgb){
		Color color = colorRegistry.get(rgb.toString());
		if(color == null){
			colorRegistry.put(rgb.toString(), rgb);
		}
		color = colorRegistry.get(rgb.toString());
		return color;
	}
 
}

Continue reading

Java实践:最好提供无参数的构造函数

一个类在没有显式定义构造函数的时候,Java会隐式提供一个无参数的构造函数。但是当显式定义了构造函数的情况下,Java就不会提供这个无参数的构造函数了。这是由于在显式定义的构造函数中可能进行了一些必要的初始化工作,此时如果错误调用默认无参构造函数会导致这些必须的初始化工作并没有完成,进而导致程序异常。

经过编程实践,本人认为还是有必要再显式定义了有参构造函数的情况下,仍然显式定义一个无参构造函数。主要原因是“最好提供缺省值”(It will be better to provide a default value),另外一个原因则是在工厂方法中大量使用的java.lang.Class.newInstance()方法只能调用无参构造函数。
Continue reading

JFace中的GridLayoutFactory和GridDataFactory

研究SWT Layout原理的时候,找到一个视频:《Creating a Custom Window Layout in Eclipse RCP Applications》,顿时被作者使用的GridLayoutFactoryGridDataFactory惊艳了,前些天,我还傻乎乎的自己写了几个简单的类实现相同的功能,无知真可怕。本文介绍了此两个类的用法,最后有一个简单的示例。

Continue reading

Eclipse RCP中获取Plugin/Bundle中文件资源的绝对路径

摘要:在进行Eclipse RCP开发的过程中,需要使用一些其他的资源(如图片、声音、配置文件、数据库等),我们一般将这些资源放到Plugin/Bundle的相应目录下(如图片资源放到icons目录,声音放到sounds目录,数据库文件放到data目录)。本文先对Eclipse Plugin和Bundle进行分析,之后讲解了如何使用Eclipse API通过这些资源的相对路径(相对于Plugin/Bundle)获取这些资源的绝对路径的方法,最后总结了org.osgi.framework.Bundle接口和FileLocator工具类的使用方法。
Continue reading