gpt4 book ai didi

swt - 无需滚动即可填充 Eclipse RCP 编辑器的可见区域

转载 作者:行者123 更新时间:2023-12-04 05:50:05 26 4
gpt4 key购买 nike

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

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

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

能满足我的要求吗?

最佳答案

成功!关键是监听 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();
}
}

关于swt - 无需滚动即可填充 Eclipse RCP 编辑器的可见区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10176942/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com