GEF问题与解决方案收集

来源:互联网 发布:草图大师 mac 编辑:程序博客网 时间:2024/06/17 23:42

1、GMF Label warp换行设置方法

     在实例化WrappingLabel 时加入如下代码,此代码作用就是在图元初始化时被设置为可换行的Label:

 public void setLabel(WrappingLabel figure) {     figure.setTextWrap(true);     figure.setAlignment(PositionConstants.CENTER);     figure.setTextJustification(PositionConstants.CENTER);     super.setLabel(figure); } protected DirectEditManager getManager() {     if (manager == null) {         /* Create custom text director which use wrap label editor */         setManager(new CustomTextDirectEditManager(this,         CustomTextDirectEditManager.getTextCellEditorClass(this),         AppreciationEditPartFactory.getTextCellEditorLocator(this)));     }     return manager;}protected void setManager(DirectEditManager manager) {     this.manager = manager;}/*** @generated*/protected void performDirectEdit(Point eventLocation) {    DirectEditManager mgr = getManager();    if (mgr instanceof TextDirectEditManager) {         ((CustomTextDirectEditManager) getManager()).show(eventLocation         .getSWTPoint());    } else {         super.performDirectEdit(eventLocation);    }}
注意:CustomTextDirectEditManager主要是用来定义CellEditor为WrapTextCellEditor,它是继承TextDirectEditManager。在实例化Editpart初始化Figure时,不能设置this.setMaximumSize这个方法,此方法主要是用来设置形状的最大尺寸,不然会导致不能换行。

 

2、连接线自动识别折线规则

 首先连接线是PolylineConnectionEx类型,然后在DiagramConnectionsPreferencePage类中加入如下代码:

public static void initDefaults(IPreferenceStore preferenceStore) {preferenceStore.setDefault(IPreferenceConstants.PREF_LINE_STYLE,Routing.RECTILINEAR);}


3、在Figure中添加子Figure图形

在Editpart类中找到Figure类,然后覆写 paintChildren方法,这里是在图形里增加了两条垂直线,例子代码如下:

public void paintChildren(Graphics graphics) {graphics.drawLine(this.getBounds().getLocation().x + 10, this.getBounds().getLocation().y, this.getBounds().getLocation().x + 10, this.getBounds().getLocation().y+ this.getBounds().height);graphics.drawLine(this.getBounds().getLocation().x + this.getBounds().width- 10, this.getBounds().getLocation().y, this.getBounds().getLocation().x+ this.getBounds().width - 10, this.getBounds().getLocation().y + this.getBounds().height);
                        super.paintChildren(graphics);}

4、Figure中图形颜色渐变

渐变,意思就是颜色的数字会从一个RGB色值到另一个RGB色值的逐渐变化,这个是我自己的理解,代码如下:

protected void fillShape(Graphics graphics) {graphics.setBackgroundColor((new Color(null, 45, 112, 170)));graphics.setForegroundColor((new Color(null, 62, 132, 190)));graphics.fillGradient(getBounds(), true);super.fillShape(graphics);}

5、RectangleFigure设置透明不起作用

当你创建RectangleFigure时,如果需要设置透明度,通过直接this.setOpaque(false);这个方法是不起作用的,然而需要通过覆写paintFigure这个方法,方法中填写如下代码:

public void paintFigure(Graphics graphics) {if (!isOpaque()) {graphics.setAlpha(100);// 设置透明度this.setOpaque(false);}}


 6、设置图形编辑器界面背景图片

增加一个层代码,如下:

import org.eclipse.draw2d.ColorConstants;import org.eclipse.draw2d.FreeformLayer;import org.eclipse.draw2d.Graphics;import org.eclipse.draw2d.geometry.Point;import org.eclipse.jface.resource.ImageDescriptor;import org.eclipse.swt.graphics.Image;public class BackGroundLayer extends FreeformLayer{public static final String BACK_GROUND_LAYER = "Back ground Layer";    private static final ImageDescriptor descriptor = null;    private Image originalImage = descriptor.createImage();    public BackGroundLayer() {        setOpaque(true);    }    @Override    protected void paintFigure(Graphics graphics) {        if (isOpaque()) {            graphics.setForegroundColor(ColorConstants.white);            graphics.setBackgroundColor(ColorConstants.white);            graphics.fillGradient(getBounds(), true);            graphics.setAlpha(100);// 设置透明度,背景还是颜色浅一点比较好            graphics.drawImage(originalImage, new Point(0, 0));        }    }}

然后在*DiagramEditor图形编辑器代码方法configureGraphicalViewer中加入如下代码:

//设置背景图片DiagramRootEditPart root = (DiagramRootEditPart) getDiagramGraphicalViewer().getRootEditPart();LayeredPane printableLayers = (LayeredPane) root.getLayer(LayerConstants.PRINTABLE_LAYERS);BackGroundLayer backgroundLayer = new BackGroundLayer();printableLayers.addLayerBefore(backgroundLayer,BackGroundLayer.BACK_GROUND_LAYER, LayerConstants.PRIMARY_LAYER);


 7、连接线颜色修改不起作用

问题主要是缺少了以下代码:

Edge edge = NotationFactory.eINSTANCE.createEdge();edge.getStyles().add(NotationFactory.eINSTANCE.createRoutingStyle());

8、让连接线自动弯曲与自动折线

自动弯曲代码如下:

    Routing routing = Routing.get(Routing.RECTILINEAR_LITERAL.getName());if (routing != null) {            ViewUtil.setStructuralFeatureValue(edge,                    NotationPackage.eINSTANCE.getRoutingStyle_Routing(),                    routing);            ViewUtil.setStructuralFeatureValue(edge,                    NotationPackage.eINSTANCE.getRoundedCornersStyle_RoundedBendpointsRadius(),                    10);            ViewUtil.setStructuralFeatureValue(edge,                    NotationPackage.eINSTANCE.getRoutingStyle_ClosestDistance(),                    false);            ViewUtil.setStructuralFeatureValue(edge,                    NotationPackage.eINSTANCE.getRoutingStyle_AvoidObstructions(),                    false);        }


自动折线:

public static void initDefaults(IPreferenceStore preferenceStore) {preferenceStore.setDefault(IPreferenceConstants.PREF_LINE_STYLE,Routing.RECTILINEAR);}


9、图元的默认底色修改方法

通过*ViewProvider类,修改其中的一下代码就可以了,fillRGB就是默认的颜色:

org.eclipse.swt.graphics.RGB fillRGB = PreferenceConverter.getColor(prefStore, IPreferenceConstants.PREF_FILL_COLOR);ViewUtil.setStructuralFeatureValue(node,NotationPackage.eINSTANCE.getFillStyle_FillColor(),FigureUtilities.RGBToInteger(fillRGB));