| /*=============================================================================# |
| # Copyright (c) 2008, 2019 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 java.util.concurrent.atomic.AtomicReference; |
| |
| import org.eclipse.core.databinding.UpdateValueStrategy; |
| import org.eclipse.core.databinding.observable.value.WritableValue; |
| import org.eclipse.core.filesystem.IFileStore; |
| import org.eclipse.core.filesystem.URIUtil; |
| import org.eclipse.core.runtime.IPath; |
| import org.eclipse.jface.dialogs.Dialog; |
| import org.eclipse.jface.dialogs.IDialogSettings; |
| import org.eclipse.osgi.util.NLS; |
| import org.eclipse.swt.SWT; |
| import org.eclipse.swt.layout.GridData; |
| import org.eclipse.swt.widgets.Composite; |
| import org.eclipse.swt.widgets.Control; |
| import org.eclipse.swt.widgets.Shell; |
| |
| 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.databinding.jface.DataBindingSupport; |
| import org.eclipse.statet.ecommons.ui.dialogs.DialogUtils; |
| import org.eclipse.statet.ecommons.ui.dialogs.TitleAreaStatusUpdater; |
| import org.eclipse.statet.ecommons.ui.util.LayoutUtils; |
| import org.eclipse.statet.ecommons.ui.util.UIAccess; |
| import org.eclipse.statet.ecommons.ui.workbench.ResourceInputComposite; |
| |
| import org.eclipse.statet.internal.nico.ui.AbstractConsoleCommandHandler; |
| import org.eclipse.statet.internal.nico.ui.Messages; |
| import org.eclipse.statet.internal.nico.ui.NicoUIPlugin; |
| import org.eclipse.statet.nico.core.runtime.ConsoleService; |
| import org.eclipse.statet.nico.core.runtime.IProgressInfo; |
| import org.eclipse.statet.nico.core.runtime.ToolProcess; |
| |
| |
| /** |
| * TODO: Better support for remote engines (resource mapping); disable OK button on errors? |
| */ |
| public class ChooseFileHandler extends AbstractConsoleCommandHandler { |
| |
| |
| public static final String CHOOSE_FILE_ID = "common/chooseFile"; //$NON-NLS-1$ |
| |
| |
| private static class ChooseFileDialog extends ToolDialog { |
| |
| private final int fMode; |
| |
| private final String fMessage; |
| |
| private ResourceInputComposite fLocationGroup; |
| private WritableValue fNewLocationString; |
| private final String fHistoryId; |
| |
| |
| public ChooseFileDialog(final Shell shell, final ToolProcess tool, final String message, final boolean newFile) { |
| super(tool, shell, null, Messages.Util_ChooseFile_Dialog_title); |
| |
| fMode = newFile ? (ResourceInputComposite.MODE_FILE | ResourceInputComposite.MODE_SAVE) |
| : (ResourceInputComposite.MODE_FILE | ResourceInputComposite.MODE_OPEN); |
| fMessage = message; |
| fHistoryId = "statet:"+getTool().getMainType()+":location.commonfile"; //$NON-NLS-1$ //$NON-NLS-2$ |
| } |
| |
| |
| @Override |
| protected IDialogSettings getDialogSettings() { |
| return DialogUtils.getDialogSettings(NicoUIPlugin.getInstance(), "tools/ChooseFileDialog"); //$NON-NLS-1$ |
| } |
| |
| @Override |
| protected Control createDialogContent(final Composite parent) { |
| setTitle(fMessage); |
| |
| final Composite composite = new Composite(parent, SWT.NONE); |
| composite.setLayout(LayoutUtils.newCompositeGrid(1)); |
| |
| final Composite inputComposite = new Composite(composite, SWT.NONE); |
| inputComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); |
| inputComposite.setLayout(LayoutUtils.newCompositeGrid(2)); |
| |
| fLocationGroup = new ResourceInputComposite(inputComposite, |
| ResourceInputComposite.STYLE_COMBO, |
| fMode, |
| Messages.Util_ChooseFile_File_label); |
| fLocationGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); |
| final String[] history = getDialogSettings().getArray(fHistoryId); |
| fLocationGroup.setHistory(history); |
| |
| final DataBindingSupport databinding = new DataBindingSupport(composite); |
| addBindings(databinding); |
| |
| fNewLocationString.setValue((history != null && history.length > 0) ? history[0] : ""); //$NON-NLS-1$ |
| |
| databinding.installStatusListener(new TitleAreaStatusUpdater(this, fMessage)); |
| |
| return composite; |
| } |
| |
| protected void addBindings(final DataBindingSupport db) { |
| final IFileStore current = getTool().getWorkspaceData().getWorkspaceDir(); |
| String dir = ""; //$NON-NLS-1$ |
| if (current != null) { |
| final IPath path = URIUtil.toPath(current.toURI()); |
| if (path != null) { |
| dir = path.toOSString(); |
| } |
| } |
| fNewLocationString = new WritableValue(dir, String.class); |
| db.getContext().bindValue( |
| fLocationGroup.getObservable(), |
| fNewLocationString, |
| new UpdateValueStrategy<>() |
| .setAfterGetValidator(fLocationGroup.getValidator()), |
| null ); |
| } |
| |
| @Override |
| protected void okPressed() { |
| saveSettings(); |
| super.okPressed(); |
| } |
| |
| public void saveSettings() { |
| final IDialogSettings settings = getDialogSettings(); |
| DialogUtils.saveHistorySettings(settings, fHistoryId, (String) fNewLocationString.getValue()); |
| } |
| |
| public IFileStore getResource() { |
| return fLocationGroup.getResourceAsFileStore(); |
| } |
| |
| } |
| |
| |
| @Override |
| public Status execute(final String id, final ConsoleService service, final Map<String, Object> data, |
| final ProgressMonitor m) { |
| final String message; |
| { String s = ToolCommandHandlerUtils.getCheckedData(data, "message", String.class, false); //$NON-NLS-1$ |
| if (s == null) { |
| final IProgressInfo progressInfo = service.getController().getProgressInfo(); |
| s = NLS.bind("Choose file (asked by {0}):", progressInfo.getLabel()); |
| } |
| message = s; |
| } |
| final Boolean newFile = ToolCommandHandlerUtils.getCheckedData(data, "newResource", Boolean.class, true); //$NON-NLS-1$ |
| final AtomicReference<IFileStore> file= new AtomicReference<>(); |
| final Runnable runnable = new Runnable() { |
| @Override |
| public void run() { |
| final ChooseFileDialog dialog = new ChooseFileDialog(UIAccess.getActiveWorkbenchShell(true), |
| service.getTool(), message, newFile.booleanValue()); |
| dialog.setBlockOnOpen(true); |
| if (dialog.open() == Dialog.OK) { |
| file.set(dialog.getResource()); |
| } |
| } |
| }; |
| UIAccess.getDisplay().syncExec(runnable); |
| if (file.get() == null) { |
| return Statuses.CANCEL_STATUS; |
| } |
| { final String fileName = file.get().toString(); |
| data.put("filename", fileName); //$NON-NLS-1$ |
| data.put("fileName", fileName); //$NON-NLS-1$ |
| } |
| return Statuses.OK_STATUS; |
| } |
| |
| } |