嘟嘟

            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 閱讀(1505) 評論(0)  編輯  收藏 所屬分類: GEF
          主站蜘蛛池模板: 尉氏县| 鹰潭市| 南召县| 保靖县| 北票市| 鹤壁市| 焦作市| 湖南省| 平舆县| 扬中市| 张北县| 马边| 桂平市| 永丰县| 军事| 曲水县| 平阳县| 墨竹工卡县| 乐业县| 高尔夫| 即墨市| 洛川县| 灯塔市| 长葛市| 拉萨市| 凉城县| 额济纳旗| 博罗县| 北碚区| 疏勒县| 玛沁县| 密云县| 健康| 葵青区| 景泰县| 元氏县| 苗栗县| 文昌市| 芦溪县| 青海省| 剑川县|