如何从首选项页面获取文件的路径,并通过工作台上的按钮在控制台上打印输出? [英] How to fetch path of a file from preference page and print the Output on console via Button on Workbench?

查看:82
本文介绍了如何从首选项页面获取文件的路径,并通过工作台上的按钮在控制台上打印输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了一个偏好页面,其编程是:

I have made one preference page whose programming is:

public class SAML
extends FieldEditorPreferencePage
implements IWorkbenchPreferencePage {

public SAML() {
    super(GRID);
    setPreferenceStore(RmpPlugin.getDefault().getPreferenceStore());
    setDescription("Browse Appropriate files");
}

public FileFieldEditor f;
public FileFieldEditor f1;
public void createFieldEditors() {
    f=new FileFieldEditor(PreferenceConstants.P_PATH, 
            "&Prism.bat File:", getFieldEditorParent());
    addField(f);

    f1=new FileFieldEditor(PreferenceConstants.P_PATH1, 
            "&NuSMV Application File:", getFieldEditorParent());
    addField(f1);
}

我想获取路径 FileFieldEditor f 并希望此路径在嵌入在工作台上的按钮上运行(但该按钮的编程位于同一工作区上的不同项目中)。
具有prism.bat文件的硬编码路径的按钮编程是:

I want to get path of FileFieldEditor f and want this path to run on a button which is embedded on workbench (but programming of that button is in different project on the same workspace). The button programming which has hard coded path of "prism.bat" file is:

try {
        //to clear the console on every click of button

        IViewPart view = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(IConsoleConstants.ID_CONSOLE_VIEW);
        if (view != null) {
            (myConsole).clearConsole();
        }           
        ProcessBuilder pb=new ProcessBuilder("C:\\Program Files\\prism-4.0\\bin\\prism.bat");
        pb.directory(new File("C:\\Program Files\\prism-4.0\\bin"));
        Process p=pb.start();

        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));

        String in;
        while((in = input.readLine()) != null) {
            out.println(in);
        }


        int exitVal=p.waitFor();            

       if(exitVal==0)
       {
            out.println("Process Successful");
            out.println("Printing on console with Exitvalue =0");

        }
       else
           {out.println("Process failed");
           out.println("Exitvalue = 1");
           }

    }
        catch (Exception e)
        {
            out.println(e.toString());
            e.printStackTrace();

        }

但是我想从我的首选页面获取文件 FileFieldEditor f ,并希望将此路径嵌入到按钮编程中,以便在按下按钮时显示结果。

But I want to fetch file from my preference page FileFieldEditor f and want this path to embed in button programming so that when button is pressed, result is shown.

推荐答案

您需要两部分:


  • 为首选项初始化默认值的代码

  • 使用当前值的代码

要设置默认值,请在激活者

public class Activator extends AbstractUIPlugin {
    @Override
    public void start(BundleContext context) throws Exception {
        super.start(context);
        IPreferenceStore ps = getPreferenceStore();
        ps.setDefault(SHOW_IMAGE, true);
    }
    public static final String SHOW_IMAGE = "showImage";
}

或者,您可以使用 org.eclipse。 core.runtime.preferences 扩展点...

Alternatively, you can use the org.eclipse.core.runtime.preferences extension point...

请注意,上面的代码假设偏好的类型是布尔值 - 数字,字符串等的其他方法...文件名是一个字符串。

Note that the code above assume that the type of the preference is Boolean - there are other methods for numbers, strings, etc... A file name is a string.

要使用当前值,只需使用

To use the current value, just use

if (Activator.getDefault().getPreferenceStore().getBoolean(Activator.SHOW_IMAGE)) {
    …
}

以下幻灯片包含更多信息...

The following slides contains a little more information...

这篇关于如何从首选项页面获取文件的路径,并通过工作台上的按钮在控制台上打印输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆