论坛首页 Java企业应用论坛

利用GEF制作表格系统(编辑器设计)

浏览 4022 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-02-27  

编辑器设计

为了便于查看,编辑器设计部分进行了简化处理,去掉了保存和导入部分,参考代码如下:

 

public class FormEditor extends GraphicalEditorWithPalette {

      public static final String ID = "com.nuoda.form.rcp.formeditor";

      GraphicalViewer viewer;

      private DiagramNode diagram;

 

     

      public DiagramNode getDiagram() {

             return diagram;

      }

      public void setDiagram(DiagramNode diagram) {

             this.diagram = diagram;

      }

     

      public FormEditor(){

             setEditDomain(new DefaultEditDomain(this));

      }

      protected PaletteRoot getPaletteRoot() {

             // TODO Auto-generated method stub

             return FormPaletteFactory.createPalette();

      }

      public void configureGraphicalViewer(){

             super.configureGraphicalViewer();

             viewer = getGraphicalViewer();

             ScalableRootEditPart rootEditPart = new ScalableRootEditPart();

            

             viewer.setRootEditPart(rootEditPart);

             viewer.setEditPartFactory(new FormPartFactory());

             KeyHandler keyHandler = new KeyHandler();

            

             keyHandler.put(

                           KeyStroke.getPressed(SWT.DEL, 127,0),

                           getActionRegistry().getAction(GEFActionConstants.DELETE)

                           );

            

             keyHandler.put(

                           KeyStroke.getPressed(SWT.F2,0),

                           getActionRegistry().getAction(GEFActionConstants.DIRECT_EDIT)

                           );

 

             getGraphicalViewer().setKeyHandler(keyHandler);

      }

      protected void initializeGraphicalViewer() {

             // TODO Auto-generated method stub

            

             this.viewer.setContents(this.diagram);

            

             IAction action=new CellOperateAction(getGraphicalViewer());

             getActionRegistry().registerAction(action);

             getSelectionActions().add(action.getId());

             TableContextMenuProvider provider=new TableContextMenuProvider(getGraphicalViewer(),getActionRegistry());

        getGraphicalViewer().setContextMenu(provider);

       

      }

      public void doSave(IProgressMonitor monitor){

 

      }

      protected void setInput(IEditorInput input){

             super.setInput(input);

            

             this.diagram=new DiagramNode();

      }

 

}

调色板设计

上述编辑器有一个固定位置的调色板,调色板参考代码如下:

public class FormPaletteFactory {

       public static PaletteRoot createPalette() {

              PaletteRoot root = new PaletteRoot();

                    PaletteGroup toolGroup = new PaletteGroup("工具");

                    ToolEntry tool = new SelectionToolEntry();

                    toolGroup.add(tool);

                    root.setDefaultEntry(tool);

                   

                    tool = new MarqueeToolEntry();

                    toolGroup.add(tool);

                   

                    PaletteDrawer drawer = new PaletteDrawer("数据元素");

                    ImageDescriptor descriptor= AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, "/gar.ico");

                   

                    CreationToolEntry creationToolEntry = new CreationToolEntry(

                                  ".Table",

                                  ".Table",

                                  new SimpleFactory(TableNode.class),

                                  descriptor,

                                  descriptor

                                  );

                    drawer.add(creationToolEntry);

                    creationToolEntry = new CreationToolEntry(

                                  ".Row",

                                  ".Row",

                                  new SimpleFactory(RowNode.class),

                                  descriptor,

                                  descriptor

                                  );

                    drawer.add(creationToolEntry);

                   

                    creationToolEntry = new CreationToolEntry(

                                  ".Column",

                                  ".Column",

                                  new SimpleFactory(ColumnNode.class),

                                  descriptor,

                                  descriptor

                                  );

                    drawer.add(creationToolEntry);

                   

                    creationToolEntry = new CreationToolEntry(

                                  ".Cell",

                                  ".Cell",

                                  new SimpleFactory(CellNode.class),

                                  descriptor,

                                  descriptor

                                  );

                    drawer.add(creationToolEntry);

                   

            

 

                    return root;

 

       }

}

EditPart工厂

模型和控制器的对应关系通过工厂来实现。

public class FormPartFactory implements EditPartFactory {

 

      public EditPart createEditPart(EditPart context, Object model) {

             // TODO Auto-generated method stub

             EditPart part = getPartForElement(model);

             if(part != null){

                    part.setModel(model);

             }

             return part;

      }

      private EditPart getPartForElement(Object modelElement){

             //System.out.println("here...TableEditPart():"+modelElement.toString());

             if(modelElement instanceof DiagramNode){

                    return new DiagramEditPart();

             }else if(modelElement instanceof TableNode){

                    //System.out.println("here...TableEditPart()");

                    FormTableEditPart part=new FormTableEditPart();

                    //System.out.println("here...TableEditPart()");

                   

                    return part;

                    //return new FormTableEditPart();

             }else if(modelElement instanceof CellNode){

                    return new CellEditPart();

             }else if(modelElement instanceof RowNode){

                    return new RowEditPart();

             }else if(modelElement instanceof ColumnNode){

                    return new ColumnEditPart();

             }else if(modelElement instanceof FieldNode){

                    return new FieldEditPart();

             }

             throw new RuntimeException("Error");

      }

 

}

 

至此,一个表格系统基本框架已经完成,当然,策略和命令系统还需要实现。有兴趣的朋友可以根据代码来研究和实现,欢迎交流。

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics