eclipse plugin: part 2 of creating project files/directories...
...based on text files
This commit is contained in:
parent
45f68ab3a2
commit
32a5b9a109
4 changed files with 91 additions and 37 deletions
|
@ -0,0 +1,16 @@
|
|||
/**
|
||||
* @author Timotei Dolean
|
||||
*
|
||||
*/
|
||||
package wesnoth_eclipse_plugin.utils;
|
||||
|
||||
public class Pair<T, K>
|
||||
{
|
||||
public T First;
|
||||
public K Second;
|
||||
|
||||
public Pair(T first, K second) {
|
||||
First = first;
|
||||
Second = second;
|
||||
}
|
||||
}
|
|
@ -2,6 +2,7 @@ package wesnoth_eclipse_plugin.wizards;
|
|||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.eclipse.core.resources.IProject;
|
||||
import org.eclipse.core.resources.IProjectDescription;
|
||||
|
@ -16,8 +17,8 @@ import org.eclipse.ui.INewWizard;
|
|||
import org.eclipse.ui.IWorkbench;
|
||||
|
||||
import wesnoth_eclipse_plugin.builder.WesnothProjectNature;
|
||||
import wesnoth_eclipse_plugin.utils.Pair;
|
||||
import wesnoth_eclipse_plugin.utils.ResourceUtils;
|
||||
import wesnoth_eclipse_plugin.utils.StringUtils;
|
||||
|
||||
public class CampaignNewWizard extends Wizard implements INewWizard
|
||||
{
|
||||
|
@ -98,25 +99,20 @@ public class CampaignNewWizard extends Wizard implements INewWizard
|
|||
if (campaignStructure == null)
|
||||
return;
|
||||
|
||||
for (String line : StringUtils.getLines(campaignStructure))
|
||||
List<Pair<String, String>> files;
|
||||
List<String> dirs;
|
||||
Pair<List<Pair<String, String>>, List<String>> tmp = TemplateProvider.getInstance().getFilesDirectories(campaignStructure);
|
||||
files = tmp.First;
|
||||
dirs = tmp.Second;
|
||||
|
||||
for (Pair<String, String> file : files)
|
||||
{
|
||||
if (StringUtils.startsWith(line, "#"))
|
||||
continue;
|
||||
|
||||
if (line.contains(":")) // file with template
|
||||
{
|
||||
String[] tmpLine = line.split(":");
|
||||
|
||||
// oops. error
|
||||
if (tmpLine.length != 2)
|
||||
continue;
|
||||
|
||||
ResourceUtils.createFile(currentProject, tmpLine[0].trim(), prepareTemplate(tmpLine[1].trim()));
|
||||
}
|
||||
else
|
||||
{
|
||||
ResourceUtils.createFolder(currentProject, line.trim());
|
||||
}
|
||||
ResourceUtils.createFile(currentProject, file.First, prepareTemplate(file.Second));
|
||||
monitor.worked(1);
|
||||
}
|
||||
for (String dir : dirs)
|
||||
{
|
||||
ResourceUtils.createFolder(currentProject, dir);
|
||||
monitor.worked(1);
|
||||
}
|
||||
|
||||
|
@ -151,7 +147,7 @@ public class CampaignNewWizard extends Wizard implements INewWizard
|
|||
params.add(new ReplaceableParameter("$$project_name", page0_.getProjectName()));
|
||||
params.add(new ReplaceableParameter("$$type", page1_.isMultiplayer() ? "campaign_mp" : "campaign"));
|
||||
|
||||
return TemplateProvider.getProcessedTemplate(templateName, params);
|
||||
return TemplateProvider.getInstance().getProcessedTemplate(templateName, params);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -64,13 +64,13 @@ public class ScenarioNewWizard extends Wizard implements INewWizard
|
|||
@Override
|
||||
public void addPages()
|
||||
{
|
||||
this.page0_ = new ScenarioPage0(this.selection);
|
||||
addPage(this.page0_);
|
||||
page0_ = new ScenarioPage0(selection);
|
||||
addPage(page0_);
|
||||
|
||||
this.page1_ = new ScenarioPage1();
|
||||
page1_ = new ScenarioPage1();
|
||||
// addPage(page1_);
|
||||
|
||||
this.lastPageHashCode_ = getPages()[getPageCount() - 1].hashCode();
|
||||
lastPageHashCode_ = getPages()[getPageCount() - 1].hashCode();
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -81,7 +81,7 @@ public class ScenarioNewWizard extends Wizard implements INewWizard
|
|||
public boolean canFinish()
|
||||
{
|
||||
IWizardPage page = getContainer().getCurrentPage();
|
||||
return super.canFinish() && page.hashCode() == this.lastPageHashCode_ && page.isPageComplete();
|
||||
return super.canFinish() && page.hashCode() == lastPageHashCode_ && page.isPageComplete();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -91,8 +91,8 @@ public class ScenarioNewWizard extends Wizard implements INewWizard
|
|||
@Override
|
||||
public boolean performFinish()
|
||||
{
|
||||
final String containerName = this.page0_.getProjectName();
|
||||
final String fileName = this.page0_.getFileName();
|
||||
final String containerName = page0_.getProjectName();
|
||||
final String fileName = page0_.getFileName();
|
||||
IRunnableWithProgress op = new IRunnableWithProgress() {
|
||||
@Override
|
||||
public void run(IProgressMonitor monitor) throws InvocationTargetException
|
||||
|
@ -135,10 +135,12 @@ public class ScenarioNewWizard extends Wizard implements INewWizard
|
|||
monitor.beginTask("Creating " + fileName, 2);
|
||||
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
|
||||
IResource resource = root.findMember(new Path(containerName));
|
||||
|
||||
if (!resource.exists() || !(resource instanceof IContainer))
|
||||
{
|
||||
throwCoreException("Container \"" + containerName + "\" does not exist.");
|
||||
}
|
||||
|
||||
IContainer container = (IContainer) resource;
|
||||
final IFile file = container.getFile(new Path(fileName));
|
||||
|
||||
|
@ -147,22 +149,23 @@ public class ScenarioNewWizard extends Wizard implements INewWizard
|
|||
InputStream stream = getScenarioStream();
|
||||
|
||||
if (stream == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (file.exists())
|
||||
{
|
||||
file.setContents(stream, true, true, monitor);
|
||||
} else
|
||||
}
|
||||
else
|
||||
{
|
||||
file.create(stream, true, monitor);
|
||||
}
|
||||
|
||||
stream.close();
|
||||
} catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
monitor.worked(1);
|
||||
monitor.setTaskName("Opening file for editing...");
|
||||
getShell().getDisplay().asyncExec(new Runnable() {
|
||||
|
@ -188,14 +191,14 @@ public class ScenarioNewWizard extends Wizard implements INewWizard
|
|||
{
|
||||
ArrayList<ReplaceableParameter> params = new ArrayList<ReplaceableParameter>();
|
||||
|
||||
params.add(new ReplaceableParameter("$$scenario_id", this.page0_.getScenarioId()));
|
||||
params.add(new ReplaceableParameter("$$next_scenario_id", this.page0_.getNextScenarioId()));
|
||||
params.add(new ReplaceableParameter("$$scenario_name", this.page0_.getScenarioName()));
|
||||
params.add(new ReplaceableParameter("$$map_data", this.page0_.getMapData()));
|
||||
params.add(new ReplaceableParameter("$$scenario_id", page0_.getScenarioId()));
|
||||
params.add(new ReplaceableParameter("$$next_scenario_id", page0_.getNextScenarioId()));
|
||||
params.add(new ReplaceableParameter("$$scenario_name", page0_.getScenarioName()));
|
||||
params.add(new ReplaceableParameter("$$map_data", page0_.getMapData()));
|
||||
|
||||
params.add(new ReplaceableParameter("$$turns_number", String.valueOf(this.page0_.getTurnsNumber())));
|
||||
params.add(new ReplaceableParameter("$$turns_number", String.valueOf(page0_.getTurnsNumber())));
|
||||
|
||||
String template = TemplateProvider.getProcessedTemplate("scenario", params);
|
||||
String template = TemplateProvider.getInstance().getProcessedTemplate("scenario", params);
|
||||
|
||||
if (template == null)
|
||||
{
|
||||
|
|
|
@ -8,8 +8,10 @@ import java.io.File;
|
|||
import java.io.FileReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import wesnoth_eclipse_plugin.Logger;
|
||||
import wesnoth_eclipse_plugin.utils.Pair;
|
||||
import wesnoth_eclipse_plugin.utils.StringUtils;
|
||||
|
||||
public class TemplateProvider
|
||||
|
@ -83,7 +85,7 @@ public class TemplateProvider
|
|||
}
|
||||
}
|
||||
|
||||
public static String getProcessedTemplate(String templateName, ArrayList<ReplaceableParameter> parameters)
|
||||
public String getProcessedTemplate(String templateName, ArrayList<ReplaceableParameter> parameters)
|
||||
{
|
||||
String tmpTemplate = TemplateProvider.getInstance().getTemplate(templateName);
|
||||
if (tmpTemplate == null)
|
||||
|
@ -125,4 +127,41 @@ public class TemplateProvider
|
|||
return "";
|
||||
return templates_.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the lists of the specified structure template. The first return
|
||||
* 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
|
||||
*/
|
||||
public Pair<List<Pair<String, String>>, List<String>> getFilesDirectories(String structureTemplate)
|
||||
{
|
||||
List<Pair<String, String>> files = new ArrayList<Pair<String, String>>();
|
||||
List<String> dirs = new ArrayList<String>();
|
||||
|
||||
for (String line : StringUtils.getLines(structureTemplate))
|
||||
{
|
||||
if (StringUtils.startsWith(line, "#"))
|
||||
continue;
|
||||
|
||||
if (line.contains(":")) // file with template
|
||||
{
|
||||
String[] tmpLine = line.split(":");
|
||||
|
||||
// oops. error
|
||||
if (tmpLine.length != 2)
|
||||
continue;
|
||||
|
||||
files.add(new Pair<String, String>(tmpLine[0].trim(), tmpLine[1].trim()));
|
||||
}
|
||||
else
|
||||
{
|
||||
dirs.add(line.trim());
|
||||
}
|
||||
}
|
||||
|
||||
return new Pair<List<Pair<String, String>>, List<String>>(files, dirs);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue