Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
776 views
in Technique[技术] by (71.8m points)

eclipse - JAVA : How to access file path from preference page and use it in Programming code

My ProcessBuilder class ---

 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
}

}

My FileFieldEditorClass

   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 class is in com.myplugin.rmp.preferences package and ProcessBuilder class is in com.myplugin.rmp package,

Now suggest me the way to way to access.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

f1.getStringValue() returns the field editor's value according to the Javadoc.

In order for your process builder to access the f1 field you must have access to f and f1 in the class creating the process builder. For that you can add these fields in this class constructor

public class CreateProcessBuilderClass {

    private final FileFieldEditor f;

    private final FileFieldEditor FileFieldEditor f1;

    // in the method creating the process builder
    ProcessBuilder pb=new ProcessBuilder(f.getStringValue());
    pb.directory(new File(f1.getStringValue());


}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...