Clean up.

Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
Emmanuel Bigeon 2018-10-15 10:57:47 -04:00
parent 4d31bbacbf
commit dc988a07ac
2 changed files with 17 additions and 8 deletions

View File

@ -131,10 +131,10 @@ public final class HistoryTextKeyListener extends KeyAdapter {
// Lower arrow retrieves next commands // Lower arrow retrieves next commands
if (keyCode == SWT.ARROW_DOWN) { if (keyCode == SWT.ARROW_DOWN) {
if (currentIndex == 0) { if (currentIndex <= 0) {
currentIndex--; currentIndex=-1;
console.setInput(EMPTY); console.setInput(EMPTY);
} else if (currentIndex > 0) { } else {
final String cmd = commands.get(commands.size() - (--currentIndex) - 1); final String cmd = commands.get(commands.size() - (--currentIndex) - 1);
console.setInput(cmd); console.setInput(cmd);
} }

View File

@ -92,6 +92,7 @@ import net.bigeon.gclc.tools.StringProvider;
* @author Emmanuel Bigeon */ * @author Emmanuel Bigeon */
public final class SWTConsole extends Composite public final class SWTConsole extends Composite
implements ConsoleDelayIO, ConsoleInput, ConsoleOutput { implements ConsoleDelayIO, ConsoleInput, ConsoleOutput {
private static final String ERROR_SYNCHRO_PROMPT = "Error in synchronization of prompting";
/** The number of columns of the layout. */ /** The number of columns of the layout. */
private static final int LAYOUT_NB_COLUMNS = 2; private static final int LAYOUT_NB_COLUMNS = 2;
/** The cmd prefix in the output console. */ /** The cmd prefix in the output console. */
@ -251,12 +252,13 @@ public final class SWTConsole extends Composite
public String prompt() throws IOException { public String prompt() throws IOException {
synchronized (promptLock) { synchronized (promptLock) {
initPrompt(prompt.apply()); initPrompt(prompt.apply());
promptLock.notifyAll();
try { try {
while (prompting) { while (prompting) {
promptLock.wait(); promptLock.wait();
} }
} catch (final InterruptedException e) { } 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; command = null;
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
} }
@ -267,6 +269,10 @@ public final class SWTConsole extends Composite
return command; 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 { private void initPrompt(final String message) throws IOException {
if (isDisposed()) { if (isDisposed()) {
throw new IOException(); throw new IOException();
@ -286,7 +292,6 @@ public final class SWTConsole extends Composite
}); });
prompting = true; prompting = true;
command = null; command = null;
promptLock.notifyAll();
} }
/* (non-Javadoc) /* (non-Javadoc)
@ -295,6 +300,7 @@ public final class SWTConsole extends Composite
public String prompt(final long timeout) throws IOException { public String prompt(final long timeout) throws IOException {
synchronized (promptLock) { synchronized (promptLock) {
initPrompt(prompt.apply()); initPrompt(prompt.apply());
promptLock.notifyAll();
try { try {
final long start = System.currentTimeMillis(); final long start = System.currentTimeMillis();
long cur = start; long cur = start;
@ -303,7 +309,7 @@ public final class SWTConsole extends Composite
cur = System.currentTimeMillis(); cur = System.currentTimeMillis();
} }
} catch (final InterruptedException e) { } 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; command = null;
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
} }
@ -320,6 +326,7 @@ public final class SWTConsole extends Composite
public String prompt(final String message) throws IOException { public String prompt(final String message) throws IOException {
synchronized (promptLock) { synchronized (promptLock) {
initPrompt(message); initPrompt(message);
promptLock.notifyAll();
try { try {
while (prompting) { while (prompting) {
promptLock.wait(); promptLock.wait();
@ -328,7 +335,7 @@ public final class SWTConsole extends Composite
throw new IOException(); throw new IOException();
} }
} catch (final InterruptedException e) { } 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; command = null;
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
} finally { } finally {
@ -344,6 +351,7 @@ public final class SWTConsole extends Composite
public String prompt(final String message, final long timeout) throws IOException { public String prompt(final String message, final long timeout) throws IOException {
synchronized (promptLock) { synchronized (promptLock) {
initPrompt(message); initPrompt(message);
promptLock.notifyAll();
try { try {
final long start = System.currentTimeMillis(); final long start = System.currentTimeMillis();
long cur = start; long cur = start;
@ -355,7 +363,7 @@ public final class SWTConsole extends Composite
throw new IOException(); throw new IOException();
} }
} catch (final InterruptedException e) { } 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; command = null;
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
} finally { } finally {
@ -365,6 +373,7 @@ public final class SWTConsole extends Composite
return command; return command;
} }
/** Reset the prompt message. */
private void resetPrompt() { private void resetPrompt() {
Display.getDefault().syncExec(new Runnable() { Display.getDefault().syncExec(new Runnable() {
@SuppressWarnings("synthetic-access") @SuppressWarnings("synthetic-access")