I wrote a terminal-like control in .NET. It's not java-based but I think that you can reuse some of the logic in there:
http://wpfterminal.codeplex.com/
Be advised that I didn't mean to release this project so soon, I did it for you, so it might require a little cleaning maybe, as well as a few bug tracking. But anyway, it worked flawlessly for now.
You can see an example in this screenshot (terminal is integrated in a bigger project) :
http://images4.hiboox.com/images/4210/0a2809b63e05c3d0cac678962e0e3d5a.jpg
My solution
.
Basic mechanisms
Actually what I did was to define a lastPromptIndex
integer, and everytime a user presses the ENTER key and a new prompt appears, this value is updated.
After that, it's simple, you just need to process any text input before the textbox validates the input. If the textbox caret was located before your lastPromptIndex
, you need to raise an error (usually a beep sound) and you must invalidate the text input, so nothing is written in the textbox. I also automatically set the caret position to the end of the textbox, so the user can immediatly input some text.
Extensions
You can enable command completion by looking for an "UP key" input if the caret is before the prompt index, etc. What you need is just to process input events before they are sent to the textbox internal mechanisms. I don't know if SWT controls allow it, but I'm pretty sure they do, like any serious UI system.
Other solution
.
JTERM
I remember that a friend of mine used a terminal in a Java application, and in his sources, I found that if was a control from JTerm, have a look at it:
http://www.acordex.com/vtj/JTerm.html