gef实现属性页

1.如果要实现在属性视图中编辑被选择的对象,则至少必须满足以下两个条件:

  • 被选择的对象必须实现或者能够适配成IPropertySource接口对象。
  • 被选择的对象必须能够被实现了ISelectionProvider接口的选择提供者提供属性视图

2.Eclipse中内置了一些实现了IPropertyDescriptor接口的类

  • PropertyDescriptor 可以实现不可编辑的属性
  • ColorPropertyDescriptor 会弹出颜色选择对话框
  • ComboBoxPropertyDescriptor 可以通过下拉框选择需要的属性
  • TextPropertyDescriptor 实现可编辑的属性
  • StandardComboBoxPropertyDescriptor

3.实现属性栏打开自定义的对话框

 

     可以参考在属性页中打开对话框 ,写得已经很详细了。

 

4.实现属性的显示顺序

       在PropertySheetPage显示的属性中, 如果需要自定义属性显示的上下顺序, 就需要给PropertySheetPage添加一个PropertySheetSorter, 从而决定属性显示的上下顺序.

 4.1 在Editor中加入如下代码:

    1. ...................  
2.     if (type.equals(IPropertySheetPage.class)) {  
3.         return new PropertySheetPage() {  
4.             public void createControl(Composite parent) {  
5.                 // super.createControl(parent);  
6.                 PropertySheetSorter sorter = new PropertySheetSorter() {  
7.                     public int compare(IPropertySheetEntry entryA,  
8.                             IPropertySheetEntry entryB) {  
9.                         return getCollator().compare(  
10.                                 entryA.getDescription(),  
11.                                 entryB.getDescription());  
12.                     }  
13.                 };  
14.                 this.setSorter(sorter);  
15.                 super.createControl(parent);  
16.             }  
17.         };  
18.     }  
19.     return super.getAdapter(type);  
20. }  

...................
if (type.equals(IPropertySheetPage.class)) {
return new PropertySheetPage() {
public void createControl(Composite parent) {
// super.createControl(parent);
PropertySheetSorter sorter = new PropertySheetSorter() {
public int compare(IPropertySheetEntry entryA,
IPropertySheetEntry entryB) {
return getCollator().compare(
entryA.getDescription(),
entryB.getDescription());
}
};
this.setSorter(sorter);
super.createControl(parent);
}
};
}
return super.getAdapter(type);
}



4.2 在处理属性视图的类中加入以下代码
Java代码

1. public IPropertyDescriptor[] getPropertyDescriptors() {  
2.         ArrayList<IPropertyDescriptor> properties = new ArrayList<IPropertyDescriptor>();  
3.         TextPropertyDescriptor name=new TextPropertyDescriptor(Node.PROPERTY_RENAME, "Name");  
4.         name.setDescription("01");//设置这个是为了排序  
5.         properties.add(name);//增加任务名  
6.         if (node instanceof TaskModel) {              
7.             TextPropertyDescriptor des=new TextPropertyDescriptor(TaskModel.PROPERTY_DESCRIPTION,  
8.             "Description");  
9.             des.setDescription("02");  
10.             properties.add(des);//增加任务描述  
11. ...........................  

public IPropertyDescriptor[] getPropertyDescriptors() {
ArrayList<IPropertyDescriptor> properties = new ArrayList<IPropertyDescriptor>();
TextPropertyDescriptor name=new TextPropertyDescriptor(Node.PROPERTY_RENAME, "Name");
name.setDescription("01");//设置这个是为了排序
properties.add(name);//增加任务名
if (node instanceof TaskModel) {            
TextPropertyDescriptor des=new TextPropertyDescriptor(TaskModel.PROPERTY_DESCRIPTION,
"Description");
des.setDescription("02");
properties.add(des);//增加任务描述

 

实例2------------------------

要添加视图,首先我们首先要添加所要依赖的Jar包,org.eclipse.ui.views Jar包,修改AbstractModel模型,实现IpropertySource接口,在HelloModel中实现最上层父类的方法

    @Override
    public IPropertyDescriptor[] getPropertyDescriptors() {
       IPropertyDescriptor []desc = new IPropertyDescriptor[]{new TextPropertyDescriptor(P_TEXT,"属性")};
       return desc;
    }

我们这里只提供一个属性。通过ID,我们获取要的数值

@Override
    public Object getPropertyValue(Object id) {
       if(id.equals(P_TEXT)){
           return text;
       }
       return null;
    }
    @Override
    public boolean isPropertySet(Object id) {
       if(id.equals(P_TEXT)){
           return true;
       }
       return false;
    }
    @Override
    public void setPropertyValue(Object id, Object value) {
       if(id.equals(P_TEXT)){
           setText((String)value);
       }
       super.setPropertyValue(id, value);
    }
}

2.在HelloEditPart中,在PropertyChange(PropertyChangeEvent evnt)中刷新视图
    @Override
    public void propertyChange(PropertyChangeEvent event) {
       super.propertyChange(event);
       if(event.getPropertyName().equals(HelloModel.P_CONSTRAINT)){
              refreshVisuals();
       }else if(event.getPropertyName().equals(HelloModel.P_TEXT)){
           Label label = (Label) getFigure();
           label.setText((String)event.getNewValue());
       }
      
    }

3.在Perspective中设置属性视图可以显示
public class Perspective implements IPerspectiveFactory {
 

    public void createInitialLayout(IPageLayout layout) {
       final String properties ="org.eclipse.ui.views.PropertySheet";
       final String editArea  = layout.getEditorArea();
       IFolderLayout leftLayout = layout.createFolder("LeftTop", IPageLayout.BOTTOM, 0.28f, editArea);
       leftLayout.addView(properties);
       layout.setEditorAreaVisible(true);
    }
}
1.       直接编辑文本

派生出DirectEditManager的子类CustomDirectEditManager,
public class CustomDirectEditManager extends DirectEditManager {
 

    private HelloModel helloModel;
    public CustomDirectEditManager(GraphicalEditPart source, Class editorType,
           CellEditorLocator locator) {
       super(source, editorType, locator);
       this.helloModel=(HelloModel) source.getModel();
    }
 

    @Override
    protected void initCellEditor() {
       getCellEditor().setValue(this.helloModel.getText());
       Text txt = (Text)getCellEditor().getControl();
       txt.selectAll();
      
    }
 

}然后我们从CellEditLocator里面派生出CustomCellEditLocator类,public class CustomCellEditorLocator implements CellEditorLocator {
 

    private IFigure figure;
    public CustomCellEditorLocator(IFigure fg){
       figure = fg;
    }
    @Override
    public void relocate(CellEditor cellEditor) {
       Text txt = (Text) cellEditor.getControl();
       Rectangle rect = figure.getBounds().getCopy();
       figure.translateToAbsolute(rect);
       txt.setBounds(rect.x, rect.y, rect.width, rect.height);
    }
 

}
在HeloEditPart中重载

    @Override
    public void propertyChange(PropertyChangeEvent event) {
       super.propertyChange(event);
       if(event.getPropertyName().equals(HelloModel.P_CONSTRAINT)){
              refreshVisuals();
       }else if(event.getPropertyName().equals(HelloModel.P_TEXT)){
           Label label = (Label) getFigure();
           label.setText((String)event.getNewValue());
       }
      
    }

运行效果图

 

 

要想能够对里面的编辑起到作用,我们需要创建策略和命令
创建Command命令
 

public class DirectEditCommmand extends Command {
 

    private String oldValue,newValue;
    private HelloModel helloModel;
    @Override
    public void execute() {
       super.execute();
       oldValue = helloModel.getText();
       helloModel.setText(newValue);
    }
    @Override
    public void undo() {
       super.undo();
       helloModel.setText(oldValue);
    }
    public void setHelloModel(HelloModel helloModel) {
       this.helloModel = helloModel;
    }
    public void setOldValue(String oldValue) {
       this.oldValue = oldValue;
    }
    public void setNewValue(String newValue) {
       this.newValue = newValue;
    }
   
   
}

创建策略CustomDirectEditPolicy

 public class CustomDirectEditPolicy extends DirectEditPolicy {
 

    @Override
    protected Command getDirectEditCommand(DirectEditRequest req) {
       DirectEditCommmand command = new DirectEditCommmand();
       command.setHelloModel((HelloModel)getHost().getModel());
       command.setNewValue((String)req.getCellEditor().getValue());
       return command;
    }
 

    @Override
    protected void showCurrentEditValue(DirectEditRequest arg0) {
 

    }
 

}
创建对应的CustomDirectEditPolicy策略后,我们需要将该策略安装到HelloEditPart中 @Override
    protected void createEditPolicies() {
       installEditPolicy(EditPolicy.COMPONENT_ROLE   , new CustomComponentEditPolicy());
       installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE, new CustomDirectEditPolicy());
    }
1.       键盘直接操作文本

    @Override
    protected void configureGraphicalViewer() {
       super.configureGraphicalViewer();
       viewer = getGraphicalViewer();
       viewer.setEditPartFactory(new PartFactory());
       //创建键盘句柄
       KeyHandler keyHandler = new KeyHandler();
       //按Del时候执行Del操作
       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);
       getGraphicalViewer().setKeyHandler(new GraphicalViewerKeyHandler(getGraphicalViewer()).setParent(keyHandler));
    }

在createActions()中注册DirectEditAction

    @Override
    protected void createActions() {
       super.createActions();
       IAction action = new UndoRetargetAction();
       action.setId(ActionFactory.UNDO.getId());
       getActionRegistry().registerAction(action);
       getSelectionActions().add(action.getId());
      
       action = new RedoRetargetAction();
       action.setId(ActionFactory.REDO.getId());
       getActionRegistry().registerAction(action);
       getSelectionActions().add(action.getId());
      
       action = new DirectEditAction(this);
       getActionRegistry().registerAction(action);
       getSelectionActions().add(action.getId());
      
      
    }
连接Connection

1.       首先创建连接模型及其控制器AbstractConnectionModel  和LineConnectionEditPart,然后把模型和控制器连接起来。在PartFactory中添加,修改节点模型,使HelloModel变成连接的Node节点,因为HelloModel既是起始节点,又是终止节点。

    //connection
    public static final String P_Source_Connection = "P_Source_Connection";
    public static final String P_Target_Connection ="P_Target_Connection";
   
   
   
    private List sourceConnection = new  ArrayList();
    private List targetConnection = new  ArrayList();
   
   
    public void addSourceConnection(Object conx){
       sourceConnection.add(conx);
       firePropertyChange(P_Source_Connection, null, null);
    }
    public void addTargetConnection(Object conx){
       targetConnection.add(conx);
       firePropertyChange(P_Target_Connection, null, null);
    }
   
    public List getModelSourceConnections(){
       return sourceConnection;
    }
    public List getModelTargetConnections(){
       return targetConnection;
    }
   
    public void removeSourceConnections(Object conx){
       sourceConnection.remove(conx);
       firePropertyChange(P_Source_Connection, null, null);
    }
    public void removeTargetConnections(Object conx){
       targetConnection.remove(conx);
       firePropertyChange(P_Target_Connection, null, null);
    }

要把HelloModel模型当做节点来看待,就需要在HelloEditPart中实现NodeEditPart

    @Override
    public ConnectionAnchor getSourceConnectionAnchor(ConnectionEditPart arg0) {
       return new ChopboxAnchor(getFigure());
    }
 

    @Override
    public ConnectionAnchor getSourceConnectionAnchor(Request arg0) {
       return new ChopboxAnchor(getFigure());
    }
 

在HelloEditPart中调用所有的SourceConnection和TargetConnection节点的方法

    @Override
    public ConnectionAnchor getTargetConnectionAnchor(ConnectionEditPart arg0) {
       return new ChopboxAnchor(getFigure());
    }
 

    @Override
    public ConnectionAnchor getTargetConnectionAnchor(Request arg0) {
       return new ChopboxAnchor(getFigure());
    }

然后在工具箱中添加工具栏

   
       PaletteDrawer   drawer1 = new PaletteDrawer("连接");
       ConnectionCreationToolEntry conncreateEntry = new ConnectionCreationToolEntry("简单连接", "创建最简单的连接", new SimpleFactory(LineConnectionModel.class), Activator.getImageDescriptor("icons/ch_callers(3).gif"), Activator.getImageDescriptor("icons/ch_callers(3).gif"));
       drawer1.add(conncreateEntry);
       root.add(drawer1);

 

2.修改连接模型public abstract class AbstractConnectionModel {
 

    private HelloModel source,target;
    public void attachSource(){
       if(!source.getModelSourceConnections().contains(this)){
           source.addSourceConnection(this);
       }
    }
    public void attachTarget(){
       if(!target.getModelTargetConnections().contains(this)){
           target.addTargetConnection(this);
       }
    }
    public void detachSource(){
       source.removeSourceConnections(this);
    }
    public void detachTarget(){
       target.removeTargetConnections(this);
    }
    public void setSource(HelloModel source) {
       this.source = source;
    }
    public void setTarget(HelloModel target) {
       this.target = target;
    }
    public HelloModel getSource() {
       return source;
    }
    public HelloModel getTarget() {
       return target;
    }
   
}

 

 

实例三---------------------

当使用Eclipse的多页签Editor时,如果按照单个Editor的Property页和Outline的设计,会出现两个Editor使用同一个CommandStack和多页签的Outline不能切换的问题,经过试验以两个页签为例使用下面的解决方案:建立在于MultPageEditorPart级别的IPropertySheetPage和IContentOutlinePage然后通过获取MultiPageEditorPart的pageChange(int index)方法来动态的设置相应的Property和Outline。具体是先步骤:
1.建立MultiPageEditorPart的子类FormEditor,主要实现CreatePages方法,在这个方法里边具体是的实现相应两个子Editor的初始化工作,并且使用阿addPage(Editor,EditorInput)方法把它们加入到MultiPageEditorPart中
2.实现相应的getAdapter(Class)方法,当传入的Class是IPropertySheetPage和IContentOutlinePage的时候,初始化相应的MultiPageEditorPart级别的PropertySheetPage和ContentOutlinePage,这两个类的实现代码如下:

  1. import java.util.HashMap;   
  2. import java.util.Map;   
  3.   
  4. import org.eclipse.jface.viewers.ISelection;   
  5. import org.eclipse.swt.SWT;   
  6. import org.eclipse.swt.widgets.Composite;   
  7. import org.eclipse.swt.widgets.Control;   
  8. import org.eclipse.ui.IActionBars;   
  9. import org.eclipse.ui.IWorkbenchPart;   
  10. import org.eclipse.ui.SubActionBars;   
  11. import org.eclipse.ui.part.PageBook;   
  12. import org.eclipse.ui.views.properties.IPropertySheetPage;   
  13. import org.eclipse.ui.views.properties.PropertySheetPage;   
  14.   
  15. /**  
  16.  * @author kang.ao  
  17.  *  
  18.  *    
  19.  */  
  20. public class FormPropertySheetPage extends PropertySheetPage implements IPropertySheetPage {   
  21.     private PageBook pagebook;   
  22.     private Map recMap = new HashMap();   
  23.     private PropertySheetPage defaultPage;   
  24.     private IActionBars actionBars;   
  25.     private IPropertySheetPage currentPage;   
  26.     private boolean disposed = false;   
  27.   
  28.     class PageRec {   
  29.         IPropertySheetPage page;   
  30.         SubActionBars bars;   
  31.         void setBarsActive(boolean active) {   
  32.             if (active)   
  33.                 bars.activate();   
  34.             else  
  35.                 bars.deactivate();   
  36.         }   
  37.     }   
  38.   
  39.     public FormPropertySheetPage() {   
  40.         defaultPage = new PropertySheetPage();   
  41.     }   
  42.   
  43.     public void createControl(Composite parent) {   
  44.         pagebook = new PageBook(parent, SWT.NULL);   
  45.         defaultPage.createControl(pagebook);   
  46.         if (currentPage != null)   
  47.             setPageActive(currentPage);   
  48.     }   
  49.     private PageRec createPageRec(IPropertySheetPage page) {   
  50.         if (actionBars == null)   
  51.             return null;   
  52.         PageRec rec = new PageRec();   
  53.         rec.page = page;   
  54.   
  55.         rec.bars = new SubActionBars(actionBars);   
  56.         getPageControl(page);   
  57.   
  58.         page.setActionBars(rec.bars);   
  59.         recMap.put(page, rec);   
  60.         return rec;   
  61.     }   
  62.     public void dispose() {   
  63.         updateActionBars();   
  64.   
  65.         if (pagebook != null && !pagebook.isDisposed())   
  66.             pagebook.dispose();   
  67.         pagebook = null;   
  68.         disposed = true;   
  69.         super.dispose();   
  70.     }   
  71.   
  72.     public boolean isDisposed() {   
  73.         return disposed;   
  74.     }   
  75.   
  76.     public Control getControl() {   
  77.         return pagebook;   
  78.     }   
  79.     private Control getPageControl(IPropertySheetPage page) {   
  80.         Control control = page.getControl();   
  81.         if (control == null || control.isDisposed()) {   
  82.             // first time   
  83.             page.createControl(pagebook);   
  84.             control = page.getControl();   
  85.         }   
  86.         return control;   
  87.     }   
  88.     public void selectionChanged(IWorkbenchPart part, ISelection sel) {   
  89.         if (currentPage != null)   
  90.             currentPage.selectionChanged(part, sel);   
  91.     }   
  92.     public void setActionBars(IActionBars bars) {   
  93.         this.actionBars = bars;   
  94.   
  95.         createPageRec(defaultPage);   
  96.   
  97.         if (currentPage != null) {   
  98.             PageRec rec = createPageRec(currentPage);   
  99.             setPageActive(rec);   
  100.             updateActionBars();   
  101.         }   
  102.     }   
  103.     public void setDefaultPageActive() {   
  104.         setPageActive(defaultPage);   
  105.     }   
  106.     public void setFocus() {   
  107.         if (currentPage != null)   
  108.             currentPage.setFocus();   
  109.     }   
  110.     private void setPageActive(PageRec pageRec) {   
  111.         IPropertySheetPage page = pageRec.page;   
  112.         Control control = getPageControl(page);   
  113.         pagebook.showPage(control);   
  114.         pageRec.setBarsActive(true);   
  115.     }   
  116.     public void setPageActive(IPropertySheetPage page) {   
  117.         IPropertySheetPage oldPage = currentPage;   
  118.         this.currentPage = page;   
  119.         if (pagebook == null) {   
  120.             return;   
  121.         }   
  122.         if (oldPage != null) {   
  123.             PageRec oldRec = (PageRec) recMap.get(oldPage);   
  124.             if (oldRec != null) {   
  125.                 oldRec.setBarsActive(false);   
  126.             }   
  127.         }   
  128.         PageRec rec = (PageRec) recMap.get(page);   
  129.         if (rec == null) {   
  130.             rec = createPageRec(page);   
  131.         }   
  132.         if (rec != null) {   
  133.             setPageActive(rec);   
  134.             updateActionBars();   
  135.         }   
  136.     }   
  137.     private void updateActionBars() {   
  138.         actionBars.updateActionBars();   
  139.     }   
  140.   
  141. }  

 

OutlinePage:

  1. /*  
  2.  * 创建日期 Oct 31, 2005  
  3.  *  
  4.  */  
  5. package com.kenoah.kde.formdesignerX;   
  6.   
  7. import org.eclipse.gef.EditPartViewer;   
  8. import org.eclipse.gef.ui.parts.ContentOutlinePage;   
  9. import org.eclipse.jface.action.Action;   
  10. import org.eclipse.jface.action.IAction;   
  11. import org.eclipse.jface.action.IToolBarManager;   
  12. import org.eclipse.jface.viewers.ISelection;   
  13. import org.eclipse.jface.viewers.ISelectionChangedListener;   
  14. import org.eclipse.swt.SWT;   
  15. import org.eclipse.swt.widgets.Composite;   
  16. import org.eclipse.swt.widgets.Control;   
  17. import org.eclipse.ui.IActionBars;   
  18. import org.eclipse.ui.part.IPageSite;   
  19. import org.eclipse.ui.part.PageBook;   
  20. import org.eclipse.ui.views.contentoutline.IContentOutlinePage;   
  21.   
  22. import com.kenoah.kde.formdesignerX.utils.FormdesignerImages;   
  23.   
  24. /**  
  25.  * @author kang.ao  
  26.  *  
  27.  *    
  28.  */  
  29. public class FormContentOutline extends ContentOutlinePage implements IContentOutlinePage {   
  30.   
  31.     private PageBook pagebook;   
  32.     private IContentOutlinePage currentPage;   
  33.     private boolean disposed;   
  34.     private IAction showOutlineAction, showOverviewAction;   
  35.     public static final int ID_OUTLINE = 0;   
  36.     public static final int ID_OVERVIEW = 1;   
  37.   
  38.     public FormContentOutline(EditPartViewer viewer) {   
  39.         super(viewer);   
  40.     }   
  41.     /**  
  42.      * @see org.eclipse.ui.part.IPage#createControl(Composite)  
  43.      */  
  44.     public void createControl(Composite parent) {   
  45.         pagebook = new PageBook(parent, SWT.NONE);   
  46.         configureOutlineViewer();   
  47.         if (currentPage != null)   
  48.             setPageActive(currentPage);   
  49.         showPage(ID_OUTLINE);   
  50.     }   
  51.   
  52.     private void configureOutlineViewer() {   
  53.         IToolBarManager tbm = getSite().getActionBars().getToolBarManager();   
  54.         showOutlineAction = new Action() {   
  55.             public void run() {   
  56.                 showPage(ID_OUTLINE);   
  57.             }   
  58.         };   
  59.         showOutlineAction.setImageDescriptor(FormdesignerImages.getImageDescriptor(FormdesignerImages.OUTLINE));   
  60.         tbm.add(showOutlineAction);   
  61.         showOverviewAction = new Action() {   
  62.             public void run() {   
  63.                 showPage(ID_OVERVIEW);   
  64.             }   
  65.         };   
  66.         showOverviewAction.setImageDescriptor(FormdesignerImages   
  67.                 .getImageDescriptor(FormdesignerImages.OVERVIEW));   
  68.         tbm.add(showOverviewAction);   
  69.     }   
  70.     protected void showPage(int id_outline2) {   
  71.         if(currentPage instanceof IOutlinePageShowPage){   
  72.             ((IOutlinePageShowPage)currentPage).showPage(id_outline2);   
  73.             if(id_outline2==ID_OVERVIEW){   
  74.                 showOutlineAction.setChecked(false);   
  75.                 showOverviewAction.setChecked(true);   
  76.             }else{   
  77.                 showOutlineAction.setChecked(true);   
  78.                 showOverviewAction.setChecked(false);   
  79.             }   
  80.         }   
  81.   
  82.     }   
  83.     private int getCurrentPageId(){   
  84.         if(showOverviewAction!=null && showOutlineAction!=null){   
  85.             if(showOverviewAction.isChecked())   
  86.                 return ID_OVERVIEW;   
  87.             if(showOutlineAction.isChecked()){   
  88.                 return ID_OUTLINE;   
  89.             }   
  90.         }   
  91.         return ID_OUTLINE;   
  92.     }   
  93.     /**  
  94.      * @see org.eclipse.ui.part.IPage#dispose()  
  95.      */  
  96.     public void dispose() {   
  97.         if (pagebook != null && !pagebook.isDisposed())   
  98.             pagebook.dispose();   
  99.         pagebook = null;   
  100.         disposed = true;   
  101.     }   
  102.   
  103.     public boolean isDisposed() {   
  104.         return disposed;   
  105.     }   
  106.   
  107.     /**  
  108.      * @see org.eclipse.ui.part.IPage#getControl()  
  109.      */  
  110.     public Control getControl() {   
  111.         return pagebook;   
  112.     }   
  113.   
  114.     /**  
  115.      * @see org.eclipse.ui.part.IPage#setActionBars(IActionBars)  
  116.      */  
  117.     public void setActionBars(IActionBars actionBars) {   
  118.     }   
  119.   
  120.     /**  
  121.      * @see org.eclipse.ui.part.IPage#setFocus()  
  122.      */  
  123.     public void setFocus() {   
  124.         if (currentPage != null)   
  125.             currentPage.setFocus();   
  126.     }   
  127.   
  128.     /**  
  129.      * @see org.eclipse.jface.viewers.ISelectionProvider#addSelectionChangedListener(ISelectionChangedListener)  
  130.      */  
  131.     public void addSelectionChangedListener(ISelectionChangedListener listener) {   
  132.         if (currentPage != null)   
  133.             currentPage.addSelectionChangedListener(listener);   
  134.     }   
  135.   
  136.     /**  
  137.      * @see org.eclipse.jface.viewers.ISelectionProvider#getSelection()  
  138.      */  
  139.     public ISelection getSelection() {   
  140.         return (currentPage != null) ? currentPage.getSelection() : null;   
  141.     }   
  142.   
  143.     /**  
  144.      * @see org.eclipse.jface.viewers.ISelectionProvider#removeSelectionChangedListener(ISelectionChangedListener)  
  145.      */  
  146.     public void removeSelectionChangedListener(ISelectionChangedListener listener) {   
  147.         if (currentPage != null)   
  148.             currentPage.removeSelectionChangedListener(listener);   
  149.     }   
  150.   
  151.     /**  
  152.      * @see org.eclipse.jface.viewers.ISelectionProvider#setSelection(ISelection)  
  153.      */  
  154.     public void setSelection(ISelection selection) {   
  155.         if (currentPage != null)   
  156.             currentPage.setSelection(selection);   
  157.     }   
  158.   
  159.     public void setPageActive(IContentOutlinePage page) {   
  160.         this.currentPage = page;   
  161.         if (pagebook == null) {   
  162.             return;   
  163.         }   
  164.         if(getSite()!=null){   
  165.             if(page instanceof ContentOutlinePage){   
  166.                 ((ContentOutlinePage)page).init(getSite());   
  167.             }   
  168.             Control control = page.getControl();   
  169.             if (control == null || control.isDisposed()) {   
  170.                 page.createControl(pagebook);   
  171.                 control = page.getControl();   
  172.                    
  173.             }   
  174.             pagebook.showPage(control);   
  175.             showPage(getCurrentPageId());   
  176.         }   
  177.     }   
  178.        
  179.     public void init(IPageSite pageSite) {   
  180.         super.init(pageSite);   
  181.     }   
  182. }  

在MultiPageEditorPart的 protected void pageChange(int newPageIndex);方法中进行更新当前的激活Editor的PropertySheetPage和OutlinePage,方法如下:
更新PropertySheet:

Java代码
  1. void updatePropertySheet(IEditorPageBase part) {   
  2.     if (part != null) {   
  3.         IPropertySheetPage propertySheetPage = part.getPropertySheetPage();   
  4.         if (propertySheetPage != null) {   
  5.             propertySheet.setPageActive(propertySheetPage);   
  6.         } else {   
  7.             propertySheet.setDefaultPageActive();   
  8.         }   
  9.     }   
  10. }  
更新OutlinePage:
Java代码
  1. void updateOutline(IEditorPageBase part) {   
  2.     IContentOutlinePage page = null;   
  3.     if (part != null) {   
  4.         page = ((IEditorPageBase) part).getOutlinePage();   
  5.         if (page != null && contentOutline!=null)   
  6.             contentOutline.setPageActive(page);   
  7.     }   
  8. }  

具体的子Editor中的获得PropertySheet和OutlinePage的方法可以参照Gef例子中的编写,在子Editor中初始化Outline的时候由于同一使用MultiPageEditorPart中的OutlinePage的Action,所以在子Editor的OutlinePage中不能去设定Action按钮,具体的参照FormContentOutline的Action的初始化,这点是与gef例子中给的不同的!

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值