Eclipse Graphiti,如何创建;定制功能';s create&;获取所有元素;?

Eclipse Graphiti,如何创建;定制功能';s create&;获取所有元素;?,eclipse,editor,graphiti,Eclipse,Editor,Graphiti,我有两个问题,也许有人能告诉我怎么做 我已经创建了从AbstractCustomFeature扩展而来的新“testFeature”,并可以在我的图表中调用它。如何获得包含图表中所有元素的列表?(我想在开始和以后更新它们的名称和颜色) 我的第二个问题是: 我正在尝试将一些元素添加到图表中,而无需从调色板拖放它们 例如,我在图表中保存了一些元素,我的“模型说我在图表中遗漏了3个元素”。我想写一个自定义功能,只需点击一次/两次就可以在Graphiti图中绘制/放置缺少的元素,也许我需要在这一部分使用

我有两个问题,也许有人能告诉我怎么做

我已经创建了从AbstractCustomFeature扩展而来的新“testFeature”,并可以在我的图表中调用它。如何获得包含图表中所有元素的列表?(我想在开始和以后更新它们的名称和颜色)

我的第二个问题是: 我正在尝试将一些元素添加到图表中,而无需从调色板拖放它们

例如,我在图表中保存了一些元素,我的“模型说我在图表中遗漏了3个元素”。我想写一个自定义功能,只需点击一次/两次就可以在Graphiti图中绘制/放置缺少的元素,也许我需要在这一部分使用Zest?但是在开始的时候,我只想放置一些元素,而不将它们从调色板中删除,我如何才能做到这一点

也许有人能给我指路

谢谢你的帮助

如何从图表中获取包含所有元素的列表

是,您可以调用
getChildren()
来检索所有形状

将一些元素添加到图表中,而无需从选项板拖放它们

对象是否已经在EMF模型中创建,并且您只希望它将其图形对应项添加到图表中?如果是这样,您需要实例化并执行相应的
XXXAddFeature


在其他地方(更可能的情况是,如果您想模拟调色板中的一些拖放操作),则必须调用适当的
XXXCreateFeature
,它将向模型中添加(“创建”,用石墨的说法)元素(通常,create body会在最后调用,这也会通过在内部调用appropiate
XXXAddFeature
)将相应的图形元素添加到图表中。

好!下面是我的解决方案:

class testFeature extends AbstractCustomFeature {
    //...
      public void execute(ICustomContext context) {
          Diagram diagram = getDiagram();                     //get Diagram
          EList<Shape> diagramChildren= diagram.getChildren();//get List with all Children's
          Iterator<Shape> it = diagramChildren.iterator();    //Build iterator for this List


          //go through all objects which are in the Diagram
          while (it.hasNext()) {
              Shape testObjekt = it.next();                                                 
              PictogramElement pe =  testObjekt.getGraphicsAlgorithm().getPictogramElement(); 
              Object bo = getBusinessObjectForPictogramElement(pe);
              //BUILD YOUR EMF & GRAPHITI projects together!!!!
              //otherwise you get always false after editor restart
              if (bo instanceof graphicElement) {
                  graphicElement sElement = (graphicElement)bo;
                  if(pe instanceof ContainerShape){
                      RoundedRectangle testR= (RoundedRectangle) pe.getGraphicsAlgorithm();
                      //testR is my RoundedRectangle like in help tutorial

                      //changes are possible here:
                      //...

                      ContainerShape cs = (ContainerShape) pe;
                      for (Shape shape : cs.getChildren()) {
                        //set Name
                          if (shape.getGraphicsAlgorithm() instanceof Text) {
                              Text text = (Text) shape.getGraphicsAlgorithm();
                                text.setValue("new name!");
                          }
                          //set Line color
                          if (shape.getGraphicsAlgorithm() instanceof Polyline) {
                              Polyline polyline = (Polyline)shape.getGraphicsAlgorithm();
                              polyline.setForeground(manageColor(myColorGreen));
                              polyline.setLineWidth(3);
                          }
                      }
                  }
              }
          }
class testFeature扩展了AbstractCustomFeature{
//...
public void execute(ICustomContext上下文){
Diagram Diagram=getDiagram();//获取图表
EList diagramChildren=diagram.getChildren();//获取包含所有子项的列表
迭代器it=diagramChildren.Iterator();//为此列表生成迭代器
//浏览图表中的所有对象
while(it.hasNext()){
Shape testObjekt=it.next();
PictogramElement pe=testObjekt.getGraphicsAlgorithm().getPictogramElement();
对象bo=getBusinessObjectForPictogramElement(pe);
//一起构建EMF和GRAPHITI项目!!!!
//否则,在重新启动编辑器后,将始终显示false
if(图形元素的bo实例){
graphicElement选择=(graphicElement)bo;
if(集装箱船pe实例){
roundedlectangle testR=(roundedlectangle)pe.getGraphicsAlgorithm();
//testR是我的roundedlectangle,就像帮助教程中一样
//此处可以进行更改:
//...
集装箱船cs=(集装箱船)pe;
对于(形状:cs.getChildren()){
//设置名称
if(shape.getGraphicsAlgorithm()文本实例){
Text Text=(Text)shape.getGraphicsAlgorithm();
text.setValue(“新名称!”);
}
//设置线条颜色
if(多段线的shape.getGraphicsAlgorithm()实例){
Polyline Polyline=(Polyline)shape.getGraphicsAlgorithm();
设置前景(manageColor(myColorGreen));
多段线。设置线宽(3);
}
}
}
}
}

谢谢你的回答!我在想没有人会回答我,同时我解决了这个问题。一开始我应该在哪里/如何解决这个问题确实很困难,但最后很容易。但也许你可以帮我回答另一个关于“Graphiti as RCP”的问题是的,Graphiti有一个小的社区,而且文档相当贫乏。顺便说一句:如果你知道你在这里提出的问题的答案,我们鼓励你自己回答。这个网站的目的不是帮助个人/孤立的问题,而是建立一个知识库,而不是帮助其他人。