9910

单飞

   :: 首页 :: 联系 :: 聚合  :: 管理
MultiPageEditorPart 的addPage方法
public int addPage(Control control)
    {
        
int i = getPageCount();
        addPage(i, control);
        
return i;
    }

    
public void addPage(int i, IEditorPart ieditorpart, IEditorInput ieditorinput)
        
throws PartInitException
    {
        IEditorSite ieditorsite 
= createSite(ieditorpart);
        ieditorpart.init(ieditorsite, ieditorinput);
        Composite composite 
= new Composite(getContainer(), getOrientation(ieditorpart));
        composite.setLayout(
new FillLayout());
        ieditorpart.createPartControl(composite);
        ieditorpart.addPropertyListener(
new IPropertyListener() {

            
public void propertyChanged(Object obj, int j)
            {
                handlePropertyChange(j);
            }

        }
);
        CTabItem ctabitem 
= createItem(i, composite);
        ctabitem.setData(ieditorpart);
        nestedEditors.add(ieditorpart);
    }

那么有办法把一个EditorPart 加入Composite再放到Page里面呢?
public void addHtmlPage(IEditorPart ieditorpart, IEditorInput ieditorinput) throws PartInitException {
        
final SashForm sashForm = new SashForm(getContainer(), SWT.NONE);
        
        
final Composite pageContainer = new Composite(sashForm, SWT.NONE);
        sashForm.setSashWidth(
1);
        
        
final Composite resultContainer = new Composite(sashForm, SWT.NONE);
        resultContainer.setLayout(
new FillLayout());
        
        sashForm.setWeights(
new int[] {11 });        
        createAnalyzePage(resultContainer);
        
        IEditorSite ieditorsite 
= createSite(ieditorpart);
        ieditorpart.init(ieditorsite, ieditorinput);

        pageContainer.setLayout(
new FillLayout());
        ieditorpart.createPartControl(pageContainer);
        ieditorpart.addPropertyListener(
new IPropertyListener() {

            
public void propertyChanged(Object obj, int j) {
                handlePropertyChange(j);
            }

        });
        
        
        
int index = addPage(sashForm);
        setPageText(index, 
"html");
    }
视乎这样就可以了,但是问题出现了.缺少了父类nestedEditors.add(ieditorpart);对editor生命周期的管理.导致属性编辑器出问题.怎么解决呢?
    private CTabFolder container;
    
private ArrayList nestedEditors;

MultiPageEditorPart 声明的都是私有的属性,按照面向对象的设计是不能被继承者访问的.
那怎么办呢?这时候就该使用锤子了."Reflect"

CTabFolder container = (CTabFolder) getParentFileValue(MultiPageEditorPart.class,(MultiPageEditorPart)this"container");
        CTabItem ctabitem 
= new CTabItem(container, 0, i);
        ctabitem.setControl(composite);
        ctabitem.setData(ieditorpart);
        ArrayList nestedEditors 
= (ArrayList) getParentFileValue(MultiPageEditorPart.class,(MultiPageEditorPart)this"nestedEditors");
        nestedEditors.add(ieditorpart);

public static Object getParentFileValue(Class parentClassType,Object object, String filedName) {
        Field fild = null;
        Object fildValue = null;
        try {
            fild = parentClassType.getDeclaredField(filedName);
            fild.setAccessible(true);// 设置安全检查,访问私有成员变量必须
            fildValue = fild.get(object);

        } catch (NoSuchFieldException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return fildValue;
    }

呵呵,问题解决了.

posted on 2009-05-27 10:31 单飞 阅读(345) 评论(0)  编辑  收藏 所属分类: Eclipse Plugin

只有注册用户登录后才能发表评论。


网站导航: