|
13 | 13 | *******************************************************************************/ |
14 | 14 | package org.eclipse.ui.internal.ide.dialogs; |
15 | 15 |
|
| 16 | +import java.util.ArrayList; |
16 | 17 | import java.util.Collections; |
17 | 18 |
|
18 | 19 | import org.eclipse.core.resources.IContainer; |
|
57 | 58 | import org.eclipse.ui.internal.IWorkbenchGraphicConstants; |
58 | 59 | import org.eclipse.ui.internal.ShowInMenu; |
59 | 60 | import org.eclipse.ui.internal.WorkbenchImages; |
| 61 | +import org.eclipse.ui.internal.WorkbenchPage; |
60 | 62 | import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; |
61 | 63 | import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; |
62 | 64 | import org.eclipse.ui.part.IShowInTarget; |
63 | 65 | import org.eclipse.ui.part.ShowInContext; |
64 | 66 | import org.eclipse.ui.statushandlers.StatusManager; |
65 | 67 | import org.eclipse.ui.views.IViewDescriptor; |
| 68 | +import org.eclipse.ui.views.IViewRegistry; |
66 | 69 |
|
67 | 70 |
|
68 | 71 | /** |
@@ -293,13 +296,73 @@ protected void updateButtonsEnableState(IStatus status) { |
293 | 296 | openWithButton.setEnabled(getSelectedItems().size() == 1); |
294 | 297 | showInButton.setEnabled(true); |
295 | 298 | } else { |
296 | | - okButton.setEnabled(false); |
| 299 | + // Exactly one non-file resource (typically a project): OK triggers |
| 300 | + // the first available Show In target. Multiple non-file resources |
| 301 | + // don't make sense for Show In, so leave OK disabled there. |
| 302 | + okButton.setEnabled(getSelectedItems().size() == 1); |
297 | 303 | openWithButton.setEnabled(false); |
298 | 304 | showInButton.setEnabled(true); |
299 | 305 | } |
300 | 306 | } |
301 | 307 | } |
302 | 308 |
|
| 309 | + @Override |
| 310 | + protected void okPressed() { |
| 311 | + IStructuredSelection selected = getSelectedItems(); |
| 312 | + Button okButton = getOkButton(); |
| 313 | + if (selected.size() == 1 && !(selected.getFirstElement() instanceof IFile) |
| 314 | + && okButton != null && okButton.isEnabled()) { |
| 315 | + if (showSelectionInFirstShowInTarget(selected)) { |
| 316 | + return; |
| 317 | + } |
| 318 | + } |
| 319 | + super.okPressed(); |
| 320 | + } |
| 321 | + |
| 322 | + /** Shows the selection in the first available Show In target. */ |
| 323 | + private boolean showSelectionInFirstShowInTarget(IStructuredSelection selection) { |
| 324 | + IWorkbenchPage activePage = getActivePage(); |
| 325 | + if (!(activePage instanceof WorkbenchPage)) { |
| 326 | + return false; |
| 327 | + } |
| 328 | + WorkbenchPage workbenchPage = (WorkbenchPage) activePage; |
| 329 | + |
| 330 | + ArrayList<String> ids = new ArrayList<>(workbenchPage.getShowInPartIds()); |
| 331 | + if (ids.isEmpty()) { |
| 332 | + return false; |
| 333 | + } |
| 334 | + workbenchPage.sortShowInPartIds(ids); |
| 335 | + |
| 336 | + IViewRegistry registry = PlatformUI.getWorkbench().getViewRegistry(); |
| 337 | + IViewDescriptor descriptor = null; |
| 338 | + for (String id : ids) { |
| 339 | + IViewDescriptor candidate = registry.find(id); |
| 340 | + if (candidate != null) { |
| 341 | + descriptor = candidate; |
| 342 | + break; |
| 343 | + } |
| 344 | + } |
| 345 | + if (descriptor == null) { |
| 346 | + return false; |
| 347 | + } |
| 348 | + |
| 349 | + computeResult(); |
| 350 | + setResult(Collections.EMPTY_LIST); |
| 351 | + close(); |
| 352 | + |
| 353 | + try { |
| 354 | + IViewPart view = activePage.showView(descriptor.getId()); |
| 355 | + IShowInTarget target = Adapters.adapt(view, IShowInTarget.class); |
| 356 | + if (target == null || !target.show(new ShowInContext(null, selection))) { |
| 357 | + activePage.getWorkbenchWindow().getShell().getDisplay().beep(); |
| 358 | + } |
| 359 | + } catch (PartInitException e) { |
| 360 | + StatusManager.getManager().handle(new Status(IStatus.ERROR, IDEWorkbenchPlugin.IDE_WORKBENCH, |
| 361 | + IStatus.ERROR, "", e)); //$NON-NLS-1$ |
| 362 | + } |
| 363 | + return true; |
| 364 | + } |
| 365 | + |
303 | 366 | private boolean isButtonReady(Button button) { |
304 | 367 | return button != null && !button.isDisposed(); |
305 | 368 | } |
|
0 commit comments