Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
Emmanuel Bigeon 2018-10-15 12:19:09 -04:00
parent d7ecd75678
commit d49a2474e0
2 changed files with 49 additions and 46 deletions

View File

@ -137,7 +137,8 @@ public final class HistoryTextKeyListener extends KeyAdapter {
}
currentIndex = -1;
} else {
final String cmd = commands.get(commands.size() - (--currentIndex) - 1);
currentIndex--;
final String cmd = commands.get(commands.size() - currentIndex - 1);
console.setInput(cmd);
}
}

View File

@ -92,30 +92,32 @@ import net.bigeon.gclc.tools.StringProvider;
* @author Emmanuel Bigeon */
public final class SWTConsole extends Composite
implements ConsoleDelayIO, ConsoleInput, ConsoleOutput {
private static final int TWO = 2;
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;
private static final int LAYOUT_NB_COLUMNS = 2;
/** The cmd prefix in the output console. */
private static final String CMD_PREFIX = "[CMD] "; //$NON-NLS-1$
private static final String CMD_PREFIX = "[CMD] "; //$NON-NLS-1$
/** The class logger. */
private static final Logger LOGGER = Logger
private static final Logger LOGGER = Logger
.getLogger(SWTConsole.class.getName());
/** The empty string constant. */
private static final String EMPTY = ""; //$NON-NLS-1$
private static final String EMPTY = ""; //$NON-NLS-1$
/** The console output text field. */
private final Text consoleOutput;
private final Text consoleOutput;
/** The console input text field. */
private final Text consoleInput;
private final Text consoleInput;
/** The prompt label. */
private final Label lblPromptlabel;
private final Label lblPromptlabel;
/** The prompt text. */
private StringProvider prompt = new ConstantString("> "); //$NON-NLS-1$
private StringProvider prompt = new ConstantString("> "); //$NON-NLS-1$
/** The command entered by the user. */
private String command = null;
private String command = null;
/** If the prompt should be active. */
private boolean prompting = false;
private boolean prompting = false;
/** The object for thread synchronization with the prompt. */
private final Object promptLock = new Object();
private final Object promptLock = new Object();
/** Create the composite.
*
@ -141,6 +143,8 @@ public final class SWTConsole extends Composite
}
/* (non-Javadoc)
* @see org.eclipse.swt.widgets.Composite#checkSubclass() */
@Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
@ -258,7 +262,7 @@ public final class SWTConsole extends Composite
promptLock.wait();
}
} catch (final InterruptedException e) {
LOGGER.log(Level.WARNING, ERROR_SYNCHRO_PROMPT, e); //$NON-NLS-1$
LOGGER.log(Level.WARNING, ERROR_SYNCHRO_PROMPT, e);
command = null;
Thread.currentThread().interrupt();
}
@ -302,21 +306,13 @@ public final class SWTConsole extends Composite
initPrompt(prompt.apply());
promptLock.notifyAll();
try {
final long start = System.currentTimeMillis();
long cur = start;
while (prompting && start + timeout > cur) {
promptLock.wait((cur - start - timeout) / 2);
cur = System.currentTimeMillis();
}
waitPromptResolution(timeout);
} catch (final InterruptedException e) {
LOGGER.log(Level.WARNING, ERROR_SYNCHRO_PROMPT, e); //$NON-NLS-1$
LOGGER.log(Level.WARNING, ERROR_SYNCHRO_PROMPT, e);
command = null;
Thread.currentThread().interrupt();
}
}
if (isDisposed()) {
throw new IOException("Input closed"); //$NON-NLS-1$
}
return command;
}
@ -335,7 +331,7 @@ public final class SWTConsole extends Composite
throw new IOException();
}
} catch (final InterruptedException e) {
LOGGER.log(Level.WARNING, ERROR_SYNCHRO_PROMPT, e); //$NON-NLS-1$
LOGGER.log(Level.WARNING, ERROR_SYNCHRO_PROMPT, e);
command = null;
Thread.currentThread().interrupt();
} finally {
@ -353,17 +349,9 @@ public final class SWTConsole extends Composite
initPrompt(message);
promptLock.notifyAll();
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();
}
waitPromptResolution(timeout);
} catch (final InterruptedException e) {
LOGGER.log(Level.WARNING, ERROR_SYNCHRO_PROMPT, e); //$NON-NLS-1$
LOGGER.log(Level.WARNING, ERROR_SYNCHRO_PROMPT, e);
command = null;
Thread.currentThread().interrupt();
} finally {
@ -373,6 +361,24 @@ public final class SWTConsole extends Composite
return command;
}
/** Wait the prompt resolution by user.
*
* @param timeout the timeout for the wait
* @throws InterruptedException if the thread is interrupted
* @throws IOException if the console is disposed. */
private void waitPromptResolution(final long timeout)
throws InterruptedException, IOException {
final long start = System.currentTimeMillis();
long cur = start;
while (prompting && start + timeout > cur) {
promptLock.wait((cur - start - timeout) / TWO);
cur = System.currentTimeMillis();
}
if (isDisposed()) {
throw new IOException();
}
}
/** Reset the prompt message. */
private void resetPrompt() {
Display.getDefault().syncExec(new Runnable() {
@ -403,8 +409,10 @@ public final class SWTConsole extends Composite
consoleInput.setSelection(input.length());
}
/* (non-Javadoc)
* @see net.bigeon.gclc.manager.ConsoleInput#setPrompt(java.lang.String) */
@Override
public void setPrompt(String prompt) {
public void setPrompt(final String prompt) {
setPrompt(new ConstantString(prompt));
}
@ -426,21 +434,15 @@ public final class SWTConsole extends Composite
});
}
/** @param string the text */
/** Set the input text.
*
* @param string the text */
public void setText(final String string) {
consoleInput.setText(string);
}
/**
*
*/
public void validateCommand() {
validateInput();
}
/**
*
*/
/* (non-Javadoc)
* @see net.bigeon.gclc.swt.ConsoleDelayIO#validateInput() */
@Override
public void validateInput() {
Display.getDefault().syncExec(new Runnable() {