【问题标题】:Eclipse Graphiti, How to create "CustomFeature's create & get all Elements"?Eclipse Graphiti,如何创建“CustomFeature 的创建和获取所有元素”?
【发布时间】:2013-05-02 11:34:03
【问题描述】:

我有两个问题,也许有人可以告诉我如何做到这一点

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

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

例如,我在图中保存了一些元素,而我的“模型说我错过了图中的 3 个元素”。我想编写一个自定义功能,只需单击一/两次即可在 Graphiti 图中绘制/放置缺失的元素,也许我需要在这部分使用 Zest?但一开始我只想放几个元素而不从调色板中删除它们,我该怎么做?

也许有人可以给我指路?

感谢您的帮助!

【问题讨论】:

    标签: eclipse editor graphiti


    【解决方案1】:

    我怎样才能得到一个包含图表中所有元素的列表?

    Diagram 是一个ContainerShape,你可以调用getChildren() 来检索所有的形状

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

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

    在其他地方(更有可能,如果您想模仿调色板中的一些拖放操作),您必须调用正确的XXXCreateFeature,这会将元素添加到模型中(用 Graphiti 的说法是“创建”) (通常,创建主体将在最后调用addGraphicalRepresentation(),它还将通过在内部调用适当的XXXAddFeature,将相应的图形元素添加到图表中。

    【讨论】:

    • 感谢您的回答!我在想没有人会回答我,与此同时我解决了这个问题。一开始我应该在哪里/如何接近真的很困难,但最后很容易。但也许你可以帮助我解决另一个关于“Graphiti as RCP”的问题? stackoverflow.com/questions/16686964/graphiti-as-rcp
    • 是的,Graphiti 的社区很小,文档也很贫乏。顺便说一句:如果您知道您在这里提出的问题的答案,我们鼓励您自己回答。本网站的目的不是帮助个人/孤立的问题,而是建立一个可以帮助其他人的知识库。
    【解决方案2】:

    好的!这是我的解决方案:

    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);
                              }
                          }
                      }
                  }
              }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-21
      • 1970-01-01
      • 2021-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多