tags:

views:

31

answers:

2

Hi all,

I'm developing a program like Terminal in Linux or Command Prompt in Windows, but I'm developing it on Linux, and I don't care much about portability.

The program is written in Java, using SWT.

The user writes the commands in a text box, and when he presses 'enter' it reads this line and interpret it.

The problem is that the user writes his commands in a text box, so he is free to write at any place inside it.

I want to restrict him to write only in the last line of the text box. I don't want him to change the position of the cursor and writes any way.

Thanks in advance

A: 

Your could use 2 text widgets to simulate a terminal session.

One for the entry of the command, and the other for the display of the command and the response.

The entry text widget only needs to accept 70 - 80 characters or so.

The display text widget will scroll and show the most recent command and response.

Gilbert Le Blanc
A: 

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

Aurélien Ribon
Alos, note that a lot of existing terminal controls (well, "a lot" is oversized since I only found one terminal control for .NET/WPF) embed textboxes inside textboxesto achieve the same goal. It works but I think that this is to much overhead for such a simple task.
Aurélien Ribon