|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2024 Eclipse Foundation and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * Eclipse Foundation - initial API and implementation |
| 13 | + *******************************************************************************/ |
| 14 | +package org.eclipse.search2.internal.ui.basic.views; |
| 15 | + |
| 16 | +import java.util.HashMap; |
| 17 | + |
| 18 | +import org.eclipse.core.commands.AbstractHandler; |
| 19 | +import org.eclipse.core.commands.Command; |
| 20 | +import org.eclipse.core.commands.ExecutionEvent; |
| 21 | +import org.eclipse.core.commands.ExecutionException; |
| 22 | +import org.eclipse.core.commands.NotEnabledException; |
| 23 | +import org.eclipse.core.commands.NotHandledException; |
| 24 | +import org.eclipse.core.commands.ParameterizedCommand; |
| 25 | +import org.eclipse.core.commands.common.NotDefinedException; |
| 26 | +import org.eclipse.core.runtime.CoreException; |
| 27 | +import org.eclipse.core.runtime.IConfigurationElement; |
| 28 | +import org.eclipse.core.runtime.IExecutableExtension; |
| 29 | +import org.eclipse.swt.widgets.Event; |
| 30 | +import org.eclipse.ui.IWorkbenchCommandConstants; |
| 31 | +import org.eclipse.ui.IWorkbenchWindow; |
| 32 | +import org.eclipse.ui.commands.ICommandService; |
| 33 | +import org.eclipse.ui.handlers.HandlerUtil; |
| 34 | +import org.eclipse.ui.handlers.IHandlerService; |
| 35 | + |
| 36 | +/** |
| 37 | + * Global handler for navigating to next/previous search results without requiring |
| 38 | + * focus on the Search view. This handler provides a seamless workflow for |
| 39 | + * navigating through search results while editing. |
| 40 | + * |
| 41 | + * @since 3.17 |
| 42 | + */ |
| 43 | +public class GlobalNextPrevSearchEntryHandler extends AbstractHandler implements IExecutableExtension { |
| 44 | + private String searchCommand = IWorkbenchCommandConstants.NAVIGATE_NEXT; |
| 45 | + |
| 46 | + @Override |
| 47 | + public Object execute(ExecutionEvent event) throws ExecutionException { |
| 48 | + IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event); |
| 49 | + ICommandService cs = (ICommandService) window.getService(ICommandService.class); |
| 50 | + |
| 51 | + // Show the Search view |
| 52 | + Command showView = cs.getCommand(IWorkbenchCommandConstants.VIEWS_SHOW_VIEW); |
| 53 | + HashMap<String, Object> parms = new HashMap<String, Object>(); |
| 54 | + parms.put(IWorkbenchCommandConstants.VIEWS_SHOW_VIEW_PARM_ID, "org.eclipse.search.ui.views.SearchView"); |
| 55 | + ParameterizedCommand showSearchView = ParameterizedCommand.generateCommand(showView, parms); |
| 56 | + |
| 57 | + IHandlerService hs = window.getService(IHandlerService.class); |
| 58 | + try { |
| 59 | + // Execute the sequence: show search view -> navigate -> activate editor |
| 60 | + hs.executeCommand(showSearchView, (Event)event.getTrigger()); |
| 61 | + hs.executeCommand(searchCommand, (Event)event.getTrigger()); |
| 62 | + hs.executeCommand(IWorkbenchCommandConstants.WINDOW_ACTIVATE_EDITOR, (Event)event.getTrigger()); |
| 63 | + } catch (NotDefinedException e) { |
| 64 | + throw new ExecutionException(e.getMessage(), e); |
| 65 | + } catch (NotEnabledException e) { |
| 66 | + throw new ExecutionException(e.getMessage(), e); |
| 67 | + } catch (NotHandledException e) { |
| 68 | + throw new ExecutionException(e.getMessage(), e); |
| 69 | + } |
| 70 | + |
| 71 | + return null; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void setInitializationData(IConfigurationElement config, String propertyName, Object data) throws CoreException { |
| 76 | + if ("previous".equals(data)) { |
| 77 | + searchCommand = IWorkbenchCommandConstants.NAVIGATE_PREVIOUS; |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments