一个简单的GMF实例

接触GMF几个月了,虽然还是比较迷糊,不过也做了点东西。这篇文章把前几天做的一个小东西分享一下,希望能对后来者有帮助。本文借鉴了中文英文各种资源,就不一一引用了(因为我实在是记不住了)。。。

上周从实验室接到这样一个需求:一个RCP欢迎界面,展示了整个大厅的所有桌子,当客人通过前台的时候刷卡(RFID),系统中显示客人的身份并且在相应的桌子号上进行提示。

示意图如下:


当客人到来时,文字区变为客人姓名,相应桌子闪烁:


那么除了进行建模及生成代码外,我需要解决一下几个问题:

1.添加背景及文字

2.使节点变色及闪烁

3.包装代码

下面进行详细说明。

 

1. 建模

本例子建模非常简单,其实只需要map和桌子两类实体就可以了。为了完整,我创建了以下实体:


接着按照步骤生成代码,这里把node的形状改为了圆形:


2.修改代码之背景

本示例中所谓的背景就是编辑器的背景,所以要进入编辑器的定义文件(HallDiagramEditor.java)中修改。修改其中的configureGraphicalViewer()函数。得到层以后插入背景层和文字层。这段代码如果我没记错的话来自八进制,感谢。

 

	protected void configureGraphicalViewer() {
		super.configureGraphicalViewer();
		DiagramEditorContextMenuProvider provider = new DiagramEditorContextMenuProvider(
				this, getDiagramGraphicalViewer());
		getDiagramGraphicalViewer().setContextMenu(provider);
		getSite().registerContextMenu(ActionIds.DIAGRAM_EDITOR_CONTEXT_MENU,
				provider, getDiagramGraphicalViewer());
                
		DiagramRootEditPart root = (DiagramRootEditPart) getDiagramGraphicalViewer()
				.getRootEditPart();
		LayeredPane printableLayers = (LayeredPane) root
				.getLayer(LayerConstants.PRINTABLE_LAYERS);
		BackgroundLayer backgroundLayer = new BackgroundLayer();
		TextLayer textLayer = new TextLayer();
		printableLayers.addLayerBefore(backgroundLayer,
				BackgroundLayer.BACKGROUND_LAYER, LayerConstants.PRIMARY_LAYER);
		printableLayers.addLayerBefore(textLayer, TextLayer.TEXT_LAYER,
				LayerConstants.PRIMARY_LAYER);
	}

 

是的,在这里插入的背景层和文字层我也自己定义了一下,主要是为了在以后想要修改的时候用着比较方便。文字层定义文字的内容和显示方式,并提供更改内容的方法。

public class TextLayer extends FreeformLayer {
	
	
	public static final String TEXT_LAYER = "Text Layer";
	
	public static final String welcomeText = "Welcome!";
	private String textContent = welcomeText;
	public TextLayer() {
		setOpaque(true);
	}
	protected void paintFigure(Graphics graphics) {
		if(isOpaque()) {
			graphics.setForegroundColor(ColorConstants.black);
			Display display = PlatformUI.getWorkbench().getDisplay();
			FontRegistry fontRegistry = new FontRegistry(display);
			fontRegistry.put("welcometext", new FontData[]{new FontData("Times New Roman", 20, SWT.BOLD)} );
			graphics.setFont(fontRegistry.get("welcometext"));
			
			graphics.drawText(textContent, 30, 100);			
		}
	}	
	public void setText(String s) {
		textContent = s;
		repaint();
	}	
}

 背景层读入背景图片,注意这里用到了一个在workspace中通过相关路径搜索文件的小技巧(来自于某篇博客)。

public class BackgroundLayer extends FreeformLayer {
	public static final String BACKGROUND_LAYER = "Background Layer";
	
	public BackgroundLayer() {
		setOpaque(true);
		findImage("content/Grand_Ballroom_floor_planV2.JPG");
	}
	
	@Override
	protected void paintFigure(Graphics graphics) {
		if(isOpaque()) {
			graphics.setForegroundColor(ColorConstants.white);
			graphics.setBackgroundColor(ColorConstants.lightBlue);
			graphics.fillGradient(getBounds(), true);
			graphics.drawImage(
						new Image(null, imagePath), 
						new Point(30,150));			
		}
	}
	private void findImage(String filePath){
		Bundle bundle = HallDiagramEditorPlugin.getInstance().getBundle();
		Path path = new Path("content/Grand_Ballroom_floor_planV2.JPG");
		URL url = FileLocator.find(bundle, path, Collections.EMPTY_MAP);
		
		try {
			URL fileUrl = FileLocator.toFileURL(url);
			imagePath = fileUrl.getPath();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}			
	}
	private static String imagePath;
}

 至此,打开编辑器时,已经可以显示出背景和文字了,通过获取text layer后可以修改文字的内容。

3.修改代码之节点

节点的闪烁问题还是花了一些时间的,最终的解决方法如下:

修改节点对应的图形类 TableEditPart.java。通过修改createNodeFigure()函数,将其设置为初始不可见,这样在每个桌子上会有一个不可见的圆形节点盖在上面。

增加blink函数,使其闪烁起来,函数的输入为桌子号,客人名字以及希望闪烁的时间。

	/*
	 * become a yellow eclipse to remind the background table number periodically
	 */
	public void blink(int tableNumber, String guestName, final int lastTime) {
		//set yellow eclipse
		primaryShape
				.setForegroundColor(org.eclipse.draw2d.ColorConstants.orange);
		primaryShape
				.setBackgroundColor(org.eclipse.draw2d.ColorConstants.orange);
		//set text area
		//1.get editor
		DiagramRootEditPart root = (DiagramRootEditPart) getRoot();
		tl = (TextLayer) root.getLayer("Text Layer");
	    tl.setText("Hello " + guestName +", your table number is " + tableNumber);
		
	    new Runnable() {
			int i = 0;
			public void run() {
				if (i >= (lastTime/1000)) {
					primaryShape.setVisible(false);
					//set text area again
					tl.setText(TextLayer.welcomeText);
					return;
				} else if (i % 2 == 0) {
					primaryShape.setVisible(false);
				} else {
					primaryShape.setVisible(true);
				}
				//period is 1 second
				Display.getDefault().timerExec(1000, this);
				i++;
			}
		}.run();
	}

 3.菜单及命令

在测试阶段我为本程序设置了两个命令 show view和blink table,前者读取用户指定的map文件建立编辑区,后者闪烁用户指定的桌子。在真正运行时会调用数据库等。将本程序作为插件运行在另一个主程序中工作正常。

 

4.代码说明及下载

我做出修改的程序分别为:

hall.diagram.edit.commands包中:

BlinkTable.java

OpenGuestViewHandler.java

SaveDiagram.java

 

hall.diagram.edit.layer包中:

BackgroundLayer.java

TextLayer.java

 

hall.diagram.edit.parts包中:

TableEditPart.java

 

hall.diagram.part包中:

HallDiagramEditor.java

 

三个项目已打包为gmf_lgylym.zip

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值