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 @Override
public String prompt() throws IOException { public String prompt() throws IOException {
synchronized (promptLock) { synchronized (promptLock) {
if (isDisposed()) { initPrompt(prompt.apply());
throw new IOException();
}
try { 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) { while (prompting) {
promptLock.wait(); promptLock.wait();
} }
@ -283,30 +267,35 @@ public final class SWTConsole extends Composite
return command; return command;
} }
private void initPrompt(final String message) throws IOException {
if (isDisposed()) {
throw new IOException();
}
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;
command = null;
promptLock.notifyAll();
}
/* (non-Javadoc) /* (non-Javadoc)
* @see fr.bigeon.gclc.manager.ConsoleInput#prompt(long) */ * @see fr.bigeon.gclc.manager.ConsoleInput#prompt(long) */
@Override @Override
public String prompt(final long timeout) throws IOException { public String prompt(final long timeout) throws IOException {
synchronized (promptLock) { synchronized (promptLock) {
if (isDisposed()) { initPrompt(prompt.apply());
throw new IOException();
}
try { 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;
command = null;
promptLock.notifyAll();
final long start = System.currentTimeMillis(); final long start = System.currentTimeMillis();
long cur = start; long cur = start;
while (prompting && start + timeout > cur) { while (prompting && start + timeout > cur) {
@ -330,25 +319,11 @@ public final class SWTConsole extends Composite
@Override @Override
public String prompt(final String message) throws IOException { public String prompt(final String message) throws IOException {
synchronized (promptLock) { synchronized (promptLock) {
if (isDisposed()) { initPrompt(message);
throw new IOException();
}
try { try {
Display.getDefault().syncExec(new Runnable() { while (prompting) {
@SuppressWarnings("synthetic-access") promptLock.wait();
@Override }
public void run() {
if (!consoleOutput.isDisposed()) {
lblPromptlabel.setText(message);
// relayout
SWTConsole.this.layout();
consoleInput.setEnabled(true);
consoleInput.setFocus();
}
}
});
prompting = true;
promptLock.wait();
if (isDisposed()) { if (isDisposed()) {
throw new IOException(); throw new IOException();
} }
@ -357,17 +332,7 @@ public final class SWTConsole extends Composite
command = null; command = null;
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
} finally { } finally {
Display.getDefault().syncExec(new Runnable() { resetPrompt();
@SuppressWarnings("synthetic-access")
@Override
public void run() {
if (!consoleOutput.isDisposed()) {
lblPromptlabel.setText(prompt.apply());
// relayout
SWTConsole.this.layout();
}
}
});
} }
} }
return command; return command;
@ -377,9 +342,41 @@ public final class SWTConsole extends Composite
* @see fr.bigeon.gclc.manager.ConsoleInput#prompt(java.lang.String, long) */ * @see fr.bigeon.gclc.manager.ConsoleInput#prompt(java.lang.String, long) */
@Override @Override
public String prompt(final String message, final long timeout) throws IOException { public String prompt(final String message, final long timeout) throws IOException {
// TODO Auto-generated method stub synchronized (promptLock) {
// return null; initPrompt(message);
throw new RuntimeException("Not implemented yet"); 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
public void run() {
if (!consoleOutput.isDisposed()) {
lblPromptlabel.setText(prompt.apply());
// relayout
SWTConsole.this.layout();
}
}
});
} }
/* (non-Javadoc) /* (non-Javadoc)
@ -450,6 +447,7 @@ public final class SWTConsole extends Composite
promptLock.wait(); promptLock.wait();
} catch (final InterruptedException e) { } catch (final InterruptedException e) {
LOGGER.log(Level.SEVERE, "Interruption while waiting prompt", e); //$NON-NLS-1$ LOGGER.log(Level.SEVERE, "Interruption while waiting prompt", e); //$NON-NLS-1$
Thread.currentThread().interrupt();
} }
} }
Display.getDefault().syncExec(new Runnable() { Display.getDefault().syncExec(new Runnable() {