diff --git a/gclc-swt/src/main/java/net/bigeon/gclc/swt/SWTConsole.java b/gclc-swt/src/main/java/net/bigeon/gclc/swt/SWTConsole.java index 5fc8c13..9842bd4 100644 --- a/gclc-swt/src/main/java/net/bigeon/gclc/swt/SWTConsole.java +++ b/gclc-swt/src/main/java/net/bigeon/gclc/swt/SWTConsole.java @@ -250,24 +250,8 @@ public final class SWTConsole extends Composite @Override public String prompt() throws IOException { synchronized (promptLock) { - if (isDisposed()) { - throw new IOException(); - } + initPrompt(prompt.apply()); try { - Display.getDefault().syncExec(new Runnable() { - @Override - public void run() { - if (!consoleInput.isDisposed()) { - consoleInput.setEnabled(true); - lblPromptlabel.setText(prompt.apply()); - // relayout - SWTConsole.this.layout(); - consoleInput.setFocus(); - } - } - }); - prompting = true; - promptLock.notifyAll(); while (prompting) { promptLock.wait(); } @@ -283,30 +267,35 @@ public final class SWTConsole extends Composite return command; } + private void initPrompt(final String message) throws IOException { + if (isDisposed()) { + throw new IOException(); + } + Display.getDefault().syncExec(new Runnable() { + @SuppressWarnings("synthetic-access") + @Override + public void run() { + if (!consoleOutput.isDisposed()) { + lblPromptlabel.setText(message); + // relayout + SWTConsole.this.layout(); + consoleInput.setEnabled(true); + consoleInput.setFocus(); + } + } + }); + prompting = true; + command = null; + promptLock.notifyAll(); + } + /* (non-Javadoc) * @see fr.bigeon.gclc.manager.ConsoleInput#prompt(long) */ @Override public String prompt(final long timeout) throws IOException { synchronized (promptLock) { - if (isDisposed()) { - throw new IOException(); - } + initPrompt(prompt.apply()); try { - Display.getDefault().syncExec(new Runnable() { - @Override - public void run() { - if (!consoleInput.isDisposed()) { - consoleInput.setEnabled(true); - lblPromptlabel.setText(prompt.apply()); - // relayout - SWTConsole.this.layout(); - consoleInput.setFocus(); - } - } - }); - prompting = true; - command = null; - promptLock.notifyAll(); final long start = System.currentTimeMillis(); long cur = start; while (prompting && start + timeout > cur) { @@ -330,25 +319,11 @@ public final class SWTConsole extends Composite @Override public String prompt(final String message) throws IOException { synchronized (promptLock) { - if (isDisposed()) { - throw new IOException(); - } + initPrompt(message); try { - Display.getDefault().syncExec(new Runnable() { - @SuppressWarnings("synthetic-access") - @Override - public void run() { - if (!consoleOutput.isDisposed()) { - lblPromptlabel.setText(message); - // relayout - SWTConsole.this.layout(); - consoleInput.setEnabled(true); - consoleInput.setFocus(); - } - } - }); - prompting = true; - promptLock.wait(); + while (prompting) { + promptLock.wait(); + } if (isDisposed()) { throw new IOException(); } @@ -357,17 +332,7 @@ public final class SWTConsole extends Composite command = null; Thread.currentThread().interrupt(); } finally { - Display.getDefault().syncExec(new Runnable() { - @SuppressWarnings("synthetic-access") - @Override - public void run() { - if (!consoleOutput.isDisposed()) { - lblPromptlabel.setText(prompt.apply()); - // relayout - SWTConsole.this.layout(); - } - } - }); + resetPrompt(); } } return command; @@ -377,9 +342,41 @@ public final class SWTConsole extends Composite * @see fr.bigeon.gclc.manager.ConsoleInput#prompt(java.lang.String, long) */ @Override public String prompt(final String message, final long timeout) throws IOException { - // TODO Auto-generated method stub - // return null; - throw new RuntimeException("Not implemented yet"); + synchronized (promptLock) { + initPrompt(message); + try { + final long start = System.currentTimeMillis(); + long cur = start; + while (prompting && start + timeout > cur) { + promptLock.wait((cur - start - timeout) / 2); + cur = System.currentTimeMillis(); + } + if (isDisposed()) { + throw new IOException(); + } + } catch (final InterruptedException e) { + LOGGER.log(Level.WARNING, "Error in synchronization of prompting", e); //$NON-NLS-1$ + command = null; + Thread.currentThread().interrupt(); + } finally { + resetPrompt(); + } + } + return command; + } + + private void resetPrompt() { + Display.getDefault().syncExec(new Runnable() { + @SuppressWarnings("synthetic-access") + @Override + public void run() { + if (!consoleOutput.isDisposed()) { + lblPromptlabel.setText(prompt.apply()); + // relayout + SWTConsole.this.layout(); + } + } + }); } /* (non-Javadoc) @@ -450,6 +447,7 @@ public final class SWTConsole extends Composite promptLock.wait(); } catch (final InterruptedException e) { LOGGER.log(Level.SEVERE, "Interruption while waiting prompt", e); //$NON-NLS-1$ + Thread.currentThread().interrupt(); } } Display.getDefault().syncExec(new Runnable() {