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

📄 nodexylayouteditpolicy.java

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


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

import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.ResizableEditPolicy;
import org.eclipse.gef.editpolicies.XYLayoutEditPolicy;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gef.requests.CreateRequest;

import com.primeton.studio.gef.core.Node;
import com.primeton.studio.gef.core.Diagram;
import com.primeton.studio.gef.core.NodeElement;
import com.primeton.studio.gef.core.Note;
import com.primeton.studio.gef.core.TNodeType;
import com.primeton.studio.gef.ui.commands.NodeCreateCommand;
import com.primeton.studio.gef.ui.commands.NodeSetConstraintCommand;
import com.primeton.studio.gef.ui.parts.NodeEditPartEx;
import com.primeton.studio.gef.ui.parts.NoteEditPart;

/**
 * TODO 此处填写 class 信息
 *
 * @author niegy (mailto:niegy@primeton.com)
 */
/*
 * 修改历史
 * $Log: NodeXYLayoutEditPolicy.java,v $
 * Revision 1.6  2006/12/30 02:45:03  niegy
 * 重构代码
 *
 * Revision 1.5  2006/12/08 07:04:32  niegy
 * 重构代码
 *
 * Revision 1.4  2006/12/05 05:18:59  niegy
 * 修改模型,增加连线的扩展点
 *
 * Revision 1.3  2006/11/23 02:42:21  niegy
 * table
 *
 * Revision 1.2  2006/11/22 07:07:38  niegy
 * 增加table
 *
 * Revision 1.1  2006/11/18 12:13:53  niegy
 * 增加容器模型
 * 
 */
public class NodeXYLayoutEditPolicy extends XYLayoutEditPolicy {
	
	protected Node model = null;
	   
	public NodeXYLayoutEditPolicy(Node node){
		super();
		model = node;
	}
	
    /* (non-Javadoc)
     * @see org.eclipse.gef.editpolicies.ConstrainedLayoutEditPolicy#createAddCommand(org.eclipse.gef.EditPart, java.lang.Object)
     */
    protected Command createAddCommand(EditPart child, Object constraint) {
        // not used in this example
        return null;
    }
    
    
    /* (non-Javadoc)
     * @see org.eclipse.gef.editpolicies.ConstrainedLayoutEditPolicy#createChangeConstraintCommand(org.eclipse.gef.requests.ChangeBoundsRequest, org.eclipse.gef.EditPart, java.lang.Object)
     */
    protected Command createChangeConstraintCommand(ChangeBoundsRequest request,
            EditPart child, Object constraint) {	
        if (child instanceof NodeEditPartEx && constraint instanceof Rectangle) {
            // return a command that can move and/or resize a Shape	
        	NodeElement nodeElement = (NodeElement) child.getModel();
        	if(nodeElement.isGrouped())
        		return null;
            return new NodeSetConstraintCommand(
            		nodeElement, request, (Rectangle) constraint);
        }
        return super.createChangeConstraintCommand(request, child, constraint);
    }
    
    /* (non-Javadoc)
     * @see org.eclipse.gef.editpolicies.ConstrainedLayoutEditPolicy#createChangeConstraintCommand(org.eclipse.gef.EditPart, java.lang.Object)
     */
    protected Command createChangeConstraintCommand(EditPart child,
            Object constraint) {
        // not used in this example
        return null;
    }
    
    /* (non-Javadoc)
     * @see org.eclipse.gef.editpolicies.LayoutEditPolicy#getCreateCommand(org.eclipse.gef.requests.CreateRequest)
     */
    protected Command getCreateCommand(CreateRequest request) {
        Object childClass = request.getNewObject();
        if(((NodeElement)childClass).getNodeType().equals(TNodeType.CHILD_LITERAL) &&
        		model instanceof Diagram)
        	return null;
        
        if(((NodeElement)childClass).getNodeType().equals(TNodeType.COMMON_LITERAL) &&
        		!(model instanceof Diagram))
        	return null;
        
//        if(((Node)childClass).getNodeType().equals(TNodeType.PARENT_LITERAL) &&
        if(model.eContainer().eContainer()!= null)
        	return null;
        
        boolean isAttri = ((NodeElement)childClass).getNodeType().equals(TNodeType.ATTRICOLUMN_LITERAL);
        boolean isOper = ((NodeElement)childClass).getNodeType().equals(TNodeType.OPERATIONCOLUMN_LITERAL);
       
        if(isAttri || isOper){
        	if(model instanceof Diagram)
        		return null;
        	else if(model instanceof NodeElement && !((NodeElement)model).getNodeType().equals(TNodeType.TABLE_LITERAL)){
        		return null;
        	}
        }

        
        Rectangle constraint = (Rectangle) getConstraintFor(request);

        if (childClass instanceof NodeElement){
        // return a command that can add a Shape to a ShapesDiagram 
           return new NodeCreateCommand(model, request, constraint);
        }
        return null;
    }
    
    /* (non-Javadoc)
     * @see org.eclipse.gef.editpolicies.LayoutEditPolicy#getDeleteDependantCommand(org.eclipse.gef.Request)
     */
    protected Command getDeleteDependantCommand(Request request) {
        // not used in this example
        return null;
    }

	/* (non-Javadoc)
	 * @see org.eclipse.gef.editpolicies.ConstrainedLayoutEditPolicy#createChildEditPolicy(org.eclipse.gef.EditPart)
	 */	
	protected EditPolicy createChildEditPolicy(EditPart child) {
		// TODO Auto-generated method stub
		ResizableEditPolicy childPolicy = new ResizableEditPolicy();
		NodeElement nodeElement = (NodeElement)child.getModel();
		if(nodeElement instanceof Note)
			return super.createChildEditPolicy(child);
		if(nodeElement.getNodeType()==TNodeType.TABLE_LITERAL)
			childPolicy.setResizeDirections(PositionConstants.EAST_WEST);
		else if(nodeElement.getNodeType()==TNodeType.COMMON_LITERAL)
			childPolicy.setResizeDirections(PositionConstants.NONE);
		return childPolicy;
	}
    

}

⌨️ 快捷键说明

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