Eclipse首选项页面

时间:2013-02-22 15:42:43

标签: java

我正在为我的eclipse应用程序(Juno)添加一个首选项页面。我想创建类似于您在以下Eclipse首选项页面上看到的内容:Eclipse(Juno)>窗口菜单>偏好> Java>编译器>建造。该首选项页面似乎是使用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");

2 个答案:

答案 0 :(得分:1)

如果您对Eclipse中任何UI元素的实现感兴趣,请安装Eclipse SDK(通过帮助>安装新软件... )并使用plug-in spy。间谍告诉你哪个类实现了UI元素(在你的情况下是org.eclipse.jdt.internal.ui.preferences.JavaBuildPreferencePage包中的org.eclipse.jdt.ui)。由于SDK包含源代码,因此您可以从间谍的弹出窗口直接跳转,并自己查找它是如何完成的。

答案 1 :(得分: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;
}