| /*=============================================================================# |
| # 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.nico.ui.util; |
| |
| import java.util.Map; |
| |
| import org.eclipse.core.filesystem.IFileStore; |
| import org.eclipse.core.resources.IFile; |
| import org.eclipse.core.resources.IWorkspaceRoot; |
| import org.eclipse.core.resources.ResourcesPlugin; |
| import org.eclipse.core.runtime.CoreException; |
| import org.eclipse.swt.widgets.Display; |
| import org.eclipse.ui.IWorkbenchPage; |
| import org.eclipse.ui.PartInitException; |
| import org.eclipse.ui.ide.IDE; |
| |
| import org.eclipse.statet.jcommons.status.ErrorStatus; |
| import org.eclipse.statet.jcommons.status.ProgressMonitor; |
| import org.eclipse.statet.jcommons.status.Status; |
| import org.eclipse.statet.jcommons.status.Statuses; |
| import org.eclipse.statet.jcommons.ts.core.util.ToolCommandHandlerUtils; |
| |
| import org.eclipse.statet.ecommons.runtime.core.util.StatusUtils; |
| import org.eclipse.statet.ecommons.ui.util.UIAccess; |
| |
| import org.eclipse.statet.internal.nico.ui.AbstractConsoleCommandHandler; |
| import org.eclipse.statet.nico.core.runtime.ConsoleService; |
| import org.eclipse.statet.nico.core.runtime.IToolEventHandler; |
| import org.eclipse.statet.nico.core.runtime.ToolWorkspace; |
| import org.eclipse.statet.nico.ui.NicoUI; |
| import org.eclipse.statet.nico.ui.views.HistoryView; |
| |
| |
| /** |
| * Handles in Eclipse IDE Platform: |
| * {@link IToolEventHandler#SHOW_HISTORY_ID} - shows the history view |
| * |
| */ |
| public class EclipseIDEOperationsHandler extends AbstractConsoleCommandHandler { |
| |
| |
| public static final String SHOW_FILE_ID = "common/showFile"; //$NON-NLS-1$ |
| |
| public static final String SHOW_HISTORY_ID = "common/showHistory"; //$NON-NLS-1$ |
| |
| |
| @Override |
| public Status execute(final String id, final ConsoleService service, final Map<String, Object> data, |
| final ProgressMonitor m) { |
| if (id.equals(SHOW_FILE_ID)) { |
| final IFileStore fileStore; |
| final IFile workspaceFile; |
| String fileName = ToolCommandHandlerUtils.getCheckedData(data, "filename", String.class, false); //$NON-NLS-1$ |
| if (fileName == null) { |
| fileName = ToolCommandHandlerUtils.getCheckedData(data, "fileName", String.class, true); //$NON-NLS-1$ |
| } |
| final ToolWorkspace workspaceData = service.getWorkspaceData(); |
| try { |
| fileStore = workspaceData.toFileStore(fileName); |
| workspaceFile = getWorkspaceFile(fileStore, m); |
| } |
| catch (final CoreException e) { |
| final Status status= new ErrorStatus(NicoUI.BUNDLE_ID, |
| "Failed to resolve filename.", |
| e ); |
| service.handleStatus(status, m); |
| return status; |
| } |
| final Display display = UIAccess.getDisplay(); |
| display.syncExec(new Runnable() { |
| @Override |
| public void run() { |
| final IWorkbenchPage page = NicoUI.getToolRegistry().findWorkbenchPage(service.getTool()); |
| try { |
| if (workspaceFile != null) { |
| IDE.openEditor(page, workspaceFile); |
| } |
| else { |
| IDE.openEditorOnFileStore(page, fileStore); |
| } |
| } |
| catch (final PartInitException e) { |
| final Status status= new ErrorStatus(NicoUI.BUNDLE_ID, |
| "An error occurred when trying open/activate the Editor.", |
| e ); |
| service.handleStatus(status, m); |
| } |
| } |
| }); |
| return Statuses.OK_STATUS; |
| } |
| if (id.equals(SHOW_HISTORY_ID)) { |
| final String pattern = ToolCommandHandlerUtils.getCheckedData(data, "pattern", String.class, false); //$NON-NLS-1$ |
| final Display display = UIAccess.getDisplay(); |
| display.syncExec(new Runnable() { |
| @Override |
| public void run() { |
| try { |
| final IWorkbenchPage page = NicoUI.getToolRegistry().findWorkbenchPage(service.getTool()); |
| final HistoryView view = (HistoryView) page.showView(NicoUI.HISTORY_VIEW_ID); |
| if (pattern != null) { |
| view.search(pattern, false); |
| } |
| } |
| catch (final PartInitException e) { |
| final Status status= new ErrorStatus(NicoUI.BUNDLE_ID, |
| "An error occurred when trying open/activate the History view.", |
| e ); |
| service.handleStatus(status, m); |
| } |
| } |
| }); |
| return Statuses.OK_STATUS; |
| } |
| throw new UnsupportedOperationException(); |
| } |
| |
| private IFile getWorkspaceFile(final IFileStore fileStore, final ProgressMonitor m) throws CoreException { |
| final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); |
| final IFile[] files = root.findFilesForLocationURI(fileStore.toURI()); |
| if (files != null && files.length > 0) { |
| final IFile file = files[0]; |
| if (!file.exists()) { // Bug 549546 |
| file.refreshLocal(0, StatusUtils.convert(m)); |
| } |
| return file; |
| } |
| return null; |
| } |
| |
| } |