嘟嘟

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            26 Posts :: 0 Stories :: 6 Comments :: 0 Trackbacks

          XYLayout: 子圖形可以在上面隨意改變大小和位置

          1: 父EditPart中添加布局管理器
          public class ContentsEditPart extends AbstractGraphicalEditPart {

           protected IFigure createFigure() {
               Layer figure  = new Layer();  //圖層
               figure.setLayoutManager(new XYLayout());
               return figure;
           }
             ......
          }

          2: 為子模型添加約束Constraint

          為了使用XYLayout,需要設置被該布局管理器管理的圖形對象的尺寸和位置(約束Constraint), 對圖形集ContentsModel 中的每個圖形都要添加約束,約束被添加到他們的模型類中.
          public class HelloModel {
              private String text = "Hello world";
              private Rectangle constraint;
                ...
              public Rectangle getConstraint() {
                  return constraint;
              }

              public void setConstraint(Rectangle constraint) {
                  this.constraint = constraint;
              }
          }
          3: 在子EditPart中把約束施加給圖形.
          public class HelloEditorPart extends AbstractGraphicalEditPart {
                ...
             protected void refreshVisuals() {
                  Rectangle constraint = ((HelloModel)getModel()).getConstraint();
                  ((GraphicalEditPart)getParent()).setLayoutConstraint(this,getFigure(),constraint);
              }
                ...
          }
          4: 繪制帶約束的圖形
          public class DiagramEditor extends GraphicalEditor {
                ...
              protected void initializeGraphicalViewer() {
            
                  viewer = getGraphicalViewer();
                  ContentsModel parent = new ContentsModel(); //父模型
            
                  HelloModel child1=new HelloModel(); //子模型
                  child1.setConstraint(new Rectangle(0,0,-1,-1)); //添加約束
                  parent.addChildren(child1);
            
                  HelloModel child2=new HelloModel();
                  child2.setConstraint(new Rectangle(30,30,-1,-1));
                  parent.addChildren(child2);
            
                  HelloModel child3=new HelloModel();
                  child3.setConstraint(new Rectangle(10,80,80,50));
                  parent.addChildren(child3);
            
                  viewer.setContents(parent);
              }
          ...
          }

          代碼: 下載

          posted on 2007-05-09 18:15 fyp1210 閱讀(1508) 評論(0)  編輯  收藏 所屬分類: GEF
          主站蜘蛛池模板: 民勤县| 吉安县| 德兴市| 沙洋县| 安图县| 龙里县| 塘沽区| 大冶市| 泾源县| 堆龙德庆县| 宁津县| 慈利县| 永福县| 钟山县| 枞阳县| 萝北县| 邢台市| 卢龙县| 秀山| 五峰| 蓬安县| 崇州市| 嫩江县| 探索| 利川市| 莆田市| 前郭尔| 武义县| 鹤岗市| 定结县| 庐江县| 榕江县| 长海县| 叙永县| 木兰县| 龙江县| 社旗县| 茶陵县| 贵阳市| 广西| 和政县|