disable menu items context-sensitive

Oxygen general issues.
Chemo
Posts: 19
Joined: Mon Aug 15, 2011 10:57 am

disable menu items context-sensitive

Post by Chemo »

hi.

i am new to oxygen and already tried to find a solution to my problem in this forum and the manual w/o any success.

i have a menu containing all the elements of my lexicography of my xml document type (the way you create one at Preferences->Document Type Associations -> 'document type' -> edit -> Author -> Menu).
i'd now like to let oxygen to grey out the menu items (actions), that are not allowed in the cursor context for a better and more intuitive usage by authors.
at the moment the menu shows all possible tags, only warning the author when he tries to use a tag in the false context. i can also restrict the usage of context-false actions by naming the valid XPath expression of the context in Preferences->Document Type Associations-> 'document type' ->Author->Actions-> 'action' ->Edit, but the author can still not tell context-valid tags apart from non-valids in the menu.

is there a way to disable menu items by the cursor context?

thanks, thomas.
Radu
Posts: 9041
Joined: Fri Jul 09, 2004 5:18 pm

Re: disable menu items context-sensitive

Post by Radu »

Hi Thomas,

Indeed by default Oxygen does not toggle the enable/disable states for actions based on whether the XPath expressions for that certain action are fulfilled. This is done because the actions can be many and evaluating XPath expression on each caret move can lead to performance problems.

But since Oxygen 12.1 we added some API which allows you to do this yourself.
If you have your own ro.sync.ecss.extensions.api.ExtensionsBundle implementation you can overwrite the method:

Code: Select all

ro.sync.ecss.extensions.api.ExtensionsBundle.createAuthorExtensionStateListener()
and when the extension state listener gets activated you can use the API like:

Code: Select all

  /**
* @see ro.sync.ecss.extensions.api.AuthorExtensionStateListener#activated(ro.sync.ecss.extensions.api.AuthorAccess)
*/
public void activated(final AuthorAccess authorAccess) {

//Add a caret listener to enable/disable extension actions:
authorAccess.getEditorAccess().addAuthorCaretListener(new AuthorCaretListener() {
@Override
public void caretMoved(AuthorCaretEvent caretEvent) {
try {
Map<String, Object> authorExtensionActions = authorAccess.getEditorAccess().getActionsProvider().getAuthorExtensionActions();
//Get the action used to insert a paragraph. It's ID is "paragraph"
AbstractAction insertParagraph = (AbstractAction) authorExtensionActions.get("paragraph");
//Evaluate an XPath expression in the context of the current node in which the caret is located
Object[] evaluateXPath = authorAccess.getDocumentController().evaluateXPath(".[ancestor-or-self::p]", false, false, false, false);
if(evaluateXPath != null && evaluateXPath.length > 0 && evaluateXPath[0] != null) {
//We are inside a paragraph, disable the action.
insertParagraph.setEnabled(false);
} else {
//Enable the action
insertParagraph.setEnabled(true);
}
} catch (AuthorOperationException e) {
e.printStackTrace();
}
}
});
when the extension is deactivated you should remove the caret listener in order to avoid adding multiple caret listeners which perform the same functionality.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Chemo
Posts: 19
Joined: Mon Aug 15, 2011 10:57 am

Re: disable menu items context-sensitive

Post by Chemo »

thanks Radu.
i will try that.
Chemo
Posts: 19
Joined: Mon Aug 15, 2011 10:57 am

Re: disable menu items context-sensitive

Post by Chemo »

nice!
works fine.
thanks
Radu
Posts: 9041
Joined: Fri Jul 09, 2004 5:18 pm

Re: disable menu items context-sensitive

Post by Radu »

Hi,

In Oxygen 14.2 which will be available in the spring of next year, Author actions will be automatically disabled when their XPath activation expression will not match the editing context.
So you will not need the workaround I suggested anymore.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Chemo
Posts: 19
Joined: Mon Aug 15, 2011 10:57 am

Re: disable menu items context-sensitive

Post by Chemo »

Hi Radu.

Since the new version 14.2 the workaround you proposed doesn't work anymore, because the actions that got disabled by the workaround are now getting re-enabled just a moment later.

I guess this is the result of the new feature that you anounced, that automatically enables/disabels author actions by their XPath constraints.

Unfortunately, the new feature is no comfortable alternative for the workaround, because now I have to define a specific XPath expression for every single author action that merely equals the schema constraints, while my implementation just checks dynamically for any author action if the inserted element is schema valid at the current cursor position.

My question is: Is there a way to disable this new feature, so that the action enabeling/disabeling of the AuthorCaretListener doesn't get overstriked anymore?

Regards and thanks,
thomas.
Radu
Posts: 9041
Joined: Fri Jul 09, 2004 5:18 pm

Re: disable menu items context-sensitive

Post by Radu »

Hi Thomas,

We have some future plans to somehow allow the developer to specify when developing a framework that an action should be enabled whenever the XML code which is to be inserted by the action is valid at the caret position.

In the meantime I changed the code I previously posted in order to try and avoid the activation which is done by our code when the caret moves based on the XPath conditions.
The code seemed to work for me:

Code: Select all

  /**
* The enabled state of an action should be changed only when we say so.
*/
private boolean enabledStatesCanBeChanged = false;

/**
* @see ro.sync.ecss.extensions.api.AuthorExtensionStateListener#activated(ro.sync.ecss.extensions.api.AuthorAccess)
*/
public void activated(final AuthorAccess authorAccess) {
this.authorAccess = authorAccess;
final Map<String, Object> authorExtensionActions = authorAccess.getEditorAccess().getActionsProvider().getAuthorExtensionActions();
Iterator<Object> actionsIter = authorExtensionActions.values().iterator();
while(actionsIter.hasNext()) {
final AbstractAction action = (AbstractAction) actionsIter.next();
action.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if("enabled".equals(evt.getPropertyName())){
if(! enabledStatesCanBeChanged) {
//Enabled states of actions cannot be changed now, probably the Oxygen code tries to
//enable or disable the action based on the XPath condition
//So set it back as it was.
try {
enabledStatesCanBeChanged = true;
action.setEnabled((Boolean) evt.getOldValue());
} finally {
enabledStatesCanBeChanged = false;
}
}
}
}
});
}

//Add a caret listener to enable/disable extension actions:
authorAccess.getEditorAccess().addAuthorCaretListener(new AuthorCaretListener() {
@Override
public void caretMoved(AuthorCaretEvent caretEvent) {
try {
//Get the action used to insert a paragraph. It's ID is "paragraph"
AbstractAction insertParagraph = (AbstractAction) authorExtensionActions.get("paragraph");
if(insertParagraph != null) {
//Evaluate an XPath expression in the context of the current node in which the caret is located
Object[] evaluateXPath = authorAccess.getDocumentController().evaluateXPath(".[ancestor-or-self::p]", false, false, false, false);
try {
enabledStatesCanBeChanged = true;
if(evaluateXPath != null && evaluateXPath.length > 0 && evaluateXPath[0] != null) {
//We are inside a paragraph, disable the action.
insertParagraph.setEnabled(false);
} else {
//Enable the action
insertParagraph.setEnabled(true);
}
} finally {
enabledStatesCanBeChanged = false;
}
}
} catch (AuthorOperationException e) {
e.printStackTrace();
}
}
});
}
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Chemo
Posts: 19
Joined: Mon Aug 15, 2011 10:57 am

Re: disable menu items context-sensitive

Post by Chemo »

Thanks, that helps a lot.
I'm looking forward to the improvements.

Regars,
thomas.
Radu
Posts: 9041
Joined: Fri Jul 09, 2004 5:18 pm

Re: disable menu items context-sensitive

Post by Radu »

Hi,

In Oxygen 15.1 we added this specific XPath function:

http://www.oxygenxml.com/doc/ug-oxygen/ ... ement.html

which can be used to enable an author operation whenever a certain element is allowed by the schema.

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