From dc988a07acedc181023f9db2872b7dbdbf3259a3 Mon Sep 17 00:00:00 2001 From: Emmanuel Bigeon Date: Mon, 15 Oct 2018 10:57:47 -0400 Subject: [PATCH] Clean up. Signed-off-by: Emmanuel Bigeon --- .../gclc/swt/HistoryTextKeyListener.java | 6 +++--- .../java/net/bigeon/gclc/swt/SWTConsole.java | 19 ++++++++++++++----- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/gclc-swt/src/main/java/net/bigeon/gclc/swt/HistoryTextKeyListener.java b/gclc-swt/src/main/java/net/bigeon/gclc/swt/HistoryTextKeyListener.java index 2080d85..4535154 100644 --- a/gclc-swt/src/main/java/net/bigeon/gclc/swt/HistoryTextKeyListener.java +++ b/gclc-swt/src/main/java/net/bigeon/gclc/swt/HistoryTextKeyListener.java @@ -131,10 +131,10 @@ public final class HistoryTextKeyListener extends KeyAdapter { // Lower arrow retrieves next commands if (keyCode == SWT.ARROW_DOWN) { - if (currentIndex == 0) { - currentIndex--; + if (currentIndex <= 0) { + currentIndex=-1; console.setInput(EMPTY); - } else if (currentIndex > 0) { + } else { final String cmd = commands.get(commands.size() - (--currentIndex) - 1); console.setInput(cmd); } 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 9842bd4..6967ea5 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 @@ -92,6 +92,7 @@ import net.bigeon.gclc.tools.StringProvider; * @author Emmanuel Bigeon */ public final class SWTConsole extends Composite implements ConsoleDelayIO, ConsoleInput, ConsoleOutput { + private static final String ERROR_SYNCHRO_PROMPT = "Error in synchronization of prompting"; /** The number of columns of the layout. */ private static final int LAYOUT_NB_COLUMNS = 2; /** The cmd prefix in the output console. */ @@ -251,12 +252,13 @@ public final class SWTConsole extends Composite public String prompt() throws IOException { synchronized (promptLock) { initPrompt(prompt.apply()); + promptLock.notifyAll(); try { while (prompting) { promptLock.wait(); } } catch (final InterruptedException e) { - LOGGER.log(Level.WARNING, "Error in synchronization of prompting", e); //$NON-NLS-1$ + LOGGER.log(Level.WARNING, ERROR_SYNCHRO_PROMPT, e); //$NON-NLS-1$ command = null; Thread.currentThread().interrupt(); } @@ -267,6 +269,10 @@ public final class SWTConsole extends Composite return command; } + /** Initialize the prompting. + * + * @param message the prompt message + * @throws IOException if the console is disposed */ private void initPrompt(final String message) throws IOException { if (isDisposed()) { throw new IOException(); @@ -286,7 +292,6 @@ public final class SWTConsole extends Composite }); prompting = true; command = null; - promptLock.notifyAll(); } /* (non-Javadoc) @@ -295,6 +300,7 @@ public final class SWTConsole extends Composite public String prompt(final long timeout) throws IOException { synchronized (promptLock) { initPrompt(prompt.apply()); + promptLock.notifyAll(); try { final long start = System.currentTimeMillis(); long cur = start; @@ -303,7 +309,7 @@ public final class SWTConsole extends Composite cur = System.currentTimeMillis(); } } catch (final InterruptedException e) { - LOGGER.log(Level.WARNING, "Error in synchronization of prompting", e); //$NON-NLS-1$ + LOGGER.log(Level.WARNING, ERROR_SYNCHRO_PROMPT, e); //$NON-NLS-1$ command = null; Thread.currentThread().interrupt(); } @@ -320,6 +326,7 @@ public final class SWTConsole extends Composite public String prompt(final String message) throws IOException { synchronized (promptLock) { initPrompt(message); + promptLock.notifyAll(); try { while (prompting) { promptLock.wait(); @@ -328,7 +335,7 @@ public final class SWTConsole extends Composite throw new IOException(); } } catch (final InterruptedException e) { - LOGGER.log(Level.WARNING, "Error in synchronization of prompting", e); //$NON-NLS-1$ + LOGGER.log(Level.WARNING, ERROR_SYNCHRO_PROMPT, e); //$NON-NLS-1$ command = null; Thread.currentThread().interrupt(); } finally { @@ -344,6 +351,7 @@ public final class SWTConsole extends Composite public String prompt(final String message, final long timeout) throws IOException { synchronized (promptLock) { initPrompt(message); + promptLock.notifyAll(); try { final long start = System.currentTimeMillis(); long cur = start; @@ -355,7 +363,7 @@ public final class SWTConsole extends Composite throw new IOException(); } } catch (final InterruptedException e) { - LOGGER.log(Level.WARNING, "Error in synchronization of prompting", e); //$NON-NLS-1$ + LOGGER.log(Level.WARNING, ERROR_SYNCHRO_PROMPT, e); //$NON-NLS-1$ command = null; Thread.currentThread().interrupt(); } finally { @@ -365,6 +373,7 @@ public final class SWTConsole extends Composite return command; } + /** Reset the prompt message. */ private void resetPrompt() { Display.getDefault().syncExec(new Runnable() { @SuppressWarnings("synthetic-access")