0

我正在向我的 Eclipse 应用程序 (Juno) 添加首选项页面。我想创建类似于您在以下 Eclipse 首选项页面上看到的内容:Eclipse (Juno) > Window Menu > Preferences > Java > Compiler > Building。该首选项页面似乎是使用 org.eclipse.swt.widgets.Tree 创建的,但我不确定。如果是这样,他们是如何创建 TreeItems 的?它们是 org.eclipse.swt.widgets.TreeItems 吗?我需要添加 StringFieldEditors 和 IntegerFieldEditors,或某种类型的字段(TextArea??),前面有一些标签,我可以稍后验证。据我了解,不可能将 Composite 添加到 TreeItem,那么我应该如何解决这个问题?非常感谢任何建议。谢谢。

需要补充一点,因为我不能使用 Eclipse 内部包,有没有其他方法可以使用公共 API 来实现我上面描述的内容?

这是一个想法,但此代码将 TreeItems 内容放在树下。想法?

    Composite comp = getFieldEditorParent();

    Tree tree = new Tree(comp, SWT.NONE);
    tree.setLayout(new FillLayout());
    tree.setHeaderVisible(true);

    TreeItem item1 = new TreeItem(tree, SWT.NONE);
    item1.setText("Name1");

    TreeItem item11 = new TreeItem(item1, SWT.NONE);
    item11.setText("Name11");
    StringFieldEditor s11 = new StringFieldEditor(
        "name11",
        "label11:",
        comp);
    item11.setData(s11);

    TreeItem item12 = new TreeItem(item1, SWT.NONE);
    item12.setText("Name12");
    StringFieldEditor s12 = new StringFieldEditor(
        "name12",
        "label12:",
        comp);
    item12.setData(s12);


    item1.setExpanded(true);
    item11.setExpanded(true);
    item12.setExpanded(true);


    TreeItem item2 = new TreeItem(tree, SWT.NONE);
    item2.setText("Name2");
4

2 回答 2

1

如果您对 Eclipse 中任何 UI 元素的实现感兴趣,请安装 Eclipse SDK(通过Help > Install New Software...)并使用插件 spy。间谍告诉您哪个类实现了 UI 元素(在您的情况下,它org.eclipse.jdt.internal.ui.preferences.JavaBuildPreferencePageorg.eclipse.jdt.ui捆绑包中)。由于 SDK 包含源代码,因此您可以直接从间谍的弹出窗口跳转到那里,并自行查看它是如何完成的。

于 2013-02-22T16:12:29.240 回答
0

使用 org.eclipse.ui.forms.widgets.ExpandableComposite 解决了这个问题。请参见下面的示例。我希望这可以帮助别人 :)。

    protected final void createFieldEditors()
{
    // Create the ScrolledComposite to scroll horizontally and vertically
    fScrolledComposite = new ScrolledComposite(
        getFieldEditorParent(),
        SWT.H_SCROLL | SWT.V_SCROLL);
    //Displays the scrollbars when the window gets smaller
    fScrolledComposite.setAlwaysShowScrollBars(false);
    // Sets the minimum size for the composite to work for scrolling
    fScrolledComposite.setMinSize(fCompositeWidth, fCompositeHeight);
    fScrolledComposite.setExpandHorizontal(true);
    fScrolledComposite.setExpandVertical(true);

    Composite composite = new Composite(fScrolledComposite, SWT.NONE);
    composite.setLayout(new GridLayout());
    fScrolledComposite.setContent(composite);
    // Sets up the toolkit.
    Display display = composite.getDisplay();
    toolkit = new FormToolkit(display);

    // Creates a form instance.
    form = toolkit.createForm(composite);
    form.getBody().setLayout(new GridLayout());
    form.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
    form.setText("Model: " + SignalGeneratorDevice.MODEL_ID);// Sets title of the Preference page

    // Add the three main nodes to the preference page
    addNode1();
  }

   /**
 * Adds the first node to the preference page
 */
private void addNode1()
{
    ExpandableComposite expandableComposite = createExpandableComposite(
        "Signal Generator Device Host/Port:",
        true);
    Composite childComposite = createChildComposite(expandableComposite);

    //Builds fields here (StringFieldEditor, IntegerFieldEditor, etc.)
            ..................
}

    /**
 * Creates an ExpandableComposite that will be added to the preference page
 * 
 * @param label
 * @param expanded
 * @return
 */
private ExpandableComposite createExpandableComposite(
    String label,
    boolean expanded)
{
    ExpandableComposite expandableComposite = null;
    if (expanded) {
        expandableComposite = toolkit.createExpandableComposite(
            form.getBody(),
            ExpandableComposite.TWISTIE | ExpandableComposite.CLIENT_INDENT
            | ExpandableComposite.EXPANDED);
    } else {
        expandableComposite = toolkit
        .createExpandableComposite(
            form.getBody(),
            ExpandableComposite.TWISTIE
            | ExpandableComposite.CLIENT_INDENT);
    }

    expandableComposite.setText(label);
    expandableComposite.setBackground(form.getBackground());
    expandableComposite.addExpansionListener(new ExpansionAdapter() {
        @Override
        public void expansionStateChanged(
            ExpansionEvent e)
        {
            form.pack();
        }
    });

    GridData gd = new GridData();
    expandableComposite.setLayoutData(gd);

    return expandableComposite;
}

/**
 * Creates a child composite for an ExpandableComposite
 * 
 * @param expandableComposite
 * @return
 */
private Composite createChildComposite(
    ExpandableComposite expandableComposite)
{
    Composite childComposite = new Composite(expandableComposite, SWT.None);

    GridData gd = new GridData(GridData.FILL_BOTH);
    gd.horizontalSpan = 2;
    //gd.horizontalAlignment = GridData.END;
    childComposite.setLayoutData(gd);

    expandableComposite.setClient(childComposite);

    return childComposite;
}
于 2013-02-28T21:59:48.067 回答