/** * 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 MenuItem createMenuItem( Menu menu, final IEditorDescriptor descriptor, final IEditorDescriptor preferredEditor) { // XXX: Would be better to use bold here, but SWT does not support it. final MenuItem menuItem = new MenuItem(menu, SWT.RADIO); boolean isPreferred = preferredEditor != null && descriptor.getId().equals(preferredEditor.getId()); menuItem.setSelection(isPreferred); menuItem.setText(descriptor.getLabel()); Image image = getImage(descriptor); if (image != null) { menuItem.setImage(image); } Listener listener = new Listener() { public void handleEvent(Event event) { switch (event.type) { case SWT.Selection: if (menuItem.getSelection()) { openEditor(descriptor, false); } break; } } }; menuItem.addListener(SWT.Selection, listener); return menuItem; }
/** Opens the given editor on the selected file revision. */ protected void openEditor(IEditorDescriptor editorDescriptor, boolean openUsingDescriptor) { IFileRevision fileRevision = getFileRevision(); if (fileRevision == null) { return; } try { IProgressMonitor monitor = new NullProgressMonitor(); IStorage storage = fileRevision.getStorage(monitor); boolean isFile = storage instanceof IFile; if (openUsingDescriptor) { // discouraged access to open system editors ((WorkbenchPage) (page.getSite().getPage())) .openEditorFromDescriptor( isFile ? new FileEditorInput((IFile) storage) : (IEditorInput) FileRevisionEditorInput.createEditorInputFor(fileRevision, monitor), editorDescriptor, true, null); } else { String editorId = editorDescriptor == null ? IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID : editorDescriptor.getId(); page.getSite() .getPage() .openEditor( isFile ? new FileEditorInput((IFile) storage) : (IEditorInput) FileRevisionEditorInput.createEditorInputFor(fileRevision, monitor), editorId, true, MATCH_BOTH); } } catch (PartInitException e) { StatusAdapter statusAdapter = new StatusAdapter(e.getStatus()); statusAdapter.setProperty( IStatusAdapterConstants.TITLE_PROPERTY, TeamUIMessages.LocalHistoryPage_OpenEditorError); StatusManager.getManager().handle(statusAdapter, StatusManager.SHOW); } catch (CoreException e) { StatusAdapter statusAdapter = new StatusAdapter(e.getStatus()); statusAdapter.setProperty( IStatusAdapterConstants.TITLE_PROPERTY, TeamUIMessages.LocalHistoryPage_OpenEditorError); StatusManager.getManager().handle(statusAdapter, StatusManager.LOG); } }
/** Returns the image descriptor for the given editor descriptor, or null if it has no image. */ private ImageDescriptor getImageDescriptor(IEditorDescriptor editorDesc) { ImageDescriptor imageDesc = null; if (editorDesc == null) { imageDesc = registry.getImageDescriptor(getFileRevision().getName()); // TODO: is this case valid, and if so, what are the implications // for content-type editor bindings? } else { imageDesc = editorDesc.getImageDescriptor(); } if (imageDesc == null) { if (editorDesc.getId().equals(IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID)) { imageDesc = registry.getSystemExternalEditorImageDescriptor(getFileRevision().getName()); } } return imageDesc; }
public void fill(Menu menu, int index) { final IFileRevision fileRevision = getFileRevision(); if (fileRevision == null) { return; } IEditorDescriptor defaultTextEditor = registry.findEditor("org.eclipse.ui.DefaultTextEditor"); // $NON-NLS-1$ IEditorDescriptor preferredEditor = Utils.getDefaultEditor(fileRevision); Object[] editors = Utils.getEditors(fileRevision); Collections.sort(Arrays.asList(editors), comparer); boolean defaultFound = false; // Check that we don't add it twice. This is possible // if the same editor goes to two mappings. ArrayList alreadyMapped = new ArrayList(); for (int i = 0; i < editors.length; i++) { IEditorDescriptor editor = (IEditorDescriptor) editors[i]; if (!alreadyMapped.contains(editor)) { createMenuItem(menu, editor, preferredEditor); if (defaultTextEditor != null && editor.getId().equals(defaultTextEditor.getId())) { defaultFound = true; } alreadyMapped.add(editor); } } // Only add a separator if there is something to separate if (editors.length > 0) { new MenuItem(menu, SWT.SEPARATOR); } // Add default editor. Check it if it is saved as the preference. if (!defaultFound && defaultTextEditor != null) { createMenuItem(menu, defaultTextEditor, preferredEditor); } // TODO : We might perhaps enable inplace and system external editors menu items /*// Add system editor IEditorDescriptor descriptor = registry .findEditor(IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID); final MenuItem systemEditorMenuItem = createMenuItem(menu, descriptor, preferredEditor); systemEditorMenuItem.setEnabled(false); // Add system in-place editor descriptor = registry .findEditor(IEditorRegistry.SYSTEM_INPLACE_EDITOR_ID); final MenuItem inPlaceEditorMenuItem = (descriptor != null) ? createMenuItem( menu, descriptor, preferredEditor) : null; if (inPlaceEditorMenuItem != null) inPlaceEditorMenuItem.setEnabled(false); Job job = new Job("updateOpenWithMenu") { //$NON-NLS-1$ protected IStatus run(IProgressMonitor monitor) { try { final boolean isFile = fileRevision.getStorage(monitor) instanceof IFile; Display.getDefault().asyncExec(new Runnable() { public void run() { if (inPlaceEditorMenuItem != null && !inPlaceEditorMenuItem.isDisposed()) inPlaceEditorMenuItem.setEnabled(isFile); if (!systemEditorMenuItem.isDisposed()) systemEditorMenuItem.setEnabled(isFile); } }); return Status.OK_STATUS; } catch (CoreException e) { return new Status(IStatus.WARNING, TeamUIPlugin.ID, null, e); } }; }; job.setSystem(true); job.schedule();*/ createDefaultMenuItem(menu, fileRevision); // add Other... menu item createOtherMenuItem(menu); }