graphiti笔记2

来源:互联网 发布:童年阴影知乎 编辑:程序博客网 时间:2024/05/01 08:01

4       Features

Features响应客户的操作,处理显示、展示、及连接模型。

1.1     创建Feature Provider

(1)    AbstractFeatureProvider/DefaultFeatureProvider扩展出自己的Feature Provider类。该类用于发布(deliverfeatures.

(2)     DagramTypeProvider()创建和设置FeatureProvider

public MyDiagramTypeProvider() {

        super();

        setFeatureProvider(new MyFeatureProvider(this));    }

 

1.2      提供 Add 功能(Functionality

为业务对象(领域模型对象)创建图形表达(图符元素。

(1)     创建add feature

①      实现接口IAddFeature,或者继承其抽象类。实现两个方法canAdd(判断给出的内容能否添加)和add(创建图形结构,确立与业务对象的联接

②      Add方法中创建绘图算法(graphics algorithm)(设置图形外观),并放到合适的位置(从给出的内容获取)。创建将被添加的对象与存放入它的容器的联接。

(2)     覆写FeatureProvider中的getAddFeature()方法,实现add feature的发布

1.3      提供Create Feature

创建一个业务对象和相应的图符元素。通常创建业务对象,然后调用Add Feature去创建相应的图符元素。创建CreateFeature之后,框架会自动集成到平台UIplatform’s UI)。

(1)     实现接口ICreateFeature,或继承抽象类AbstractCreateFeature.覆写canCreatecreate(创建业务对象,并为其添加图形表达).

(2)     覆写FeatureProvider类的getCreateFeatures()方法发布create feature

父图形(container)的位置和大小的改变可以由graphiti框架的默认功能更新,但是子图形的更新必须开发者自己实现。

IPeService:为图符元素的创建和布局提供服务(例如形状shape、线条connection),扩展自IPeCreateService, IPeLayoutService

IGaService:为绘图算法(graphics algorithm,外观)的创建和布局提供服务。

1.4     更新(Update)功能

图元的属性值是存储在绘图算法中的( graphics algorithm)它只在创建是存储一次。如果想在图上或属性页编辑后直接生效,则必须增加更新能力。

1.4.1       Graphiti支持两种更新策略:

(1)   如果自动更新是激活状态,业务模型的更改会立即更新到图(diagram)中。

(2)   如果自动更新是未激活状态,业务模型的更改后,在diagram中仅标记图形以过期。用户可以手动的更新这些图形。

(3)   默认情况下自动更新是未激活的,但是可以通过覆写IDiagramTypeProvider类中的以下方法配置:

①      isAutoUpdateAtStartup:当文件输入更改后刷新

②      isAutoUpdateAtRuntime:数据模型更改后刷新

③      isAutoUpdateAtReset:当编辑器获得焦点后刷新

1.4.2       创建update feature

(1)    实现接口IUpdateFeature.或者继承其子类如AbstractUpdateFeature

①      canUpdate:检查给定上下文的当前图符元素(pictogram element)的业务对象是否能被更新。

②      updateNeeded检查图符元素的值是否最新。

③      update:从业务对象复制最新值到图符元素的绘图算法来更新图符元素。

(2)     feature provider中通过方法getUpdateFeature发布。

1.5      移除和删除功能

(1)     区别

①      Remove feature仅仅移把图符元素从图符模型中移除,底层的业务对象(领域模型元素)仍然存在。它基本上是与add feature相反的功能。

②      Delete feature是删除了相应的业务对象(域模型元素)。它基本上是Create Feature相反的功能。Delete feature通常调用Remove feature去删除的图符元素,然后删除相应的业务对象。

(2)     注意

①      通常情况下开发者不必实现这两个功能,graphiti框架提供了良好的默认实现(DefaultRemoveFeature类)。

     如果定义了自己的Remove feature delete feature 也需要在feature provider中发布。getRemoveFeature

1.6    提供移动功能(Move Feature

实现接口IMoveFeature,或者继承其具体的子接口或抽象子类。

一般要覆写canMoveShapemoveShape方法。

1.7    提供调整大小的功能

实现接口IResizeShapeFeature,或者继承其具体的子接口或抽象子类。

一般要覆写canResizeShape,resizeShape方法。

1.8    提供布局功能

支持重新计算的图符模型(pictogram model)内的位置和大小。

实现接口ILayoutFeature,或者继承其具体的子接口或抽象子类。覆写canLayoutlayout方法。

1.9      提供自定义功能

实现接口ICustomFeature,或者继承其具体的子接口或抽象子类。

覆写canExecuteexecute方法。

这两种方法获得一个用户上下文作为参数。接口ICostomContext提供内部的图符元素和内部的图形算法的另外象形元素(选择)。这些是将鼠标指针置于/点击的内部元件。

 

1.10提供添加连线(Connection

1.10.1    步骤

(1)     创建add feature必须实现接口IAddFeature,或者继承其抽象类。实现两个方法canAdd(判断给出的内容能否添加)和add(创建图形结构,确立与业务对象的联接

(2)    feature provider 中用方法getAddFeature发布。

(3)     Creating a Create-Connection-Feature

      必须实现接口ICreateConnectionFeature或者继承其抽象子类,如AbstractCreateConnectionFeature

      覆写方法canCreate(检查给定内容是否能作为线两端点被连接的对象)canStartConnection(检查连线是否能从给定的源头锚开始)create,必须给要连接的对象的图形添加anchors(图形的Add Featureadd方法中peCreateService.createChopboxAnchor(containerShape);)发布。

      必须给容器图形添加锚

 

1.10.2    Connection anchors

Anchor是一个计算过的固定的位置,通常与图形表示法有关。用户必须提供源头和目标锚,通常是通过拖放到锚的图形表达上,然后可以自动计算连接点。

 

框架目前默认支持3种锚。

方盒锚(Chop box anchor):方盒锚在父图形的中心,是唯一不需要绘图算法的。连线的端点并不能直接练到锚上,而上在连线与图形的边缘焦点上,因此该焦点的位置会根据图形和线的大小位置实时计算。(IPeCreateService.createChopboxAnchor

Figure: Chop box anchor (always pointing to the center)

 Box Relative Anchors:其位置与父图形的大小位置有关,例如可以设置在右边中点上。IPeCreateService.createBoxRelativeAnchor

Figure: Box relative anchor (on middle-right border outside of shape)

 Fix Point Anchors:其在父图形中固定位置上。(IPeCreateService.createFixPointAnchor

连接线可以是一条折线;固定点锚的可见的位置端点location和逻辑端点reference-point是同一点。所有锚点都有一个图形表示,如果连线有折点,则连接线是指从关联端点到邻接的一个折点的线段。

 

Note: All anchors have a graphical representationand connections are starting and ending virtually in the center of the graphical representation. But how can it be achieved that connections are starting precisely from the border of this graphical representation? For this purpose define a teh anchor location there and use setUseAnchorLocationAsConnectionEndpoint. At the end of this chapter is an example for this use case.

1.10.3    Connection Decorators修饰

Graphiti框架支持两种修饰:

静态修饰:(不活动图符元素),通常用于连接线的末端(如箭头),静态修饰只支持polyline polygon否则不能旋转。

 

Figure: Static connection decorator

 动态修饰:激活的图符元素,可以被拖动(如线的文本标签)。

Figure: Dynamic connection decorator

 创建连线修饰的步骤:

    创建一个连接线修饰符。

    添加一个有效的图形算法给修饰符

    添加这个修饰符到连接线。

    (可选)链接修饰符到业务对象

这个修饰符将被添加到add connection feature在类中添加

private Polyline createArrow(GraphicsAlgorithmContainer gaContainer)

在add方法中创建修饰符。

Graphiti Developer Guide >Tutorial > Features > Add Connection Feature

1.10.4    创建通过在图元上拖拽的实现连接的连接线

1.11提供直接编辑图元的功能

直接编辑图形,图形实时的现实修改。

1.11.1    Creating a Direct Editing Feature

(1)     创建Direct Editing Feature必须实现接口IDirectEditingFeature或者继承其子类如AbstractDirectEditingFeature.

(2)     实现或覆写方法:

①      getEditingType返回用于编辑该值的编辑器的类型

②      canDirectEdit:该方法检查给定的上下文,并因此决定是否支持直接编辑。

③      getInitialValue:返回编辑器用以进行初始化的初始值,这通常是在当前显示的值。

④      checkValueValid:对当前被编辑器编辑而变化的值进行检查。

⑤      setValue:在编辑过程的结束时,把编辑的值设置到模型。

(3)     feature provider中覆写getDirectEditingFeature发布。

1.11.2    图元对象被创建之后就立即激活直接修改功能。

一个新的图元在创建时会调用不同的featurecreate feature, add feature and update feature.为了自动切换到直接编辑模式,我们需要从这些features中收集一些信息,并将其存储在接口IDirectEditingInfo。所有的features通过调用getFeatureProvider().getDirectEditingInfo().来访问直接编辑信息。在创建过程中的自动直接编辑的必须在create feature中激活,在create方法的后部实现。

In the add feature the outer container shape of the newly created object must be setthe shape (pictogram element) and its graphics algorithm have to be specified, where the direct editing editor shall be opened.

// set container shape for direct editing after object creation

directEditingInfo.setMainPictogramElement(containerShape);

// set shape and graphics algorithm where the editor for

// direct editing shall be opened after object creation

directEditingInfo.setPictogramElement(shape);

directEditingInfo.setGraphicsAlgorithm(text);

 

1.12   提供复制粘贴功能

在图形编辑器,复制和粘贴的经常在graphical model-elements.上执行。

1.12.1    Creating a Copy Feature

(1)      必须实现接口ICopyFeature或者继承其子类如AbstractCopyFeature

(2)      覆写方法:canCopycopy到剪贴板。

(3)      feature provider中覆写getCopyFeature发布。

1.12.2    Creating a Paste Feature

(1)      必须实现接口IPasteFeature.或者继承其子类如AbstractPasteFeature

(2)     覆写方法:canPastepaste

(3)      feature provider中覆写getPasteFeature发布。

1.13   Providing Drill-Down Behavior(挖掘、细分)

Drill-Down Behavior是指图表之间的导航,Graphiti会搜索所有有关联的业务对象的图表。

drill-down feature需要继承AbstractDrillDownFeature。由于AbstractDrillDownFeature的功能是不依赖于tool的,所有方法getDiagrams只能通过tool有效的实现。

创建之后要feature provider.发布