editor.createContentReader() not giving the text with the un

Having trouble installing Oxygen? Got a bug to report? Post it all here.
ashulinskiy
Posts: 4
Joined: Fri Aug 16, 2013 6:48 am

editor.createContentReader() not giving the text with the un

Post by ashulinskiy »

Hey folks,

I am doing some Oxygen customizations and have run into a it of a problem.

I need to prevent the saving of the docs that do not have a valid XML, malformed or not conforming to the schema.

So in my plugin I add an WSEditorChangeListener in the applicationStarted method.

In the editorOpened method I add a WSEditorListener where I override the editorAboutToBeSavedVeto method to get the Editor content and validate it.

I use the editor.createContentReader() method to get the content.

According to the API doc the method
Create a reader over the whole editor's content (exactly the XML content which gets saved on disk). The unsaved changes are included
And it works like magic when I do the modifications in the Text mode.

However, in the Author mode if I change something and hit save right away, the content I get in the listener does not have my change hence the validation succeeds.
However, the content being saved does have the change which is potentially invalid.

So the Editor's text representation in the author mode and the actual underlying text seem to be out of sync for a few moments.

Any idea how to fix or work around this problem please?

The code snippet showing the listeners' setup and the content retrieval are below.

Any help is greatly appreciated.

Thanks,
Andrey Shulinskiy

Code: Select all

    public void applicationStarted(final StandalonePluginWorkspace pluginWorkspaceAccess) {

pluginWorkspaceAccess.addEditorChangeListener(
new WSEditorChangeListener() {
@Override
public void editorOpened(final URL editorLocation) {
final WSEditor editor = pluginWorkspaceAccess.getEditorAccess(editorLocation, StandalonePluginWorkspace.MAIN_EDITING_AREA);
if (editor != null) {
editor.addEditorListener(new WSEditorListener() {
@Override
public boolean editorAboutToBeSavedVeto(int operationType) {
Reader reader = null;
try {
reader = editor.createContentReader();
alex_jitianu
Posts: 1016
Joined: Wed Nov 16, 2005 11:11 am

Re: editor.createContentReader() not giving the text with th

Post by alex_jitianu »

Hi,

I tried to reproduce the issue by following the scenario but in my case I always get correct content. I also didn't see anything wrong with the code.

How are you making the change? Are you just typing some text or is it some custom action that might perform the change on a thread other than AWT? I'm asking this because from your description I infer that if you wait a bit before saving then it all works (so perhaps it's a threading issue).

If you go into Options->Preferences... on page Editor / Edit modes / Author and under Format and indent you check The entire document, do you still get the behavior?

It might also help if you could send us the options file on support@oxygenxml.com. The options file is named something like oxyOptionsSa15.0.xml and is located as follows:
- Windows XP - [user-home-folder]\Application Data\com.oxygenxml
- Windows Vista/7 - [user-home-folder]\AppData\Roaming\com.oxygenxml
- Mac OS X - [user-home-folder]/Library/Preferences/com.oxygenxml
- Mac OS X - ~/Library/Preferences/com.oxygenxml
- Linux - [user-home-folder]/.com.oxygenxml


Best regards,
Alex
ashulinskiy
Posts: 4
Joined: Fri Aug 16, 2013 6:48 am

Re: editor.createContentReader() not giving the text with th

Post by ashulinskiy »

Hi Alex,

Thanks a lot for the prompt reply.
I tried to reproduce the issue by following the scenario but in my case I always get correct content
We have a somewhat complicated schema and CSS UI customization for the Author mode. The document isn't too large though.
How are you making the change? Are you just typing some text
Correct
from your description I infer that if you wait a bit before saving then it all works
Correct as well

So if it a threading issue it has something to do with Oxygen's internal threads. I have no customizations yet other that the validation plugin which just adds the Listeners.
If you go into Options->Preferences... on page Editor / Edit modes / Author and under Format and indent you check The entire document, do you still get the behavior?
I do unfortunately
send us the options file on support@oxygenxml.com
Sent four files from C:\Users\ashulinskiy\AppData\Roaming\com.oxygenxml
pluginsOptions15.0.xml
pluginsOptionsDeveloper15.0.xml
oxyDeveloperOptionsSa15.0.xml
oxyOptionsSa15.0.xml


Thanks,
Andrey
ashulinskiy
Posts: 4
Joined: Fri Aug 16, 2013 6:48 am

Re: editor.createContentReader() not giving the text with th

Post by ashulinskiy »

An update on the issue.

It seems it's not actually the timing. The test that fails for me is entering an invalid value for an attribute that has a restriction like this

Code: Select all


  <xs:simpleType name="someAttribute">
<xs:restriction base="xs:string">
<xs:enumeration value="Value1" />
<xs:enumeration value="Value2" />
</xs:restriction>
</xs:simpleType>
If after entering an invalid value I jump to another field and hit save (no delays), the validation works.
However, if I just wait and hit save even in 10 sec, the validation doesn't work.

Almost certainly a threading issue, however, haven't manage to solve it despite trying quite a few things. Frankly, getting a little desperate, it seems the fairly basic functionality is not working as expected

So any further help is greatly appreciated.

Thanks,
Andrey
alex_jitianu
Posts: 1016
Joined: Wed Nov 16, 2005 11:11 am

Re: editor.createContentReader() not giving the text with th

Post by alex_jitianu »

Hi,

Using the attribute editing scenario I've also managed to reproduce the issue. What's happening when you're editing an attribute and hit Save is that the editor gets saved without the currently modified attribute. That's why you don't see the updated attribute value on the validation listener and that's why the editor is marked as dirty after the action. If you open the file in an external application you will notice that the attribute value has not been committed. An additional save will commit the updated value too.

So you've not been lied, and the content you get on the listener is actual the content being saved. I will add an issue to change the behavior so that the saved content also has the updated attribute in it.

Meanwhile, as a workaround, you can add this code on your editor about to be saved listener, before requesting the reader:

Code: Select all


WSEditorPage currentPage = editor.getCurrentPage();
if (currentPage instanceof WSAuthorEditorPage) {
WSAuthorEditorPage authorPage = (WSAuthorEditorPage) currentPage;
final Object authorComponent = authorPage.getAuthorComponent();
if (authorComponent instanceof JComponent) {
Runnable focusRunnable = new Runnable() {
@Override
public void run() {
((JComponent) authorComponent).requestFocus();
}
};
if (SwingUtilities.isEventDispatchThread()) {
focusRunnable.run();
} else {
try {
int count = 0;
while (DefaultKeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != authorComponent
&& count < 5) {
SwingUtilities.invokeAndWait(focusRunnable);
count ++;
}
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Best regards,
Alex
ashulinskiy
Posts: 4
Joined: Fri Aug 16, 2013 6:48 am

Re: editor.createContentReader() not giving the text with th

Post by ashulinskiy »

Hey Alex,

First of all, thanks a lot for the workaround, it solves the problem.

Without it the editor does get saved with the updated value though. The editor's text mode would show it.

Anyhow, the solution is perfectly fine, thanks again.

Andrey.
Radu
Posts: 9449
Joined: Fri Jul 09, 2004 5:18 pm

Re: editor.createContentReader() not giving the text with th

Post by Radu »

Hi Andrey,

Oxygen 16.0 which was released a few days ago should have a builtin fix for this issue, saving the editor will first commit the current edited value in the Attributes editor.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Post Reply