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 單飛 閱讀(363) 評論(0)  編輯  收藏 所屬分類: Eclipse Plugin
          主站蜘蛛池模板: 凤山县| 连城县| 福泉市| 洮南市| 潞城市| 商洛市| 双城市| 隆回县| 区。| 宁波市| 成都市| 澄城县| 周至县| 昆山市| 舞钢市| 喜德县| 绵阳市| 玛沁县| 塘沽区| 德钦县| 扶风县| 曲靖市| 罗山县| 乐清市| 巴楚县| 微山县| 锡林郭勒盟| 易门县| 义乌市| 班玛县| 扬州市| 克拉玛依市| 城固县| 利津县| 永胜县| 叶城县| 五寨县| 连城县| 天门市| 隆安县| 镇远县|