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

📄 connectioncreatecommand.java

📁 对eclipse gef进行封装,可以生成图形化编辑器
💻 JAVA
字号:
/*******************************************************************************
 * $Header: /cvsroot/EOS6/work_dir/niegy/com.primeton.studio.gef.ui/src/com/primeton/studio/gef/ui/commands/ConnectionCreateCommand.java,v 1.6 2006/12/30 02:45:04 niegy Exp $
 * $Revision: 1.6 $
 * $Date: 2006/12/30 02:45:04 $
 *
 *==============================================================================
 *
 * Copyright (c) 2001-2006 Primeton Technologies, Ltd.
 * All rights reserved. 
 * 
 * Created on 2006-10-31
 *******************************************************************************/


package com.primeton.studio.gef.ui.commands;

import java.util.Iterator;
import java.util.List;

import org.eclipse.gef.commands.Command;
import org.eclipse.gef.requests.CreateRequest;
import org.eclipse.ui.part.FileEditorInput;

import com.primeton.studio.gef.core.Connection;
import com.primeton.studio.gef.core.NodeElement;
import com.primeton.studio.gef.ui.editor.AbstractGraphicalEditor;
import com.primeton.studio.gef.ui.parts.ExtenderReader;
/**
 * TODO此处填写 class 信息
 *
 * @author niegy (mailto:niegy@primeton.com)
 */
/*
 * 修改历史
 * $Log: ConnectionCreateCommand.java,v $
 * Revision 1.6  2006/12/30 02:45:04  niegy
 * 重构代码
 *
 * Revision 1.5  2006/12/20 02:57:07  niegy
 * 重构代码
 *
 * Revision 1.4  2006/12/16 09:04:42  niegy
 * 重构代码
 *
 * Revision 1.3  2006/12/08 07:04:32  niegy
 * 重构代码
 *
 * Revision 1.2  2006/12/05 05:18:59  niegy
 * 修改模型,增加连线的扩展点
 *
 * Revision 1.1  2006/11/17 03:15:13  niegy
 * create
 * 
 */
 public class ConnectionCreateCommand extends Command{
    /** The connection instance. */
    protected Connection connection = null;    
    /** Start endpoint for the connection. */
    protected final NodeElement source;
    /** Target endpoint for the connection. */
    protected  NodeElement target;  
    
    private String pathName;
    private AbstractGraphicalEditor graphicalEditor;
    private final CreateRequest request;
    
    /**
     *  Instantiate a command that can create a connection between two shapes.
     * @param source the source endpoint (a non-null Shape instance)
     * @param lineStyle the desired line style. See Connection#setLineStyle(int) for details
     * @throws IllegalArgumentException if source is null
     */
    public ConnectionCreateCommand(NodeElement source,CreateRequest req,AbstractGraphicalEditor graphicalEditor) {
        if (source == null) {
            throw new IllegalArgumentException();
        }
        setLabel("connection creation");
        this.source = source;
        this.request = req;
        this.graphicalEditor = graphicalEditor;
        pathName = ((FileEditorInput)graphicalEditor.getEditorInput()).getFile().getFullPath().toOSString();
    }
    

    public Connection getConnection(){
    	if(connection == null){
    		connection = (Connection) request.getNewObject();
    	}
    	return connection;
    }

    /* (non-Javadoc)
     * @see org.eclipse.gef.commands.Command#canExecute()
     */
    public boolean canExecute() {
        return true;            
    }
    
//    public void setConnection(Connection conn){
//    	connection = conn;
//    }
    
    /* (non-Javadoc)
     * @see org.eclipse.gef.commands.Command#execute()
     */
    public void execute() {
        // create a new connection between source and target  
    	getConnection();
        connection.setSourceNode(source);
        connection.setTargetNode(target);
               
        source.getSourceConnections().add(connection);
        target.getTargetConnections().add(connection); 
//        List<String> list =  ExtenderReader.getInstance().getLinkIdList(pluginId);
//        int size = list.size()+1;
        String linkId = getNodeId("link");
//        list.add(linkId);
        connection.setId(linkId);
    }
	
	private String getNodeId(String nodeName){
        int i= 0;
        String name = null;
        List<String> list =  ExtenderReader.getInstance().getLinkIdList(graphicalEditor.getPluginId()+pathName);
        while(true){     	
            boolean flag = false;
            name = nodeName+i;
            for (Iterator iter = list.iterator(); iter.hasNext();) {
                String id = (String) iter.next();                                 
                if(id.equals(name)){
                    flag = true;
                    break;
                }
            }
            i++;
            if(!flag){
            	list.add(name);
                return name;
            }
            
        }
    }
	
    /* (non-Javadoc)
     * @see org.eclipse.gef.commands.Command#redo()
     */
    public void redo() {
        source.getSourceConnections().add(connection);
        target.getTargetConnections().add(connection); 
        List<String> list =  ExtenderReader.getInstance().getLinkIdList(graphicalEditor.getPluginId()+
        		pathName);
        list.add(connection.getId());
    }
    
    /**
     * Set the target endpoint for the connection.
     * @param target that target endpoint (a non-null Shape instance)
     * @throws IllegalArgumentException if target is null
     */
    public void setTarget(NodeElement target) {
        if (target == null) {
            throw new IllegalArgumentException();
        }
        this.target = target;
    }
    
    /* (non-Javadoc)
     * @see org.eclipse.gef.commands.Command#undo()
     */
    public void undo() {
        source.getSourceConnections().remove(connection);
        target.getTargetConnections().remove(connection);
        List<String> list =  ExtenderReader.getInstance().getLinkIdList(graphicalEditor.getPluginId()+pathName);
        list.remove(connection.getId());
    }
}

⌨️ 快捷键说明

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