stream in ResultsManager

Oxygen general issues.
Pascal
Posts: 3
Joined: Thu Nov 16, 2023 6:25 pm

stream in ResultsManager

Post by Pascal »

I try to display a BufferedReader in ResultManager.
It works fine for System.println but only at the end of the stream for ResultManager.
How can I force the refresh?

Code: Select all

var is =  Packages.java.io.BufferedReader(Packages.java.io.InputStreamReader(process.getInputStream(  )));
var line ="";
var i = 0;
...
...
....
            while ((line = is.readLine(  )) != null) {
                if (line.trim() != ""){
                    dpi = new Packages.ro.sync.document.DocumentPositionedInfo(i);
                    dpi.setLength(1);
                    dpi.setMessage(line.trim());
                    resultManager.addResult("ResultsCommand", dpi, type, true, true);
                    Packages.java.lang.System.err.println("actionPerformed "+name+" :|" + line +"|");
                    i++;
                }
            }
            
Radu
Posts: 9059
Joined: Fri Jul 09, 2004 5:18 pm

Re: stream in ResultsManager

Post by Radu »

Hi Pascal,

What do you mean exactly by this?
but only at the end of the stream for ResultManager
That only the last DocumentPositionedInfo object that you create in the Javascript code is shown in the Results "ResultsCommand" tab?
Other than that you seem to be properly using the "addResult" API:
https://www.oxygenxml.com/InstData/Edit ... n,boolean)

Other than that the constructor that you are using for the DocumentPositionedInfo object is this one:

Code: Select all

  /**
   *  Constructor for a DPI used only to hold the current position in the editor.
   *  
   * @param offset Start of selection.
   */
  public DocumentPositionedInfo(int offset) {
so you would need to pass to it the offset in the file (number of characters counted until the current line), not the line number.
You could also use this constructor instead:

Code: Select all

  /**
   *  Constructor.
   *
   *@param  severity  the severity level of the message.
   *@param  message   the error message.
   *@param  systemID  the system ID.
   *@param  line      the line on which the error occurred in the document.
   *@param  column    the column on which the error occurred.
   *@param  length    the length of the text to be selected.
   */
  public DocumentPositionedInfo(
      int severity, String message, String systemID, int line, int column, int length) {
with the severity being one of the constants in "ro.sync.document.DocumentPositionedInfo.SEVERITY_INFO".... The systemID would be the URL of the file in which you want to locate the problem when the DocumentPositionedInfo will be double clicked in the Results view. Same for the line/column/length.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Pascal
Posts: 3
Joined: Thu Nov 16, 2023 6:25 pm

Re: stream in ResultsManager

Post by Pascal »

Thanks for the constructor.
I change it.
But I still have the result displayed only at the end of the while loop.

Code: Select all

var params = java.lang.reflect.Array.newInstance(java.lang.String, 1);
params[0] = "dir c:\\temp";
var process = Packages.java.lang.Runtime.getRuntime().exec(params);
var resultManager = Packages.ro.sync.exml.workspace.api.PluginWorkspaceProvider.getPluginWorkspace().getResultsManager();
var is =  Packages.java.io.BufferedReader(Packages.java.io.InputStreamReader(process.getInputStream(  )));
var line ="";
            while ((line = is.readLine(  )) != null) {
                if (line.trim() != ""){
                    dpi = new Packages.ro.sync.document.DocumentPositionedInfo(0,line.trim());
                    resultManager.addResult("ResultsCommand", dpi, type, true, true);
                    Packages.java.lang.System.err.println("actionPerformed :|" + line +"|");
                }
Radu
Posts: 9059
Joined: Fri Jul 09, 2004 5:18 pm

Re: stream in ResultsManager

Post by Radu »

Hi,
To make sure I understand, all the results appear but they appear after your process ends, not while your process is running, correct?
The Java Swing architecture updates all components on the AWT Event Queue Thread.
Our implementation of the "resultManager.addResult" API uses "SwingUtilities.invokeLater(...)" to make the changes in the user's interface and add the result in the results list.
Looking at the code you sent me I do not have enough context to know on which thread your while is running. I suspect that you added an action which runs the code. As it is a Java Swing action, it runs on the AWT event queue thread. So as the while loop blocks the entire AWT Event Queue thread by running on it, the items added with "SwingUtilities.invokeLater(...)" will appear only after the action ends and the AWT Event Queue thread can again process events.
I would suggest you create and start a new Java thread which runs the process and uses inside it the while loop to add results. This would be also beneficial and avoid blocking the entire application until the process ends as probably is the case now.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Pascal
Posts: 3
Joined: Thu Nov 16, 2023 6:25 pm

Re: stream in ResultsManager

Post by Pascal »

To make sure I understand, all the results appear but they appear after your process ends, not while your process is running, correct?
Correct
Try Thread.
It works fine thanks.
For context and result :

Code: Select all

	var abstractAction = new JavaAdapter(Packages.javax.swing.AbstractAction, Packages.java.awt.event.ActionListener, {
        actionPerformed: function (e) {
            var th = new JavaAdapter(Packages.java.lang.Thread, {
                run: function() {
                    editor = Packages.ro.sync.exml.workspace.api.PluginWorkspaceProvider.getPluginWorkspace().getCurrentEditorAccess(0);
                    // Prepare command
                    var params = java.lang.reflect.Array.newInstance(java.lang.String, );
                    params[0] = "dir c:\\temp";

                    var process = Packages.java.lang.Runtime.getRuntime().exec(params);
                    var resultManager = Packages.ro.sync.exml.workspace.api.PluginWorkspaceProvider.getPluginWorkspace().getResultsManager();
                    var is =  Packages.java.io.BufferedReader(Packages.java.io.InputStreamReader(process.getInputStream(  )));
                    var line ="";
                    var type = Packages.ro.sync.exml.workspace.api.results.ResultsManager.ResultType.GENERIC;
                    var dpi_sev = 0;
                    while ((line = is.readLine(  )) != null) {
                        if (line.trim() != ""){
                            var dpi = new Packages.ro.sync.document.DocumentPositionedInfo(dpi_sev,line.trim());
                            resultManager.addResult("Results", dpi, type, true, true);
                        }
                    }
                }
            };
            th.start()
        }
    };
    
Radu
Posts: 9059
Joined: Fri Jul 09, 2004 5:18 pm

Re: stream in ResultsManager

Post by Radu »

Hi Pascal,
Great, thanks for posting how the final code solution works!
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Post Reply