eclipse plugin: add wizard for faction creation

This commit is contained in:
Timotei Dolean 2010-07-05 18:13:35 +00:00
parent bdb785f200
commit a5d2ff37aa
7 changed files with 587 additions and 1 deletions

View file

@ -229,6 +229,14 @@
name="Wesnoth Era"
project="false">
</wizard>
<wizard
category="wesnoth.eclipse.newWizards"
class="wesnoth_eclipse_plugin.wizards.FactionNewWizard"
icon="icons/wesnoth-icon_16.png"
id="wesnoth_eclipse_plugin.wizard.factionNewWizard"
name="Wesnoth Faction"
project="false">
</wizard>
</extension>
<extension
point="org.eclipse.ui.commands">

View file

@ -101,7 +101,7 @@ public class EraPage0 extends WizardPage
txtEraID_.addModifyListener(modifyListener);
Label lblEraName = new Label(container, SWT.NONE);
lblEraName.setText("Era name:");
lblEraName.setText("Era name*:");
txtEraName_ = new Text(container, SWT.BORDER);
txtEraName_.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
@ -162,6 +162,12 @@ public class EraPage0 extends WizardPage
return;
}
if (getEraName().isEmpty())
{
setErrorMessage("The era name cannot be empty.");
return;
}
setErrorMessage(null);
setPageComplete(true);
}

View file

@ -0,0 +1,159 @@
/**
* @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 FactionNewWizard extends NewWizardTemplate
{
FactionPage0 page0_;
FactionPage1 page1_;
@Override
public void addPages()
{
page0_ = new FactionPage0(selection_);
addPage(page0_);
page1_ = new FactionPage1();
addPage(page1_);
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 = getFactionStream();
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 getFactionStream()
{
ArrayList<ReplaceableParameter> params = new ArrayList<ReplaceableParameter>();
params.add(new ReplaceableParameter("$$faction_id", String.valueOf(page0_.getFactionId())));
params.add(new ReplaceableParameter("$$faction_name", String.valueOf(page0_.getFactionName())));
params.add(new ReplaceableParameter("$$faction_type", String.valueOf(page0_.getType())));
params.add(new ReplaceableParameter("$$leader", String.valueOf(page0_.getLeader())));
params.add(new ReplaceableParameter("$$random_leader", String.valueOf(page0_.getRandomLeader())));
params.add(new ReplaceableParameter("$$terrain_liked", String.valueOf(page0_.getTerrainLiked())));
params.add(new ReplaceableParameter("$$random_faction", String.valueOf(page1_.getIsRandomFaction())));
params.add(new ReplaceableParameter("$$choices", String.valueOf(page1_.getChoices())));
params.add(new ReplaceableParameter("$$except", String.valueOf(page1_.getExcept())));
String template = TemplateProvider.getInstance().getProcessedTemplate("faction", params);
if (template == null)
{
GUIUtils.showMessageBox(WorkspaceUtils.getWorkbenchWindow(), "Template for \"faction\" not found.");
return null;
}
return new ByteArrayInputStream(template.getBytes());
}
}

View file

@ -0,0 +1,299 @@
/**
* @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 FactionPage0 extends WizardPage
{
private ISelection selection_;
private Text txtFileName_;
private Text txtDirectory_;
private Text txtFactionId_;
private Text txtFactionName_;
private Text txtType_;
private Text txtLeader_;
private Text txtRandomLeader_;
private Text txtTerrainLiked_;
private Text text;
/**
* Create the wizard.
*/
public FactionPage0(ISelection selection) {
super("wizardPage");
setTitle("New faction wizard");
setDescription("Create a new faction");
}
/**
* Create contents of the wizard.
*
* @param parent
*/
@Override
public void createControl(Composite parent)
{
Composite container = new Composite(parent, SWT.NULL);
ModifyListener modifyListener = new ModifyListener() {
@Override
public void modifyText(ModifyEvent e)
{
updatePageIsComplete();
}
};
setControl(container);
container.setLayout(new GridLayout(3, false));
Label label = new Label(container, SWT.NONE);
GridData gd_label = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
gd_label.widthHint = 95;
label.setLayoutData(gd_label);
label.setText("Directory* :");
txtDirectory_ = new Text(container, SWT.BORDER);
txtDirectory_.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
txtDirectory_.addModifyListener(modifyListener);
Button button = new Button(container, SWT.NONE);
button.setText("Browse...");
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e)
{
handleBrowse();
}
});
Label label_4 = new Label(container, SWT.NONE);
label_4.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
label_4.setText("File name* :");
txtFileName_ = new Text(container, SWT.BORDER);
txtFileName_.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
new Label(container, SWT.NONE);
txtFileName_.addModifyListener(modifyListener);
Label lblFactionId = new Label(container, SWT.NONE);
lblFactionId.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
lblFactionId.setText("Faction Id*:");
txtFactionId_ = new Text(container, SWT.BORDER);
txtFactionId_.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
new Label(container, SWT.NONE);
txtFactionId_.addModifyListener(modifyListener);
Label lblName = new Label(container, SWT.NONE);
lblName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
lblName.setText("Faction name*:");
txtFactionName_ = new Text(container, SWT.BORDER);
txtFactionName_.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
new Label(container, SWT.NONE);
txtFactionName_.addModifyListener(modifyListener);
Label lblType = new Label(container, SWT.NONE);
lblType.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
lblType.setText("Type:");
txtType_ = new Text(container, SWT.BORDER);
txtType_.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
new Label(container, SWT.NONE);
Label lblLeader = new Label(container, SWT.NONE);
lblLeader.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
lblLeader.setText("Leader:");
txtLeader_ = new Text(container, SWT.BORDER);
txtLeader_.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
new Label(container, SWT.NONE);
Label lblRandomLeaders = new Label(container, SWT.NONE);
lblRandomLeaders.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
lblRandomLeaders.setText("Random leader:");
txtRandomLeader_ = new Text(container, SWT.BORDER);
txtRandomLeader_.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
new Label(container, SWT.NONE);
Label lblTerrainLiked = new Label(container, SWT.NONE);
lblTerrainLiked.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
lblTerrainLiked.setText("Terrain liked:");
txtTerrainLiked_ = new Text(container, SWT.BORDER);
txtTerrainLiked_.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
new Label(container, SWT.NONE);
Label lblRecruit = new Label(container, SWT.NONE);
lblRecruit.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
lblRecruit.setText("Recruit:");
text = new Text(container, SWT.BORDER);
text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
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 (getFactionId().isEmpty())
{
setErrorMessage("The faction ID cannot be empty.");
return;
}
if (getFactionName().isEmpty())
{
setErrorMessage("The faction name cannot be empty.");
return;
}
setErrorMessage(null);
setPageComplete(true);
}
/**
* Tests if the current workbench selection is a suitable campaign to use.
*/
private void initialize()
{
setPageComplete(true);
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());
}
}
}
public String getDirectoryName()
{
return txtDirectory_.getText();
}
public String getFactionId()
{
return txtFactionId_.getText();
}
public String getFactionName()
{
return txtFactionName_.getText();
}
public String getFileName()
{
return txtFileName_.getText();
}
public String getLeader()
{
return txtLeader_.getText();
}
public String getTerrainLiked()
{
return txtTerrainLiked_.getText();
}
public String getRandomLeader()
{
return txtRandomLeader_.getText();
}
public String getType()
{
return txtType_.getText();
}
}

View file

@ -0,0 +1,101 @@
/**
* @author Timotei Dolean
*
*/
package wesnoth_eclipse_plugin.wizards;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
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;
public class FactionPage1 extends WizardPage
{
private Text txtChoices_;
private Text txtExcept_;
private Button chkRandomFaction_;
/**
* Create the wizard.
*/
public FactionPage1() {
super("wizardPage");
setTitle("New faction wizard");
setDescription("Faction details");
}
/**
* Create contents of the wizard.
*
* @param parent
*/
@Override
public void createControl(Composite parent)
{
Composite container = new Composite(parent, SWT.NULL);
// the page doesn't have any requirements, so it's complete by default
setPageComplete(true);
setControl(container);
container.setLayout(new GridLayout(2, false));
new Label(container, SWT.NONE);
chkRandomFaction_ = new Button(container, SWT.CHECK);
chkRandomFaction_.setText("Random faction");
chkRandomFaction_.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e)
{
if (!(e.getSource() instanceof Button))
return;
setExtraFactionEnableness(((Button) e.getSource()).getSelection());
}
});
Label lblR = new Label(container, SWT.NONE);
GridData gd_lblR = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
gd_lblR.widthHint = 77;
lblR.setLayoutData(gd_lblR);
lblR.setText("Choices:");
txtChoices_ = new Text(container, SWT.BORDER);
txtChoices_.setEnabled(false);
txtChoices_.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
Label lblExcept = new Label(container, SWT.NONE);
lblExcept.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
lblExcept.setText("Except");
txtExcept_ = new Text(container, SWT.BORDER);
txtExcept_.setEnabled(false);
txtExcept_.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
}
private void setExtraFactionEnableness(boolean state)
{
txtChoices_.setEnabled(state);
txtExcept_.setEnabled(state);
}
public boolean getIsRandomFaction()
{
return chkRandomFaction_.getSelection();
}
public String getChoices()
{
return getIsRandomFaction() ? txtChoices_.getText() : "";
}
public String getExcept()
{
return getIsRandomFaction() ? txtExcept_.getText() : "";
}
}

View file

@ -0,0 +1,12 @@
[multiplayer_side]
id=$$faction_id
name=$$faction_name
type=$$faction_type
leader=$$leader
random_leader=$$random_leader
terrain_liked=$$terrain_liked
random_faction=$$random_faction
choices=$$choices
except=$$except
[/multiplayer_side]

View file

@ -7,3 +7,4 @@ pbl templates/pbl.txt
build_xml templates/build.xml
multiplayer templates/multiplayer.txt
era templates/era.txt
faction templates/faction.txt