以前給結點設置圖片都不能保存,下次打開後仍是原來的圖片。接下來就是實現永久保存,其實就是給Shape結點所對應的模型元素添加一個屬性ID,這個ID的值在建立模型時肯定,而且,咱們選擇圖片後將圖片複製到一個肯定的文件夾下,圖片名就是ID名。java
一、給AbstractShape模型添加屬性ID:修改工程org.eclipse.myTest.shapes下的models下的shapes.ecore_diagram,這樣它所對應的shapes.ecore文件也會改變,爲AbstractShape添加屬性ID,類型是EString,具體操做參考GMF之Shapes實例Part1。dom
二、保存shapes.ecore_diagram,刪除shapes.genmodel,從新建立一個shapes.genmodel,相似GMF之Shapes實例Part1中,別忘了設置Base Package爲org.eclipse.myTest,而後從新生成Model Code、Edit Code和Editor Code,參考GMF之Shapes實例Part1。eclipse
三、在工程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(); } }
四、在工程org.eclipse.myTest.shapes中的org.eclipse.myTest.shapes.impl包中的AbstractShapeImpl類中修改其構造函數爲:ui
/** * <!-- begin-user-doc --> * <!-- end-user-doc --> * @generated NOT 構造函數,設置ID */ protected AbstractShapeImpl() { super(); this.setID(GUID.GenerateGUID()); }
五、在ReserveShape1EditPart中添加屬性runtimePath和ID,類型都是String:this
/** * @generated NOT */ private String runtimePath; private String ID;
六、修改ReserveShape1EditPart的構造函數:.net
/** * @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(); } }
七、在ReserveShape1EditPart中新建一個方法changeImageData():code
/** * 功能:根據ID建立OrgImageData */ public void changeImageData() { OrgImageData = new ImageData(runtimePath + ID + ".jpg"); }
八、修改ChangeImageAction中doRun函數中if中的代碼,添加複製圖片的操做以下:orm
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); }
九、修改ChangeImageAction子類ChangeImageCommand的構造函數,並去掉成員變量imagePath:圖片
public ChangeImageCommand( TransactionalEditingDomain transactionalEditingDomain, ReserveShape1EditPart editPart) { super(transactionalEditingDomain); this.editPart = editPart; }
修改其doExecute方法:
protected void doExecute() { editPart.changeImageData(); editPart.getPrimaryShape().repaint(); }
好了,至此大功告成!