2

In Antlr 4, such code works in a general main function.

public static void main(String[] args) {
   .....
   SicstusPrologParser parser = new SicstusPrologParser(tokens);
   ParserRuleContext tree =(ParserRuleContext)parser.program();
   tree.inspect(parser);
}

The last statement pops up a model JDialog which shows the parser tree structure. But I copied the code into a junit test case as below:

 @Test
public void testParserClause() { //clause
    .....
   SicstusPrologParser parser = new SicstusPrologParser(tokens);
   ParserRuleContext tree =(ParserRuleContext)parser.program();
   tree.inspect(parser);
 }

The JDialog created by "tree.inpect(parser)" has just been closed by junt before I clicked the "OK" button. I dived into the "inspect" function, its main logic flow is as the following:

   .....
 Callable<JDialog> callable = new Callable<JDialog>() {
    JDialog result;

    @Override
        public JDialog call() throws Exception {
            SwingUtilities.invokeAndWait(new Runnable() {
                @Override
                public void run() {
                                         //fill the tree nodes and show the dialog.
                    result = showInDialog(viewer);
                }
            });

            return result;
        }
    };

    ExecutorService executor = Executors.newSingleThreadExecutor();

    try {
        return executor.submit(callable);
    }
    finally {
        executor.shutdown();
    }

Why is the model JDialog closed before I do sth with it? I used the return value of "inspect", but it still did work.

       Future<JDialog> fu = tree.inpect(parser);
       fu.get();

any help?

frankli22586
  • 710
  • 6
  • 19

1 Answers1

3

A utility method is provided in case you need to wait for the window to close before proceeding:

Future<JDialog> future = tree.inspect(parser);
Utils.waitForClose(future.get());
Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
  • `Utils.waitForClose` has been removed, see https://github.com/antlr/antlr4/commit/4a1ec7df3828d2415680dfb6766c79caa3b449bb#diff-e414337876c195a0e9e6ab1e2fbcede6 – Andrii Chernenko Sep 03 '16 at 22:16