private void addRadio(RuleAction.Choices choices) { List<URL> icons = choices.getIconUrls(); List<String> titles = choices.getTitles(); List<String> ids = choices.getIds(); String current = choices.getCurrent() != null ? choices.getCurrent() : ""; // $NON-NLS-1$ assert icons != null; assert icons.size() == titles.size(); for (int i = 0; i < icons.size(); i++) { URL iconUrl = icons.get(i); String title = titles.get(i); final String id = ids.get(i); final ToolItem item = new ToolItem(mLayoutToolBar, SWT.RADIO); item.setToolTipText(title); item.setImage(IconFactory.getInstance().getIcon(iconUrl)); item.setData(choices); item.setData(ATTR_ID, id); item.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { if (item.getSelection()) { RuleAction.Choices choices = (Choices) item.getData(); choices.getCallback().action(choices, getSelectedNodes(), id, null); updateSelection(); } } }); boolean selected = current.equals(id); if (selected) { item.setSelection(true); } } }
private void addPlainAction(RuleAction menuAction) { final ToolItem button = new ToolItem(mLayoutToolBar, SWT.PUSH); URL iconUrl = menuAction.getIconUrl(); String title = menuAction.getTitle(); if (iconUrl != null) { button.setImage(IconFactory.getInstance().getIcon(iconUrl)); button.setToolTipText(title); } else { button.setText(title); } button.setData(menuAction); button.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { RuleAction menuAction = (RuleAction) button.getData(); menuAction .getCallback() .action(menuAction, getSelectedNodes(), menuAction.getId(), false); updateSelection(); } }); }
private void addToggle(Toggle toggle) { final ToolItem button = new ToolItem(mLayoutToolBar, SWT.CHECK); URL iconUrl = toggle.getIconUrl(); String title = toggle.getTitle(); if (iconUrl != null) { button.setImage(IconFactory.getInstance().getIcon(iconUrl)); button.setToolTipText(title); } else { button.setText(title); } button.setData(toggle); button.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { Toggle toggle = (Toggle) button.getData(); toggle .getCallback() .action(toggle, getSelectedNodes(), toggle.getId(), button.getSelection()); updateSelection(); } }); if (toggle.isChecked()) { button.setSelection(true); } }
private void addDropdown(RuleAction.Choices choices) { final ToolItem combo = new ToolItem(mLayoutToolBar, SWT.DROP_DOWN); URL iconUrl = choices.getIconUrl(); if (iconUrl != null) { combo.setImage(IconFactory.getInstance().getIcon(iconUrl)); combo.setToolTipText(choices.getTitle()); } else { combo.setText(choices.getTitle()); } combo.setData(choices); Listener menuListener = new Listener() { @Override public void handleEvent(Event event) { Menu menu = new Menu(mLayoutToolBar.getShell(), SWT.POP_UP); RuleAction.Choices choices = (Choices) combo.getData(); List<URL> icons = choices.getIconUrls(); List<String> titles = choices.getTitles(); List<String> ids = choices.getIds(); String current = choices.getCurrent() != null ? choices.getCurrent() : ""; // $NON-NLS-1$ for (int i = 0; i < titles.size(); i++) { String title = titles.get(i); final String id = ids.get(i); URL itemIconUrl = icons != null && icons.size() > 0 ? icons.get(i) : null; MenuItem item = new MenuItem(menu, SWT.CHECK); item.setText(title); if (itemIconUrl != null) { Image itemIcon = IconFactory.getInstance().getIcon(itemIconUrl); item.setImage(itemIcon); } boolean selected = id.equals(current); if (selected) { item.setSelection(true); } item.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { RuleAction.Choices choices = (Choices) combo.getData(); choices.getCallback().action(choices, getSelectedNodes(), id, null); updateSelection(); } }); } Rectangle bounds = combo.getBounds(); Point location = new Point(bounds.x, bounds.y + bounds.height); location = combo.getParent().toDisplay(location); menu.setLocation(location.x, location.y); menu.setVisible(true); } }; combo.addListener(SWT.Selection, menuListener); }
/** * Creates with the help of the given menu manager a menu. If the given tool item is selected, the * menu is shown. * * @param parent * @param toolItem tool item with menu * @param topMenuManager menu manager * @return menu */ public Menu createMenu( final Composite parent, final ToolItem toolItem, final MenuManager topMenuManager) { final Menu menu = topMenuManager.createContextMenu(parent); toolItem.setData(topMenuManager); topMenuManager.updateAll(true); addListeners(toolItem, menu); return menu; }
/** * Creates a top-level menu and adds it to the Riena menu bar. * * @param menuManager * @param listener */ private ToolItem createAndAddMenu( final MenuManager menuManager, final ToolBarMenuListener listener) { if (menuManager.isVisible()) { final ToolItem toolItem = new ToolItem(menuToolBar, SWT.CHECK); toolItem.setText(menuManager.getMenuText()); final MenuManagerHelper helper = new MenuManagerHelper(); final Menu menu = helper.createMenu(menuToolBar, toolItem, menuManager); toolItem.setData(MENU_DATA_KEY, menu); calcSize(menuCoolItem); toolItem.addSelectionListener(listener); return toolItem; } return null; }
private void makeEntry(final IContributionItem item) { IContributionItem tempItem = null; if (item instanceof SubContributionItem) { SubContributionItem subItem = (SubContributionItem) item; tempItem = subItem.getInnerItem(); } else if (item instanceof MenuManager) { tempItem = item; } if (tempItem != null && tempItem instanceof MenuManager) { final MenuManager manager = (MenuManager) tempItem; int style = extractStyle(manager); final ToolItem toolItem = new ToolItem(toolbar, style); toolItem.setText(manager.getMenuText()); toolItem.setData(WidgetUtil.CUSTOM_VARIANT, MENU_BAR_VARIANT); createMenu(manager, toolItem); // needed to clear all controls in case of an update toolItemList.add(toolItem); } }
private void createMenu(final MenuManager manager, final ToolItem toolItem) { final Menu menu = new Menu(menuParent); toolItem.setData(menu); menu.setData(WidgetUtil.CUSTOM_VARIANT, MENU_BAR_VARIANT); toolItem.addSelectionListener( new SelectionAdapter() { public void widgetSelected(final SelectionEvent e) { // cleanup the menu MenuItem[] menuItems = menu.getItems(); for (int i = 0; i < menuItems.length; i++) { menuItems[i].dispose(); } hookMenuToToolItem(manager, menu); // set the menu position Display display = toolItem.getDisplay(); Rectangle bounds = toolItem.getBounds(); int leftIndent = bounds.x; int topIndent = bounds.y + bounds.height; Point indent = new Point(leftIndent, topIndent); Point menuLocation = display.map(toolbar, toolbar.getShell(), indent); menu.setLocation(menuLocation); // style the menuitems and show the menu menu.setData(WidgetUtil.CUSTOM_VARIANT, MENU_BAR_VARIANT); styleMenuItems(menu); menu.setVisible(true); } private void hookMenuToToolItem(final MenuManager manager, final Menu menu) { IContributionItem[] contribItems = manager.getItems(); if (contribItems != null && contribItems.length > 0) { for (int i = 0; i < contribItems.length; i++) { if (i > 0 || !(contribItems[i] instanceof Separator)) { contribItems[i].fill(menu, -1); } } } }; }); }
@Override public void fill(final ToolBar parent, final int index) { if (this.widget != null || parent == null) { return; } ToolItem item = null; if (index >= 0) { item = new ToolItem(parent, this.style, index); } else { item = new ToolItem(parent, this.style); } item.setData(this); item.addListener(SWT.Selection, getItemListener()); item.addListener(SWT.Dispose, getItemListener()); this.widget = item; update(null); updateIcons(); }
@Override protected Control createControl(Composite parent) { bar = new ToolBar(parent, SWT.HORIZONTAL | SWT.FLAT | SWT.RIGHT); ToolItem item; for (IPerspectiveDescriptor desc : PlatformUI.getWorkbench().getPerspectiveRegistry().getPerspectives()) { item = new ToolItem(bar, SWT.CHECK); item.setImage(desc.getImageDescriptor().createImage()); item.setText(desc.getLabel()); item.setToolTipText(desc.getDescription()); item.setSelection( desc.getId() .equals(PlatformUI.getWorkbench().getPerspectiveRegistry().getDefaultPerspective())); item.setData(desc.getId()); item.addSelectionListener( new SelectionListenerWithData<String>(desc.getId()) { public void widgetSelected(SelectionEvent e) { selectPerspective(data()); } }); } return bar; }
/* * (non-Javadoc) * * @see org.eclipse.jface.action.ContributionItem#fill(org.eclipse.swt.widgets.ToolBar, * int) */ public void fill(ToolBar parent, int index) { if (command == null) { return; } if (widget != null || parent == null) { return; } ToolItem item = null; if (index >= 0) { item = new ToolItem(parent, style, index); } else { item = new ToolItem(parent, style); } item.setData(this); item.addListener(SWT.Selection, getItemListener()); item.addListener(SWT.Dispose, getItemListener()); widget = item; update(null); }
/** * Creates all items * * @param toolbar * @param items * @param label */ private void createItems(ToolBar toolbar, List<MainMenuItem> items, String label) { // For each item for (final MainMenuItem item : items) { // Skip items that are not buttons if (!item.isButton()) { continue; } // Create group if (item instanceof MainMenuGroup) { MainMenuGroup group = (MainMenuGroup) item; if (!this.toolitems.isEmpty()) { ToolItem menuItem = new ToolItem(toolbar, SWT.SEPARATOR); menuItem.setEnabled(false); menuItem.setData(item); this.toolitems.add(menuItem); } createItems( toolbar, group.getItems(), label.length() != 0 ? label + " -> " + group.getLabel() : group.getLabel()); // $NON-NLS-1$ // Create separator } else if (item instanceof MainMenuSeparator) { if (!this.toolitems.isEmpty()) { ToolItem menuItem = new ToolItem(toolbar, SWT.SEPARATOR); this.toolitems.add(menuItem); } // Create item } else { ToolItem menuItem = new ToolItem(toolbar, SWT.PUSH); menuItem.setToolTipText( label.length() != 0 ? label + " -> " + item.getLabel() : item.getLabel()); // $NON-NLS-1$ if (item.getImage() != null) { menuItem.setImage(item.getImage()); SWTUtil.createDisabledImage(menuItem); } menuItem.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(final SelectionEvent e) { item.action(getController()); } }); menuItem.setData(item); menuItem.setEnabled(false); this.toolitems.add(menuItem); } } }
public void createPartControl(org.eclipse.swt.widgets.Composite frame) { final org.eclipse.swt.examples.hoverhelp.HoverHelp.ToolTipHandler tooltip = new org.eclipse.swt.examples.hoverhelp.HoverHelp.ToolTipHandler(frame.getShell()); org.eclipse.swt.layout.GridLayout layout = new org.eclipse.swt.layout.GridLayout(); layout.numColumns = 3; frame.setLayout(layout); java.lang.String platform = SWT.getPlatform(); java.lang.String helpKey = "F1"; if (platform.equals("gtk")) { helpKey = "Ctrl+F1"; } if (platform.equals("carbon") || platform.equals("cocoa")) { helpKey = "Help"; } org.eclipse.swt.widgets.ToolBar bar = new org.eclipse.swt.widgets.ToolBar(frame, SWT.BORDER); for (int i = 0; i < 5; i++) { org.eclipse.swt.widgets.ToolItem item = new org.eclipse.swt.widgets.ToolItem(bar, SWT.PUSH); item.setText( getResourceString("ToolItem.text", new java.lang.Object[] {new java.lang.Integer(i)})); item.setData( "TIP_TEXT", getResourceString("ToolItem.tooltip", new java.lang.Object[] {item.getText(), helpKey})); item.setData( "TIP_HELPTEXTHANDLER", new org.eclipse.swt.examples.hoverhelp.HoverHelp.ToolTipHelpTextHandler() { public java.lang.String getHelpText(org.eclipse.swt.widgets.Widget widget) { org.eclipse.swt.widgets.Item item = (org.eclipse.swt.widgets.Item) widget; return getResourceString("ToolItem.help", new java.lang.Object[] {item.getText()}); } }); } org.eclipse.swt.layout.GridData gridData = new org.eclipse.swt.layout.GridData(); gridData.horizontalSpan = 3; bar.setLayoutData(gridData); tooltip.activateHoverHelp(bar); org.eclipse.swt.widgets.Table table = new org.eclipse.swt.widgets.Table(frame, SWT.BORDER); for (int i = 0; i < 4; i++) { org.eclipse.swt.widgets.TableItem item = new org.eclipse.swt.widgets.TableItem(table, SWT.PUSH); item.setText(getResourceString("Item", new java.lang.Object[] {new java.lang.Integer(i)})); item.setData("TIP_IMAGE", images[hhiInformation]); item.setText( getResourceString("TableItem.text", new java.lang.Object[] {new java.lang.Integer(i)})); item.setData( "TIP_TEXT", getResourceString("TableItem.tooltip", new java.lang.Object[] {item.getText(), helpKey})); item.setData( "TIP_HELPTEXTHANDLER", new org.eclipse.swt.examples.hoverhelp.HoverHelp.ToolTipHelpTextHandler() { public java.lang.String getHelpText(org.eclipse.swt.widgets.Widget widget) { org.eclipse.swt.widgets.Item item = (org.eclipse.swt.widgets.Item) widget; return getResourceString("TableItem.help", new java.lang.Object[] {item.getText()}); } }); } table.setLayoutData(new org.eclipse.swt.layout.GridData(GridData.VERTICAL_ALIGN_FILL)); tooltip.activateHoverHelp(table); org.eclipse.swt.widgets.Tree tree = new org.eclipse.swt.widgets.Tree(frame, SWT.BORDER); for (int i = 0; i < 4; i++) { org.eclipse.swt.widgets.TreeItem item = new org.eclipse.swt.widgets.TreeItem(tree, SWT.PUSH); item.setText(getResourceString("Item", new java.lang.Object[] {new java.lang.Integer(i)})); item.setData("TIP_IMAGE", images[hhiWarning]); item.setText( getResourceString("TreeItem.text", new java.lang.Object[] {new java.lang.Integer(i)})); item.setData( "TIP_TEXT", getResourceString("TreeItem.tooltip", new java.lang.Object[] {item.getText(), helpKey})); item.setData( "TIP_HELPTEXTHANDLER", new org.eclipse.swt.examples.hoverhelp.HoverHelp.ToolTipHelpTextHandler() { public java.lang.String getHelpText(org.eclipse.swt.widgets.Widget widget) { org.eclipse.swt.widgets.Item item = (org.eclipse.swt.widgets.Item) widget; return getResourceString("TreeItem.help", new java.lang.Object[] {item.getText()}); } }); } tree.setLayoutData(new org.eclipse.swt.layout.GridData(GridData.VERTICAL_ALIGN_FILL)); tooltip.activateHoverHelp(tree); org.eclipse.swt.widgets.Button button = new org.eclipse.swt.widgets.Button(frame, SWT.PUSH); button.setText(getResourceString("Hello.text")); button.setData("TIP_TEXT", getResourceString("Hello.tooltip")); tooltip.activateHoverHelp(button); }
/** * Attempts to update the existing toolbar actions, if the action list is similar to the current * list. Returns false if this cannot be done and the contents must be replaced. */ private boolean updateActions(@NonNull List<RuleAction> actions) { List<RuleAction> before = mPrevActions; List<RuleAction> after = actions; if (before == null) { return false; } if (!before.equals(after) || after.size() > mLayoutToolBar.getItemCount()) { return false; } int actionIndex = 0; for (int i = 0, max = mLayoutToolBar.getItemCount(); i < max; i++) { ToolItem item = mLayoutToolBar.getItem(i); int style = item.getStyle(); Object data = item.getData(); if (data != null) { // One action can result in multiple toolbar items (e.g. a choice action // can result in multiple radio buttons), so we've have to replace all of // them with the corresponding new action RuleAction prevAction = before.get(actionIndex); while (prevAction != data) { actionIndex++; if (actionIndex == before.size()) { return false; } prevAction = before.get(actionIndex); if (prevAction == data) { break; } else if (!(prevAction instanceof RuleAction.Separator)) { return false; } } RuleAction newAction = after.get(actionIndex); assert newAction.equals(prevAction); // Maybe I can do this lazily instead? // Update action binding to the new action item.setData(newAction); // Sync button states: the checked state is not considered part of // RuleAction equality if ((style & SWT.CHECK) != 0) { assert newAction instanceof Toggle; Toggle toggle = (Toggle) newAction; item.setSelection(toggle.isChecked()); } else if ((style & SWT.RADIO) != 0) { assert newAction instanceof Choices; Choices choices = (Choices) newAction; String current = choices.getCurrent(); String id = (String) item.getData(ATTR_ID); boolean selected = Strings.nullToEmpty(current).equals(id); item.setSelection(selected); } } else { // Must be a separator, or a label (which we insert for nested widgets) assert (style & SWT.SEPARATOR) != 0 || !item.getText().isEmpty() : item; } } return true; }
private ToolItem addPerspectiveItem(MPerspective persp) { int perspIndex = persp.getParent().getChildren().indexOf(persp); int index = perspIndex + 2; // HACK !! accounts for the 'open' and the // separator final ToolItem psItem = index < psTB.getItemCount() ? new ToolItem(psTB, SWT.RADIO, index) : new ToolItem(psTB, SWT.RADIO); psItem.setData(persp); IPerspectiveDescriptor descriptor = getDescriptorFor(persp.getElementId()); boolean foundImage = false; if (descriptor != null) { ImageDescriptor desc = descriptor.getImageDescriptor(); if (desc != null) { final Image image = desc.createImage(false); if (image != null) { psItem.setImage(image); psItem.addListener( SWT.Dispose, new Listener() { public void handleEvent(org.eclipse.swt.widgets.Event event) { image.dispose(); } }); foundImage = true; psItem.setToolTipText(persp.getLocalizedLabel()); } } } if (!foundImage || PrefUtil.getAPIPreferenceStore() .getBoolean(IWorkbenchPreferenceConstants.SHOW_TEXT_ON_PERSPECTIVE_BAR)) { psItem.setText(persp.getLocalizedLabel()); psItem.setToolTipText(persp.getLocalizedTooltip()); } psItem.setSelection(persp == persp.getParent().getSelectedElement()); psItem.addSelectionListener( new SelectionListener() { public void widgetSelected(SelectionEvent e) { MPerspective persp = (MPerspective) e.widget.getData(); persp.getParent().setSelectedElement(persp); } public void widgetDefaultSelected(SelectionEvent e) { MPerspective persp = (MPerspective) e.widget.getData(); persp.getParent().setSelectedElement(persp); } }); psItem.addListener( SWT.MenuDetect, new Listener() { public void handleEvent(org.eclipse.swt.widgets.Event event) { MPerspective persp = (MPerspective) event.widget.getData(); openMenuFor(psItem, persp); } }); // update the size fixSize(); return psItem; }