eclipse plugin: Refactor a bit the project creation wizards...
...to remove redundant code
This commit is contained in:
parent
6494f1a6e8
commit
0db32b1e32
4 changed files with 109 additions and 174 deletions
|
@ -8,6 +8,12 @@
|
|||
*******************************************************************************/
|
||||
package org.wesnoth.wizards;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.eclipse.core.resources.IProject;
|
||||
import org.eclipse.core.resources.IProjectDescription;
|
||||
import org.eclipse.core.resources.ResourcesPlugin;
|
||||
import org.eclipse.core.runtime.IProgressMonitor;
|
||||
import org.eclipse.swt.SWT;
|
||||
import org.eclipse.swt.layout.GridData;
|
||||
import org.eclipse.swt.layout.GridLayout;
|
||||
|
@ -15,10 +21,13 @@ import org.eclipse.swt.widgets.Combo;
|
|||
import org.eclipse.swt.widgets.Composite;
|
||||
import org.eclipse.swt.widgets.Label;
|
||||
import org.eclipse.ui.dialogs.WizardNewProjectCreationPage;
|
||||
import org.wesnoth.installs.WesnothInstall;
|
||||
import org.wesnoth.installs.WesnothInstallsUtils;
|
||||
import org.wesnoth.preferences.Preferences;
|
||||
import org.wesnoth.preferences.Preferences.Paths;
|
||||
import org.wesnoth.projects.ProjectUtils;
|
||||
import org.wesnoth.templates.ReplaceableParameter;
|
||||
import org.wesnoth.templates.TemplateProvider;
|
||||
import org.wesnoth.utils.Pair;
|
||||
import org.wesnoth.utils.ResourceUtils;
|
||||
|
||||
/**
|
||||
|
@ -57,20 +66,7 @@ public class WizardProjectPageTemplate extends WizardNewProjectCreationPage
|
|||
gd_cmbInstalls.widthHint = 154;
|
||||
cmbInstalls_.setLayoutData(gd_cmbInstalls);
|
||||
|
||||
// fill the installs
|
||||
String defaultInstallName = Preferences.getDefaultInstallName( );
|
||||
for ( WesnothInstall install : WesnothInstallsUtils.getInstalls( ) ) {
|
||||
cmbInstalls_.add( install.getName( ) );
|
||||
|
||||
// select the default
|
||||
if ( install.getName( ).equals( defaultInstallName ) )
|
||||
cmbInstalls_.select( cmbInstalls_.getItemCount( ) - 1 );
|
||||
}
|
||||
|
||||
// select the first if there is no other selected
|
||||
if ( cmbInstalls_.getSelectionIndex( ) == -1 &&
|
||||
cmbInstalls_.getItemCount( ) > 0 )
|
||||
cmbInstalls_.select( 0 );
|
||||
WesnothInstallsUtils.fillComboWithInstalls( cmbInstalls_ );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -93,4 +89,69 @@ public class WizardProjectPageTemplate extends WizardNewProjectCreationPage
|
|||
{
|
||||
return cmbInstalls_.getText( );
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the project this page was setup with
|
||||
* @return The newly created project's handle
|
||||
*/
|
||||
public IProject createProject( IProgressMonitor monitor,
|
||||
String templateName, List<ReplaceableParameter> params,
|
||||
boolean generatePBL )
|
||||
{
|
||||
monitor.subTask( "Creating the project structure");
|
||||
|
||||
IProject currentProject = getProjectHandle();
|
||||
|
||||
// the project
|
||||
if ( getLocationPath().equals(ResourcesPlugin.getWorkspace().getRoot().getLocation()))
|
||||
{
|
||||
ProjectUtils.createWesnothProject(currentProject, null,
|
||||
getSelectedInstallName( ), true, monitor);
|
||||
}
|
||||
else
|
||||
{
|
||||
IProjectDescription newDescription = ResourcesPlugin.getWorkspace().
|
||||
newProjectDescription(getProjectName());
|
||||
newDescription.setLocation(getLocationPath());
|
||||
ProjectUtils.createWesnothProject(currentProject, newDescription,
|
||||
getSelectedInstallName( ), true, monitor);
|
||||
}
|
||||
|
||||
monitor.worked(2);
|
||||
|
||||
String projectTemplate =
|
||||
TemplateProvider.getInstance().getProcessedTemplate(templateName, params);
|
||||
|
||||
List<Pair<String, String>> files;
|
||||
List<String> dirs;
|
||||
Pair<List<Pair<String, String>>, List<String>> tmp =
|
||||
TemplateProvider.getInstance().getFilesDirectories( projectTemplate );
|
||||
files = tmp.First;
|
||||
dirs = tmp.Second;
|
||||
|
||||
for (Pair<String, String> file : files)
|
||||
{
|
||||
if ( file.Second.equals("pbl") && //$NON-NLS-1$
|
||||
! generatePBL )
|
||||
continue;
|
||||
|
||||
if ( file.Second.equals("build_xml") && //$NON-NLS-1$
|
||||
! needsBuildXML( ) )
|
||||
continue;
|
||||
|
||||
ResourceUtils.createFile( currentProject, file.First,
|
||||
TemplateProvider.getInstance().getProcessedTemplate( file.Second, params ),
|
||||
true );
|
||||
monitor.worked(1);
|
||||
}
|
||||
|
||||
for (String dir : dirs)
|
||||
{
|
||||
ResourceUtils.createFolder(currentProject, dir);
|
||||
monitor.worked(1);
|
||||
}
|
||||
|
||||
monitor.done();
|
||||
return currentProject;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,14 +22,11 @@ public abstract class WizardTemplate extends Wizard implements INewWizard
|
|||
{
|
||||
protected IStructuredSelection selection_;
|
||||
protected IContainer selectionContainer_;
|
||||
protected int lastPageHashCode_ = 0;
|
||||
protected IWizardPage lastPage_;
|
||||
protected boolean isFinished_ = false;
|
||||
protected Object data_ = null;
|
||||
protected String objectName_ = ""; //$NON-NLS-1$
|
||||
|
||||
// TODO: wizards should ask the install
|
||||
// TODO: detect automatically whether a project is in data or add-ons/
|
||||
// without the need for the user to specify
|
||||
public WizardTemplate()
|
||||
{
|
||||
setNeedsProgressMonitor(true);
|
||||
|
@ -85,15 +82,15 @@ public abstract class WizardTemplate extends Wizard implements INewWizard
|
|||
public boolean canFinish()
|
||||
{
|
||||
IWizardPage page = getContainer().getCurrentPage();
|
||||
return super.canFinish() && page.hashCode() == lastPageHashCode_ && page.isPageComplete();
|
||||
return super.canFinish() && page == lastPage_ && page.isPageComplete();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addPages()
|
||||
public void addPage( IWizardPage page )
|
||||
{
|
||||
if (getPageCount() == 0)
|
||||
return;
|
||||
lastPageHashCode_ = getPages()[getPageCount() - 1].hashCode();
|
||||
super.addPage( page );
|
||||
|
||||
lastPage_ = page;
|
||||
}
|
||||
|
||||
public boolean isFinished()
|
||||
|
|
|
@ -13,27 +13,26 @@ import java.util.ArrayList;
|
|||
import java.util.List;
|
||||
|
||||
import org.eclipse.core.resources.IProject;
|
||||
import org.eclipse.core.resources.IProjectDescription;
|
||||
import org.eclipse.core.resources.ResourcesPlugin;
|
||||
import org.eclipse.core.runtime.IProgressMonitor;
|
||||
import org.eclipse.jface.operation.IRunnableWithProgress;
|
||||
import org.wesnoth.Logger;
|
||||
import org.wesnoth.Messages;
|
||||
import org.wesnoth.projects.ProjectUtils;
|
||||
import org.wesnoth.templates.ReplaceableParameter;
|
||||
import org.wesnoth.templates.TemplateProvider;
|
||||
import org.wesnoth.utils.Pair;
|
||||
import org.wesnoth.utils.ResourceUtils;
|
||||
import org.wesnoth.wizards.WizardProjectPageTemplate;
|
||||
import org.wesnoth.wizards.WizardTemplate;
|
||||
|
||||
|
||||
public class CampaignNewWizard extends WizardTemplate
|
||||
{
|
||||
protected WizardProjectPageTemplate page0_;
|
||||
protected WizardProjectPageTemplate page0_;
|
||||
protected CampaignPage1 page1_;
|
||||
protected CampaignPage2 page2_;
|
||||
|
||||
public CampaignNewWizard() {
|
||||
setWindowTitle(Messages.CampaignNewWizard_0);
|
||||
setNeedsProgressMonitor(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addPages()
|
||||
{
|
||||
|
@ -46,13 +45,6 @@ public class CampaignNewWizard extends WizardTemplate
|
|||
|
||||
page2_ = new CampaignPage2();
|
||||
addPage(page2_);
|
||||
|
||||
super.addPages();
|
||||
}
|
||||
|
||||
public CampaignNewWizard() {
|
||||
setWindowTitle(Messages.CampaignNewWizard_0);
|
||||
setNeedsProgressMonitor(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -65,7 +57,15 @@ public class CampaignNewWizard extends WizardTemplate
|
|||
@Override
|
||||
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException
|
||||
{
|
||||
createProject(monitor);
|
||||
IProject currentProject =
|
||||
page0_.createProject( monitor, "campaign_structure",
|
||||
getParameters( ), page1_.needsPBLFile( ) );
|
||||
|
||||
// store some campaign-related info
|
||||
ProjectUtils.getPropertiesForProject( currentProject ).
|
||||
put("difficulties", page2_.getDifficulties()); //$NON-NLS-1$
|
||||
ProjectUtils.getCacheForProject( currentProject ).saveCache( );
|
||||
|
||||
monitor.done();
|
||||
}
|
||||
});
|
||||
|
@ -77,67 +77,9 @@ public class CampaignNewWizard extends WizardTemplate
|
|||
return true;
|
||||
}
|
||||
|
||||
public void createProject(IProgressMonitor monitor)
|
||||
private List<ReplaceableParameter> getParameters( )
|
||||
{
|
||||
monitor.beginTask(Messages.CampaignNewWizard_1, 15);
|
||||
|
||||
IProject currentProject = page0_.getProjectHandle();
|
||||
|
||||
// the project
|
||||
if (page0_.getLocationPath().equals(ResourcesPlugin.getWorkspace().getRoot().getLocation()))
|
||||
{
|
||||
ProjectUtils.createWesnothProject(currentProject, null,
|
||||
page0_.getSelectedInstallName( ), true, monitor);
|
||||
}
|
||||
else
|
||||
{
|
||||
IProjectDescription newDescription = ResourcesPlugin.getWorkspace().
|
||||
newProjectDescription(page0_.getProjectName());
|
||||
newDescription.setLocation(page0_.getLocationPath());
|
||||
ProjectUtils.createWesnothProject(currentProject, newDescription,
|
||||
page0_.getSelectedInstallName( ), true, monitor);
|
||||
}
|
||||
monitor.worked(2);
|
||||
|
||||
String campaignStructure = prepareTemplate("campaign_structure"); //$NON-NLS-1$
|
||||
if (campaignStructure == null)
|
||||
return;
|
||||
|
||||
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 (file.Second.equals("pbl") && //$NON-NLS-1$
|
||||
page1_.needsPBLFile( ) == false)
|
||||
continue;
|
||||
|
||||
if ( file.Second.equals("build_xml") && //$NON-NLS-1$
|
||||
! page0_.needsBuildXML( ) )
|
||||
continue;
|
||||
|
||||
ResourceUtils.createFile(currentProject, file.First, prepareTemplate(file.Second), true);
|
||||
monitor.worked(1);
|
||||
}
|
||||
for (String dir : dirs)
|
||||
{
|
||||
ResourceUtils.createFolder(currentProject, dir);
|
||||
monitor.worked(1);
|
||||
}
|
||||
|
||||
// store some campaign-related info
|
||||
ProjectUtils.getPropertiesForProject(currentProject).put("difficulties", page2_.getDifficulties()); //$NON-NLS-1$
|
||||
ProjectUtils.getCacheForProject( currentProject ).saveCache( );
|
||||
|
||||
monitor.done();
|
||||
}
|
||||
|
||||
private String prepareTemplate(String templateName)
|
||||
{
|
||||
ArrayList<ReplaceableParameter> params = new ArrayList<ReplaceableParameter>();
|
||||
List<ReplaceableParameter> params = new ArrayList<ReplaceableParameter>();
|
||||
|
||||
params.add(new ReplaceableParameter("$$campaign_name", page1_.getCampaignName())); //$NON-NLS-1$
|
||||
params.add(new ReplaceableParameter("$$author", page1_.getAuthor())); //$NON-NLS-1$
|
||||
|
@ -158,6 +100,6 @@ public class CampaignNewWizard extends WizardTemplate
|
|||
params.add(new ReplaceableParameter("$$project_dir_name", page0_.getProjectName())); //$NON-NLS-1$
|
||||
params.add(new ReplaceableParameter("$$type", page1_.isMultiplayer() ? "campaign_mp" : "campaign")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
||||
|
||||
return TemplateProvider.getInstance().getProcessedTemplate(templateName, params);
|
||||
return params;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,27 +12,23 @@ 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;
|
||||
import org.eclipse.core.resources.ResourcesPlugin;
|
||||
import org.eclipse.core.runtime.IProgressMonitor;
|
||||
import org.eclipse.jface.operation.IRunnableWithProgress;
|
||||
import org.wesnoth.Logger;
|
||||
import org.wesnoth.Messages;
|
||||
import org.wesnoth.projects.ProjectUtils;
|
||||
import org.wesnoth.templates.ReplaceableParameter;
|
||||
import org.wesnoth.templates.TemplateProvider;
|
||||
import org.wesnoth.utils.Pair;
|
||||
import org.wesnoth.utils.ResourceUtils;
|
||||
import org.wesnoth.wizards.WizardProjectPageTemplate;
|
||||
import org.wesnoth.wizards.WizardTemplate;
|
||||
|
||||
|
||||
public class EmptyProjectNewWizard extends WizardTemplate
|
||||
{
|
||||
protected WizardProjectPageTemplate page0_;
|
||||
protected EmptyProjectPage1 page1_;
|
||||
|
||||
public EmptyProjectNewWizard() {
|
||||
setWindowTitle(Messages.EmptyProjectNewWizard_0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addPages()
|
||||
{
|
||||
|
@ -42,12 +38,6 @@ public class EmptyProjectNewWizard extends WizardTemplate
|
|||
|
||||
page1_ = new EmptyProjectPage1();
|
||||
addPage(page1_);
|
||||
|
||||
super.addPages();
|
||||
}
|
||||
|
||||
public EmptyProjectNewWizard() {
|
||||
setWindowTitle(Messages.EmptyProjectNewWizard_0);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -68,7 +58,8 @@ public class EmptyProjectNewWizard extends WizardTemplate
|
|||
public void run(IProgressMonitor monitor)
|
||||
throws InvocationTargetException, InterruptedException
|
||||
{
|
||||
createProject(monitor);
|
||||
page0_.createProject( monitor, "empty_project",
|
||||
getParameters( ), page1_.getGeneratePBLFile( ) );
|
||||
monitor.done();
|
||||
}
|
||||
});
|
||||
|
@ -80,65 +71,9 @@ public class EmptyProjectNewWizard extends WizardTemplate
|
|||
return true;
|
||||
}
|
||||
|
||||
public void createProject(IProgressMonitor monitor)
|
||||
private List<ReplaceableParameter> getParameters( )
|
||||
{
|
||||
monitor.beginTask(Messages.EmptyProjectNewWizard_1, 15);
|
||||
|
||||
IProject currentProject = page0_.getProjectHandle();
|
||||
|
||||
// the project
|
||||
if (page0_.getLocationPath().equals(ResourcesPlugin.getWorkspace().getRoot().getLocation()))
|
||||
{
|
||||
ProjectUtils.createWesnothProject(currentProject, null,
|
||||
page0_.getSelectedInstallName( ), true, monitor);
|
||||
}
|
||||
else
|
||||
{
|
||||
IProjectDescription newDescription = ResourcesPlugin.getWorkspace().
|
||||
newProjectDescription(page0_.getProjectName());
|
||||
newDescription.setLocation(page0_.getLocationPath());
|
||||
ProjectUtils.createWesnothProject(currentProject, newDescription,
|
||||
page0_.getSelectedInstallName( ), true, monitor);
|
||||
}
|
||||
|
||||
monitor.worked(2);
|
||||
|
||||
String emptyProjectStructure = prepareTemplate("empty_project"); //$NON-NLS-1$
|
||||
if (emptyProjectStructure == null)
|
||||
return;
|
||||
|
||||
List<Pair<String, String>> files;
|
||||
List<String> dirs;
|
||||
Pair<List<Pair<String, String>>, List<String>> tmp =
|
||||
TemplateProvider.getInstance().getFilesDirectories(emptyProjectStructure);
|
||||
files = tmp.First;
|
||||
dirs = tmp.Second;
|
||||
|
||||
for (Pair<String, String> file : files)
|
||||
{
|
||||
if (file.Second.equals("pbl") && //$NON-NLS-1$
|
||||
page1_.getGeneratePBLFile() == false)
|
||||
continue;
|
||||
|
||||
if ( file.Second.equals("build_xml") && //$NON-NLS-1$
|
||||
! page0_.needsBuildXML( ) )
|
||||
continue;
|
||||
|
||||
ResourceUtils.createFile(currentProject, file.First, prepareTemplate(file.Second), true);
|
||||
monitor.worked(1);
|
||||
}
|
||||
for (String dir : dirs)
|
||||
{
|
||||
ResourceUtils.createFolder(currentProject, dir);
|
||||
monitor.worked(1);
|
||||
}
|
||||
|
||||
monitor.done();
|
||||
}
|
||||
|
||||
private String prepareTemplate(String templateName)
|
||||
{
|
||||
ArrayList<ReplaceableParameter> params = new ArrayList<ReplaceableParameter>();
|
||||
List<ReplaceableParameter> params = new ArrayList<ReplaceableParameter>();
|
||||
|
||||
params.add(new ReplaceableParameter("$$campaign_name", page1_.getCampaignName())); //$NON-NLS-1$
|
||||
params.add(new ReplaceableParameter("$$author", page1_.getAuthor())); //$NON-NLS-1$
|
||||
|
@ -153,6 +88,6 @@ public class EmptyProjectNewWizard extends WizardTemplate
|
|||
params.add(new ReplaceableParameter("$$project_dir_name", page0_.getProjectName())); //$NON-NLS-1$
|
||||
params.add(new ReplaceableParameter("$$type", page1_.getType())); //$NON-NLS-1$
|
||||
|
||||
return TemplateProvider.getInstance().getProcessedTemplate(templateName, params);
|
||||
return params;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue