Skip to content

How to wait for async open file action? #100

Description

@jose1711

The goal is to have a script open a local file, then manipulate the layers. Issue I am facing here is that I cannot find a way to tell script "wait until the file is loaded and only then continue execute the remaining tasks". As a result layer count is not updated in the code below:

Jython:

from javax.swing import JOptionPane
from org.openstreetmap.josm.gui import MainApplication
from org.openstreetmap.josm.actions import OpenFileAction
import java.io.File as File
from java.lang import Thread

# JOSM is started, no layer is opened
layerManager = MainApplication.getLayerManager()
all_layers = layerManager.getLayers()
JOptionPane.showMessageDialog(MainApplication.getMainFrame(), 'Layer count: {}'.format(len(all_layers)))
# Layer count: 0

# async open of a single file, we now (should) have 1 layer opened
# but we need this task to finish first
open_file_action = OpenFileAction.openFiles([File('/path/to/local/file.osm')])

'''
# this blocks forever:
while True:
    if open_file_action.isDone():
        break
    Thread.sleep(300);

# this blocks forever too:
open_file_action.get()
'''

# this works but a user must click [OK] to continue
JOptionPane.showMessageDialog(MainApplication.getMainFrame(), 'wait for open')

all_layers = layerManager.getLayers()
JOptionPane.showMessageDialog(MainApplication.getMainFrame(), 'Layer count: {}'.format(len(all_layers)))
# Layer count: 1

JS (courtesy of @zdila)

import josm from 'josm' 

const OpenFileAction = Java.type('org.openstreetmap.josm.actions.OpenFileAction');
const File = Java.type('java.io.File');

const future = OpenFileAction.openFiles([new File("/path/to/local/file.osm")]);

josm.console.println("LOOP ");

// no setTimeout available so let's try it hardcore
for (let i = 0; i < 100000; i++) {
  if (future.isDone()) {
    break;
  }
  
  josm.console.println("Layers: " + josm.layers.length + " " + i);
}

josm.console.println("DONE");

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions