Java : How to access file path from preference page and use it in Programming code

标签 java eclipse eclipse-plugin

我的 ProcessBuilder 类 ---

 public class HelloWorldAction implements IWorkbenchWindowActionDelegate {
IWorkbenchWindow activeWindow = null;

public void run(IAction proxyAction) {

    MessageConsole myConsole = null;
    String name = "outputConsole";

    ConsolePlugin plugin = ConsolePlugin.getDefault();
    IConsoleManager conMan = plugin.getConsoleManager();
    IConsole[] existing = conMan.getConsoles();
    for (int i = 0; i < existing.length; i++)
       if (name.equals(existing[i].getName()))
           myConsole = (MessageConsole) existing[i];

      //no console found, so create a new one
    if (myConsole == null)
        myConsole = new MessageConsole(name, null);

    conMan.addConsoles(new IConsole[]{myConsole});

    IWorkbench wb = PlatformUI.getWorkbench();
    IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
    IWorkbenchPage page = win.getActivePage();

    String id = IConsoleConstants.ID_CONSOLE_VIEW;
    try
    {
        IConsoleView view = (IConsoleView) page.showView(id);
        view.display(myConsole);

    }
    catch (Exception e)
    {

    }

    MessageConsoleStream out = myConsole.newMessageStream();
    out.println("Prism Button Works !");


    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("Printing on console");

        }
       else
           out.println("Process failed");
    }
        catch (Exception e)
        {
            out.println(e.toString());
            e.printStackTrace();

        }
    } 
// IActionDelegate method
public void selectionChanged(IAction proxyAction, ISelection selection) {
    // do nothing, action is not dependent on the selection
}

// IWorkbenchWindowActionDelegate method
public void init(IWorkbenchWindow window) {
    activeWindow = window;

}

// IWorkbenchWindowActionDelegate method
public void dispose() {
    //  nothing to do
}

我的 FileFieldEditor 类

   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);
}
public void init(IWorkbench workbench) {
}

FileFieldEditor 类在 com.myplugin.rmp.preferences 包中 ProcessBuilder 类在 com.myplugin.rmp 包中,

现在建议我访问的方式。

最佳答案

在构建 ProcessBuilder 的 try block 中尝试这个:

 IPreferenceStore store = plugin.getPreferenceStore();

 ProcessBuilder pb=new ProcessBuilder(store.getString(PreferenceConstants.P_PATH);
 pb.directory(new File(store.getString(PreferenceConstants.P_PATH1));
 Process p=pb.start();

关于Java : How to access file path from preference page and use it in Programming code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6896101/

相关文章:

eclipse - 将我的 GWT、GAE 安装移动到我的项目中以进行源代码控制

eclipse-plugin - 如何通过EGit/JGit插件为Git事件创建监听器?

java - 如何将文件写入 Android 中的外部公共(public)存储以便它们在 Windows 中可见?

java - 隔离 RSS 提要中的链接

java - Rally Java 回顾 API

eclipse RCP : how to observe the states of the cut/copy/paste commands?

java - 使用 Eclipse 在 64 位机器上编译 32 位 Java 构建

java - 使用pig将数据存储到Hbase中

java - 使用另一个 JDK 编译 Maven 依赖项

api - 无法在 Eclipse 中将 Karate 编写的功能文件作为 cucumber 功能运行