| /*=============================================================================# |
| # Copyright (c) 2009, 2020 Stephan Wahlbrink and others. |
| # |
| # This program and the accompanying materials are made available under the |
| # terms of the Eclipse Public License 2.0 which is available at |
| # https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| # which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| # |
| # SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| # |
| # Contributors: |
| # Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation |
| #=============================================================================*/ |
| |
| package org.eclipse.statet.ltk.ui.sourceediting; |
| |
| import java.util.Arrays; |
| |
| import org.eclipse.jface.action.Action; |
| import org.eclipse.jface.action.IMenuManager; |
| import org.eclipse.jface.action.Separator; |
| import org.eclipse.jface.text.IRegion; |
| import org.eclipse.jface.text.TextSelection; |
| import org.eclipse.ui.IWorkbenchCommandConstants; |
| import org.eclipse.ui.handlers.IHandlerService; |
| import org.eclipse.ui.menus.CommandContributionItem; |
| import org.eclipse.ui.menus.CommandContributionItemParameter; |
| import org.eclipse.ui.part.IPageSite; |
| import org.eclipse.ui.services.IServiceLocator; |
| |
| import org.eclipse.statet.ecommons.ui.SharedUIResources; |
| import org.eclipse.statet.ecommons.ui.actions.HandlerCollection; |
| |
| import org.eclipse.statet.internal.ltk.ui.EditingMessages; |
| import org.eclipse.statet.ltk.model.core.elements.ISourceStructElement; |
| import org.eclipse.statet.ltk.refactoring.core.CommonRefactoringFactory; |
| import org.eclipse.statet.ltk.refactoring.core.RefactoringAdapter; |
| import org.eclipse.statet.ltk.ui.refactoring.AbstractElementsHandler; |
| import org.eclipse.statet.ltk.ui.refactoring.CopyElementsHandler; |
| import org.eclipse.statet.ltk.ui.refactoring.CopyNamesHandler; |
| import org.eclipse.statet.ltk.ui.refactoring.CutElementsHandler; |
| import org.eclipse.statet.ltk.ui.refactoring.DeleteElementsHandler; |
| import org.eclipse.statet.ltk.ui.refactoring.PasteElementsHandler; |
| import org.eclipse.statet.ltk.ui.util.LTKSelectionUtils; |
| |
| |
| public abstract class SourceEditor2OutlinePage extends SourceEditor1OutlinePage { |
| |
| |
| protected class SelectCodeRangeAction extends Action { |
| |
| |
| public SelectCodeRangeAction(final CommonRefactoringFactory refactoring) { |
| super(); |
| setText(EditingMessages.SelectSourceCode_label); |
| |
| setEnabled(!getSelection().isEmpty()); |
| } |
| |
| @Override |
| public void run() { |
| final ISourceStructElement[] elements= LTKSelectionUtils.getSelectedSourceStructElements(getSelection()); |
| if (elements != null) { |
| RefactoringAdapter adapter= SourceEditor2OutlinePage.this.refactoring.createAdapter(elements); |
| if (adapter == null) { |
| adapter= SourceEditor2OutlinePage.this.refactoring.createAdapter(null); |
| } |
| Arrays.sort(elements, adapter.getModelElementComparator()); |
| final IRegion range= adapter.getContinuousSourceRange(elements); |
| if (range != null) { |
| selectInEditor(new TextSelection(range.getOffset(), range.getLength())); |
| } |
| } |
| } |
| |
| } |
| |
| |
| private final CommonRefactoringFactory refactoring; |
| |
| |
| public SourceEditor2OutlinePage(final SourceEditor1 editor, final String mainType, |
| final CommonRefactoringFactory refactoring, final String contextMenuId) { |
| super(editor, mainType, contextMenuId); |
| this.refactoring= refactoring; |
| } |
| |
| |
| protected CommonRefactoringFactory getRefactoringFactory() { |
| return this.refactoring; |
| } |
| |
| @Override |
| protected void initActions(final IServiceLocator serviceLocator, final HandlerCollection handlers) { |
| super.initActions(serviceLocator, handlers); |
| |
| final IHandlerService handlerService= serviceLocator.getService(IHandlerService.class); |
| { final AbstractElementsHandler handler= new CutElementsHandler(this.refactoring); |
| handlers.add(IWorkbenchCommandConstants.EDIT_CUT, handler); |
| registerHandlerToUpdate(handler); |
| handlerService.activateHandler(IWorkbenchCommandConstants.EDIT_CUT, handler); |
| } |
| { final AbstractElementsHandler handler= new CopyElementsHandler(this.refactoring); |
| handlers.add(IWorkbenchCommandConstants.EDIT_COPY, handler); |
| registerHandlerToUpdate(handler); |
| handlerService.activateHandler(IWorkbenchCommandConstants.EDIT_COPY, handler); |
| } |
| { final AbstractElementsHandler handler= new CopyNamesHandler(this.refactoring); |
| handlers.add(ISourceEditorCommandIds.COPY_ELEMENT_NAME, handler); |
| registerHandlerToUpdate(handler); |
| handlerService.activateHandler(ISourceEditorCommandIds.COPY_ELEMENT_NAME, handler); |
| } |
| { final AbstractElementsHandler handler= new PasteElementsHandler(getSourceEditor(), this.refactoring); |
| handlers.add(IWorkbenchCommandConstants.EDIT_PASTE, handler); |
| handlerService.activateHandler(IWorkbenchCommandConstants.EDIT_PASTE, handler); |
| } |
| { final AbstractElementsHandler handler= new DeleteElementsHandler(this.refactoring); |
| handlers.add(IWorkbenchCommandConstants.EDIT_DELETE, handler); |
| handlerService.activateHandler(IWorkbenchCommandConstants.EDIT_DELETE, handler); |
| } |
| } |
| |
| @Override |
| protected void contextMenuAboutToShow(final IMenuManager m) { |
| final IPageSite site= getSite(); |
| |
| m.add(new SelectCodeRangeAction(this.refactoring)); |
| |
| m.add(new Separator(SharedUIResources.EDIT_COPYPASTE_MENU_ID)); |
| m.add(new CommandContributionItem(new CommandContributionItemParameter( |
| site, null, IWorkbenchCommandConstants.EDIT_CUT, CommandContributionItem.STYLE_PUSH))); |
| m.add(new CommandContributionItem(new CommandContributionItemParameter( |
| site, null, IWorkbenchCommandConstants.EDIT_COPY, CommandContributionItem.STYLE_PUSH))); |
| m.add(new CommandContributionItem(new CommandContributionItemParameter( |
| site, null, ISourceEditorCommandIds.COPY_ELEMENT_NAME, CommandContributionItem.STYLE_PUSH))); |
| m.add(new CommandContributionItem(new CommandContributionItemParameter( |
| site, null, IWorkbenchCommandConstants.EDIT_PASTE, CommandContributionItem.STYLE_PUSH))); |
| |
| super.contextMenuAboutToShow(m); |
| } |
| |
| } |