GMF之Shapes实例Part6:给结点添加图片并实现保存

之前给结点设置图片都不能保存,下次打开后还是原来的图片。接下来就是实现永久保存,其实就是给Shape结点所对应的模型元素添加一个属性ID,这个ID的值在创建模型时确定,并且,我们选择图片后将图片复制到一个确定的文件夹下,图片名就是ID名。

1、给AbstractShape模型添加属性ID:修改工程org.eclipse.myTest.shapes下的models下的shapes.ecore_diagram,这样它所对应的shapes.ecore文件也会改变,为AbstractShape添加属性ID,类型是EString,具体操作参考GMF之Shapes实例Part1。

2、保存shapes.ecore_diagram,删除shapes.genmodel,重新创建一个shapes.genmodel,类似GMF之Shapes实例Part1中,别忘了设置Base Package为org.eclipse.myTest,然后重新生成Model Code、Edit Code和Editor Code,参考GMF之Shapes实例Part1。

3、在工程org.eclipse.myTest.shapes下的org.eclipse.myTest.shapes.util下新建一个类,用于产生一个全局唯一的ID:

package org.eclipse.myTest.shapes.util;

import java.util.UUID;

public class GUID {
    /**
     *  功能:产生一个全局唯一的ID
     */
    public static final String GenerateGUID() {
        UUID uuid = UUID.randomUUID();
        return uuid.toString();
    }
}

4、在工程org.eclipse.myTest.shapes中的org.eclipse.myTest.shapes.impl包中的AbstractShapeImpl类中修改其构造函数为:

/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated NOT 构造函数,设置ID
 */
protected AbstractShapeImpl() {
    super();
    this.setID(GUID.GenerateGUID());
}

5、在ReserveShape1EditPart中添加属性runtimePath和ID,类型都是String:

/**
 * @generated NOT
 */
private String runtimePath;
private String ID;

 6、修改ReserveShape1EditPart的构造函数:

/**
 * @generated NOT
 */
public ReserveShape1EditPart(View view) {
    super(view);
    runtimePath = Platform.getInstanceLocation().getURL().getPath();
    // 设置图片存放的目录为runtimePath\images,如果这个目录不存在则要创建它
    runtimePath += "images/";
    // 判断文件夹是否存在,不存在创建
    File file = new File(runtimePath);
    if (!file.isDirectory()) {
        file.mkdir();
    }
    //获得ID
    ID=((ReserveShape1)((View) this.getModel()).getElement()).getID();
    //判断目录下是否有ID.jpg文件,如果有的话就调用changeImageData
    file=new File(runtimePath+ID+".jpg");
    if(file.exists()){
        changeImageData();
    }
}

7、在ReserveShape1EditPart中新建一个方法changeImageData():

/**
 * 功能:根据ID创建OrgImageData
 */
public void changeImageData() {
    OrgImageData = new ImageData(runtimePath + ID + ".jpg");
}

8、修改ChangeImageAction中doRun函数中if中的代码,添加复制图片的操作如下:

if (selEditPart instanceof ReserveShape1EditPart) {
    ReserveShape1EditPart editPart = (ReserveShape1EditPart) selEditPart;
    // 获得模型(此处未用到)
    ReserveShape1 model = (ReserveShape1) editPart
            .resolveSemanticElement();
    //将选择的图片拷贝到指定目录下
    String runtimePath = Platform.getInstanceLocation().getURL().getPath();
    runtimePath += "images/";
    runtimePath+=model.getID();
    runtimePath+=".jpg";
    try {
        FileOption.copyFile(imagePath, runtimePath);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return ;
    }
    // 获得领域模型
    TransactionalEditingDomain domain = editPart.getEditingDomain();
    // 创建命令
    ChangeImageCommand command = new ChangeImageCommand(domain, editPart);
    // 执行命令
    domain.getCommandStack().execute(command);
}

9、修改ChangeImageAction子类ChangeImageCommand的构造函数,并去掉成员变量imagePath:

public ChangeImageCommand(
        TransactionalEditingDomain transactionalEditingDomain,
        ReserveShape1EditPart editPart) {
    super(transactionalEditingDomain);
    this.editPart = editPart;
}

修改其doExecute方法:

protected void doExecute() {
    editPart.changeImageData();
    editPart.getPrimaryShape().repaint();
}

好了,至此大功告成!

代码下载:http://www.oschina.net/code/snippet_164134_6489

转载于:https://my.oschina.net/plumsoft/blog/32961

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值