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
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);
}

View File

@ -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")