Preference Page和PropertyPage的一些相关知识.

PropertyPage实际上是继承PreferencePage的. 这个从类就可以看到.

package org.eclipse.ui.dialogs ..... public abstract class PropertyPage extends PreferencePage implements         IWorkbenchPropertyPage { }
打开PreferencePageDialog:
        PreferenceManager manager = window.getWorkbench().getPreferenceManager();
        PreferenceDialog dialog = new PreferenceDialog(window.getShell(), manager);
        dialog.open();

打开PropertyPageDialog:
        PropertyDialog dialog = PropertyDialog.createDialogOn(shell, null, element);
        dialog.open();

打开只有选定首选项页的Dialog:
                WorkbenchPreferenceDialog dialog = WorkbenchPreferenceDialog.createDialogOn(parent.getShell(), id);
                dialog.showOnly(new String[] { id });
                dialog.open();

这些都是常规方法, 应该可以满足RCP开发的基本需求了.

另外就是大家可以看到有些PropertyPage上有"Enable project specific settings" 的CheckBox和"Configure Workspace Setting"的超链. 这个其实是JDT里面带的页面, 类名应该是PreferenceAndPropertyPage. 不过因为RCP不一定会用到JDT, 所以我写了一个简单的实现, 如下:

其中StingIDEMessages是一个NLS类, 应该不用多解释了.

/**
 * Copyright 2007~2008 Brad.Wu, All rights reserved.<br>
 * Created at Jun 3, 2008 9:55:03 AM
 */
package test;

import net.sf.sting.ide.messages.StingIDEMessages;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.ui.IWorkbenchPropertyPage;
import org.eclipse.ui.dialogs.PropertyPage;
import org.eclipse.ui.internal.dialogs.WorkbenchPreferenceDialog;

/**
 * 关联到首选项的属性页面
 *
 * @author brad.wu
 * @since 0.1
 */
public abstract class PreferenceAndPropertyPage extends PropertyPage implements
        IWorkbenchPropertyPage {
    private Button btnEnableProjectSettings;

    /**
     * (non-Javadoc)
     *
     * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
     */
    @Override
    protected final Control createContents(final Composite parent) {
        Composite container = new Composite(parent, SWT.NONE);
        container.setLayoutData(new GridData(GridData.FILL_BOTH));
        GridLayout layout = new GridLayout();
        layout.numColumns = 2;
        container.setLayout(layout);

        btnEnableProjectSettings = new Button(container, SWT.CHECK);
        btnEnableProjectSettings
                .setText(StingIDEMessages.PreferenceAndPropertyPage_enableProjectSettingsLabel);
        btnEnableProjectSettings.setLayoutData(new GridData(GridData.BEGINNING,
                GridData.CENTER, true, false, 1, 1));
        Link link = new Link(container, SWT.NONE);
        link
                .setText("<a>"
                        + StingIDEMessages.PreferenceAndPropertyPage_configWorkspaceSettingsLink
                        + "</a>");
        link.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER,
                false, false, 1, 1));

        Label label = new Label(container, SWT.SEPARATOR | SWT.HORIZONTAL);
        label.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true,
                false, 2, 1));

        final Composite comp = new Composite(container, SWT.NONE);
        comp.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true,
                true, 2, 1));
        comp.setLayout(new GridLayout());
        createCustomContents(comp);
        setEnabled(comp, false);

        btnEnableProjectSettings.addSelectionListener(new SelectionAdapter() {
            /**
             * (non-Javadoc)
             *
             * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
             */
            @Override
            public void widgetSelected(SelectionEvent e) {
                boolean selected = btnEnableProjectSettings.getSelection();
                setEnabled(comp, selected);
            }
        });
        link.addSelectionListener(new SelectionAdapter() {
            /**
             * (non-Javadoc)
             *
             * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
             */
            @Override
            public void widgetSelected(SelectionEvent e) {
                String id = getRelatedPreferencePageId();
                WorkbenchPreferenceDialog dialog = WorkbenchPreferenceDialog
                        .createDialogOn(parent.getShell(), id);
                dialog.showOnly(new String[] { id });
                dialog.open();
            }
        });
        return container;
    }

    /**
     * 创建组件
     *
     * @param parent
     */
    protected abstract void createCustomContents(Composite parent);

    /**
     * 获取相关的首选项页面ID
     *
     * @return
     */
    protected abstract String getRelatedPreferencePageId();

    /**
     * 获取项目设置是否已经被启用
     *
     * @return
     */
    protected boolean isProjectSettingsEnabled() {
        return btnEnableProjectSettings.getSelection();
    }

    /**
     * 设置可用性
     *
     * @param comp
     * @param selected
     */
    private void setEnabled(final Composite comp, boolean selected) {
        comp.setEnabled(selected);
        Control[] controls = comp.getChildren();
        if (controls != null) {
            for (Control control : controls) {
                control.setEnabled(selected);
                if (control instanceof Composite) {
                    setEnabled((Composite) control, selected);
                }
            }
        }
    }

    /**
     * 设置是否启用项目设置
     *
     * @param enabled
     */
    protected final void setEnableProjectSettings(boolean enabled) {
        btnEnableProjectSettings.setSelection(enabled);
    }
}
展开阅读全文

没有更多推荐了,返回首页