eclipse plugin - disable menu based on value of a preference variable -


i have menu item xyz. have preference page mypref has check box , couple of text fields. when check box in mypref check, want menu xyz enabled else should disabled. there way achieve this.

yes, way exist.

1). create entry in ipreferencestore - class extends abstractuiplugin , following:

ipreferencestore store = iextendabstractuiplugin.getdefault()       .getpreferencestore(); 

then store value reflect checkbox state:

preferencestore.setvalue("xyzmenupreference", false); // if disabled 

or

preferencestore.setvalue("xyzmenupreference", true); // if enabled 

2). create property tester extention plugin.xml:

<extension point="org.eclipse.core.expressions.propertytesters">   <propertytester      class="org.xyz.propertytester"      id="org.xyz.propertytester"      type="java.lang.object"      namespace="org.xyz"      properties="xyzmenu">   </propertytester> </extension>  

3). when declaring menu handler in plugin.xml should add following:

<handler  class="your-menu-handler-class"  commandid="your-command-id">      <enabledwhen>      <with variable="selection">                          <test property="org.xyz.xyzmenu" value="true" forcepluginactivation="true"/>       </with>       </enabledwhen>            </handler> 

4). need class "org.xyz.propertytester" (as defined in plugin.xml) extend org.eclipse.core.expressions.propertytester , override method test(<some args>) must check property value:

if (property.equals("xyzmenu"){    ipreferencestore store = iextendabstractuiplugin.getdefault()                     .getpreferencestore();    return store.getboolean("xyzmenupreference"); } 

5). after add change listener checkbox , use re-evaluate visibility of menu item:

ievaluationservice service = (ievaluationservice) platformui    .getworkbench().getservice(ievaluationservice.class); service.requestevaluation("org.xyz.xyzmenu"); 

it force method test() org.xyz.propertytester class called , enable menu item if preference "xyzmenupreference" set true.

upd.

namespace - unique id determining name space properties added properties - comma separated list of properties provided property tester 

this official eclipse tutorial can feel free define namespace , property name, should use in point 5.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -