@Override public void fill(final Menu menu, final int index) { final IScaEditorDescriptor defaultEditor = ScaContentTypeRegistry.INSTANCE.getScaEditorDescriptor(this.selection); final IScaEditorDescriptor[] editors = ScaContentTypeRegistry.INSTANCE.getAllScaEditorDescriptors(this.selection); Collections.sort(Arrays.asList(editors), OpenWithMenu.COMPARER); // Check that we don't add it twice. This is possible // if the same editor goes to two mappings. final Set<IScaEditorDescriptor> alreadyMapped = new HashSet<IScaEditorDescriptor>(); for (final IScaEditorDescriptor editor : editors) { if (!alreadyMapped.contains(editor)) { createMenuItem(menu, editor); if (defaultEditor != null && defaultEditor .getEditorDescriptor() .getId() .equals(editor.getEditorDescriptor().getId())) { continue; } alreadyMapped.add(editor); } } }
private Image getImage(final IScaEditorDescriptor descriptor) { if (descriptor == null || descriptor.getEditorDescriptor() == null || descriptor.getEditorDescriptor().getImageDescriptor() == null) { return null; } return ResourceRegistry.INSTANCE .getResourceManager() .createImage(descriptor.getEditorDescriptor().getImageDescriptor()); }
/** * Creates the menu item for the editor descriptor. * * @param menu the menu to add the item to * @param descriptor the editor descriptor, or null for the system editor * @param preferredEditor the descriptor of the preferred editor, or <code>null</code> */ private void createMenuItem(final Menu menu, final IScaEditorDescriptor descriptor) { // XXX: Would be better to use bold here, but SWT does not support it. final MenuItem menuItem = new MenuItem(menu, SWT.None); menuItem.setText(descriptor.getEditorDescriptor().getLabel()); final Image image = getImage(descriptor); if (image != null) { menuItem.setImage(image); } final Listener listener = new Listener() { @Override public void handleEvent(final Event event) { switch (event.type) { case SWT.Selection: OpenEditorUtil.openEditor(page, descriptor); break; default: break; } } }; menuItem.addListener(SWT.Selection, listener); }
@Override public int compare(final IScaEditorDescriptor arg0, final IScaEditorDescriptor arg1) { final String s1 = arg0.getEditorDescriptor().getLabel(); final String s2 = arg1.getEditorDescriptor().getLabel(); return this.collator.compare(s1, s2); }