Added templates for wizards

Modified the new campaign wizard
This commit is contained in:
Timotei Dolean 2010-05-15 19:26:44 +00:00
parent 08ff595a63
commit 413865e60b
10 changed files with 506 additions and 46 deletions

View file

@ -28,6 +28,7 @@ public class Activator extends AbstractUIPlugin {
public void start(BundleContext context) throws Exception {
super.start(context);
plugin = this;
TemplateProvider.getInstance().loadTemplates();
}
/*

View file

@ -0,0 +1,16 @@
/**
* @author Timotei Dolean
*/
package wesnoth_eclipse_plugin;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringUtils {
public static boolean startsWith(String target, String sequence)
{
Pattern pattern = Pattern.compile("[\t| ]*"+sequence);
Matcher matcher = pattern.matcher(target);
return (matcher.find() && matcher.start() == 0);
}
}

View file

@ -0,0 +1,78 @@
/**
* @author Timotei Dolean
*/
package wesnoth_eclipse_plugin;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
public class TemplateProvider {
private static TemplateProvider instance_;
private HashMap<String, String> templates_ = new HashMap<String, String>();
public static TemplateProvider getInstance(){
if (instance_ == null)
instance_ = new TemplateProvider();
return instance_;
}
public final String templatesFile = "templatesIndex.txt";
private final String pluginFullPath_ = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
/** Loads the templates from the file system
*/
public void loadTemplates()
{
try {
Logger.print("reading templates...");
Logger.print(pluginFullPath_+ templatesFile);
BufferedReader reader = new BufferedReader(
new FileReader(getClass().getProtectionDomain().getCodeSource().getLocation().getPath()+
templatesFile));
BufferedReader tmpReader;
String line,tmpLine,content;
// read the main "templatesIndex.txt" file
while((line = reader.readLine()) != null)
{
// comment
if (line.startsWith("#"))
continue;
// 0 - template name | 1 - template file
String[] tokensStrings = line.split(" ");
if (tokensStrings.length != 2)
continue;
content ="";
if (new File(pluginFullPath_+tokensStrings[1]).exists())
{
tmpReader = new BufferedReader(new FileReader(pluginFullPath_+tokensStrings[1]));
while((tmpLine = tmpReader.readLine())!=null)
{
content += tmpLine + '\n';
}
templates_.put(tokensStrings[0],content);
System.out.println(String.format("read %s with content: %s\n",tokensStrings[0],content));
tmpReader.close();
}
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public String getTemplate(String name)
{
if (templates_.get(name) == null)
return "";
return templates_.get(name);
}
}

View file

@ -19,11 +19,13 @@ import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.views.markers.MarkerField;
import wesnoth_eclipse_plugin.Logger;
import wesnoth_eclipse_plugin.StringUtils;
import wesnoth_eclipse_plugin.TemplateProvider;
public class CampaignNewWizard extends Wizard implements INewWizard {
@ -32,6 +34,7 @@ public class CampaignNewWizard extends Wizard implements INewWizard {
protected CampaignPage0 page0_;
protected CampaignPage1 page1_;
protected CampaignPage2 page2_;
protected int _lastPageHashCode=0;
@ -44,7 +47,10 @@ public class CampaignNewWizard extends Wizard implements INewWizard {
page1_ = new CampaignPage1();
addPage(page1_);
_lastPageHashCode = page1_.hashCode();
page2_ = new CampaignPage2();
addPage(page2_);
_lastPageHashCode = getPages()[getPageCount()-1].hashCode();
}
public CampaignNewWizard() {
@ -68,42 +74,103 @@ public class CampaignNewWizard extends Wizard implements INewWizard {
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException,
InterruptedException {
monitor.beginTask("Creating the project structure...", 10);
try {
IProject currentProject = page0_.getProjectHandle();
// the project
currentProject.create(new NullProgressMonitor());
monitor.worked(2);
// directory structure
createFolder(currentProject, "ai");
createFolder(currentProject, "images");
createFolder(currentProject, "utils");
createFolder(currentProject, "maps");
createFolder(currentProject, "scenarios");
monitor.worked(6);
// _main.cfg
createFile(currentProject, "_main.cfg",String.format("# Hello! \n# Campaign name: %s ",page1_.getCampaignName()));
monitor.worked(2);
} catch (CoreException e) {
e.printStackTrace();
}
monitor.done();
createProject(monitor);
}
});
}
catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public void createProject(IProgressMonitor monitor)
{
monitor.beginTask("Creating the project structure...", 10);
try {
IProject currentProject = page0_.getProjectHandle();
// the project
currentProject.create(new NullProgressMonitor());
monitor.worked(2);
// directory structure
createFolder(currentProject, "ai");
createFolder(currentProject, "images");
createFolder(currentProject, "utils");
createFolder(currentProject, "maps");
createFolder(currentProject, "scenarios");
createFolder(currentProject, "units");
monitor.worked(5);
// _main.cfg
createFile(currentProject, "_main.cfg",prepareTemplate("campaign"));
monitor.worked(2);
// campaign_name.pbl - for uploading the campaign on the webserver
createFile(currentProject, page1_.getCampaignName()+".pbl",prepareTemplate("pbl"));
} catch (CoreException e) {
e.printStackTrace();
}
monitor.done();
}
public String prepareTemplate(String templateName)
{
String tmpTemplate = TemplateProvider.getInstance().getTemplate(templateName);
if (tmpTemplate == null)
{
MessageBox box = new MessageBox(this.getShell());
box.setMessage(String.format("Template for %s not found.", templateName));
box.open();
return "";
}
// get the lines
String[] template = tmpTemplate.split("\\r?\\n");
replaceParameter(templateName,template,"$$campaign_name", page1_.getCampaignName());
replaceParameter(templateName,template,"$$author", page1_.getAuthor());
replaceParameter(templateName,template,"$$version", page1_.getVersion());
replaceParameter(templateName,template,"$$description", page1_.getDescription());
replaceParameter(templateName,template,"$$icon", page1_.getIconPath());
replaceParameter(templateName,template,"$$email", page1_.getEmail());
replaceParameter(templateName,template,"$$passphrase", page1_.getPassphrase());
replaceParameter(templateName,template,"$$email", page1_.getEmail());
replaceParameter(templateName,template,"$$translations_dir", page1_.getTranslationDir());
replaceParameter(templateName,template,"$$abrev", page2_.getAbbrev());
replaceParameter(templateName,template,"$$define", page2_.getDefine());
replaceParameter(templateName,template,"$$difficulties", page2_.getDifficulties());
replaceParameter(templateName,template,"$$first_scenario", page2_.getFirstScenario());
replaceParameter(templateName,template,"$$project_name", page0_.getProjectName());
replaceParameter(templateName,template,"$$type", page1_.isMultiplayer()?"campaign_mp":"campaign");
tmpTemplate = "";
for (String line: template) {
tmpTemplate += (line +"\n");
}
return tmpTemplate;
}
public void replaceParameter(String templateName, String[] template, String paramName,String paramValue)
{
for (int i=0;i<template.length;++i) {
if (template[i].contains(paramName))
{
template[i] = template[i].replace(paramName, paramValue);
if (paramValue == null || paramValue.length() == 0)
{
// we don't have any value supplied -
// let's comment that line (if it's not already commented)
if (!(StringUtils.startsWith(template[i],"#")))
template[i]= "#" + template[i];
}
}
}
}
/* (non-Javadoc)
* @see org.eclipse.jface.wizard.Wizard#canFinish()
*/

View file

@ -5,18 +5,25 @@ package wesnoth_eclipse_plugin.wizards;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
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 wesnoth_eclipse_plugin.Logger;
import wesnoth_eclipse_plugin.StringUtils;
public class CampaignPage1 extends WizardPage {
private Text _txtCampaignName;
private Text txtCampaignName_;
private Text txtVersion_;
private Text txtTranslationDir_;
private Text txtAuthor_;
private Text txtEmail_;
private Text txtDescription_;
private Text txtPassphrase_;
private Text txtIcon_;
private Button chkMultiCampaign_;
/**
* Create the wizard.
@ -36,41 +43,171 @@ public class CampaignPage1 extends WizardPage {
Composite container = new Composite(parent, SWT.NULL);
setControl(container);
_txtCampaignName = new Text(container, SWT.BORDER);
_txtCampaignName.setBounds(112, 7, 179, 21);
_txtCampaignName.addModifyListener(new ModifyListener() {
ModifyListener updatePageCompleteListener = new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
updateIsPageComplete();
}
});
};
txtCampaignName_ = new Text(container, SWT.BORDER);
txtCampaignName_.setBounds(122, 7, 206, 21);
txtCampaignName_.addModifyListener(updatePageCompleteListener);
Label _lblCampaignName = new Label(container, SWT.NONE);
_lblCampaignName.setBounds(10, 10, 96, 15);
_lblCampaignName.setText("Campaign name:");
_lblCampaignName.setText("Campaign name* :");
Label lblVersion = new Label(container, SWT.NONE);
lblVersion.setText("Version* :");
lblVersion.setBounds(10, 37, 96, 15);
txtVersion_ = new Text(container, SWT.BORDER);
txtVersion_.setBounds(122, 34, 206, 21);
txtVersion_.addModifyListener(updatePageCompleteListener);
Label lblTranslationsDir = new Label(container, SWT.NONE);
lblTranslationsDir.setText("Translations folder:");
lblTranslationsDir.setBounds(10, 64, 106, 15);
txtTranslationDir_ = new Text(container, SWT.BORDER);
txtTranslationDir_.setBounds(122, 61, 206, 21);
Label lblRelativeToThe = new Label(container, SWT.NONE);
lblRelativeToThe.setBounds(334, 64, 174, 15);
lblRelativeToThe.setText("Relative to the data folder");
txtAuthor_ = new Text(container, SWT.BORDER);
txtAuthor_.setBounds(122, 121, 206, 21);
Label lblAuthor = new Label(container, SWT.NONE);
lblAuthor.setText("Author:");
lblAuthor.setBounds(10, 124, 96, 15);
txtEmail_ = new Text(container, SWT.BORDER);
txtEmail_.setBounds(122, 148, 206, 21);
Label lblDescription = new Label(container, SWT.NONE);
lblDescription.setText("Email:");
lblDescription.setBounds(10, 151, 96, 15);
txtDescription_ = new Text(container, SWT.BORDER);
txtDescription_.setBounds(122, 175, 206, 21);
Label lblDescription_1 = new Label(container, SWT.NONE);
lblDescription_1.setText("Description:");
lblDescription_1.setBounds(10, 178, 96, 15);
txtPassphrase_ = new Text(container, SWT.BORDER);
txtPassphrase_.setBounds(122, 202, 206, 21);
Label lblIcon = new Label(container, SWT.NONE);
lblIcon.setText("Passphrase:");
lblIcon.setBounds(10, 205, 96, 15);
Label lblFormat = new Label(container, SWT.NONE);
lblFormat.setToolTipText("Displayed to the right of the title, it is just text. However,\r\nstarting with Wesnoth 1.6, the required format is x.y.z \r\nwhere x, y and z are numbers and a value for x greater than 0 \r\nimplies the campaign is complete and balanced. \r\nTrailing non-numeric elements are ok, but nothing should\r\nappear before the numbers. This is necessary for the Update \r\nadd-ons button to work correctly.");
lblFormat.setBounds(334, 37, 72, 15);
lblFormat.setText("Format: x.y.z");
Label lblIcon_1 = new Label(container, SWT.NONE);
lblIcon_1.setText("Icon:");
lblIcon_1.setBounds(10, 232, 96, 15);
txtIcon_ = new Text(container, SWT.BORDER);
txtIcon_.setBounds(122, 229, 206, 21);
Label lblRelativeToThe_1 = new Label(container, SWT.NONE);
lblRelativeToThe_1.setToolTipText("An image, displayed leftmost on the \"download campaigns\" screen.\r\nIt must be a standard Wesnoth graphic and not a custom one. \r\n(Well, a custom graphic will work if the user already has the campaign \r\ninstalled, or if it is a custom graphic from a different campaign that the \r\nuser has installed but others won't see it!) (Note that the icon used to \r\ndisplay your campaign for when it is played can be custom; for more\r\ninformation see CampaignWML.) If the icon is a unit with magenta color,\r\nplease use ImagePathFunctionWML to team-color it. ");
lblRelativeToThe_1.setBounds(334, 232, 230, 15);
lblRelativeToThe_1.setText("Relative to the data/core/images folder");
chkMultiCampaign_ = new Button(container, SWT.CHECK);
chkMultiCampaign_.setBounds(10, 86, 181, 16);
chkMultiCampaign_.setText("This is a multiplayer campaign");
updateIsPageComplete();
}
/* (non-Javadoc)
* @see org.eclipse.jface.wizard.WizardPage#canFlipToNextPage()
*/
@Override
public boolean canFlipToNextPage() {
return getNextPage() != null ;
return (isPageComplete() && getNextPage() != null);
}
public void updateIsPageComplete()
{
boolean res = true;
if (_txtCampaignName.getText().length() == 0)
res = false;
setPageComplete(false);
if (txtCampaignName_.getText().length() == 0)
{
setErrorMessage("Campaign name is mandatory");
return;
}
setPageComplete(res);
// match the pattern x.y.z
if (txtVersion_.getText().length() == 0 ||
!(txtVersion_.getText().matches("[\\d]+\\.[\\d]+\\.\\d[\\w\\W\\d\\D\\s\\S]*")))
{
setErrorMessage("The version must have the format: x.y.z");
return;
}
setErrorMessage(null);
setPageComplete(true);
}
/**
* @return the Campaign Name
*/
public String getCampaignName() {
return _txtCampaignName.getText();
return txtCampaignName_.getText();
}
/**
* @return the author
*/
public String getAuthor() {
return txtAuthor_.getText();
}
/**
* @return the version
*/
public String getVersion() {
return txtVersion_.getText();
}
/**
* @return the description
*/
public String getDescription() {
return txtDescription_.getText();
}
/**
* @return the Icon
*/
public String getIconPath() {
return txtIcon_.getText();
}
/**
* @return the email
*/
public String getEmail() {
return txtEmail_.getText();
}
/**
* @return the passphrase
*/
public String getPassphrase() {
return txtPassphrase_.getText();
}
/**
* @return the translation directory
*/
public String getTranslationDir() {
return txtTranslationDir_.getText();
}
/**
* @return true if the campaign is multiplayer
*/
public boolean isMultiplayer(){
return chkMultiCampaign_.getSelection();
}
}

View file

@ -0,0 +1,120 @@
/**
* @author Timotei Dolean
*/
package wesnoth_eclipse_plugin.wizards;
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.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
public class CampaignPage2 extends WizardPage {
private Text txtAbbrev_;
private Text txtDefine_;
private Text txtDifficulties_;
private Text txtFirstScenario_;
/**
* Create the wizard.
*/
public CampaignPage2() {
super("wizardPage");
setTitle("Campaign details");
setDescription("Set the campaign details");
}
/**
* Create contents of the wizard.
* @param parent
*/
public void createControl(Composite parent) {
Composite container = new Composite(parent, SWT.NULL);
setControl(container);
ModifyListener modifyListener = new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
updatePageIsComplete();
}
};
Label lblAbbreviation = new Label(container, SWT.NONE);
lblAbbreviation.setText("Abbreviation* :");
lblAbbreviation.setBounds(10, 13, 76, 13);
txtAbbrev_ = new Text(container, SWT.BORDER);
txtAbbrev_.setBounds(92, 10, 179, 19);
txtAbbrev_.addModifyListener(modifyListener);
Label lblDefine = new Label(container, SWT.NONE);
lblDefine.setText("Define* :");
lblDefine.setBounds(10, 35, 49, 13);
txtDefine_ = new Text(container, SWT.BORDER);
txtDefine_.setBounds(92, 32, 179, 19);
txtDefine_.addModifyListener(modifyListener);
Label lblDifficulties = new Label(container, SWT.NONE);
lblDifficulties.setText("Difficulties:");
lblDifficulties.setBounds(10, 57, 63, 13);
txtDifficulties_ = new Text(container, SWT.BORDER);
txtDifficulties_.setBounds(92, 54, 179, 19);
Label lblFirstScenario = new Label(container, SWT.NONE);
lblFirstScenario.setText("First Scenario:");
lblFirstScenario.setBounds(10, 90, 76, 13);
txtFirstScenario_ = new Text(container, SWT.BORDER);
txtFirstScenario_.setBounds(92, 87, 179, 19);
updatePageIsComplete();
}
public void updatePageIsComplete()
{
setPageComplete(false);
if (txtAbbrev_.getText().length() == 0)
{
setErrorMessage("Please specify an abbreviation.");
return;
}
if (txtDefine_.getText().length() == 0)
{
setErrorMessage("Please specify a define.");
return;
}
setErrorMessage(null);
setPageComplete(true);
}
/**
* @return returns the abbreviation of the campaign
*/
public String getAbbrev() {
return txtAbbrev_.getText();
}
/**
* @return returns the define of the campaign
*/
public String getDefine() {
return txtDefine_.getText();
}
/**
* @return returns the difficulties of the campaign
*/
public String getDifficulties() {
return txtDifficulties_.getText();
}
/**
* @return returns the first scenario of the campaign
*/
public String getFirstScenario() {
return txtFirstScenario_.getText();
}
}

View file

@ -0,0 +1,26 @@
[textdomain]
name="wesnoth-$$campaign_name"
# translations directory path
path="$$translations_dir"
[/textdomain]
[campaign]
#textdomain wesnoth-$$campaign_name
name= _ "$$campaign_name"
abbrev="$$abrev"
define="$$define"
icon= $$icon
description= _ "$$description"
difficulties=$$difficulties
first_scenario=$$first_scenario
#ifdef $$define
# add here custom code
{campaigns/$$project_name/scenarios}
[+units]
{campaigns/$$project_name/units}
[/units]
#endif
[/campaign]

View file

@ -0,0 +1,8 @@
author="$$author"
title="$$campaign_name"
icon="$$icon"
version="$$version"
description="$$description"
type="$$type"
email="$$email"
passphrase="$$passphrase"

View file

@ -0,0 +1,2 @@
[scenario]
[/scenario]

View file

@ -0,0 +1,5 @@
# a hashtable of templates
# template_name template_file
campaign templates/campaign.txt
scenario templates/scenario.txt
pbl templates/pbl.txt