Changed exit mechanism on gclc
This commit is contained in:
@@ -51,13 +51,18 @@
|
||||
<dependency>
|
||||
<groupId>fr.bigeon</groupId>
|
||||
<artifactId>gclc</artifactId>
|
||||
<version>1.1.2</version>
|
||||
<version>1.2.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.swt.gtk.linux</groupId>
|
||||
<artifactId>x86_64</artifactId>
|
||||
<version>3.3.0-v3346</version>
|
||||
<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>
|
||||
<groupId>fr.bigeon</groupId>
|
||||
<artifactId>collections</artifactId>
|
||||
|
||||
@@ -100,9 +100,9 @@ public class SWTConsole extends Composite implements ConsoleManager {
|
||||
if (e.keyCode == SWT.ARROW_UP &&
|
||||
currentIndex < commands.size() - 1) {
|
||||
currentIndex++;
|
||||
consoleInput.setText(
|
||||
commands.get(commands.size() - currentIndex - 1));
|
||||
consoleInput.setSelection(consoleInput.getText().length());
|
||||
String cmd = commands.get(commands.size() - currentIndex - 1);
|
||||
consoleInput.setText(cmd);
|
||||
consoleInput.setSelection(cmd.length());
|
||||
}
|
||||
|
||||
// Lower arrow retrieves next commands
|
||||
@@ -111,9 +111,10 @@ public class SWTConsole extends Composite implements ConsoleManager {
|
||||
currentIndex--;
|
||||
consoleInput.setText(new String());
|
||||
} else if (currentIndex > 0) {
|
||||
consoleInput.setText(commands
|
||||
.get(commands.size() - (--currentIndex) - 1));
|
||||
consoleInput.setSelection(consoleInput.getText().length());
|
||||
String cmd = commands
|
||||
.get(commands.size() - (--currentIndex) - 1);
|
||||
consoleInput.setText(cmd);
|
||||
consoleInput.setSelection(cmd.length());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -245,8 +246,11 @@ public class SWTConsole extends Composite implements ConsoleManager {
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.ConsoleManager#prompt() */
|
||||
@Override
|
||||
public String prompt() {
|
||||
public String prompt() throws IOException {
|
||||
synchronized (promptLock) {
|
||||
if (isDisposed()) {
|
||||
throw new IOException();
|
||||
}
|
||||
try {
|
||||
Display.getDefault().syncExec(new Runnable() {
|
||||
@SuppressWarnings("synthetic-access")
|
||||
@@ -268,8 +272,11 @@ public class SWTConsole extends Composite implements ConsoleManager {
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.ConsoleManager#prompt(java.lang.String) */
|
||||
@Override
|
||||
public String prompt(final String message) {
|
||||
public String prompt(final String message) throws IOException {
|
||||
synchronized (promptLock) {
|
||||
if (isDisposed()) {
|
||||
throw new IOException();
|
||||
}
|
||||
try {
|
||||
Display.getDefault().syncExec(new Runnable() {
|
||||
@SuppressWarnings("synthetic-access")
|
||||
@@ -284,6 +291,9 @@ public class SWTConsole extends Composite implements ConsoleManager {
|
||||
});
|
||||
prompting = true;
|
||||
promptLock.wait();
|
||||
if (isDisposed()) {
|
||||
throw new IOException();
|
||||
}
|
||||
} catch (final InterruptedException e) {
|
||||
command = null;
|
||||
} finally {
|
||||
@@ -292,7 +302,8 @@ public class SWTConsole extends Composite implements ConsoleManager {
|
||||
@Override
|
||||
public void run() {
|
||||
lblPromptlabel.setText(prompt);
|
||||
lblPromptlabel.pack();
|
||||
// relayout
|
||||
SWTConsole.this.layout();
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -317,7 +328,8 @@ public class SWTConsole extends Composite implements ConsoleManager {
|
||||
@Override
|
||||
public void run() {
|
||||
lblPromptlabel.setText(prompt);
|
||||
lblPromptlabel.pack();
|
||||
// relayout
|
||||
SWTConsole.this.layout();
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -325,9 +337,22 @@ public class SWTConsole extends Composite implements ConsoleManager {
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.manager.ConsoleManager#close() */
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
public void close() {
|
||||
synchronized (promptLock) {
|
||||
promptLock.notify();
|
||||
}
|
||||
if (consoleInput.isDisposed()) {
|
||||
return;
|
||||
}
|
||||
consoleInput.setEnabled(false);
|
||||
consoleOutput.setEnabled(false);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.manager.ConsoleManager#isClosed() */
|
||||
@Override
|
||||
public boolean isClosed() {
|
||||
return isDisposed();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -79,4 +79,12 @@ public class SWTConsoleShell extends Shell {
|
||||
public ConsoleManager getManager() {
|
||||
return console;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.eclipse.swt.widgets.Shell#dispose() */
|
||||
@Override
|
||||
public void dispose() {
|
||||
super.dispose();
|
||||
console.close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,114 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
package fr.bigeon.gclc.swt;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
/** Unit test for simple App. */
|
||||
public class AppTest extends TestCase {
|
||||
protected static final long TWO_SECONDS = 2000;
|
||||
|
||||
/** Create the test case
|
||||
*
|
||||
* @param testName name of the test case */
|
||||
public AppTest(String testName) {
|
||||
super(testName);
|
||||
}
|
||||
|
||||
/** @return the suite of tests being tested */
|
||||
public static Test suite() {
|
||||
return new TestSuite(AppTest.class);
|
||||
}
|
||||
|
||||
/** Rigourous Test :-) */
|
||||
@SuppressWarnings("static-method")
|
||||
public void testApp() {
|
||||
// Display display = new Display();
|
||||
// final Shell shell = new Shell(display);
|
||||
// shell.setLayout(new GridLayout(1, false));
|
||||
// SWTConsole swtConsole = new SWTConsole(shell, SWT.NONE);
|
||||
// swtConsole.setLayoutData(
|
||||
// new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
|
||||
// final ConsoleApplication appl = new ConsoleApplication(swtConsole,
|
||||
// "exit", "Hello", "See you");
|
||||
// try {
|
||||
// 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();
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
// } catch (InvalidCommandName e) {
|
||||
// // TODO Auto-generated catch block
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// shell.pack();
|
||||
// shell.open();
|
||||
// Thread applThread = new Thread(new Runnable() {
|
||||
//
|
||||
// @Override
|
||||
// public void run() {
|
||||
// appl.start();
|
||||
// Display.getDefault().syncExec(new Runnable() {
|
||||
// @Override
|
||||
// public void run() {
|
||||
// shell.dispose();
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
// });
|
||||
// applThread.start();
|
||||
// while (!shell.isDisposed()) {
|
||||
// if (!display.readAndDispatch()) {
|
||||
// display.sleep();
|
||||
// }
|
||||
// }
|
||||
// display.dispose();
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
/**
|
||||
* gclc-swt:fr.bigeon.gclc.swt.SWTConsoleShellTest.java
|
||||
* Created on: Jun 8, 2016
|
||||
*/
|
||||
package fr.bigeon.gclc.swt;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.eclipse.swt.widgets.Display;
|
||||
import org.junit.Test;
|
||||
|
||||
import fr.bigeon.gclc.ConsoleApplication;
|
||||
import fr.bigeon.gclc.command.Command;
|
||||
import fr.bigeon.gclc.exception.InvalidCommandName;
|
||||
import fr.bigeon.gclc.manager.ConsoleManager;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* TODO
|
||||
*
|
||||
* @author Emmanuel Bigeon
|
||||
*
|
||||
*/
|
||||
public class SWTConsoleShellTest {
|
||||
|
||||
protected static final long TWO_SECONDS = 2000;
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
Display display = new Display();
|
||||
final SWTConsoleShell shell = new SWTConsoleShell(display);
|
||||
ConsoleManager swtConsole = shell.getManager();
|
||||
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();
|
||||
}
|
||||
}
|
||||
});
|
||||
// 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() {
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user