Method factoring

Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
Emmanuel Bigeon 2018-10-15 09:48:10 -04:00
parent 889433d800
commit 977c9a8ec8

View File

@ -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,23 +267,19 @@ public final class SWTConsole extends Composite
return command;
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.manager.ConsoleInput#prompt(long) */
@Override
public String prompt(final long timeout) throws IOException {
synchronized (promptLock) {
private void initPrompt(final String message) throws IOException {
if (isDisposed()) {
throw new IOException();
}
try {
Display.getDefault().syncExec(new Runnable() {
@SuppressWarnings("synthetic-access")
@Override
public void run() {
if (!consoleInput.isDisposed()) {
consoleInput.setEnabled(true);
lblPromptlabel.setText(prompt.apply());
if (!consoleOutput.isDisposed()) {
lblPromptlabel.setText(message);
// relayout
SWTConsole.this.layout();
consoleInput.setEnabled(true);
consoleInput.setFocus();
}
}
@ -307,6 +287,15 @@ public final class SWTConsole extends Composite
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) {
initPrompt(prompt.apply());
try {
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;
while (prompting) {
promptLock.wait();
}
if (isDisposed()) {
throw new IOException();
}
@ -357,6 +332,40 @@ public final class SWTConsole extends Composite
command = null;
Thread.currentThread().interrupt();
} finally {
resetPrompt();
}
}
return command;
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.manager.ConsoleInput#prompt(java.lang.String, long) */
@Override
public String prompt(final String message, final long timeout) throws IOException {
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
@ -369,18 +378,6 @@ public final class SWTConsole extends Composite
}
});
}
}
return command;
}
/* (non-Javadoc)
* @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");
}
/* (non-Javadoc)
* @see org.eclipse.swt.widgets.Composite#setFocus() */
@ -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() {