Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 324667

Summary: Extend StyledText API to save/restore editor state
Product: [Eclipse Project] Platform Reporter: Aaron Digulla <digulla>
Component: SWTAssignee: Platform-SWT-Inbox <platform-swt-inbox>
Status: RESOLVED WONTFIX QA Contact:
Severity: enhancement    
Priority: P3 CC: eclipse.felipe, remy.suen
Version: 4.0   
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard:

Description Aaron Digulla CLA 2010-09-07 11:41:19 EDT
Build Identifier: 3.6 Helios

I need an API to save/restore the current editor state, that is: caret position, positions of the scrollbars, top/left corner.

The state should be a String, to make it easy to save it somewhere.

Reproducible: Always

Steps to Reproduce:
1. setText(longText)
2. Scroll somewhere and set the caret in the middle of the screen
3. saveState()
4. setText(""); setText(longText); restoreState();

Now the editor should look exactly as it was after step #2.
Comment 1 Aaron Digulla CLA 2010-09-07 11:42:38 EDT
The reason why I ask for a specific API:

1. It's hard to figure out which properties should be saved
2. Even if I save all properties that the public API exposes, these values get modified when I write them back. For example, it's not possible to set the top position to a pixel; the value is always rounded to a whole line.
Comment 2 Felipe Heidrich CLA 2010-09-07 12:15:44 EDT
I believe Eclipse does that already (save/restore the editor), isn't this featuer available in JFace text (or somewhere else) ?

Note, to properly restore the edtior the size also needs to be preserved, and this only the application can do.

Finally, you can use setTopPixel/getTopPixel instead of setTopIndex/getTopIndex to save/restore the position of the scrollbar in pixel.
Comment 3 Aaron Digulla CLA 2010-09-08 07:39:40 EDT
(In reply to comment #2)
> I believe Eclipse does that already (save/restore the editor), isn't this
> featuer available in JFace text (or somewhere else) ?
> 
> Note, to properly restore the edtior the size also needs to be preserved, and
> this only the application can do.
> 
> Finally, you can use setTopPixel/getTopPixel instead of setTopIndex/getTopIndex
> to save/restore the position of the scrollbar in pixel.

Both assumptions are false. Eclipse can restore which editors are open but it can't restore the cursor position. It doesn't even move you to the line you were. There is even a bug that prevents you from ending up in the same editor that you were when you closed Eclipse.

Next, setTopPixel() will modify the value you pass in, so you can't go back to any position. Whatever you pass in, StyledText will always make sure that the line at topIndex is completely visible.

I'm aware of the size constraint but this API is most useful when restarting an application to restore the editor as it was when you left. Since that setup includes restoring the whole window, the size should be the same.

Of course, the screen size could have changed (think laptop coming home and docking). So it might be a good idea to save the widget size in the state, too. If the new size is different, just restore the caret position and scroll to make it visible.

If you can do that, then IDEs could optimize the text editing by providing just a single instance StyledText and switching between states (plus caching of the text and maybe the style ranges).
Comment 4 Felipe Heidrich CLA 2010-09-09 15:55:01 EDT
I don't think this API makes sense for StyledText.
To restore the editor we would need to save
the text
the styles
the selection
the bounds
the horizontal/vertical scroll offset
the font
the foreground/background
other line attributes (alignment, justify, wrap, indent, wrapIndent)
bidi segments
etc

closing as won't fix.

on a side note, I tested setTopPixel() and it worked fine, please report a bug
if you have case where it fails.