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

📄 connectioneditpart.java

📁 对eclipse gef进行封装,可以生成图形化编辑器
💻 JAVA
字号:
/*******************************************************************************
 * $Header: /cvsroot/EOS6/work_dir/niegy/com.primeton.studio.gef.ui/src/com/primeton/studio/gef/ui/parts/ConnectionEditPart.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-9-28
 *******************************************************************************/


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

import org.eclipse.draw2d.BendpointConnectionRouter;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.PolygonDecoration;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editparts.AbstractConnectionEditPart;
import org.eclipse.gef.editpolicies.ConnectionEditPolicy;
import org.eclipse.gef.requests.GroupRequest;
import org.eclipse.ui.views.properties.IPropertySource;

import com.primeton.studio.gef.core.Connection;
import com.primeton.studio.gef.core.CorePackage;
import com.primeton.studio.gef.ui.commands.ConnectionDeleteCommand;
import com.primeton.studio.gef.ui.editor.AbstractGraphicalEditor;
import com.primeton.studio.gef.ui.properties.NodePropertySource;
import com.primeton.studio.gef.ui.ui.policy.ConnectionEndpointEditPolicyEx;
/**
 * TODO此处填写 class 信息
 *
 * @author niegy (mailto:niegy@primeton.com)
 */
/*
 * 修改历史
 * $Log: ConnectionEditPart.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/05 05:18:59  niegy
 * 修改模型,增加连线的扩展点
 *
 * Revision 1.2  2006/11/18 12:13:53  niegy
 * 增加容器模型
 *
 * Revision 1.1  2006/11/17 03:15:13  niegy
 * create
 * 
 */
public class ConnectionEditPart extends AbstractConnectionEditPart {	
    PolylineConnection connectionFig;
	private ConnectionAdapter adapter = new ConnectionAdapter(this);
    private AbstractGraphicalEditor editor;
	public class ConnectionAdapter implements Adapter {
		
		private Notifier target;
		private ConnectionEditPart editPart;
		
		public ConnectionAdapter(ConnectionEditPart part) {
			this.editPart = part;
		}
		
		/**
		 * @see org.eclipse.emf.common.notify.Adapter#notifyChanged(org.eclipse.emf.common.notify.Notification)
		 */
		public void notifyChanged(Notification notification) {			
			int type = notification.getEventType();
			int featureId = notification.getFeatureID(CorePackage.class);
			switch(type) {
			case Notification.SET:
			case Notification.ADD:
			case Notification.REMOVE:
				switch(featureId) {
				case CorePackage.LOCATION__X:
				case CorePackage.LOCATION__Y:
//				case CorePackage.SIZE__WIDTH:
//				case CorePackage.SIZE__HEIGHT:
				case CorePackage.NODE_ELEMENT__LOCATION:
				case CorePackage.NODE_ELEMENT__SIZE:
					refreshVisuals();
					break;
				}
			}
		}
		
		/**
		 * @see org.eclipse.emf.common.notify.Adapter#setTarget(org.eclipse.emf.common.notify.Notifier)
		 */
		public void setTarget(Notifier newTarget) {
			target = newTarget;
		}
		
		/**
		 * @see org.eclipse.emf.common.notify.Adapter#isAdapterForType(java.lang.Object)
		 */
		public boolean isAdapterForType(Object type) {
			return (getModel().getClass() == type);
		}
		
		/**
		 * @see org.eclipse.emf.common.notify.Adapter#getTarget()
		 */
		public Notifier getTarget() {
			return target;
		}
		
	}
	
	public ConnectionEditPart(AbstractGraphicalEditor editor){
		this.editor = editor;
	}
	/**
	 * Upon activation, attach to the model element as a property change listener.
	 */
	public void activate() {
		if (!isActive()) {
			hookIntoModel(getCastedModel());
			super.activate();
		}
	}
	
	/**
	 * Upon deactivation, detach from the model element as a property change listener.
	 */
	public void deactivate() {
		if (isActive()) {
			unhookFromModel(getCastedModel());
			super.deactivate();
		}
	}
	
	protected void hookIntoModel(EObject model) {
		if(model != null) {
			model.eAdapters().add(adapter);
		}
	}
	
	protected void unhookFromModel(EObject model) {
		if(model != null) {
			model.eAdapters().remove(adapter);
		}
	}
	
	public Object getAdapter(Class key) {
		if (IPropertySource.class == key)
			return new NodePropertySource(getModel(),editor.getPluginId());
		return super.getAdapter(key);
	}
	
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
	 */
	protected void createEditPolicies() {
		// Selection handle edit policy. 
		// Makes the connection show a feedback, when selected by the user.
		installEditPolicy(EditPolicy.CONNECTION_ENDPOINTS_ROLE,
				new ConnectionEndpointEditPolicyEx());
		// Allows the removal of the connection model element
		installEditPolicy(EditPolicy.CONNECTION_ROLE, new ConnectionEditPolicy() {
			protected Command getDeleteCommand(GroupRequest request) {
				return new ConnectionDeleteCommand(getCastedModel(),editor);
			}
		});
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
	 */
	protected IFigure createFigure() {		
	    connectionFig = new PolylineConnection();
	    connectionFig.setLineStyle(Graphics.LINE_CUSTOM);
		connectionFig.setTargetDecoration(new PolygonDecoration());
		connectionFig.setConnectionRouter(new BendpointConnectionRouter());
		return connectionFig;
	}
	
	
	protected Connection getCastedModel() {
		return (Connection) getModel();
	}
	
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractEditPart#refreshVisuals()
	 */
	protected void refreshVisuals() {
	}

}

⌨️ 快捷键说明

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