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();
  }
예제 #2
0
  public void fill(Menu menu, int index) {
    if (getParent() instanceof MenuManager) {
      ((MenuManager) getParent()).addMenuListener(menuListener);
    }

    if (!dirty) {
      return;
    }

    if (currentManager != null && currentManager.getSize() > 0) {
      IMenuService service = (IMenuService) locator.getService(IMenuService.class);
      service.releaseContributions(currentManager);
      currentManager.removeAll();
    }

    currentManager = new MenuManager();
    fillMenu(currentManager);
    IContributionItem[] items = currentManager.getItems();
    if (items.length == 0) {
      MenuItem item = new MenuItem(menu, SWT.NONE, index++);
      item.setText(NO_TARGETS_MSG);
      item.setEnabled(false);
    } else {
      for (int i = 0; i < items.length; i++) {
        if (items[i].isVisible()) {
          items[i].fill(menu, index++);
        }
      }
    }
    dirty = false;
  }
예제 #3
0
 @Override
 public void dispose() {
   disposeScheduleAction();
   if (headerImage != null) {
     headerImage.dispose();
   }
   if (editorBusyIndicator != null) {
     editorBusyIndicator.stop();
   }
   if (activateAction != null) {
     activateAction.dispose();
   }
   if (menuService != null) {
     if (leftToolBarManager != null) {
       menuService.releaseContributions(leftToolBarManager);
     }
     if (toolBarManager instanceof ContributionManager) {
       menuService.releaseContributions((ContributionManager) toolBarManager);
     }
   }
   if (textSupport != null) {
     textSupport.dispose();
   }
   if (messageHyperLinkListener instanceof IDisposable) {
     ((IDisposable) messageHyperLinkListener).dispose();
   }
   super.dispose();
 }
예제 #4
0
 private void addContributions(IToolBarManager toolBarManager) {
   IMenuService menuService = (IMenuService) getSite().getService(IMenuService.class);
   if (menuService != null && toolBarManager instanceof ContributionManager) {
     ContributionManager contributionManager = (ContributionManager) toolBarManager;
     String toolbarUri = "toolbar:" + TOOLBAR_HEADER_ID; // $NON-NLS-1$
     menuService.populateContributionManager(contributionManager, toolbarUri);
   }
 }
  public void testVisibilityTracksEnablement() throws Exception {
    final MenuManager manager = new MenuManager();
    final CommandContributionItemParameter parm =
        new CommandContributionItemParameter(
            window,
            null,
            COMMAND_ID,
            Collections.EMPTY_MAP,
            null,
            null,
            null,
            null,
            null,
            null,
            CommandContributionItem.STYLE_PUSH,
            null,
            true);
    final CommandContributionItem item = new CommandContributionItem(parm);

    AbstractContributionFactory factory =
        new AbstractContributionFactory(LOCATION, TestPlugin.PLUGIN_ID) {
          @Override
          public void createContributionItems(
              IServiceLocator menuService, IContributionRoot additions) {
            additions.addContributionItem(item, null);
          }
        };

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

    assertFalse(item.isEnabled());
    assertFalse("starting state", item.isVisible());

    IHandlerService handlers = window.getService(IHandlerService.class);
    TestEnabled handler = new TestEnabled();
    IHandlerActivation activateHandler = handlers.activateHandler(COMMAND_ID, handler);

    assertTrue(handler.isEnabled());
    assertTrue(item.isEnabled());
    assertTrue("activated handler", item.isVisible());

    handler.setEnabled(false);

    assertFalse("set enabled == false", item.isVisible());

    handler.setEnabled(true);

    assertTrue("set enabled == true", item.isVisible());

    handlers.deactivateHandler(activateHandler);

    assertFalse("deactivate handler", item.isVisible());

    menuService.releaseContributions(manager);
    menuService.removeContributionFactory(factory);
    manager.dispose();
  }
예제 #6
0
 /*
  * (non-Javadoc)
  *
  * @see org.eclipse.jface.action.ContributionItem#dispose()
  */
 public void dispose() {
   if (currentManager != null && currentManager.getSize() > 0) {
     IMenuService service = (IMenuService) locator.getService(IMenuService.class);
     if (service != null) {
       service.releaseContributions(currentManager);
     }
     currentManager.removeAll();
   }
 }
  public void testBasicContribution() throws Exception {

    IAction a =
        new Action() {
          @Override
          public void run() {
            System.out.println("Hello action");
          }
        };
    final MenuManager manager = new MenuManager();
    final ActionContributionItem item = new ActionContributionItem(a);
    final Expression activeContextExpr =
        new ActiveContextExpression(
            MenuContributionHarness.CONTEXT_TEST1_ID, new String[] {ISources.ACTIVE_CONTEXT_NAME});
    AbstractContributionFactory factory =
        new AbstractContributionFactory(LOCATION, TestPlugin.PLUGIN_ID) {
          @Override
          public void createContributionItems(
              IServiceLocator menuService, IContributionRoot additions) {
            additions.addContributionItem(item, activeContextExpr);
          }
        };

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

    Shell shell = window.getShell();

    // Test the initial menu creation
    final Menu menuBar = manager.createContextMenu(shell);
    Event e = new Event();
    e.type = SWT.Show;
    e.widget = menuBar;
    menuBar.notifyListeners(SWT.Show, e);

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

    activeContext = contextService.activateContext(MenuContributionHarness.CONTEXT_TEST1_ID);
    menuBar.notifyListeners(SWT.Show, e);

    assertTrue("active context", item.isVisible());

    contextService.deactivateContext(activeContext);
    activeContext = null;
    menuBar.notifyListeners(SWT.Show, e);

    assertFalse("after deactivation", item.isVisible());

    menuService.releaseContributions(manager);
    menuService.removeContributionFactory(factory);
    manager.dispose();
  }
예제 #8
0
  /** Fills the menu with Show In actions. */
  private void fillMenu(IMenuManager innerMgr) {
    // Remove all.
    innerMgr.removeAll();

    IWorkbenchPart sourcePart = getSourcePart();
    if (sourcePart == null) {
      return;
    }
    ShowInContext context = getContext(sourcePart);
    if (context == null) {
      return;
    }
    if (context.getInput() == null
        && (context.getSelection() == null || context.getSelection().isEmpty())) {
      return;
    }

    IViewDescriptor[] viewDescs = getViewDescriptors(sourcePart);
    if (viewDescs.length == 0) {
      return;
    }

    for (int i = 0; i < viewDescs.length; ++i) {
      IAction action = getAction(viewDescs[i]);
      if (action != null) {
        innerMgr.add(action);
      }
    }
    if (innerMgr instanceof MenuManager) {
      ISourceProviderService sps =
          (ISourceProviderService) locator.getService(ISourceProviderService.class);
      ISourceProvider sp = sps.getSourceProvider(ISources.SHOW_IN_SELECTION);
      if (sp instanceof ActivePartSourceProvider) {
        ((ActivePartSourceProvider) sp).checkActivePart();
      }
      IMenuService service = (IMenuService) locator.getService(IMenuService.class);
      service.populateContributionManager((ContributionManager) innerMgr, MenuUtil.SHOW_IN_MENU_ID);
    }
  }
  /** Dispose of the menu extender. Should only be called when the part is disposed. */
  public void dispose() {
    clearStaticActions();
    final IMenuService menuService = (IMenuService) part.getSite().getService(IMenuService.class);
    if (menuService != null) {
      menuService.releaseContributions(menu);
    }
    Platform.getExtensionRegistry().removeRegistryChangeListener(this);
    menu.removeMenuListener(this);

    if (menuModel != null) {
      // unlink ourselves from the renderer
      IRendererFactory factory = modelPart.getContext().get(IRendererFactory.class);
      AbstractPartRenderer obj = factory.getRenderer(menuModel, null);
      if (obj instanceof MenuManagerRenderer) {
        MenuManagerRenderer renderer = (MenuManagerRenderer) obj;
        unlink(renderer, menuModel);
        renderer.clearModelToManager(menuModel, menu);
      }

      modelPart.getMenus().remove(menuModel);
    }
  }
예제 #10
0
  /* (non-Javadoc)
   * @see org.eclipse.ui.forms.editor.FormPage#createFormContent(org.eclipse.ui.forms.IManagedForm)
   */
  @Override
  protected void createFormContent(IManagedForm managedForm) {
    this.mForm = managedForm;
    // form
    ScrolledForm form = managedForm.getForm();
    this.toolkit = OpenDartsFormsToolkit.getToolkit();
    form.setText(this.game.getName());
    this.toolkit.decorateFormHeading(form.getForm());

    GridDataFactory playerData;
    GridDataFactory scoreData;
    scoreData = GridDataFactory.fillDefaults().grab(true, false).span(2, 1);

    List<IPlayer> players = this.game.getPlayers();
    boolean twoPlayer = (players.size() == 2);
    // body
    int nbCol;
    int tableSpan;
    if (twoPlayer) {
      nbCol = 4;
      tableSpan = 2;
      playerData = GridDataFactory.fillDefaults().grab(false, true);
      scoreData = GridDataFactory.fillDefaults().grab(true, false).span(2, 1);
    } else {
      nbCol = players.size();
      tableSpan = nbCol;
      playerData = GridDataFactory.fillDefaults().grab(false, true);
      scoreData = GridDataFactory.fillDefaults().grab(true, false);
    }
    this.body = form.getBody();
    GridLayoutFactory.fillDefaults()
        .margins(5, 5)
        .numColumns(nbCol)
        .equalWidth(true)
        .applyTo(this.body);

    if (twoPlayer) {
      // First Player Status
      Composite cmpPlayerOne = this.createPlayerComposite(this.body, this.game.getFirstPlayer());
      playerData.copy().applyTo(cmpPlayerOne);
    } else {
      // create multi player stats
    }

    // Score
    Composite cmpScore = this.createScoreTableComposite(this.body);
    GridDataFactory.fillDefaults().grab(true, true).span(tableSpan, 1).applyTo(cmpScore);

    if (twoPlayer) {
      // Second Player Status
      Composite cmpPlayerTwo = this.createPlayerComposite(this.body, this.game.getSecondPlayer());
      playerData.copy().applyTo(cmpPlayerTwo);
    }

    // Left score
    Composite leftScoreMain = this.createLeftScoreComposite(nbCol, scoreData);
    GridDataFactory.fillDefaults().grab(true, false).span(nbCol, 1).applyTo(leftScoreMain);

    // Toolbar
    ToolBarManager manager = (ToolBarManager) form.getToolBarManager();
    IMenuService menuService = (IMenuService) this.getSite().getService(IMenuService.class);
    menuService.populateContributionManager(manager, "toolbar:openwis.editor.game.toolbar");
    manager.add(
        new ControlContribution("openwis.editor.game.toolbar.help") {
          @Override
          protected Control createControl(Composite parent) {
            Label helpLabel = new Label(parent, SWT.NONE);
            helpLabel.setImage(ProtoPlugin.getImage("/icons/actions/help.png"));
            ToolTip toolTip = new ShortcutsTooltip(helpLabel);
            toolTip.setPopupDelay(0);
            return helpLabel;
          }
        });
    manager.update(true);

    // Register listener
    this.game.addListener(this);

    // initialize game
    for (TableViewer viewer : this.scoreViewers.values()) {
      viewer.setInput(this.game);
    }
    this.handlePlayer(this.game.getCurrentPlayer(), this.game.getCurrentEntry());
  }
예제 #11
0
  /** @since 3.0 */
  public void updateHeaderToolBar() {
    if (isHeaderFormDisposed()) {
      return;
    }

    final Form form = getHeaderForm().getForm().getForm();
    toolBarManager = form.getToolBarManager();

    toolBarManager.removeAll();
    //		toolBarManager.update(true);

    TaskRepository outgoingNewRepository = TasksUiUtil.getOutgoingNewTaskRepository(task);
    final TaskRepository taskRepository =
        (outgoingNewRepository != null)
            ? outgoingNewRepository
            : taskEditorInput.getTaskRepository();
    ControlContribution repositoryLabelControl =
        new ControlContribution(Messages.AbstractTaskEditorPage_Title) {
          @Override
          protected Control createControl(Composite parent) {
            FormToolkit toolkit = getHeaderForm().getToolkit();
            Composite composite = toolkit.createComposite(parent);
            RowLayout layout = new RowLayout();
            if (PlatformUiUtil.hasNarrowToolBar()) {
              layout.marginTop = 0;
              layout.marginBottom = 0;
              layout.center = true;
            }
            composite.setLayout(layout);
            composite.setBackground(null);
            String label = taskRepository.getRepositoryLabel();
            if (label.indexOf("//") != -1) { // $NON-NLS-1$
              label =
                  label.substring(
                      (taskRepository.getRepositoryUrl().indexOf("//") + 2)); // $NON-NLS-1$
            }

            ImageHyperlink link = new ImageHyperlink(composite, SWT.NONE);
            link.setText(label);
            link.setFont(JFaceResources.getBannerFont());
            link.setForeground(toolkit.getColors().getColor(IFormColors.TITLE));
            link.setToolTipText(Messages.TaskEditor_Edit_Task_Repository_ToolTip);
            link.addHyperlinkListener(
                new HyperlinkAdapter() {
                  @Override
                  public void linkActivated(HyperlinkEvent e) {
                    TasksUiUtil.openEditRepositoryWizard(taskRepository);
                  }
                });

            return composite;
          }
        };
    toolBarManager.add(repositoryLabelControl);

    toolBarManager.add(new GroupMarker("repository")); // $NON-NLS-1$
    toolBarManager.add(new GroupMarker("new")); // $NON-NLS-1$
    toolBarManager.add(new GroupMarker("open")); // $NON-NLS-1$
    toolBarManager.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));

    openWithBrowserAction = new OpenWithBrowserAction();
    openWithBrowserAction.selectionChanged(new StructuredSelection(task));
    if (openWithBrowserAction.isEnabled()) {
      //			ImageDescriptor overlay =
      // TasksUiPlugin.getDefault().getOverlayIcon(taskRepository.getConnectorKind());
      //			ImageDescriptor compositeDescriptor = new
      // TaskListImageDescriptor(TasksUiImages.REPOSITORY_SMALL_TOP,
      //					overlay, false, true);
      openWithBrowserAction.setImageDescriptor(CommonImages.WEB);
      // openWithBrowserAction.setImageDescriptor(CommonImages.BROWSER_OPEN_TASK);
      openWithBrowserAction.setToolTipText(Messages.AbstractTaskEditorPage_Open_with_Web_Browser);
      toolBarManager.appendToGroup("open", openWithBrowserAction); // $NON-NLS-1$
    } else {
      openWithBrowserAction = null;
    }

    if (activateAction == null) {
      activateAction =
          new ToggleTaskActivationAction(task) {
            @Override
            public void run() {
              TaskList taskList = TasksUiPlugin.getTaskList();
              if (taskList.getTask(task.getRepositoryUrl(), task.getTaskId()) == null) {
                setMessage(
                    Messages.TaskEditor_Task_added_to_the_Uncategorized_container,
                    IMessageProvider.INFORMATION);
              }
              super.run();
            }
          };
    }

    toolBarManager.add(new Separator("planning")); // $NON-NLS-1$
    disposeScheduleAction();
    scheduleAction = new TaskEditorScheduleAction(task);
    toolBarManager.add(scheduleAction);

    toolBarManager.add(new GroupMarker("page")); // $NON-NLS-1$
    for (IFormPage page : getPages()) {
      if (page instanceof TaskFormPage) {
        TaskFormPage taskEditorPage = (TaskFormPage) page;
        taskEditorPage.fillToolBar(toolBarManager);
      }
    }

    toolBarManager.add(new Separator("activation")); // $NON-NLS-1$

    //		ContributionItem spacer = new ContributionItem() {
    //			@Override
    //			public void fill(ToolBar toolbar, int index) {
    //				ToolItem item = new ToolItem(toolbar, SWT.NONE);
    //				int scaleHeight = 42;
    //				if (PlatformUtil.needsCarbonToolBarFix()) {
    //					scaleHeight = 32;
    //				}
    //				final Image image = new Image(toolbar.getDisplay(),
    // CommonImages.getImage(CommonImages.BLANK)
    //						.getImageData()
    //						.scaledTo(1, scaleHeight));
    //				item.setImage(image);
    //				item.addDisposeListener(new DisposeListener() {
    //					public void widgetDisposed(DisposeEvent e) {
    //						image.dispose();
    //					}
    //				});
    //				item.setWidth(5);
    //				item.setEnabled(false);
    //			}
    //		};
    //		toolBarManager.add(spacer);

    //		for (IFormPage page : getPages()) {
    //			if (page instanceof AbstractTaskEditorPage) {
    //				AbstractTaskEditorPage taskEditorPage = (AbstractTaskEditorPage) page;
    //				taskEditorPage.fillLeftHeaderToolBar(toolBarManager);
    //			} else if (page instanceof TaskPlanningEditor) {
    //				TaskPlanningEditor taskEditorPage = (TaskPlanningEditor) page;
    //				taskEditorPage.fillLeftHeaderToolBar(toolBarManager);
    //			}
    //		}

    // add external contributions
    menuService = (IMenuService) getSite().getService(IMenuService.class);
    if (menuService != null && toolBarManager instanceof ContributionManager) {
      menuService.populateContributionManager(
          (ContributionManager) toolBarManager,
          "toolbar:" //$NON-NLS-1$
              + ID_TOOLBAR_HEADER
              + "."
              + taskRepository.getConnectorKind()); // $NON-NLS-1$
      menuService.populateContributionManager(
          (ContributionManager) toolBarManager,
          "toolbar:" //$NON-NLS-1$
              + ID_TOOLBAR_HEADER);
    }

    toolBarManager.update(true);

    // XXX move this call
    updateLeftHeaderToolBar();
    updateHeader();
  }
예제 #12
0
  private void updateLeftHeaderToolBar() {
    leftToolBarManager.removeAll();

    leftToolBarManager.add(new Separator("activation")); // $NON-NLS-1$
    leftToolBarManager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));

    //		initialLeftToolbarSize = leftToolBarManager.getSize();

    leftToolBarManager.add(activateAction);

    //		for (IFormPage page : getPages()) {
    //			if (page instanceof AbstractTaskEditorPage) {
    //				AbstractTaskEditorPage taskEditorPage = (AbstractTaskEditorPage) page;
    //				taskEditorPage.fillLeftHeaderToolBar(leftToolBarManager);
    //			} else if (page instanceof TaskPlanningEditor) {
    //				TaskPlanningEditor taskEditorPage = (TaskPlanningEditor) page;
    //				taskEditorPage.fillLeftHeaderToolBar(leftToolBarManager);
    //			}
    //		}

    // add external contributions
    menuService = (IMenuService) getSite().getService(IMenuService.class);
    if (menuService != null && leftToolBarManager instanceof ContributionManager) {
      TaskRepository outgoingNewRepository = TasksUiUtil.getOutgoingNewTaskRepository(task);
      TaskRepository taskRepository =
          (outgoingNewRepository != null)
              ? outgoingNewRepository
              : taskEditorInput.getTaskRepository();
      menuService.populateContributionManager(
          leftToolBarManager,
          "toolbar:"
              + ID_LEFT_TOOLBAR_HEADER
              + "." //$NON-NLS-1$ //$NON-NLS-2$
              + taskRepository.getConnectorKind());
    }

    leftToolBarManager.update(true);

    if (hasLeftToolBar()) {
      // XXX work around a bug in Gtk that causes the toolbar size to be incorrect if no
      // tool bar buttons are contributed
      //			if (leftToolBar != null) {
      //				Point size = leftToolBar.computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
      //				boolean changed = false;
      //				for (Control control : leftToolBar.getChildren()) {
      //					final Point childSize = control.computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
      //					if (childSize.y > size.y) {
      //						size.y = childSize.y;
      //						changed = true;
      //					}
      //				}
      //				if (changed) {
      //					leftToolBar.setSize(size);
      //				}
      //			}
      //
      //			if (PlatformUtil.isToolBarHeightBroken(leftToolBar)) {
      //				ToolItem item = new ToolItem(leftToolBar, SWT.NONE);
      //				item.setEnabled(false);
      //				item.setImage(CommonImages.getImage(CommonImages.BLANK));
      //				item.setWidth(1);
      //				noExtraPadding = true;
      //			} else if (PlatformUtil.needsToolItemToForceToolBarHeight()) {
      //				ToolItem item = new ToolItem(leftToolBar, SWT.NONE);
      //				item.setEnabled(false);
      //				int scaleHeight = 22;
      //				if (PlatformUtil.needsCarbonToolBarFix()) {
      //					scaleHeight = 32;
      //				}
      //				final Image image = new Image(item.getDisplay(),
      // CommonImages.getImage(CommonImages.BLANK)
      //						.getImageData()
      //						.scaledTo(1, scaleHeight));
      //				item.setImage(image);
      //				item.addDisposeListener(new DisposeListener() {
      //					public void widgetDisposed(DisposeEvent e) {
      //						image.dispose();
      //					}
      //				});
      //				item.setWidth(1);
      //				noExtraPadding = true;
      //			}

      // fix size of toolbar on Gtk with Eclipse 3.3
      Point size = leftToolBar.getSize();
      if (size.x == 0 && size.y == 0) {
        size = leftToolBar.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
        leftToolBar.setSize(size);
      }
    }
  }