Monday, June 23, 2008

JSyntaxPane new version - now with auto indentation and undo/redo

I had some more free time to work on JSyntaxPane. I did some refactoring, that should not change the way to use it though.
Refactoring was done by creating a new SyntaxDocument to store the Tokens, instead of having the SyntaxView store them. The former way was actually very stupid!
The Document should know about its own tokens, and the View gets the tokens from the Document. That is how it is done now. And this way you can also retrieve data about the SyntaxDocument by knowing its EditorPane control. And no need for listeners!
The new SyntaxDocument also has built in undo and redo.
A new class SyntaxActions was also created with several useful TextActions thrown in. These include Smart and Java Indentation, and mapping the TAB / Shift-TAB to indent / unindent the selected lines. Also mapped the default undo / redo keys to behave properly.
I also modified the Test application, and now it will display the token details under the cursor. This is very helpful for debugging the lexers.

Also changed is the way Fonts are used. Now the font of the Component is used, and you can only change the style (bold/italic) and color. Which means that the entire EditorPane will have one single font face and size. This actually suites my taste, unlike Notepad++ which by default uses different fonts and sizes for comments.

Again, all you need to do is one line to set the EditorKit. All the above is done for you automatically.

Have a look at the Test Application using Java WebStart here. Java 1.5+ is needed.

Project home is on Google Code here.

Hmm.. what next? Scripting? Nah.. I'm probably done, for now. I needed a simple control to edit scripts in my Java Swing applications, and thats probably it. unless...


David Qiao said...

This is David from JIDE. I noticed this project and it is trying to address the same issue as JIDE CodeEditor is doing. So we will probably facing the same issues down the line.

I just tried the undo feature. I suggest you grab the DelayUndoManager class from the open source JIDE Common Layer. It will undo as a group depending on how fast user types instead of undo one char by one char.

Ayman said...

Thanks for dropping by. However I think we are targeting two different areas. I'm more interested in a very simple to use, light and small script viewer and editor to replace the almost featureless JEditorPane. I have used and created some Swing apps with an almost unusable editor pane where Scripts, SQL code and others must be edited and just hated that. Remind me to add SQL support!
Code completion, folding, bracket matching, line numbers and others are currently out of scope, but may be nice to have.

I'll have a look at the DelayUndoManager.

Just Google it!