【问题标题】:Fill Visible Area of Eclipse RCP Editor Without Scrolling在不滚动的情况下填充 Eclipse RCP 编辑器的可见区域
【发布时间】:2012-05-25 00:26:09
【问题描述】:

这是一个关于采用哪种通用方法的问题,所以我没有包含任何代码。

要求: 我需要在一个包含两个垂直部分的多页编辑器中创建一个页面。顶部有一个树,底部有一个文本字段。树和文本字段应填充各自的部分。每个部分都应该独立滚动,并且在它们之间应该有一个分隔符。打开编辑器时,我希望根据我提供的某个比例将编辑器的可见区域划分为两个部分。然后当编辑器调整大小时,这两个部分会按比例调整以保持比例并适合页面。这样编辑器页面本身就不会有滚动条,只有两个部分。

建议的解决方案: 我的想法是在编辑器页面中添加一个SashForm,并将SashForm 的大小设置为与编辑器的可见区域相同。然后我会在编辑器页面中添加一个调整大小的侦听器并调整SashForm 的大小,使其与页面保持同步。但是,我找不到获取编辑器可见区域的方法。因此,当我添加 SashForm 时,它只会使每个部分足够大以容纳其数据并在编辑器页面本身上添加滚动。

是否可以满足我的要求?

【问题讨论】:

    标签: swt eclipse-rcp


    【解决方案1】:

    成功!关键是监听ScrolledForm 上的调整大小事件。我只在 Fedora 上进行了测试,但我很快就会在 Windows 上进行测试。唯一困扰我的是缓冲区常量的使用似乎有点 hacky。

    /**
     * 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();
        }
    }
    

    【讨论】:

      【解决方案2】:

      为什么要明确设置SashForm 的大小?为什么不直接将其添加到编辑器的父级Composite 中?父 Composite 有一个 FillLayout,因此 SashForm 会自动填充编辑器区域。

      【讨论】:

      • 我的问题,至少据我所知,是FormPage.getManagedForm().getForm() 返回ScrolledForm 的一个实例。此表单将自动添加滚动条以适应其内容。由于我在那里添加我的内容,页面会自动扩展以适应所有内容。因此,我在整个页面上都得到了滚动条,而不是在单个部分上。我错过了什么吗?
      • 为什么需要Formpage
      • 我的编辑器扩展了FormEditor,所以我覆盖了addPages,其Javadoc 声明应使用addPage(IFormPage) 添加页面。但是,我现在看到FormEditor 也有addPage(Control)。我会试试看是否可以添加符合我要求的非滚动页面。但是我想知道为什么 FormPage 的 Javadoc 将该类描述为“一个基类,所有应该添加到 FormEditor 的页面都必须是子类。”
      • 嗯...我对FormEditor和朋友没有太多经验...所以希望您能找到一个好的解决方案...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-08
      • 2010-11-19
      • 1970-01-01
      • 1970-01-01
      • 2021-12-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多