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

📄 diagrameditpart.java

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


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

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.draw2d.ConnectionLayer;
import org.eclipse.draw2d.FanRouter;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FreeformLayer;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.MarginBorder;
import org.eclipse.draw2d.ShortestPathConnectionRouter;
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.CompoundSnapToHelper;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.LayerConstants;
import org.eclipse.gef.SnapToGeometry;
import org.eclipse.gef.SnapToGrid;
import org.eclipse.gef.SnapToGuides;
import org.eclipse.gef.SnapToHelper;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.eclipse.gef.editpolicies.RootComponentEditPolicy;
import org.eclipse.gef.editpolicies.SnapFeedbackPolicy;
import org.eclipse.gef.rulers.RulerProvider;
import org.eclipse.swt.SWT;
import org.eclipse.ui.views.properties.IPropertySource;

import com.primeton.studio.gef.core.CorePackage;
import com.primeton.studio.gef.core.Diagram;
import com.primeton.studio.gef.ui.properties.NodePropertySource;
import com.primeton.studio.gef.ui.ui.policy.DiagramXYLayoutEditPolicy;

/**
 * TODO此处填写 class 信息
 *
 * @author niegy (mailto:niegy@primeton.com)
 */
/*
 * 修改历史
 * $Log: DiagramEditPart.java,v $
 * Revision 1.6  2006/12/16 09:04:42  niegy
 * 重构代码
 *
 * Revision 1.5  2006/12/12 08:26:39  niegy
 * 重构代码,实现一般图元的编辑框
 *
 * Revision 1.4  2006/12/05 05:18:59  niegy
 * 修改模型,增加连线的扩展点
 *
 * Revision 1.3  2006/11/22 07:07:38  niegy
 * 增加table
 *
 * Revision 1.2  2006/11/18 12:13:53  niegy
 * 增加容器模型
 *
 * Revision 1.1  2006/11/17 03:15:13  niegy
 * create
 * 
 */
public class DiagramEditPart extends AbstractGraphicalEditPart implements
        Adapter,LayerConstants {

	private String pluginId;
    private Notifier target;
    private boolean shouldUpdatePageBreakLocation = false;
    private boolean isSupportingViewActions = true;
    
    public DiagramEditPart(String id){
    	pluginId = id;
    }
    /**
     * Upon activation, attach to the model element as a property change listener.
     */
    public void activate() {
        if (!isActive()) {
            hookIntoModel(getCastedModel());
            super.activate();
        }
    }
    
    /* (non-Javadoc)
     * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
     */
    protected void createEditPolicies() {
        // disallows the removal of this edit part from its parent
        installEditPolicy(EditPolicy.COMPONENT_ROLE, new RootComponentEditPolicy());
        // handles constraint changes (e.g. moving and/or resizing) of model elements
        // and creation of new model elements
        installEditPolicy(EditPolicy.LAYOUT_ROLE,  new DiagramXYLayoutEditPolicy(getCastedModel(),pluginId));
        
        installEditPolicy("Snap Feedback", new SnapFeedbackPolicy()); 
        ///////////////
//        installEditPolicy(EditPolicyRoles.POPUPBAR_ROLE,
//                new gmf.DiagramPopupBarEditPolicy());
    }
    
    /* (non-Javadoc)
     * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
     */
    protected IFigure createFigure() {
        Figure f = new FreeformLayer();
        f.setBorder(new MarginBorder(3));
        f.setLayoutManager(new DiagramFreeformLayout());
        
        // Override the containsPoint and findFigureAt methods
        // to treat this layer (Primary Layer) as if it were opaque.

        // This is for the grid layer so that it can be seen beneath the
        // figures.
//        IFigure f = new BorderItemsAwareFreeFormLayer() {   
//            /* (non-Javadoc)
//             * @see org.eclipse.draw2d.Layer#containsPoint(int, int)
//             */
//            public boolean containsPoint(int x, int y) {
//                return getBounds().contains(x, y);
//            }
//
//            /* (non-Javadoc)
//             * @see org.eclipse.draw2d.Layer#findFigureAt(int, int, org.eclipse.draw2d.TreeSearch)
//             */
//            public IFigure findFigureAt(int x, int y, TreeSearch search) {
//                if (!isEnabled())
//                    return null;
//                if (!containsPoint(x, y))
//                    return null;
//                if (search.prune(this))
//                    return null;
//                IFigure child = findDescendantAtExcluding(x, y, search);
//                if (child != null)
//                    return child;
//                if (search.accept(this))
//                    return this;
//                return null;
//            }
//
//            /* (non-Javadoc)
//             * @see org.eclipse.draw2d.Figure#validate()
//             */
//            public void validate() {                
//                super.validate();
////                if (shouldUpdatePageBreakLocation){
////                    shouldUpdatePageBreakLocation = false;
////                    updatePageBreaksLocation();
////                }
//            }
//        };
//        f.setLayoutManager(new FreeformLayout());
//        f.addLayoutListener(LayoutAnimator.getDefault());
////        f.addLayoutListener(new PageBreaksLayoutListener());
        
        ConnectionLayer connLayer = (ConnectionLayer)getLayer(LayerConstants.CONNECTION_LAYER);
    	FanRouter router = new FanRouter();
    	router.setSeparation(20);
    	router.setNextRouter(new ShortestPathConnectionRouter(f));
    	connLayer.setConnectionRouter(router);
    	connLayer.setAntialias(SWT.ON);
    	
        return f;
    }
    
    

    
    /**
     * Upon deactivation, detach from the model element as a property change listener.
     */
    public void deactivate() {
        if (isActive()) {
            unhookFromModel(getCastedModel());
            super.deactivate();
        }
    }
    
    public Diagram getCastedModel() {
        return (Diagram) getModel();
    }
    
    /* (non-Javadoc)
     * @see org.eclipse.gef.editparts.AbstractEditPart#getModelChildren()
     */
    protected List getModelChildren() {
        return getCastedModel().getNodes(); // return a list of shapes
    }
    
    
    private void hookIntoModel(EObject model) {
        if(model != null) {
            model.eAdapters().add(this);
        }
    }
    
    private void unhookFromModel(EObject model) {
        if(model != null) {
            model.eAdapters().remove(this);
        }
    }
    
    public void notifyChanged(Notification notification) {
        int type = notification.getEventType();
        int featureId = notification.getFeatureID(CorePackage.class);
        if(type == Notification.ADD || type == Notification.REMOVE) {
//          switch(featureId) {
//          case ModelPackage.SHAPES_DIAGRAM__SHAPES:
                refreshChildren();
//              break;
//          }
        }
    }
    
    public Notifier getTarget() {
        return target;
    }
    
    public void setTarget(Notifier newTarget) {
        target = newTarget;
    }
    
    public boolean isAdapterForType(Object type) {
        return (getModel().getClass() == type);
    }
    
    
    public Object getAdapter(Class adapter) {
        if (adapter == SnapToHelper.class) {
            List snapStrategies = new ArrayList();
            Boolean val = (Boolean)getViewer().getProperty(RulerProvider.PROPERTY_RULER_VISIBILITY);
            if (val != null && val.booleanValue())
                snapStrategies.add(new SnapToGuides(this));
            val = (Boolean)getViewer().getProperty(SnapToGeometry.PROPERTY_SNAP_ENABLED);
            if (val != null && val.booleanValue())
                snapStrategies.add(new SnapToGeometry(this));
            val = (Boolean)getViewer().getProperty(SnapToGrid.PROPERTY_GRID_ENABLED);
            if (val != null && val.booleanValue())
                snapStrategies.add(new SnapToGrid(this));
            
            if (snapStrategies.size() == 0)
                return null;
            if (snapStrategies.size() == 1)
                return (SnapToHelper)snapStrategies.get(0);

            SnapToHelper ss[] = new SnapToHelper[snapStrategies.size()];
            for (int i = 0; i < snapStrategies.size(); i++)
                ss[i] = (SnapToHelper)snapStrategies.get(i);
            return new CompoundSnapToHelper(ss);
        }
        if (IPropertySource.class == adapter)
            return new NodePropertySource(getModel(),pluginId);
        return super.getAdapter(adapter);
    }

    
    //////////////////////////////////
    /* (non-Javadoc)
     * @see org.eclipse.gmf.runtime.diagram.ui.internal.editparts.ISurfaceEditPart#getPrimaryEditParts()
     */
    public List getPrimaryEditParts() {
        // TODO Auto-generated method stub
        return Collections.EMPTY_LIST;
    }

    /* (non-Javadoc)
     * @see org.eclipse.gmf.runtime.diagram.ui.internal.editparts.ISurfaceEditPart#isSupportingViewActions()
     */
    public boolean isSupportingViewActions() {
        // TODO Auto-generated method stub
        return this.isSupportingViewActions;
    }

    /* (non-Javadoc)
     * @see org.eclipse.gmf.runtime.diagram.ui.internal.editparts.ISurfaceEditPart#setIsSupportingViewActions(boolean)
     */
    public void setIsSupportingViewActions(boolean supportsViewActions) {
        // TODO Auto-generated method stub
        this.isSupportingViewActions = supportsViewActions;
    }

}

⌨️ 快捷键说明

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