title: How to set initial state of an Eclipse action contribution
body: <p>What I'm trying to achieve is to contribute an action to the IProject pop up menu.
  The action is defined like this in my plugin.xml:</p>
  
  <pre><code>   &lt;extension
           point="org.eclipse.ui.popupMenus"&gt;
        &lt;objectContribution
              adaptable="true"
              objectClass="org.eclipse.core.resources.IProject"
              nameFilter="*"
              id="RemoteSync.contribution1"&gt;
           &lt;action
                 label="Enable RemoteSync"
                 class="remotesync.builder.ToggleNatureAction"
                 menubarPath="additions"
                 enablesFor="1"
                 id="RemoteSync.addRemoveNatureAction"
                 style="toggle"&gt;
           &lt;/action&gt;
        &lt;/objectContribution&gt;
     &lt;/extension&gt;
  </code></pre>
  
  <p>On run() I do setPersistentProperty() in order to save the menu toggle state and I want to restore that later when the plug in starts or whenever the pop up menu is displayed on setActivePart().</p>
  
  <p>Here are the relevant pieces of code:</p>
  
  <pre><code>public void run(IAction action) {
      if (selection instanceof IStructuredSelection) {
          for (Iterator it = ((IStructuredSelection) selection).iterator(); it
                  .hasNext();) {
              Object element = it.next();
              IProject project = null;
              if (element instanceof IProject) {
                  project = (IProject) element;
              } else if (element instanceof IAdaptable) {
                  project = (IProject) ((IAdaptable) element)
                          .getAdapter(IProject.class);
              }
              if (project != null) {
                  toggleNature(project);
                  try {
                      ((IResource) project).setPersistentProperty(
                              new QualifiedName("", ENABLED_PROPERTY), new Boolean(action.isChecked()).toString());
                  } catch (CoreException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
              }
          }
      }
  }
  
  
  public void setActivePart(IAction action, IWorkbenchPart targetPart) {
      if (selection instanceof IStructuredSelection) {
          for (Iterator it = ((IStructuredSelection) selection).iterator(); it
                  .hasNext();) {
              Object element = it.next();
              IProject project = null;
              if (element instanceof IProject) {
                  project = (IProject) element;
              } else if (element instanceof IAdaptable) {
                  project = (IProject) ((IAdaptable) element)
                          .getAdapter(IProject.class);
              }
              if (project != null) {
                  toggleNature(project);
                  try {
                      String status = ((IResource) project).getPersistentProperty(
                              new QualifiedName("", ENABLED_PROPERTY));
                      action.setChecked(new Boolean(status));
                  } catch (CoreException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>Everything works as expected but one thing - when the IProject context menu is first activated it does not reflect the saved state, but if I bring the menu up again it shows the saved state as expected. Then if I bring the menu up for another project it once again shows with incorrect state, but the second time works fine.</p>