blob: 7a60c05889deb4a97c6b1378ade4544c3188b5c2 [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2005, 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.internal.r.ui.wizards;
import java.lang.reflect.InvocationTargetException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.statet.ecommons.ui.dialogs.DialogUtils;
import org.eclipse.statet.base.ext.ui.wizards.NewElementWizard;
import org.eclipse.statet.base.ext.ui.wizards.NewElementWizardPage.ResourceGroup;
import org.eclipse.statet.internal.r.ui.RUIPlugin;
import org.eclipse.statet.ltk.ui.templates.TemplatesUtil.EvaluatedTemplate;
import org.eclipse.statet.r.codegeneration.CodeGeneration;
import org.eclipse.statet.r.core.RCore;
import org.eclipse.statet.r.core.RResourceUnit;
import org.eclipse.statet.r.ui.RUI;
public class NewRdFileCreationWizard extends NewElementWizard {
private static class NewRdFileCreator extends NewFileCreator {
public NewRdFileCreator(final IPath containerPath, final String resourceName) {
super(containerPath, resourceName, RCore.RD_CONTENT_TYPE);
}
@Override
protected String getInitialFileContent(final IFile newFileHandle,
final SubMonitor progress) {
final String lineDelimiter= System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
try {
final RResourceUnit rcu= RResourceUnit.createTempUnit(newFileHandle, "rd"); //$NON-NLS-1$
final EvaluatedTemplate data= CodeGeneration.getNewRdFileContent(rcu, lineDelimiter);
if (data != null) {
this.initialSelection= data.getRegionToSelect();
return data.getContent();
}
} catch (final CoreException e) {
RUIPlugin.logError(RUIPlugin.IO_ERROR, "Error occured when applying template to new Rd file.", e); //$NON-NLS-1$
}
return null;
}
}
private NewRdFileCreationWizardPage firstPage;
private NewFileCreator newRdFile;
public NewRdFileCreationWizard() {
setDialogSettings(DialogUtils.getDialogSettings(RUIPlugin.getInstance(), "NewElementWizard")); //$NON-NLS-1$
setDefaultPageImageDescriptor(RUI.getImageDescriptor(RUIPlugin.IMG_WIZBAN_NEWRDFILE));
setWindowTitle(Messages.NewRDocFileWizard_title);
}
@Override
public void addPages() {
super.addPages();
this.firstPage= new NewRdFileCreationWizardPage(getSelection());
addPage(this.firstPage);
}
@Override
protected ISchedulingRule getSchedulingRule() {
final ISchedulingRule rule= createRule(this.newRdFile.getFileHandle());
if (rule != null) {
return rule;
}
return super.getSchedulingRule();
}
@Override
public boolean performFinish() {
// befor super, so it can be used in getSchedulingRule
final ResourceGroup resourceGroup= this.firstPage.getResourceGroup();
this.newRdFile= new NewRdFileCreator(
resourceGroup.getContainerFullPath(),
resourceGroup.getResourceName() );
final boolean result= super.performFinish();
if (result && this.newRdFile.getFileHandle() != null) {
// select and open file
selectAndReveal(this.newRdFile.getFileHandle());
openResource(this.newRdFile);
}
return result;
}
@Override
protected void doFinish(final IProgressMonitor monitor) throws InterruptedException, CoreException, InvocationTargetException {
final SubMonitor m= SubMonitor.convert(monitor, "Create new Rd file...", 10 + 1);
this.newRdFile.createFile(m.newChild(10));
this.firstPage.saveSettings();
m.worked(1);
}
}