| /*=============================================================================# |
| # Copyright (c) 2008, 2017 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.core.runtime; |
| |
| import java.util.Map; |
| |
| import org.eclipse.core.filesystem.EFS; |
| import org.eclipse.core.filesystem.IFileStore; |
| import org.eclipse.core.runtime.CoreException; |
| import org.eclipse.core.runtime.IProgressMonitor; |
| import org.eclipse.core.runtime.IStatus; |
| import org.eclipse.core.runtime.OperationCanceledException; |
| import org.eclipse.core.runtime.Status; |
| import org.eclipse.osgi.util.NLS; |
| |
| import org.eclipse.statet.ecommons.ts.core.ToolCommandHandler; |
| import org.eclipse.statet.ecommons.ts.core.ToolService; |
| import org.eclipse.statet.ecommons.ts.core.util.ToolCommandHandlerUtil; |
| |
| import org.eclipse.statet.internal.nico.core.Messages; |
| import org.eclipse.statet.nico.core.NicoCore; |
| |
| |
| /** |
| * {@link #LOAD_HISTORY_ID} |
| * {@link #SAVE_HISTORY_ID} |
| * {@link #ADDTO_HISTORY_ID} |
| */ |
| public class HistoryOperationsHandler implements ToolCommandHandler { |
| |
| |
| public static final String LOAD_HISTORY_ID = "console/loadHistory"; //$NON-NLS-1$ |
| |
| public static final String SAVE_HISTORY_ID = "console/saveHistory"; //$NON-NLS-1$ |
| |
| public static final String ADDTO_HISTORY_ID = "console/addtoHistory"; //$NON-NLS-1$ |
| |
| |
| @Override |
| public IStatus execute(final String id, final ToolService service, final Map<String, Object> data, final IProgressMonitor monitor) { |
| if (service instanceof IConsoleService) { |
| final IConsoleService console = (IConsoleService) service; |
| if (id.equals(LOAD_HISTORY_ID)) { |
| return loadHistory(console, data, monitor); |
| } |
| if (id.equals(SAVE_HISTORY_ID)) { |
| return saveHistory(console, data, monitor); |
| } |
| if (id.equals(ADDTO_HISTORY_ID)) { |
| final String item = ToolCommandHandlerUtil.getCheckedData(data, "text", String.class, true); //$NON-NLS-1$ |
| console.getTool().getHistory().addCommand(item, console.getController().getCurrentSubmitType()); |
| return Status.OK_STATUS; |
| } |
| } |
| throw new UnsupportedOperationException(); |
| } |
| |
| |
| protected IStatus loadHistory(final IConsoleService tools, final Map<String, Object> data, final IProgressMonitor monitor) { |
| try { |
| CoreException fileException = null; |
| IFileStore fileStore = null; |
| final String filename = ToolCommandHandlerUtil.getCheckedData(data, "filename", String.class, true); //$NON-NLS-1$ |
| final ToolWorkspace workspaceData = tools.getWorkspaceData(); |
| try { |
| fileStore = workspaceData.toFileStore(filename); |
| } |
| catch (final CoreException e) { |
| fileException = e; |
| } |
| final IStatus status; |
| if (fileStore == null) { |
| status = new Status(IStatus.ERROR, NicoCore.BUNDLE_ID, -1, NLS.bind( |
| Messages.ToolController_FileOperation_error_CannotResolve_message, filename), |
| fileException); |
| } |
| else { |
| status = tools.getTool().getHistory().load(fileStore, workspaceData.getEncoding(), false, monitor); |
| } |
| tools.handleStatus(status, monitor); |
| return status; |
| } |
| catch (final OperationCanceledException e) { |
| return Status.CANCEL_STATUS; |
| } |
| } |
| |
| protected IStatus saveHistory(final IConsoleService tools, final Map<String, Object> data, final IProgressMonitor monitor) { |
| try { |
| CoreException fileException = null; |
| IFileStore fileStore = null; |
| final IStatus status; |
| |
| final String filename = ToolCommandHandlerUtil.getCheckedData(data, "filename", String.class, true); //$NON-NLS-1$ |
| final ToolWorkspace workspaceData = tools.getWorkspaceData(); |
| try { |
| fileStore = workspaceData.toFileStore(filename); |
| } |
| catch (final CoreException e) { |
| fileException = e; |
| } |
| if (fileStore == null) { |
| status = new Status(IStatus.ERROR, NicoCore.BUNDLE_ID, -1, NLS.bind( |
| Messages.ToolController_FileOperation_error_CannotResolve_message, filename), |
| fileException); |
| } |
| else { |
| status = tools.getTool().getHistory().save(fileStore, EFS.NONE, workspaceData.getEncoding(), false, monitor); |
| } |
| tools.handleStatus(status, monitor); |
| return status; |
| } |
| catch (final OperationCanceledException e) { |
| return Status.CANCEL_STATUS; |
| } |
| } |
| |
| } |