posts - 1,  comments - 25,  trackbacks - 0

曾经很蠢的自己去写了一个Dialog去包装preference View.
代码如下 在TS1方法下:

package com.wipro.testtools.ui.internal.action;

import java.util.Iterator;

import org.eclipse.ui.IPerspectiveDescriptor;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.WorkbenchException;
import org.eclipse.ui.internal.OpenPreferencesAction;
import org.eclipse.ui.internal.Workbench;
import org.eclipse.ui.internal.WorkbenchPlugin;
import org.eclipse.ui.internal.actions.OpenPerspectiveDialogAction;
import org.eclipse.ui.internal.dialogs.SelectPerspectiveDialog;
import org.eclipse.ui.dialogs.PreferencesUtil;
import org.eclipse.ui.help.WorkbenchHelp;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.preference.IPreferenceNode;
import org.eclipse.jface.preference.PreferenceManager;
import org.eclipse.jface.preference.IPreferencePage;
import org.eclipse.jface.preference.PreferenceDialog;
import org.eclipse.swt.widgets.Shell;

public class OpenPreferencePages implements IWorkbenchWindowActionDelegate {
    public OpenPreferencePages() {
        super();
    }

    private IWorkbenchWindow workbenchWindow;

    public void dispose() {
    }

    public void init(IWorkbenchWindow window) {
        workbenchWindow = window;
    }

    public void run(IAction action) {

        ts2();
    }

    private void ts2() {
        // OpenPreferencesAction action = new OpenPreferencesAction();
        // OpenPerspectiveDialogAction action = new
        // OpenPerspectiveDialogAction(null);
        // action.run();
        String ourPreferenceDialog = "com.wipro.testtools.ui.preferences.SamplePreferencePage";
        PreferenceDialog dialog = PreferencesUtil.createPreferenceDialogOn(null, ourPreferenceDialog, new String[]{ourPreferenceDialog}, null);
       
        dialog.open();

    }

    /**
     *
     */
    private void ts1() {
        String PreferenceNodeId = "org.eclipse.ui.preferencePages.Workbench";
        PreferenceDialogWrapper dialog = null;
        PreferenceManager manager = WorkbenchPlugin.getDefault()
                .getPreferenceManager();
        if (manager != null) {
            dialog = new PreferenceDialogWrapper(getShell(), manager);
            dialog.create();
            // WorkbenchHelp.setHelp(dialog.getShell(),
            // IHelpContextIds.PREFERENCE_DIALOG);

            for (Iterator iterator = manager.getElements(
                    PreferenceManager.PRE_ORDER).iterator(); iterator.hasNext();) {
                IPreferenceNode node = (IPreferenceNode) iterator.next();
                System.out.println(node.getId());
                if (node.getId().equals(PreferenceNodeId)) {
                    dialog.showPage(node);
                }
            }
            dialog.open();
        }
    }

    public void selectionChanged(IAction action, ISelection selection) {
    }

    private Shell getShell() {
        return DialogCheck.getShell();
    }

}

class PreferenceDialogWrapper extends PreferenceDialog {
    public PreferenceDialogWrapper(Shell parentShell, PreferenceManager manager) {
        super(parentShell, manager);
        super.setSelectedNodePreference("PreferenceNodeId");// this is added
    }

    public boolean showPage(IPreferenceNode node) {
        return super.showPage(node);
    }

    public IPreferencePage getPage(IPreferenceNode node) {
        if (node == null)
            return null;

        if (node.getPage() == null)
            node.createPage();

        if (node.getPage() == null)
            return null;

        return node.getPage();
    }
}

class DialogCheck {
    private DialogCheck() {
    }

    private static VerifyDialog _verifyDialog;

    public static Shell getShell() {
        Shell shell = WorkbenchPlugin.getDefault().getWorkbench()
                .getActiveWorkbenchWindow().getShell();
        _verifyDialog = new VerifyDialog(shell);
        _verifyDialog.create();
        return _verifyDialog.getShell();
    }

}

class VerifyDialog extends TitleAreaDialog {
    public VerifyDialog(Shell parent) {
        super(parent);
    }
}


其实eclipse提供了这样的API: See the TS2 Method:
Just two line codes. :(

posted on 2008-05-23 10:52 Daniel 阅读(678) 评论(0)  编辑  收藏 所属分类: Eclipse的相关

只有注册用户登录后才能发表评论。


网站导航:
 
<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

常用链接

留言簿(3)

随笔档案

文章分类

文章档案

相册

搜索

  •  

最新评论