Compare commits

..

8 Commits

14 changed files with 474 additions and 127 deletions

View File

@@ -76,6 +76,7 @@ of Emmanuel Bigeon. -->
<url>http://www.bigeon.fr/emmanuel</url> <url>http://www.bigeon.fr/emmanuel</url>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.scm.id>git.bigeon.net</project.scm.id>
</properties> </properties>
<dependencies> <dependencies>
<dependency> <dependency>
@@ -87,7 +88,7 @@ of Emmanuel Bigeon. -->
<dependency> <dependency>
<groupId>fr.bigeon</groupId> <groupId>fr.bigeon</groupId>
<artifactId>gclc</artifactId> <artifactId>gclc</artifactId>
<version>1.2.0</version> <version>1.2.2</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>fr.bigeon</groupId> <groupId>fr.bigeon</groupId>
@@ -102,4 +103,8 @@ of Emmanuel Bigeon. -->
</parent> </parent>
<name>GCLC Socket</name> <name>GCLC Socket</name>
<description>Socket implementation of GCLC</description> <description>Socket implementation of GCLC</description>
<scm>
<developerConnection>scm:git:gogs@git.code.bigeon.net:emmanuel/gclc.git</developerConnection>
<tag>HEAD</tag>
</scm>
</project> </project>

View File

@@ -142,50 +142,57 @@ public class SocketConsoleApplicationShell implements Runnable {
* @see java.lang.Runnable#run() */ * @see java.lang.Runnable#run() */
@Override @Override
public void run() { public void run() {
// Create the server
try (ServerSocket actualServerSocket = new ServerSocket(port)) { try (ServerSocket actualServerSocket = new ServerSocket(port)) {
this.serverSocket = actualServerSocket; this.serverSocket = actualServerSocket;
final ConsoleRunnable runnable = new ConsoleRunnable(app,
promptingLock);
final Thread appTh = new Thread(runnable);
running = true; running = true;
// Create the streams
try (PipedOutputStream outStream = new PipedOutputStream(); try (PipedOutputStream outStream = new PipedOutputStream();
BufferedWriter writer = new BufferedWriter( BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(outStream))) { new OutputStreamWriter(outStream));
InputStreamReader isr = new InputStreamReader(consoleInput);
BufferedReader inBuf = new BufferedReader(isr)) {
consoleInput.connect(outStream); consoleInput.connect(outStream);
try (InputStreamReader isr = new InputStreamReader(
consoleInput);
BufferedReader inBuf = new BufferedReader(isr);) {
consoleManager.setInput(inBuf); consoleManager.setInput(inBuf);
runSokectServer(appTh, writer); runSokectServer(writer);
// Close the application // Close the application
// Pass command to application // Pass command to application
if (app.isRunning()) {
writer.write(applicationShutdown + EOL); writer.write(applicationShutdown + EOL);
writer.flush(); writer.flush();
} }
} }
} catch (final IOException e) { } catch (
final IOException e) {
LOGGER.log(Level.SEVERE, LOGGER.log(Level.SEVERE,
"Communication error between client and server", e); //$NON-NLS-1$ "Communication error between client and server", e); //$NON-NLS-1$
} }
} }
/** @param appTh the application thread /** @param writer the writer to the application
* @param writer the writer to the application
* @throws IOException if the communication with the client failed */ * @throws IOException if the communication with the client failed */
private void runSokectServer(Thread appTh, private void runSokectServer(BufferedWriter writer) throws IOException {
BufferedWriter writer) throws IOException { final ConsoleRunnable runnable = new ConsoleRunnable(app,
promptingLock);
Thread appThOld = null;
Thread appThNext = new Thread(runnable);
while (running) { while (running) {
try (Socket clientSocket = serverSocket.accept(); try (Socket clientSocket = serverSocket.accept();
PrintWriter out = new PrintWriter( PrintWriter out = new PrintWriter(
clientSocket.getOutputStream(), true); clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader( InputStreamReader isr = new InputStreamReader(
clientSocket.getInputStream()));) { clientSocket.getInputStream());
BufferedReader in = new BufferedReader(isr);) {
// this is not threaded to avoid several clients at the same // this is not threaded to avoid several clients at the same
// time // time
consoleManager.setOutput(out); consoleManager.setOutput(out);
// Initiate application // Initiate application
if (!appTh.isAlive()) { if (appThOld == null || !appThOld.isAlive()) {
appTh.start(); appThNext.start();
// Prepare next start
appThOld = appThNext;
appThNext = new Thread(runnable);
} else { } else {
out.println("Reconnected"); //$NON-NLS-1$ out.println("Reconnected"); //$NON-NLS-1$
} }
@@ -249,7 +256,7 @@ public class SocketConsoleApplicationShell implements Runnable {
private void communicateLoop(BufferedReader in, private void communicateLoop(BufferedReader in,
BufferedWriter writer) throws IOException { BufferedWriter writer) throws IOException {
String ln; String ln;
while (running && (ln = in.readLine()) != null) { while (app.isRunning() && (ln = in.readLine()) != null) {
if (ln.equals(close)) { if (ln.equals(close)) {
break; break;
} }

View File

@@ -76,6 +76,23 @@ public class ConsoleTestApplication extends ConsoleApplication {
} }
} }
}); });
add(new Command("long") {
@Override
public String tip() {
return "A long run test command";
}
@Override
public void execute(String... args) throws CommandRunException {
try {
Thread.sleep(2000);
manager.println("Test command ran fine");
} catch (IOException | InterruptedException e) {
throw new CommandRunException("manager closed", e);
}
}
});
} catch (final InvalidCommandName e) { } catch (final InvalidCommandName e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@@ -77,7 +77,7 @@ public class SocketConsoleApplicationTest {
String fromServer; String fromServer;
int i = 0; int i = 0;
String[] cmds = {"help", "test", "close"}; String[] cmds = {"help", "toto", "test", "close"};
while ((fromServer = in.readLine()) != null) { while ((fromServer = in.readLine()) != null) {
System.out.println("Server: \n" + ENCODER.decode(fromServer)); System.out.println("Server: \n" + ENCODER.decode(fromServer));
if (fromServer.equals("Bye.")) { if (fromServer.equals("Bye.")) {

View File

@@ -37,7 +37,7 @@
> >
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>gclc-swt</artifactId> <artifactId>gclc-swt</artifactId>
<version>1.0.2-SNAPSHOT</version> <version>1.0.5-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<url>http://www.bigeon.fr/emmanuel</url> <url>http://www.bigeon.fr/emmanuel</url>
<properties> <properties>
@@ -53,17 +53,7 @@
<dependency> <dependency>
<groupId>fr.bigeon</groupId> <groupId>fr.bigeon</groupId>
<artifactId>gclc</artifactId> <artifactId>gclc</artifactId>
<version>1.2.0</version> <version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.eclipse.swt</groupId>
<artifactId>org.eclipse.swt.gtk.linux.x86_64</artifactId>
<version>4.3</version>
</dependency>
<dependency>
<groupId>org.eclipse.swt</groupId>
<artifactId>org.eclipse.swt.win32.win32.x86_64</artifactId>
<version>4.3</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>fr.bigeon</groupId> <groupId>fr.bigeon</groupId>
@@ -78,4 +68,36 @@
<developerConnection>scm:git:gogs@git.code.bigeon.net:emmanuel/gclc.git</developerConnection> <developerConnection>scm:git:gogs@git.code.bigeon.net:emmanuel/gclc.git</developerConnection>
<tag>HEAD</tag> <tag>HEAD</tag>
</scm> </scm>
<profiles>
<profile>
<id>linux-deps</id>
<activation>
<os>
<family>Linux</family>
</os>
</activation>
<dependencies>
<dependency>
<groupId>org.eclipse.swt</groupId>
<artifactId>org.eclipse.swt.gtk.linux.x86_64</artifactId>
<version>4.3</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>windows-deps</id>
<activation>
<os>
<family>Windows</family>
</os>
</activation>
<dependencies>
<dependency>
<groupId>org.eclipse.swt</groupId>
<artifactId>org.eclipse.swt.win32.win32.x86_64</artifactId>
<version>4.3</version>
</dependency>
</dependencies>
</profile>
</profiles>
</project> </project>

View File

@@ -0,0 +1,111 @@
/*
* 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.HistoryTextKeyListener.java
* Created on: Jun 9, 2016
*/
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;
/** A key listener to validate commands and manage the history of commands
*
* @author Emmanuel Bigeon */
public final class HistoryTextKeyListener extends KeyAdapter {
/** The size of commands history */
private static final int DEFAULT_HISTORY_SIZE = 10;
/** The history ribbon */
private final Ribbon<String> commands;
/** The current index in history search */
private int currentIndex = 0;
/** The console to write the commands in */
private final Text consoleInput;
/** The console to notify of command validation */
private final SWTConsole console;
/** @param console the console
* @param consoleInput the text to write commands in */
public HistoryTextKeyListener(SWTConsole console, Text consoleInput) {
super();
this.console = console;
this.consoleInput = consoleInput;
this.commands = new ArrayRibbon<>(DEFAULT_HISTORY_SIZE);
}
@Override
public void keyPressed(KeyEvent e) {
pressedKeyCode(e.keyCode);
}
/** @param keyCode */
public void pressedKeyCode(int keyCode) {
// Enter validates the command if prompting
if (keyCode == '\r') {
commands.add(consoleInput.getText());
console.validateInput();
currentIndex = -1;
}
// Upper arrow retrieves previous commands
if (keyCode == SWT.ARROW_UP &&
currentIndex < commands.size() - 1) {
currentIndex++;
String cmd = commands.get(commands.size() - currentIndex - 1);
consoleInput.setText(cmd);
consoleInput.setSelection(cmd.length());
}
// Lower arrow retrieves next commands
if (keyCode == SWT.ARROW_DOWN) {
if (currentIndex == 0) {
currentIndex--;
consoleInput.setText(new String());
} else if (currentIndex > 0) {
String cmd = commands
.get(commands.size() - (--currentIndex) - 1);
consoleInput.setText(cmd);
consoleInput.setSelection(cmd.length());
}
}
}
}

View File

@@ -39,12 +39,12 @@
package fr.bigeon.gclc.swt; package fr.bigeon.gclc.swt;
import java.io.IOException; import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.eclipse.swt.SWT; import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusAdapter; import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent; import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Composite;
@@ -52,8 +52,6 @@ import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text; import org.eclipse.swt.widgets.Text;
import fr.bigeon.collections.ArrayRibbon;
import fr.bigeon.collections.Ribbon;
import fr.bigeon.gclc.ConsoleApplication; import fr.bigeon.gclc.ConsoleApplication;
import fr.bigeon.gclc.manager.ConsoleManager; import fr.bigeon.gclc.manager.ConsoleManager;
@@ -62,70 +60,15 @@ import fr.bigeon.gclc.manager.ConsoleManager;
* *
* @author Emmanuel Bigeon */ * @author Emmanuel Bigeon */
public class SWTConsole extends Composite implements ConsoleManager { public class SWTConsole extends Composite implements ConsoleManager {
/** A key listener to validate commands and manage the history of commands
*
* @author Emmanuel Bigeon */
public static final class HistoryTextKeyListener extends KeyAdapter {
/** The size of commands history */
private static final int DEFAULT_HISTORY_SIZE = 10;
/** The history ribbon */
private final Ribbon<String> commands;
/** The current index in history search */
private int currentIndex = 0;
/** The console to write the commands in */
private final Text consoleInput;
/** The console to notify of command validation */
private final SWTConsole console;
/** @param console the console
* @param consoleInput the text to write commands in */
public HistoryTextKeyListener(SWTConsole console, Text consoleInput) {
super();
this.console = console;
this.consoleInput = consoleInput;
this.commands = new ArrayRibbon<>(DEFAULT_HISTORY_SIZE);
}
@Override
public void keyPressed(KeyEvent e) {
// Enter validates the command if prompting
if (e.keyCode == '\r') {
commands.add(consoleInput.getText());
console.validateInput();
currentIndex = -1;
}
// Upper arrow retrieves previous commands
if (e.keyCode == SWT.ARROW_UP &&
currentIndex < commands.size() - 1) {
currentIndex++;
String cmd = commands.get(commands.size() - currentIndex - 1);
consoleInput.setText(cmd);
consoleInput.setSelection(cmd.length());
}
// Lower arrow retrieves next commands
if (e.keyCode == SWT.ARROW_DOWN) {
if (currentIndex == 0) {
currentIndex--;
consoleInput.setText(new String());
} else if (currentIndex > 0) {
String cmd = commands
.get(commands.size() - (--currentIndex) - 1);
consoleInput.setText(cmd);
consoleInput.setSelection(cmd.length());
}
}
}
}
/** /**
* *
*/ */
private static final int LAYOUT_NB_COLUMNS = 2; private static final int LAYOUT_NB_COLUMNS = 2;
/** The cmd prefix in the output console */ /** 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
.getLogger(SWTConsole.class.getName());
/** The console output text field */ /** The console output text field */
private final Text consoleOutput; private final Text consoleOutput;
/** The console input text field */ /** The console input text field */
@@ -169,8 +112,8 @@ public class SWTConsole extends Composite implements ConsoleManager {
consoleInput = new Text(this, SWT.BORDER); consoleInput = new Text(this, SWT.BORDER);
consoleInput.setLayoutData( consoleInput.setLayoutData(
new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1)); new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
consoleInput.addKeyListener( consoleInput
new HistoryTextKeyListener(this, consoleInput)); .addKeyListener(new HistoryTextKeyListener(this, consoleInput));
} }
@@ -178,16 +121,34 @@ public class SWTConsole extends Composite implements ConsoleManager {
* *
*/ */
protected void validateInput() { protected void validateInput() {
if (prompting) { Display.getDefault().syncExec(new Runnable() {
@SuppressWarnings("synthetic-access")
@Override
public void run() {
consoleInput.setEnabled(false);
}
});
synchronized (promptLock) { synchronized (promptLock) {
while (!prompting) {
try {
promptLock.wait();
} catch (InterruptedException e) {
LOGGER.log(Level.SEVERE,
"Interruption while waiting prompt", e); //$NON-NLS-1$
}
}
Display.getDefault().syncExec(new Runnable() {
@SuppressWarnings("synthetic-access")
@Override
public void run() {
command = consoleInput.getText(); command = consoleInput.getText();
prompting = false; prompting = false;
consoleInput.setText(new String()); consoleInput.setText(new String());
consoleInput.setEnabled(false); consoleOutput.append(
consoleOutput CMD_PREFIX + command + System.lineSeparator());
.append(CMD_PREFIX + command + System.lineSeparator());
promptLock.notifyAll();
} }
});
promptLock.notifyAll();
} }
} }
@@ -261,11 +222,15 @@ public class SWTConsole extends Composite implements ConsoleManager {
} }
}); });
prompting = true; prompting = true;
promptLock.notifyAll();
promptLock.wait(); promptLock.wait();
} catch (final InterruptedException e) { } catch (final InterruptedException e) {
command = null; command = null;
} }
} }
if (isDisposed()) {
throw new IOException("Input closed"); //$NON-NLS-1$
}
return command; return command;
} }
@@ -355,4 +320,16 @@ public class SWTConsole extends Composite implements ConsoleManager {
return isDisposed(); return isDisposed();
} }
/** @param string the text */
public void setText(String string) {
consoleInput.setText(string);
}
/**
*
*/
public void validateCommand() {
validateInput();
}
} }

View File

@@ -0,0 +1,57 @@
/*
* 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: Jun 9, 2016
*/
package fr.bigeon.gclc.swt;
import org.junit.Test;
/**
* <p>
* TODO
*
* @author Emmanuel Bigeon
*
*/
public class HistoryTextKeyListenerTest {
@Test
public void test() {
}
}

View File

@@ -41,30 +41,159 @@ package fr.bigeon.gclc.swt;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.io.IOException;
import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Display;
import org.junit.Test; import org.junit.Test;
import fr.bigeon.gclc.ConsoleApplication; import fr.bigeon.gclc.ConsoleApplication;
import fr.bigeon.gclc.command.Command; import fr.bigeon.gclc.command.Command;
import fr.bigeon.gclc.exception.CommandRunException;
import fr.bigeon.gclc.exception.InvalidCommandName; import fr.bigeon.gclc.exception.InvalidCommandName;
import fr.bigeon.gclc.manager.ConsoleManager;
/** /** <p>
* <p>
* TODO * TODO
* *
* @author Emmanuel Bigeon * @author Emmanuel Bigeon */
*
*/
public class SWTConsoleShellTest { public class SWTConsoleShellTest {
protected static final long TWO_SECONDS = 2000; protected static final long TWO_SECONDS = 2000;
private static final Display DISPLAY = new Display();
@Test
public void testConsoleClose() {
final SWTConsoleShell shell = new SWTConsoleShell(DISPLAY);
final SWTConsole swtConsole = (SWTConsole) shell.getManager();
swtConsole.close();
swtConsole.setPrompt(":");
try {
final ConsoleApplication appl = new ConsoleApplication(swtConsole,
"exit", "Hello", "See you");
appl.add(new Command("long") {
@Override
public String tip() {
return "a long running command";
}
@Override
public void execute(String... args) {
try {
Thread.sleep(TWO_SECONDS);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
appl.add(new Command("test") {
@Override
public String tip() {
return "a prompting running command";
}
@Override
public void execute(String... args) throws CommandRunException {
try {
appl.getManager().prompt("Test");
} catch (IOException e) {
throw new CommandRunException("No input", e);
}
}
});
// shell.pack();
shell.open();
Thread applThread = new Thread(new Runnable() {
@Override
public void run() {
appl.start();
}
});
Thread testThread = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(TWO_SECONDS);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
swtConsole.setText("test"); //$NON-NLS-1$
swtConsole.validateCommand();
}
});
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
swtConsole.setText("ok"); //$NON-NLS-1$
}
});
swtConsole.validateCommand();
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
swtConsole.setText("long"); //$NON-NLS-1$
}
});
swtConsole.validateCommand();
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
swtConsole.setText("test"); //$NON-NLS-1$
}
});
swtConsole.validateCommand();
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
swtConsole.setText("test"); //$NON-NLS-1$
}
});
swtConsole.validateCommand();
try {
Thread.sleep(TWO_SECONDS);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
shell.dispose();
}
});
}
});
applThread.start();
testThread.start();
while (!shell.isDisposed()) {
if (!DISPLAY.readAndDispatch()) {
DISPLAY.sleep();
}
}
// DISPLAY.dispose();
assertTrue(appl.getManager().isClosed());
Thread.sleep(TWO_SECONDS);
assertFalse(appl.isRunning());
} catch (InvalidCommandName e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test @Test
public void test() { public void test() {
Display display = new Display(); final SWTConsoleShell shell = new SWTConsoleShell(DISPLAY);
final SWTConsoleShell shell = new SWTConsoleShell(display); final SWTConsole swtConsole = (SWTConsole) shell.getManager();
ConsoleManager swtConsole = shell.getManager();
try { try {
final ConsoleApplication appl = new ConsoleApplication(swtConsole, final ConsoleApplication appl = new ConsoleApplication(swtConsole,
"exit", "Hello", "See you"); "exit", "Hello", "See you");
@@ -98,6 +227,19 @@ public class SWTConsoleShellTest {
@Override @Override
public void run() { public void run() {
try {
Thread.sleep(TWO_SECONDS);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
swtConsole.setText("test"); //$NON-NLS-1$
swtConsole.validateCommand();
}
});
try { try {
Thread.sleep(TWO_SECONDS); Thread.sleep(TWO_SECONDS);
} catch (InterruptedException e) { } catch (InterruptedException e) {
@@ -115,11 +257,11 @@ public class SWTConsoleShellTest {
applThread.start(); applThread.start();
testThread.start(); testThread.start();
while (!shell.isDisposed()) { while (!shell.isDisposed()) {
if (!display.readAndDispatch()) { if (!DISPLAY.readAndDispatch()) {
display.sleep(); DISPLAY.sleep();
} }
} }
// display.dispose(); // DISPLAY.dispose();
assertTrue(appl.getManager().isClosed()); assertTrue(appl.getManager().isClosed());
Thread.sleep(TWO_SECONDS); Thread.sleep(TWO_SECONDS);
assertFalse(appl.isRunning()); assertFalse(appl.isRunning());

View File

@@ -35,7 +35,7 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>gclc</artifactId> <artifactId>gclc</artifactId>
<version>1.2.1-SNAPSHOT</version> <version>1.2.3-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<url>http://www.bigeon.fr/emmanuel</url> <url>http://www.bigeon.fr/emmanuel</url>
<properties> <properties>

View File

@@ -51,11 +51,19 @@ import fr.bigeon.gclc.exception.CommandParsingException;
* @author Emmanuel Bigeon */ * @author Emmanuel Bigeon */
public class GCLCConstants { public class GCLCConstants {
/** The escaping character */
private static final char ESCAPING_CHAR = getSystemEscapingChar();
/** Hide utility class constructor */ /** Hide utility class constructor */
private GCLCConstants() { private GCLCConstants() {
// utility class // utility class
} }
/** @return the escaping character */
private static char getSystemEscapingChar() {
return '\\';
}
/** Splits a command in the diferrent arguments /** Splits a command in the diferrent arguments
* *
* @param cmd the command to split in its parts * @param cmd the command to split in its parts
@@ -71,7 +79,7 @@ public class GCLCConstants {
while (index < cmd.length()) { while (index < cmd.length()) {
char c = cmd.charAt(index); char c = cmd.charAt(index);
index++; index++;
if (escaped || c == '\\') { if (escaped || c == ESCAPING_CHAR) {
escaped = !escaped; escaped = !escaped;
continue; continue;
} }
@@ -102,11 +110,11 @@ public class GCLCConstants {
private static String removeEscaped(String arg) { private static String removeEscaped(String arg) {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
int index = 0; int index = 0;
int endIndex = arg.indexOf('\\'); int endIndex = arg.indexOf(ESCAPING_CHAR);
while (endIndex != -1) { while (endIndex != -1) {
builder.append(arg.subSequence(index, endIndex)); builder.append(arg.subSequence(index, endIndex));
index = endIndex + 1; index = endIndex + 1;
endIndex = arg.indexOf('\\', index + 1); endIndex = arg.indexOf(ESCAPING_CHAR, index + 1);
} }
builder.append(arg.substring(index)); builder.append(arg.substring(index));
return builder.toString(); return builder.toString();

View File

@@ -83,7 +83,7 @@ public class CommandProvider implements ICommandProvider {
} }
} }
throw new CommandRunException( throw new CommandRunException(
Messages.getString("CommandProvider.unrecognized0", cmd)); //$NON-NLS-1$ Messages.getString("CommandProvider.unrecognized", cmd)); //$NON-NLS-1$
} }
/* (non-Javadoc) /* (non-Javadoc)

View File

@@ -1,2 +1,2 @@
CommandProvider.unrecognized=Unrecognized command '{0}' CommandProvider.unrecognized=Unrecognized command "{0}"
ConsoleApplication.cmd.failed=The command '{0}' failed due to : ConsoleApplication.cmd.failed=The command "{0}" failed due to :

View File

@@ -76,7 +76,7 @@ public class ConsoleApplicationTest {
final PipedInputStream snk = new PipedInputStream(); final PipedInputStream snk = new PipedInputStream();
PrintStream out = new PrintStream(new PipedOutputStream(snk)); PrintStream out = new PrintStream(new PipedOutputStream(snk));
final ConsoleTestApplication app = new ConsoleTestApplication( final ConsoleTestApplication app = new ConsoleTestApplication(
new SystemConsoleManager(out, in)); new SystemConsoleManager(System.out, in));
Thread th = new Thread(new Runnable() { Thread th = new Thread(new Runnable() {
@Override @Override
@@ -93,6 +93,7 @@ public class ConsoleApplicationTest {
public void run() { public void run() {
try (PrintWriter writer = new PrintWriter(src, true)) { try (PrintWriter writer = new PrintWriter(src, true)) {
writer.println("test"); writer.println("test");
writer.println("toto");
writer.println("long"); writer.println("long");
writer.println("exit"); writer.println("exit");
} }