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