eclipse plugin: add new era creation wizard

This commit is contained in:
Timotei Dolean 2010-07-05 16:00:45 +00:00
parent 39485213ee
commit 9d4ae69c69
6 changed files with 421 additions and 2 deletions

View file

@ -221,6 +221,14 @@
project="false">
<description>Create a Wesnoth scenario.</description>
</wizard>
<wizard
category="wesnoth.eclipse.newWizards"
class="wesnoth_eclipse_plugin.wizards.EraNewWizard"
icon="icons/wesnoth-icon_16.png"
id="wesnoth_eclipse_plugin.wizards.eraNewWizard"
name="Wesnoth Era"
project="false">
</wizard>
</extension>
<extension
point="org.eclipse.ui.commands">

View file

@ -0,0 +1,153 @@
/**
* @author Timotei Dolean
*
*/
package wesnoth_eclipse_plugin.wizards;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;
import wesnoth_eclipse_plugin.utils.GUIUtils;
import wesnoth_eclipse_plugin.utils.WorkspaceUtils;
public class EraNewWizard extends NewWizardTemplate
{
EraPage0 page0_;
public EraNewWizard() {
setWindowTitle("New Wizard");
setNeedsProgressMonitor(true);
}
@Override
public void addPages()
{
page0_ = new EraPage0(selection_);
addPage(page0_);
super.addPages();
}
@Override
public boolean performFinish()
{
final String containerName = page0_.getDirectoryName();
final String fileName = page0_.getFileName();
IRunnableWithProgress op = new IRunnableWithProgress() {
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException
{
try
{
doFinish(containerName, fileName, monitor);
} catch (CoreException e)
{
throw new InvocationTargetException(e);
} finally
{
monitor.done();
}
}
};
try
{
getContainer().run(false, false, op);
} catch (InterruptedException e)
{
return false;
} catch (InvocationTargetException e)
{
Throwable realException = e.getTargetException();
MessageDialog.openError(getShell(), "Error", realException.getMessage());
return false;
}
return true;
}
private void doFinish(String containerName, String fileName, IProgressMonitor monitor) throws CoreException
{
// create a sample file
monitor.beginTask("Creating " + fileName, 10);
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IResource resource = root.findMember(new Path(containerName));
IContainer container = (IContainer) resource;
final IFile file = container.getFile(new Path(fileName));
try
{
InputStream stream = getEraStream();
if (stream == null)
return;
if (file.exists())
{
file.setContents(stream, true, true, monitor);
}
else
{
file.create(stream, true, monitor);
}
stream.close();
} catch (IOException e)
{
e.printStackTrace();
}
monitor.worked(5);
monitor.setTaskName("Opening file for editing...");
getShell().getDisplay().asyncExec(new Runnable() {
@Override
public void run()
{
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
try
{
IDE.openEditor(page, file, true);
} catch (PartInitException e)
{
}
}
});
monitor.worked(5);
}
private InputStream getEraStream()
{
ArrayList<ReplaceableParameter> params = new ArrayList<ReplaceableParameter>();
params.add(new ReplaceableParameter("$$era_id", String.valueOf(page0_.getEraID())));
params.add(new ReplaceableParameter("$$era_name", String.valueOf(page0_.getEraName())));
params.add(new ReplaceableParameter("$$require_era", String.valueOf(page0_.getRequiresEra())));
String template = TemplateProvider.getInstance().getProcessedTemplate("era", params);
if (template == null)
{
GUIUtils.showMessageBox(WorkspaceUtils.getWorkbenchWindow(), "Template for \"era\" not found.");
return null;
}
return new ByteArrayInputStream(template.getBytes());
}
}

View file

@ -0,0 +1,254 @@
/**
* @author Timotei Dolean
*
*/
package wesnoth_eclipse_plugin.wizards;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.ContainerSelectionDialog;
public class EraPage0 extends WizardPage
{
private ISelection selection_;
private Text txtDirectory_;
private Text txtFileName_;
private Text txtEraID_;
private Text txtEraName_;
private Button chkRequireEra_;
/**
* Create the wizard.
*/
public EraPage0(ISelection selection) {
super("wizardPage");
setTitle("New era wizard");
setDescription("Create a new era");
selection_ = selection;
}
/**
* Create contents of the wizard.
*
* @param parent
*/
@Override
public void createControl(Composite parent)
{
ModifyListener modifyListener = new ModifyListener() {
@Override
public void modifyText(ModifyEvent e)
{
updatePageIsComplete();
}
};
Composite container = new Composite(parent, SWT.NULL);
setControl(container);
container.setLayout(new GridLayout(3, false));
Label lblProject = new Label(container, SWT.NONE);
GridData gd_lblProject = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
gd_lblProject.widthHint = 99;
lblProject.setLayoutData(gd_lblProject);
lblProject.setText("Directory* :");
txtDirectory_ = new Text(container, SWT.BORDER);
txtDirectory_.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
txtDirectory_.addModifyListener(modifyListener);
Button btnBrowse = new Button(container, SWT.NONE);
btnBrowse.setText("Browse...");
btnBrowse.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e)
{
handleBrowse();
}
});
Label lblFileName = new Label(container, SWT.NONE);
lblFileName.setText("File name* :");
txtFileName_ = new Text(container, SWT.BORDER);
txtFileName_.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
new Label(container, SWT.NONE);
txtFileName_.addModifyListener(modifyListener);
Label lblEraID = new Label(container, SWT.NONE);
lblEraID.setText("Era Id*:");
txtEraID_ = new Text(container, SWT.BORDER);
txtEraID_.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
new Label(container, SWT.NONE);
txtEraID_.addModifyListener(modifyListener);
Label lblEraName = new Label(container, SWT.NONE);
lblEraName.setText("Era name:");
txtEraName_ = new Text(container, SWT.BORDER);
txtEraName_.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
new Label(container, SWT.NONE);
txtEraName_.addModifyListener(modifyListener);
chkRequireEra_ = new Button(container, SWT.CHECK);
chkRequireEra_
.setToolTipText("whether clients are required to have this era installed beforehand to be allowed join a game using this era. Possible values 'yes' (the default) and 'no'. ");
chkRequireEra_.setText("Require era");
new Label(container, SWT.NONE);
new Label(container, SWT.NONE);
initialize();
updatePageIsComplete();
}
private void updatePageIsComplete()
{
IResource container = ResourcesPlugin.getWorkspace().getRoot().findMember(new Path(getDirectoryName()));
setPageComplete(false);
String fileName = getFileName();
if (getDirectoryName().isEmpty())
{
setErrorMessage("You need to specify a valid directory path first.");
return;
}
if (container == null || !container.exists() || !(container instanceof IContainer))
{
setErrorMessage("The directory must be created first and the selected folder to exist.");
return;
}
if (fileName.isEmpty())
{
setErrorMessage("File name must be specified.");
return;
}
if (fileName.replace('\\', '/').indexOf('/', 1) > 0)
{
setErrorMessage("File name must be valid.");
return;
}
int dotLoc = fileName.lastIndexOf('.');
if (dotLoc == -1 || fileName.substring(dotLoc + 1).equalsIgnoreCase("cfg") == false)
{
setErrorMessage("File extension must be 'cfg'.");
return;
}
if (getEraID().isEmpty())
{
setErrorMessage("The era ID cannot be empty.");
return;
}
setErrorMessage(null);
setPageComplete(true);
}
/**
* Tests if the current workbench selection is a suitable campaign to use.
*/
private void initialize()
{
if (selection_ != null && selection_.isEmpty() == false && selection_ instanceof IStructuredSelection)
{
IStructuredSelection ssel = (IStructuredSelection) selection_;
if (ssel.size() > 1)
{
return;
}
Object obj = ssel.getFirstElement();
if (obj instanceof IResource)
{
IContainer container;
if (obj instanceof IContainer)
{
container = (IContainer) obj;
}
else
{
container = ((IResource) obj).getParent();
}
txtDirectory_.setText(container.getFullPath().toString());
}
}
}
/**
* Uses the standard container selection dialog to choose the new value for
* the directory field.
*/
private void handleBrowse()
{
ContainerSelectionDialog dialog = new ContainerSelectionDialog(getShell(), ResourcesPlugin.getWorkspace().getRoot(), false, "Select a campaign project");
if (dialog.open() == ContainerSelectionDialog.OK)
{
Object[] result = dialog.getResult();
if (result.length == 1)
{
txtDirectory_.setText(((Path) result[0]).toString());
}
}
}
/**
* @return true if this requires the era to be installed in user's game
*/
public boolean getRequiresEra()
{
return chkRequireEra_.getSelection();
}
/**
* @return the era id
*/
public String getEraID()
{
return txtEraID_.getText();
}
/**
* @return the era name
*/
public String getEraName()
{
return txtEraName_.getText();
}
/**
* @return the filename containing the era
*/
public String getFileName()
{
return txtFileName_.getText();
}
/**
* @return the directory where the file will be placed
*/
public String getDirectoryName()
{
return txtDirectory_.getText();
}
}

View file

@ -103,7 +103,7 @@ public class TemplateProvider
{
template[i] = template[i].replace(param.paramName, param.paramValue);
if ((templateName!= "build_xml") && (param.paramValue == null || param.paramValue.isEmpty()))
if (!templateName.equals("build_xml") && (param.paramValue == null || param.paramValue.isEmpty()))
{
// we don't have any value supplied -
// let's comment that line (if it's not already
@ -133,6 +133,7 @@ public class TemplateProvider
* value is a list of <String, String> that consist of <Filename, Template
* used for file contents> and the second return value is a list of String
* with directories names
*
* @param structureTemplate the template
* @return
*/

View file

@ -1,2 +1,5 @@
[era]
id=$$era_id
name=$$era_name
require_era=$$require_era
[/era]

View file

@ -1,2 +1,2 @@
[multiplayer]
[/multiplayer]
[/multiplayer]