| /*=============================================================================# |
| # Copyright (c) 2008, 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.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.OperationCanceledException; |
| import org.eclipse.osgi.util.NLS; |
| |
| 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.ToolCommandHandler; |
| import org.eclipse.statet.jcommons.ts.core.ToolService; |
| import org.eclipse.statet.jcommons.ts.core.util.ToolCommandHandlerUtils; |
| |
| 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 Status execute(final String id, final ToolService service, final Map<String, Object> data, |
| final ProgressMonitor m) { |
| if (service instanceof ConsoleService) { |
| final ConsoleService console = (ConsoleService) service; |
| if (id.equals(LOAD_HISTORY_ID)) { |
| return loadHistory(console, data, m); |
| } |
| if (id.equals(SAVE_HISTORY_ID)) { |
| return saveHistory(console, data, m); |
| } |
| if (id.equals(ADDTO_HISTORY_ID)) { |
| final String item = ToolCommandHandlerUtils.getCheckedData(data, "text", String.class, true); //$NON-NLS-1$ |
| console.getTool().getHistory().addCommand(item, console.getController().getCurrentSubmitType()); |
| return Statuses.OK_STATUS; |
| } |
| } |
| throw new UnsupportedOperationException(); |
| } |
| |
| |
| protected Status loadHistory(final ConsoleService tools, final Map<String, Object> data, |
| final ProgressMonitor m) { |
| try { |
| CoreException fileException = null; |
| IFileStore fileStore = null; |
| final String filename = ToolCommandHandlerUtils.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 Status status; |
| if (fileStore == null) { |
| status= new ErrorStatus(NicoCore.BUNDLE_ID, NLS.bind( |
| Messages.ToolController_FileOperation_error_CannotResolve_message, |
| filename ), |
| fileException ); |
| } |
| else { |
| status= tools.getTool().getHistory().load(fileStore, |
| workspaceData.getEncoding(), false, m ); |
| } |
| tools.handleStatus(status, m); |
| return status; |
| } |
| catch (final OperationCanceledException e) { |
| return Statuses.CANCEL_STATUS; |
| } |
| } |
| |
| protected Status saveHistory(final ConsoleService tools, final Map<String, Object> data, |
| final ProgressMonitor m) { |
| try { |
| CoreException fileException = null; |
| IFileStore fileStore = null; |
| final Status status; |
| |
| final String filename = ToolCommandHandlerUtils.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 ErrorStatus(NicoCore.BUNDLE_ID, NLS.bind( |
| Messages.ToolController_FileOperation_error_CannotResolve_message, |
| filename ), |
| fileException ); |
| } |
| else { |
| status= tools.getTool().getHistory().save(fileStore, |
| EFS.NONE, workspaceData.getEncoding(), false, m ); |
| } |
| tools.handleStatus(status, m); |
| return status; |
| } |
| catch (final OperationCanceledException e) { |
| return Statuses.CANCEL_STATUS; |
| } |
| } |
| |
| } |