Building a Rich Text editor for an RCP based application

2009-03-10 21:43 by Jon Buck (15 comments)

Rich Text Editor
Firstly a disclaimer, this entry is not intended to be a detailed "how to", if anyone expresses an interest I can provide the source code for the project and perhaps accompany it with a more intimate version but for now this is just a general overview.

Recently I found myself needing a Rich Text Editor for an RCP based application I am working on. My first thoughts were that this should be a no brainer and that I surely couldn't be the first person looking for this type of widget, however after scouring the SWT site there didn't seem to be a widget that fitted the bill off the shelf and after much searching I couldn't find any significant threads nor any satisfactory solutions. The SWT site does provide an example of a rich text editor based on the Styled Text widget however the one thing the examples don't seem to include is how to persist or load the text and style information which of course is pretty important for a workable solution. I really wanted a pure SWT solution as I wanted it to be as native looking as possible so I messed around with the styled text widget example for a fair while however I couldn't work out how to easily or cleanly deal with the text and style information therefore I ended up looking for an alternative.

After trying a few different things I thought about building a solution using a browser based text editor running inside the SWT browser widget, I had used TinyMCE in a few web based applications and knew that it was pretty capable therefore I thought I would start with that. I wanted to keep the code together and expose it as a generic component that could be called or used in multiple ways so I started off by creating a new plugin project. To the project I added the source files for TinyMCE, a HTML page that contained the the code to initialize the editor and also a custom TinyMCE save method, more on that later. Next I created a TextEditor class that sub classed the jFace Dialog class and within the createDialogArea I set things up by creating a new composite and new browser widget, next I then loaded the editor HTML page into the browser. Once that was done it checks to see whether the content field is empty (this is the field that is used to set and get the text from the editor) if the content field is not empty then I a progress listener is added to the browser and once the document has finished loading it sets the contents of that field into the editor by invoking a simple JavaScript call to set the value of the textarea that TinyMCE is using. I found a need here to do some replacement of quotes inside the HTML string itself as the JavaScript method that you construct is in itself a string therefore I need to be sure that any quotes contained within the HTML would not cause the method string to break and therefore the JavaScript method to fail. From there I added a StatusTextListener to the browser which is used to pick up the content when Save is called from within the dialog, this changed method within the StatusTextListener fires when the status bar value changes within the browser widget and the code looks for a particular identifier at the start of the status string to identify that a save is taking place, now going back to the custom save method this is what it is responsible for i.e getting the content from the editor, adding the identifier to the beginning of the string and setting the status to be the string. if it finds the identifier then it takes the content and places it into the content field within the editor, sets the return code to OK and closes the dialog. From here the calling code can interrogate the dialog class to retrieve and persist the HTML string.

Now at this point in time I haven't fully tested this code to destruction but on the face of it it seems to be a good interim solution until a proper SWT widget is released as part of the core code.

RCP Based Rich Text Editor


Go back

Comment by daniel | 2009-06-01

I'm newer in RCP and interested in your rich text editor..
Could you provide your source code ?
It can help me that I'm frustrated in my rcp project...

Comment by Jon Buck | 2009-06-01

Hey Daniel, sure let me get the project together and I have your e-mail so will send it to you shortly.

Comment by daniel | 2009-06-10

Thanks jon
I've envolved in groupware projects of small company
I'll check the e-mail...Thanks...

Comment by Greg | 2009-06-23

I like the simplicity of the design and the extensibility that TinyMCE provides
I'll be interested in your source code also, so count me in on the mail

Comment by Anoop | 2009-06-26

Me too. Have been trying a lot; extending StyledText and so on; but is not stable enough. Your approach looks promising.!

Comment by Eric Finley | 2009-07-08

Hi Jon,
Currently i've embedded Microsoft word into my RCP application, and although it gives me the world it is obviously very heavy. I'm interested in trying your solution as it seems much lighter and more native. Could you please send me the project?

Thanks,
Eric

Comment by Mike | 2009-08-22

Hi,
I'm currently developing RCP application and I have to use HTML editor. I'm interesting in trying your solution. Could you send me the project source?

Thanks in advance,

Mike

Comment by ali | 2009-08-26

hi , i want to convert styled text to html .
please send me source code.
as soon as possible.
i am realy thank to you

Comment by ali | 2009-08-29

please send me source codeof html editor in swt and source code
i am thanks to you

Comment by Jacob | 2009-12-01

Jon. It's an attractive solution for RCP developer. Could you send me the code and then we can have a try. Any idea if you can start a project on sourceforge and make it opensource?

Comment by Antonio | 2009-12-05

hi Jon, can you sand me your project?, i am a student and your project resolv all my problem
best regads
Antonio

Comment by Jamal | 2010-02-20

please send me source codeof html editor in swt and source code
i am thanks to you

Comment by Nitesh | 2010-02-24

Hi Jon,

I had been looking for a good rich text editor since last couple of days. Your project seems to be really interesting.
I wonder if you can please provide me the source code for it.

Thanks in advance.

Comment by Jacek | 2010-02-25

Hi Jon,

your idea looks very interesting. Could you send me the sources and "how to use"?

Thanks,
Jacek

Comment by s33d | 2010-03-01

Hello,
this editor looks very good. Can you please send me source to e-mail ? I would like to use it in my small application :)
Thanks,
s33d

Add a comment

*
*
*
Please add 7 and 4.