+ * TODO
+ *
+ * @author Emmanuel Bigeon
+ *
+ */
+public interface ConsoleDelayIO extends ConsoleManager {
+ /** Actually send the input as the prompt next input. */
+ void validateInput();
+
+ /** @param input the input to set */
+ void setInput(String input);
+
+ /** @return the non validated input */
+ String getInput();
+}
diff --git a/gclc-swt/src/main/java/fr/bigeon/gclc/swt/HistoryTextKeyListener.java b/gclc-swt/src/main/java/fr/bigeon/gclc/swt/HistoryTextKeyListener.java
index d595f7b..28913dd 100644
--- a/gclc-swt/src/main/java/fr/bigeon/gclc/swt/HistoryTextKeyListener.java
+++ b/gclc-swt/src/main/java/fr/bigeon/gclc/swt/HistoryTextKeyListener.java
@@ -41,7 +41,6 @@ package fr.bigeon.gclc.swt;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
-import org.eclipse.swt.widgets.Text;
import fr.bigeon.collections.ArrayRibbon;
import fr.bigeon.collections.Ribbon;
@@ -53,21 +52,19 @@ public final class HistoryTextKeyListener extends KeyAdapter {
/** The size of commands history */
private static final int DEFAULT_HISTORY_SIZE = 10;
+ /** The empty string constant */
+ private static final String EMPTY = ""; //$NON-NLS-1$
/** The history ribbon */
private final Ribbon
*
* @author Emmanuel Bigeon */
-public class SWTConsole extends Composite implements ConsoleManager {
+public class SWTConsole extends Composite implements ConsoleDelayIO {
/**
*
*/
@@ -69,6 +68,8 @@ public class SWTConsole extends Composite implements ConsoleManager {
/** The class logger */
private static final Logger LOGGER = Logger
.getLogger(SWTConsole.class.getName());
+ /** The empty string constant */
+ private static final String EMPTY = ""; //$NON-NLS-1$
/** The console output text field */
private final Text consoleOutput;
/** The console input text field */
@@ -112,15 +113,15 @@ public class SWTConsole extends Composite implements ConsoleManager {
consoleInput = new Text(this, SWT.BORDER);
consoleInput.setLayoutData(
new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
- consoleInput
- .addKeyListener(new HistoryTextKeyListener(this, consoleInput));
+ consoleInput.addKeyListener(new HistoryTextKeyListener(this));
}
/**
*
*/
- protected void validateInput() {
+ @Override
+ public void validateInput() {
Display.getDefault().syncExec(new Runnable() {
@SuppressWarnings("synthetic-access")
@Override
@@ -143,7 +144,7 @@ public class SWTConsole extends Composite implements ConsoleManager {
public void run() {
command = consoleInput.getText();
prompting = false;
- consoleInput.setText(new String());
+ consoleInput.setText(EMPTY);
consoleOutput.append(
CMD_PREFIX + command + System.lineSeparator());
}
@@ -336,4 +337,19 @@ public class SWTConsole extends Composite implements ConsoleManager {
validateInput();
}
+ /* (non-Javadoc)
+ * @see fr.bigeon.gclc.swt.ConsoleDelayIO#setInput(java.lang.String) */
+ @Override
+ public void setInput(String input) {
+ consoleInput.setText(input);
+ consoleInput.setSelection(input.length());
+ }
+
+ /* (non-Javadoc)
+ * @see fr.bigeon.gclc.swt.ConsoleDelayIO#getInput() */
+ @Override
+ public String getInput() {
+ return consoleInput.getText();
+ }
+
}
diff --git a/gclc-swt/src/test/java/fr/bigeon/gclc/swt/HistoryTextKeyListenerTest.java b/gclc-swt/src/test/java/fr/bigeon/gclc/swt/HistoryTextKeyListenerTest.java
new file mode 100644
index 0000000..da2bd2b
--- /dev/null
+++ b/gclc-swt/src/test/java/fr/bigeon/gclc/swt/HistoryTextKeyListenerTest.java
@@ -0,0 +1,158 @@
+/*
+ * Copyright E. Bigeon (2015)
+ *
+ * emmanuel@bigeon.fr
+ *
+ * This software is a computer program whose purpose is to
+ * provide a swt window for console applications.
+ *
+ * This software is governed by the CeCILL license under French law and
+ * abiding by the rules of distribution of free software. You can use,
+ * modify and/or redistribute the software under the terms of the CeCILL
+ * license as circulated by CEA, CNRS and INRIA at the following URL
+ * "http://www.cecill.info".
+ *
+ * As a counterpart to the access to the source code and rights to copy,
+ * modify and redistribute granted by the license, users are provided only
+ * with a limited warranty and the software's author, the holder of the
+ * economic rights, and the successive licensors have only limited
+ * liability.
+ *
+ * In this respect, the user's attention is drawn to the risks associated
+ * with loading, using, modifying and/or developing or reproducing the
+ * software by the user in light of its specific status of free software,
+ * that may mean that it is complicated to manipulate, and that also
+ * therefore means that it is reserved for developers and experienced
+ * professionals having in-depth computer knowledge. Users are therefore
+ * encouraged to load and test the software's suitability as regards their
+ * requirements in conditions enabling the security of their systems and/or
+ * data to be ensured and, more generally, to use and operate it in the
+ * same conditions as regards security.
+ *
+ * The fact that you are presently reading this means that you have had
+ * knowledge of the CeCILL license and that you accept its terms.
+ */
+/**
+ * gclc-swt:fr.bigeon.gclc.swt.HistoryTextKeyListenerTest.java
+ * Created on: Nov 19, 2016
+ */
+package fr.bigeon.gclc.swt;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+
+import org.eclipse.swt.SWT;
+import org.junit.Test;
+
+/**
+ * TODO
+ *
+ * @author Emmanuel Bigeon */
+public class HistoryTextKeyListenerTest {
+
+ /** Test method for
+ * {@link fr.bigeon.gclc.swt.HistoryTextKeyListener#keyPressed(org.eclipse.swt.events.KeyEvent)}. */
+ @Test
+ public final void testKeyPressedKeyEvent() {
+ ConsoleDelayIO io = new ConsoleDelayIO() {
+ private String input = "";
+
+ @Override
+ public void setPrompt(String prompt) {
+ //
+ }
+
+ @Override
+ public String prompt(String message) throws IOException {
+ return null;
+ }
+
+ @Override
+ public String prompt() throws IOException {
+ return null;
+ }
+
+ @Override
+ public void println(String message) throws IOException {
+ //
+ }
+
+ @Override
+ public void println() throws IOException {
+ //
+ }
+
+ @Override
+ public void print(String text) throws IOException {
+ //
+ }
+
+ @Override
+ public boolean isClosed() {
+ return false;
+ }
+
+ @Override
+ public String getPrompt() {
+ return null;
+ }
+
+ @Override
+ public void close() throws IOException {
+ //
+ }
+
+ @Override
+ public void validateInput() {
+ input = "";
+ }
+
+ @Override
+ public void setInput(String input) {
+ this.input = input;
+ }
+
+ @Override
+ public String getInput() {
+ return input;
+ }
+ };
+ HistoryTextKeyListener listener = new HistoryTextKeyListener(io);
+
+ // no effects
+ assertEquals("", io.getInput());
+ listener.pressedKeyCode(SWT.ARROW_UP);
+ assertEquals("", io.getInput());
+ listener.pressedKeyCode(SWT.ARROW_DOWN);
+ assertEquals("", io.getInput());
+ listener.pressedKeyCode('\r');
+ assertEquals("", io.getInput());
+
+ io.setInput("cmd arg");
+ listener.pressedKeyCode(SWT.ARROW_UP);
+ assertEquals("cmd arg", io.getInput());
+ listener.pressedKeyCode(SWT.ARROW_DOWN);
+ assertEquals("cmd arg", io.getInput());
+ listener.pressedKeyCode('\r');
+ assertEquals("", io.getInput());
+
+ io.setInput("cmd arg2");
+ listener.pressedKeyCode(SWT.ARROW_UP);
+ assertEquals("cmd arg", io.getInput());
+ listener.pressedKeyCode(SWT.ARROW_DOWN);
+ assertEquals("", io.getInput());
+ io.setInput("cmd arg2");
+ listener.pressedKeyCode('\r');
+ assertEquals("", io.getInput());
+ listener.pressedKeyCode(SWT.ARROW_UP);
+ assertEquals("cmd arg2", io.getInput());
+ listener.pressedKeyCode(SWT.ARROW_UP);
+ assertEquals("cmd arg", io.getInput());
+ listener.pressedKeyCode(SWT.ARROW_UP);
+ assertEquals("cmd arg", io.getInput());
+ listener.pressedKeyCode(SWT.ARROW_DOWN);
+ assertEquals("cmd arg2", io.getInput());
+ }
+
+}