File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Building a java editor..need help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Building a java editor..need help" Watch "Building a java editor..need help" New topic
Author

Building a java editor..need help

Stephen Batsas
Ranch Hand

Joined: Jan 22, 2002
Posts: 117
Dear Ranchers,
Im building a Java Editor - to colour the keywords as you type code. I want a Guru to steer me onto the right path. Which is the way?
1.Run a thread in the background to sweep through the document - tokenize the text into words and check for keywords, changing their colour along the way and then updateing the TextPane window
2.Listen for CaretListener event -typing a space to invoke a word check on the last word entered,
if the word is a key word, remove the word from the document, and reinsert the word in a different colour using StyledEditor.setForeground(Color..) on the range of text (the word).
3.
Any other suggestions welcomed.
Note Im using a JTextPane and a StyledDocument. implement a CaretListener.
What about ranchers - which one is the right way?
Regards
Stephen Batsas
SCJP2 (studying SCWCD)
Stephen Batsas
Ranch Hand

Joined: Jan 22, 2002
Posts: 117
Ranchers,
I got this far with the editor:
I can perform the usual file management (open,close,save,saveas) using the FileChooser class (what a beauty), change font,color,type,style,copy,cut,paste,select. I have equipped the editor with dialogs for errors,and user hints and information. Now the final step is highlighting those keywords as the user types them so it resembles an ide window.
Spent 8 hours so far. Someone mentioned it would take me two weeks, in another forum. Tell me ranchers which is the best way to handle the color change of keywords. I load the keywords from a text file into a ArrayList in lexical order, and use binary search method to compare words typed by the user(VERY FAST RESULTS). What is the next step to color the words. After this stage is complete I will move on to string quotes "" , comment quotes /**/*// and the brace {.
regards
Stephen Batsas
SCPJ2
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
There are probably many ways to do this, and the first way you come up with may not end up being the best way. But here's an idea.
You can keep track of newly added text in a simple StringBuffer array. Each elment would represent text that had been added to the document and not yet scanned. You will have to keep track of the state of a data-entry activity to know when to add new entries to this array.
For example, when the caret position changes, this would start a new entry. Any characters the user types after that go into the same StringBuffer. When the caret moves (because the user has clicked with the mouse in another part of the document, or used the arrow keys) that ends the data-gathering for that StringBuffer.
You will also need, for each StringBuffer, to keep track of where that text is in relation to the whole document, ie, character position, or some other reference.
You can then have a background thread that periodically scans this StringBuffer array, and when it finds keywords, can use the position information to update style information for the text in the document. Once a StringBuffer's contents have been processed, it is removed from the array.


Rob
SCJP 1.4
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
For identifying keywords, I think you'll find that using a HashSet is even faster, and much simpler to code. Of course, when you get to parsing comments and quotes, that will necessarily affect youd identification of keywords as well - something to think about if you haven't already.
It sounds like the main problem right now though is not identifying keywords, but changing colors. This is really more of a Swing question - you might try that forum. Be sure to check out this tutorial here if you haven't. Their example sets colors (and other style attributes) as the text is inserted. Less obvious is how to change these after the fact. Let's say you have a StyledDocument ref "doc" and you want to make character positions 10-19 red. Looking through the API, it seems you should be able to do something like this:

Let me know if it works.


"I'm not back." - Bill Harding, Twister
Stephen Batsas
Ranch Hand

Joined: Jan 22, 2002
Posts: 117
Thanks Jim,
As for the HashSet, I cant see how that will speed the search up. Binary search on an ordered or sorted list is the fastest.
As for the formating - I like the idea about Stringbuffer - except that there is a limit to capacity (255), and how do I handle when the user clicks into a word(somewhere into the document) and inserts a double quote or a comment. For example if you types an opening quote " all of the text following should appear as literal string( color green ) until the user types the closing quote ".
Same for the comment /* // /** .
This project is important to me as it tests my ability to convert my knowledge learned for the SCJP to produce a tangible product.
Regards
Stephen Batsas
SCJP
Greg Brouelette
Ranch Hand

Joined: Jan 23, 2002
Posts: 144
I use a free, open source text editor which is written in Java called Jext. It's available from www.jext.org Since it's open source you could d-load the source and see how they did their color syntaxing. After seeing a code example you may come up with an even better method.
Hope it helps.


For a good Prime, call:<br />29819592777931214269172453467810429868925511217482600306406141434158089
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
As for the HashSet, I cant see how that will speed the search up. Binary search on an ordered or sorted list is the fastest.
A binary search requires log n time; a HashSet uses constant time (on average). For very small n the binary search is probably faster; as n increases the HashSet will be superior. I have little idea where the break-even point is, without testing - probably the number of keywords is not that great anyway, and so it doesn't matter much in this case.
As for the formating - I like the idea about Stringbuffer - except that there is a limit to capacity (255)
Where did this idea come from? I know of no such limit, and have certainly made StringBuffers bigger than 255.
Note that if you like the array of StringBuffers idea, you probably will want a LinkedList rather than an array, as it will be much simpler to insert and delete within the list. Note that the Document model also offers a way of storing the content being edited. Since you apparently need to use StyledDocument in order to color the text (unless there's another way), there may be little point in also creating the List of StringBuffers. You can access the contents of the Document just as well, I think.
When the user types a " or /*, I think first you want to know if the text at that point was already part of a quote or comment. If you've been successfully, reliably parsing all edits as they are made, you can just look at the color of the current position; otherwise you'll have to start back somewhere earlier (a "last known normal text section") and parse forward from there. For a ", you'll also want to know if the preceding character was a \. Anyway, from all this, you can determine whether the effecto fo the " or /* was to "activate" quote or comment mode in subsequent text, or to turn it off. Scan forward from there to find the end of the affected section. For quotes, you usually will need to recolor everything up to the next line break. Any subsequent " encountered will flip states between quote and non-quote. A /* in non-quote mode will mean you can stop scanning forward, as will a newline. For /*, scan forward until you find either the */ which closes it, or another /*, indicating subsequent text is already in comment mode.
OK, I'm sure I missed some stuff there, or just plain got it wrong, but that's at least a general way to approach the problem. Enjoy.
[ March 01, 2002: Message edited by: Jim Yingst ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Building a java editor..need help