⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 modelmanager.java

📁 对eclipse gef进行封装,可以生成图形化编辑器
💻 JAVA
字号:
/*******************************************************************************
 * $Header: /cvsroot/EOS6/work_dir/niegy/com.primeton.studio.gef.core/src/com/primeton/studio/gef/core/ModelManager.java,v 1.2 2006/12/07 09:16:31 niegy Exp $
 * $Revision: 1.2 $
 * $Date: 2006/12/07 09:16:31 $
 *
 *==============================================================================
 *
 * Copyright (c) 2001-2006 Primeton Technologies, Ltd.
 * All rights reserved. 
 * 
 * Created on 2006-9-28
 *******************************************************************************/


package com.primeton.studio.gef.core;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.core.runtime.IPath;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.xmi.XMIResource;

/**
 * 编辑器模型管理器
 *
 * @author niegy (mailto:niegy@primeton.com)
 */
/*
 * 修改历史
 * $Log: ModelManager.java,v $
 * Revision 1.2  2006/12/07 09:16:31  niegy
 * 重构代码
 *
 * Revision 1.1  2006/11/17 03:13:51  niegy
 * create
 * 
 */
abstract public class ModelManager {
    /**
     * For the purpose of the simple editor, a file can only contain a workflow.
     * In EMF, a resource provides the way to have access to the model content.
     */
    protected Resource resource = null;

    /**
     * Contains the factory associated with the model.
     */
    private static CoreFactory coreFactory = null;

    /**
     * Gives access to the top level workflow contained in the resource.
     */
    protected Diagram diagram = null;

    /**
     * Returns the resource containing the workflow. Uses lazy initialization.
     * @param path
     * @return
     */
    protected Resource getResource(IPath path) {
        if (resource == null) {
            ResourceSet resSet = getResourceSet();
            resource =
                resSet.getResource(
                    URI.createFileURI(path.toString()),
                    true);
        }
        return resource;
    }

    /**
     * Returns the resource containing the workflow. Uses lazy initialization.
     * @param path
     * @return
     */
    protected Resource getUtilResource(IPath path) {
        ResourceSet resSet = getResourceSet();
        resource =
            resSet.getResource(
                URI.createFileURI(path.toString()),
                true);
        return resource;
    }

    
    /**
     * Returns the resource containing the workflow. Throw a runtime exception 
     * if the resource does not exist.
     * @param path
     * @return
     */
    protected Resource getResource() {
        if (resource == null) {
            throw new RuntimeException("Ressource supposed to be already created");
        }
        return resource;
    }

    abstract protected ResourceSet getResourceSet();
    /**
     * Creates a resource to contain the workflow. The resource file does not exist yet.
     * @param path
     * @return
     */
    protected Resource createResource(IPath path) {
        if (resource == null) {
            ResourceSet resSet = getResourceSet();
            resource =
                resSet.createResource(URI.createURI(path.toString()));
        }
        return resource;
    }

    /**
     * 根据指定的路径创建新的空模型.
     * @param 
     * @return
     */
    abstract public Object createNewDiagram(IPath path);

    
    /**
     * Loads the content of the model from the file.
     * @param path
     */
    protected void modelLoad(IPath path) throws IOException {
        diagram = null;
        getUtilResource(path);
        Map options = new HashMap();
                
        options.put(XMIResource.OPTION_DECLARE_XML, Boolean.TRUE);
        options.put(XMIResource.OPTION_ENCODING,"UTF-8");
        resource.load(options);
        
    }

    protected void dispase(){
        resource.unload();
    }
    

    /**
     * 根据文件的path加载模型
     * @param path
     * @throws IOException
     */
    public void load(IPath path) throws IOException {
        getResource(path);
        Map options = new HashMap();    
        options.put(XMIResource.OPTION_DECLARE_XML, Boolean.TRUE);
        options.put(XMIResource.OPTION_ENCODING,"UTF-8");
        resource.load(options);
    }

    /**
     * reloads the content of the model from the file.
     * @param path
     */
    protected void reload(IPath path) throws IOException {
        getResource(path).unload();
        load(path);
    }

    /**
     * 根据文件的path保存模型内容
     * @param path
     */
    public void save(IPath path) throws IOException {
        // TODO: Nothing is done of the path if the resource already exist...
        // Not really what we would expect, but ok for the Workflow application.
        // A second save() method with no argument should be added.
        getResource(path);
                
        Map options = new HashMap();
        options.put(XMIResource.OPTION_DECLARE_XML, Boolean.TRUE);
        options.put(XMIResource.OPTION_ENCODING,"UTF-8");
        resource.save(options);
    }

    

    /**
     * 加载后得到模型容器
     * @return
     */
    public Diagram getModel() {
        if (null == diagram) {
            EList l = resource.getContents();
            Iterator i = l.iterator();
            while (i.hasNext()) {
                Object o = i.next();
                if (o instanceof Diagram)
                    diagram = (Diagram) o;
            }
        }
        return diagram;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -