gclc clean up. socket and swt configured for current stable gclc

gclc items moved around. gclc ready for release.
gclc internationalization
gclc extract command interface
socket server issue (close socket) "fixed"
swt minor improvments (user con't input commands while the application
is actually running one, the history keeps track of mispelled commands
to).
This commit is contained in:
2016-06-02 12:50:36 -04:00
parent 87a668d308
commit 1ffe321898
34 changed files with 2139 additions and 1100 deletions

View File

@@ -61,7 +61,7 @@
<dependency>
<groupId>fr.bigeon</groupId>
<artifactId>collections</artifactId>
<version>1.0.1-SNAPSHOT</version>
<version>1.0.1</version>
</dependency>
</dependencies>
<inceptionYear>2015</inceptionYear>

View File

@@ -52,15 +52,75 @@ import org.eclipse.swt.widgets.Text;
import fr.bigeon.collections.ArrayRibbon;
import fr.bigeon.collections.Ribbon;
import fr.bigeon.gclc.CommandRequestListener;
import fr.bigeon.gclc.ConsoleApplication;
import fr.bigeon.gclc.ConsoleManager;
/** A SWT component to connect to gclc {@link ConsoleApplication}
* <p>
*
*
* @author Emmanuel Bigeon */
public class SWTConsole extends Composite implements ConsoleManager, CommandRequestListener {
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++;
consoleInput.setText(
commands.get(commands.size() - currentIndex - 1));
consoleInput.setSelection(consoleInput.getText().length());
}
// Lower arrow retrieves next commands
if (e.keyCode == SWT.ARROW_DOWN) {
if (currentIndex == 0) {
currentIndex--;
consoleInput.setText(new String());
} else if (currentIndex > 0) {
consoleInput.setText(commands
.get(commands.size() - (--currentIndex) - 1));
consoleInput.setSelection(consoleInput.getText().length());
}
}
}
}
/**
*
*/
private static final int LAYOUT_NB_COLUMNS = 2;
/** The cmd prefix in the output console */
private static final String CMD_PREFIX = "[CMD] "; //$NON-NLS-1$
/** The console output text field */
@@ -78,25 +138,19 @@ public class SWTConsole extends Composite implements ConsoleManager, CommandRequ
/** The object for thread synchronization with the prompt */
private final Object promptLock = new Object();
/** The ribbon of commands */
private final Ribbon<String> commands;
/** The current index in the ribbon */
private int currentIndex = 0;
/** Create the composite.
*
*
* @param parent the prent composite
* @param style the composite style */
public SWTConsole(Composite parent, int style) {
super(parent, style);
// inner elements
commands = new ArrayRibbon<>(10);
setLayout(new GridLayout(LAYOUT_NB_COLUMNS, false));
setLayout(new GridLayout(2, false));
consoleOutput = new Text(this, SWT.BORDER | SWT.READ_ONLY | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
consoleOutput.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
consoleOutput = new Text(this, SWT.BORDER | SWT.READ_ONLY | SWT.WRAP |
SWT.V_SCROLL | SWT.MULTI);
consoleOutput.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true,
LAYOUT_NB_COLUMNS, 1));
consoleOutput.setRedraw(true);
consoleOutput.addFocusListener(new FocusAdapter() {
@SuppressWarnings("synthetic-access")
@@ -110,45 +164,28 @@ public class SWTConsole extends Composite implements ConsoleManager, CommandRequ
lblPromptlabel.setText(prompt);
consoleInput = new Text(this, SWT.BORDER);
consoleInput.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
consoleInput.addKeyListener(new KeyAdapter() {
@SuppressWarnings("synthetic-access")
@Override
public void keyPressed(KeyEvent e) {
// Enter validates the command if prompting
if (e.keyCode == '\r' && prompting) {
synchronized (promptLock) {
command = consoleInput.getText();
prompting = false;
consoleInput.setText(new String());
consoleOutput.append(CMD_PREFIX + command + System.lineSeparator());
currentIndex = -1;
promptLock.notifyAll();
}
}
consoleInput.setLayoutData(
new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
consoleInput.addKeyListener(
new HistoryTextKeyListener(this, consoleInput));
// Upper arrow retrieves previous commands
if (e.keyCode == SWT.ARROW_UP) {
if (currentIndex < commands.size() - 1) {
currentIndex++;
consoleInput.setText(commands.get(commands.size() - currentIndex - 1));
consoleInput.setSelection(consoleInput.getText().length());
}
}
}
// Lower arrow retrieves next commands
if (e.keyCode == SWT.ARROW_DOWN) {
if (currentIndex == 0) {
currentIndex--;
consoleInput.setText(new String());
} else if (currentIndex > 0) {
consoleInput.setText(commands.get(commands.size() - (--currentIndex) - 1));
consoleInput.setSelection(consoleInput.getText().length());
}
}
/**
*
*/
protected void validateInput() {
if (prompting) {
synchronized (promptLock) {
command = consoleInput.getText();
prompting = false;
consoleInput.setText(new String());
consoleInput.setEnabled(false);
consoleOutput
.append(CMD_PREFIX + command + System.lineSeparator());
promptLock.notifyAll();
}
});
}
}
@Override
@@ -156,21 +193,6 @@ public class SWTConsole extends Composite implements ConsoleManager, CommandRequ
// Disable the check that prevents subclassing of SWT components
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.ConsoleManager#setPrompt(java.lang.String) */
@Override
public void setPrompt(final String prompt) {
this.prompt = prompt;
Display.getDefault().syncExec(new Runnable() {
@SuppressWarnings("synthetic-access")
@Override
public void run() {
lblPromptlabel.setText(prompt);
lblPromptlabel.pack();
}
});
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.ConsoleManager#getPrompt() */
@Override
@@ -178,76 +200,6 @@ public class SWTConsole extends Composite implements ConsoleManager, CommandRequ
return prompt;
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.ConsoleManager#prompt() */
@Override
public String prompt() {
synchronized (promptLock) {
try {
Display.getDefault().syncExec(new Runnable() {
@SuppressWarnings("synthetic-access")
@Override
public void run() {
consoleInput.setFocus();
}
});
prompting = true;
promptLock.wait();
} catch (InterruptedException e) {
command = null;
}
}
return command;
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.ConsoleManager#prompt(java.lang.String) */
@Override
public String prompt(final String message) {
synchronized (promptLock) {
try {
Display.getDefault().syncExec(new Runnable() {
@SuppressWarnings("synthetic-access")
@Override
public void run() {
lblPromptlabel.setText(message);
// relayout
SWTConsole.this.layout();
consoleInput.setFocus();
}
});
prompting = true;
promptLock.wait();
} catch (InterruptedException e) {
command = null;
} finally {
Display.getDefault().syncExec(new Runnable() {
@SuppressWarnings("synthetic-access")
@Override
public void run() {
lblPromptlabel.setText(prompt);
lblPromptlabel.pack();
}
});
}
}
return command;
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.ConsoleManager#println(java.lang.String) */
@Override
public void println(final String message) {
Display.getDefault().syncExec(new Runnable() {
@SuppressWarnings("synthetic-access")
@Override
public void run() {
consoleOutput.append(message + System.lineSeparator());
}
});
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.ConsoleManager#print(java.lang.String) */
@Override
@@ -274,6 +226,78 @@ public class SWTConsole extends Composite implements ConsoleManager, CommandRequ
});
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.ConsoleManager#println(java.lang.String) */
@Override
public void println(final String message) {
Display.getDefault().syncExec(new Runnable() {
@SuppressWarnings("synthetic-access")
@Override
public void run() {
consoleOutput.append(message + System.lineSeparator());
}
});
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.ConsoleManager#prompt() */
@Override
public String prompt() {
synchronized (promptLock) {
try {
Display.getDefault().syncExec(new Runnable() {
@SuppressWarnings("synthetic-access")
@Override
public void run() {
consoleInput.setEnabled(true);
consoleInput.setFocus();
}
});
prompting = true;
promptLock.wait();
} catch (final InterruptedException e) {
command = null;
}
}
return command;
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.ConsoleManager#prompt(java.lang.String) */
@Override
public String prompt(final String message) {
synchronized (promptLock) {
try {
Display.getDefault().syncExec(new Runnable() {
@SuppressWarnings("synthetic-access")
@Override
public void run() {
lblPromptlabel.setText(message);
// relayout
SWTConsole.this.layout();
consoleInput.setEnabled(true);
consoleInput.setFocus();
}
});
prompting = true;
promptLock.wait();
} catch (final InterruptedException e) {
command = null;
} finally {
Display.getDefault().syncExec(new Runnable() {
@SuppressWarnings("synthetic-access")
@Override
public void run() {
lblPromptlabel.setText(prompt);
lblPromptlabel.pack();
}
});
}
}
return command;
}
/* (non-Javadoc)
* @see org.eclipse.swt.widgets.Composite#setFocus() */
@Override
@@ -282,11 +306,18 @@ public class SWTConsole extends Composite implements ConsoleManager, CommandRequ
}
/* (non-Javadoc)
* @see
* fr.bigeon.gclc.CommandRequestListener#commandRequest(java.lang.String) */
* @see fr.bigeon.gclc.ConsoleManager#setPrompt(java.lang.String) */
@Override
public void commandRequest(String request) {
commands.add(request);
public void setPrompt(final String prompt) {
this.prompt = prompt;
Display.getDefault().syncExec(new Runnable() {
@SuppressWarnings("synthetic-access")
@Override
public void run() {
lblPromptlabel.setText(prompt);
lblPromptlabel.pack();
}
});
}
}

View File

@@ -43,49 +43,40 @@ import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import fr.bigeon.gclc.CommandRequestListener;
import fr.bigeon.gclc.ConsoleManager;
/** A shell containing a {@link SWTConsole}
* <p>
*
*
* @author Emmanuel Bigeon */
public class SWTConsoleShell extends Shell {
/** The console component */
private final SWTConsole console;
private SWTConsole console;
/** Create the shell.
*
*
* @param display the display */
public SWTConsoleShell(Display display) {
super(display, SWT.SHELL_TRIM);
setLayout(new FillLayout(SWT.HORIZONTAL));
console = new SWTConsole(this, SWT.NONE);
createContents();
}
/**
* Create contents of the shell.
*/
protected void createContents() {
setText("Console Application"); //$NON-NLS-1$
setSize(450, 300);
}
@Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}
/** Create contents of the shell. */
protected void createContents() {
console = new SWTConsole(this, SWT.NONE);
setText("Console Application"); //$NON-NLS-1$
}
/** @return the console manager */
public ConsoleManager getManager() {
return console;
}
/** @return the element awaiting commands */
public CommandRequestListener getCommandListener() {
return console;
}
}

View File

@@ -38,45 +38,55 @@ import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
/** 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 );
* @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 );
/** @return the suite of tests being tested */
public static Test suite() {
return new TestSuite(AppTest.class);
}
/**
* Rigourous Test :-)
*/
/** Rigourous Test :-) */
@SuppressWarnings("static-method")
public void testApp()
{
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));
// swtConsole.setLayoutData(
// new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
// final ConsoleApplication appl = new ConsoleApplication(swtConsole,
// "exit",
// "Hello", "See you");
// "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() {
@@ -94,9 +104,11 @@ public class AppTest
// });
// applThread.start();
// while (!shell.isDisposed()) {
// if (!display.readAndDispatch()) display.sleep();
// if (!display.readAndDispatch()) {
// display.sleep();
// }
// }
// display.dispose();
assertTrue( true );
assertTrue(true);
}
}