2012-04-16 46 views
2

這是一個問題關於其一般做法採取,所以我沒有將任何代碼。填寫Eclipse RCP的編輯器可視區域不滾動

要求: 我需要在一個多頁面編輯器中創建一個頁面,其中有兩個垂直部分。頂部有一個樹,底部有一個文本框。樹和文本字段應填充它們各自的部分。每個部分應該獨立滾動,並且應該有一個分隔符。當編輯器打開時,我希望編輯器的可見區域根據我提供的某個比率在兩部分之間分配。然後當編輯器調整大小時,兩部分將按比例調整以保持比例並適合頁面。這樣,編輯器頁面上就不會出現滾動條,只有兩個部分。

建議的解決方案: 我的想法是一個SashForm添加到編輯頁面,並設置SashForm的大小是一樣的編輯器的可視區域。然後,我會將一個調整大小的監聽器添加到編輯器頁面,並調整SashForm的大小,以便它與頁面保持同步。但是,我找不到一種方法來獲取編輯器的可見區域。所以當我添加SashForm時,它只是讓每個部分足夠大以適應其數據,並在編輯器頁面上添加一個滾動條。

是否有可能滿足我的要求嗎?

回答

2

成功!關鍵是要聽取ScrolledForm上的調整大小事件。我只測試了Fedora,但我很快就會在Windows上看一看。唯一令我困擾的是使用緩衝區常量似乎有點冒險。

/** 
* Form page that contains a sash form and a button. The sash form is dynamically sized to ensure 
* that it always fills the available space on the page. 
*/ 
public class SashFormDemoPage extends FormPage 
{ 
    /** Horizontal buffer needed to ensure that content fits inside the page */ 
    private static final int HORIZONTAL_BUFFER = 8; 
    /** Vertical buffer needed to ensure that content fits inside the page */ 
    private static final int VERTICAL_BUFFER = 12; 

    /** Percentages of the sash form occupied by the tree and text box respectively */ 
    private static final int[] SASH_FORM_WEIGHTS = new int[] {30, 70}; 


    /** 
    * Constructor 
    * 
    * @param editor parent editor 
    */ 
    public SashFormDemoPage(ComponentEditor editor) 
    { 
     super(editor, "sashFormDemoPage", "Demo"); 
    } 


    /** 
    * {@inheritDoc} 
    */ 
    @Override 
    protected void createFormContent(IManagedForm managedForm) 
    { 
     // Set page title 
     ScrolledForm scrolledForm = managedForm.getForm(); 
     scrolledForm.setText("SashForm Demo"); 

     // Set page layout and add a sash form 
     final Composite parent = scrolledForm.getBody(); 
     parent.setLayout(new GridLayout()); 
     final SashForm sashForm = new SashForm(parent, SWT.VERTICAL); 

     // Add a tree as the top row of the sash form and fill it with content 
     FormToolkit toolkit = managedForm.getToolkit(); 
     int style = SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER; 
     Tree tree = toolkit.createTree(sashForm, style); 

     for (int i = 0; i < 40; i++) { 
      TreeItem parentNode = new TreeItem(tree, SWT.NONE); 
      parentNode.setText("parent-" + i); 
      for (int j = 0; j < 3; j++) { 
       TreeItem childNode = new TreeItem(parentNode, SWT.NONE); 
       childNode.setText("child-" + i + "-" + j); 
      } 
     } 

     // Add a text box as the bottom row of the sash form and fill it with content 
     style = SWT.MULTI | SWT.V_SCROLL | SWT.WRAP | SWT.BORDER; 
     Text text = toolkit.createText(sashForm, null, style); 

     String message = ""; 
     for (int i = 0; i < 100; i++) { 
      message += "This is a test of the layout demo system. This is only a test. "; 
     } 
     text.setText(message); 

     // Add button below sash form 
     final Button button = toolkit.createButton(parent, "Test", SWT.NONE); 

     // Add resize listener to sash form's parent so that sash form always fills the page 
     parent.addControlListener(new ControlListener() { 
      @Override 
      public void controlMoved(ControlEvent e) 
      { 
       // Stub needed to implement ControlListener 
      } 

      @Override 
      public void controlResized(ControlEvent e) 
      { 
       GridData data = new GridData(); 
       Point size = parent.getSize(); 
       data.widthHint = size.x - HORIZONTAL_BUFFER; 
       data.heightHint = size.y - button.getSize().y - VERTICAL_BUFFER; 
       sashForm.setLayoutData(data); 
      } 
     }); 

     // Set sash form's weights and pack its parent so that the initial layout is correct 
     sashForm.setWeights(SASH_FORM_WEIGHTS); 
     parent.pack(); 
    } 
} 
1

爲什麼設置SashForm明確的大小?爲什麼不把它添加到編輯器的父項Composite?父Composite有一個FillLayout,因此SashForm自動填充編輯區。

+0

我的問題,至少據我所知,是'FormPage.getManagedForm()。getForm()'返回ScrolledForm'的'一個實例。該表單將自動添加滾動條以適應其內容。由於我在那裏添加了我的內容,因此頁面會自動展開以適應所有內容。因此,我在整個頁面上都會看到滾動條,而不是在各個部分上。我錯過了什麼嗎? – user1155252 2012-04-24 15:04:22

+0

爲什麼你需要'Formpage'? – 2012-04-25 08:05:22

+0

我的編輯器擴展了'FormEditor',所以我重寫了'addPages',它的Javadoc聲明應該使用'addPage(IFormPage)'添加頁面。不過,我現在看到'FormEditor'也有'addPage(Control)'。我會試着看看我能否添加一個適合我需求的非滾動頁面。 但是我不知道爲什麼'的Javadoc FormPage'描述類爲「一個基類,應該添加到'FormEditor'所有頁面都必須繼承。」 – user1155252 2012-04-25 12:21:41