当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Eclipse preference page

问题描述:

I'm working on adding a preference page to my eclipse application (Juno). I would like create something similar to what you see on the following Eclipse preference page: Eclipse (Juno) > Window Menu > Preferences > Java > Compiler > Building. That preference page appears to be created using org.eclipse.swt.widgets.Tree, but I'm not sure. If that is the case, how did they create the TreeItems? Are they org.eclipse.swt.widgets.TreeItems? I need to add StringFieldEditors and IntegerFieldEditors, or some type of fields (TextArea??), with some labels in front of them, that I could validate later on. From what I understand, it's not possible to add a Composite to a TreeItem, so how should I go around this problem? Any suggestion is greatly appreciated. Thanks.

Need to add that, since I can't use the Eclipse internal packages, is there other way to implement what I described above using the public API?

Here is an idea, but this code places the TreeItems contents under the tree. Thoughts?

 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");

网友答案:

If you are interested in the implementation of any UI element in Eclipse, then install the Eclipse SDK (via Help > Install New Software...) and use the plug-in spy. The spy tells you which class implements the UI element (in your case it's org.eclipse.jdt.internal.ui.preferences.JavaBuildPreferencePage in the org.eclipse.jdt.ui bundle). Since the SDK includes the source, you can jump right there from the spy's pop-up and look for yourself how it's done.

网友答案:

The problem was solved by using org.eclipse.ui.forms.widgets.ExpandableComposite. See the example below. I hope this helps someone :).

    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;
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: