Example #1
0
  public void testPropertyChange() throws Exception {
    IWorkbenchWindow window = openTestWindow();
    IEvaluationService service = (IEvaluationService) window.getService(IEvaluationService.class);
    assertNotNull(service);
    MyEval listener = new MyEval();
    IExtensionRegistry registry = Platform.getExtensionRegistry();
    IConfigurationElement element = null;
    IConfigurationElement[] elements =
        registry.getConfigurationElementsFor("org.eclipse.core.expressions.definitions");
    for (int i = 0; i < elements.length && element == null; i++) {
      if (elements[i].getAttribute("id").equals("org.eclipse.ui.tests.defWithPropertyTester")) {
        element = elements[i];
      }
    }

    assertNotNull(element);
    Expression expr = ExpressionConverter.getDefault().perform(element.getChildren()[0]);
    service.addEvaluationListener(expr, listener, IEvaluationService.RESULT);
    assertFalse(listener.currentValue);
    assertEquals(1, listener.count);

    StaticVarPropertyTester.result = true;
    assertFalse(listener.currentValue);
    assertEquals(1, listener.count);

    service.requestEvaluation("org.eclipse.ui.tests.class.method");
    assertTrue(listener.currentValue);
    assertEquals(2, listener.count);

    service.requestEvaluation("org.eclipse.ui.tests.class.method");
    assertTrue(listener.currentValue);
    assertEquals(2, listener.count);
  }
  public void testExtensionContributionExpression() throws Exception {
    IAction a =
        new Action() {
          @Override
          public void run() {
            System.out.println("Hello action");
          }
        };
    final MenuManager manager = new MenuManager();
    final ActionContributionItem aci = new ActionContributionItem(a);

    IExtensionRegistry reg = Platform.getExtensionRegistry();
    IExtensionPoint menusExtension = reg.getExtensionPoint("org.eclipse.ui.menus");
    IExtension extension = menusExtension.getExtension(EXTENSION_ID);

    IConfigurationElement[] mas = extension.getConfigurationElements();
    final Expression activeContextExpr[] = new Expression[1];
    for (IConfigurationElement ma : mas) {
      IConfigurationElement[] items = ma.getChildren();
      for (IConfigurationElement item : items) {
        String id = item.getAttribute("id");
        if (id != null && id.equals("org.eclipse.ui.tests.menus.itemX1")) {
          IConfigurationElement visibleWhenElement = item.getChildren("visibleWhen")[0];
          activeContextExpr[0] =
              ExpressionConverter.getDefault().perform(visibleWhenElement.getChildren()[0]);
        }
      }
    }
    assertNotNull("Failed to find expression", activeContextExpr[0]);
    AbstractContributionFactory factory =
        new AbstractContributionFactory(LOCATION, TestPlugin.PLUGIN_ID) {
          @Override
          public void createContributionItems(
              IServiceLocator menuService, IContributionRoot additions) {
            additions.addContributionItem(aci, activeContextExpr[0]);
          }
        };

    menuService.addContributionFactory(factory);
    menuService.populateContributionManager(manager, LOCATION);

    assertFalse("starting state", aci.isVisible());

    activeContext = contextService.activateContext(MenuContributionHarness.CONTEXT_TEST1_ID);
    final Menu menu = manager.createContextMenu(window.getShell());
    menu.notifyListeners(SWT.Show, new Event());
    assertTrue("active context", aci.isVisible());
    menu.notifyListeners(SWT.Hide, new Event());

    contextService.deactivateContext(activeContext);
    activeContext = null;

    menu.notifyListeners(SWT.Show, new Event());
    assertFalse("after deactivation", aci.isVisible());
    menu.notifyListeners(SWT.Hide, new Event());

    menuService.releaseContributions(manager);
    menuService.removeContributionFactory(factory);
    manager.dispose();
  }
  public Expression getEnablementExpression() throws CoreException {
    if (fEnablementExpression == null) {
      IConfigurationElement[] elements = fConfig.getChildren(ExpressionTagNames.ENABLEMENT);
      IConfigurationElement enablement = elements.length > 0 ? elements[0] : null;

      if (enablement != null) {
        fEnablementExpression = ExpressionConverter.getDefault().perform(enablement);
      }
    }
    return fEnablementExpression;
  }
  public boolean matches(IJavaProject javaProject) {
    if (fStatus != null) {
      return fStatus.booleanValue();
    }

    IConfigurationElement[] children =
        fConfigurationElement.getChildren(ExpressionTagNames.ENABLEMENT);
    if (children.length == 1) {
      try {
        ExpressionConverter parser = ExpressionConverter.getDefault();
        Expression expression = parser.perform(children[0]);
        EvaluationContext evalContext = new EvaluationContext(null, javaProject);
        evalContext.addVariable("project", javaProject); // $NON-NLS-1$
        evalContext.addVariable(
            "sourceLevel", javaProject.getOption(JavaCore.COMPILER_SOURCE, true)); // $NON-NLS-1$
        return expression.evaluate(evalContext) == EvaluationResult.TRUE;
      } catch (CoreException e) {
        JavaPlugin.log(e);
      }
      return false;
    }
    fStatus = Boolean.FALSE;
    return false;
  }
Example #5
0
    /** @see ActionUI#getEnabledWhen() */
    @Override
    public Expression getEnabledWhen() {
      IConfigurationElement[] children = conf.getChildren("enabledWhen");
      if (children != null && children.length > 0) {
        // get child of enabled when
        children = children[0].getChildren();

        if (children != null && children.length > 0) {
          try {
            return ElementHandler.getDefault()
                .create(ExpressionConverter.getDefault(), children[0]);
          } catch (CoreException e) {
            log.error("Could not evaluate expression for action enablement.", e);
          }
        }
      }

      return null;
    }