eclipse plugin: schema-based wizards - step 5

- generating the WML code

- inserting the WML in a new file or in edited file in the editor
This commit is contained in:
Timotei Dolean 2010-07-08 22:08:43 +00:00
parent 2e67bbbd7f
commit 04c2323917
7 changed files with 237 additions and 44 deletions

View File

@ -169,4 +169,16 @@ public class StringUtils
return string.replace(source, ListUtils.concatenateArray(tmpTarget, "\n")); return string.replace(source, ListUtils.concatenateArray(tmpTarget, "\n"));
} }
public static String multiples(String sequence, int times)
{
if (sequence == null)
return null;
StringBuilder res = new StringBuilder(sequence.length() * times);
for (int i = 0; i < times; i++)
{
res.append(sequence);
}
return res.toString();
}
} }

View File

@ -30,6 +30,7 @@ public class SchemaParser
public void parseSchema(boolean force) public void parseSchema(boolean force)
{ {
//TODO: sort tags's keys by cardinality (required first) ??
if (parsingDone_ && !force) if (parsingDone_ && !force)
return; return;

View File

@ -4,21 +4,23 @@
*/ */
package wesnoth_eclipse_plugin.wizards.generator; package wesnoth_eclipse_plugin.wizards.generator;
import java.util.List; import org.eclipse.jface.wizard.IWizardPage;
import wesnoth_eclipse_plugin.utils.StringUtils;
import wesnoth_eclipse_plugin.wizards.NewWizardTemplate; import wesnoth_eclipse_plugin.wizards.NewWizardTemplate;
import wesnoth_eclipse_plugin.wizards.WizardsConstants; import wesnoth_eclipse_plugin.wizards.WizardsConstants;
public class WizardGenerator extends NewWizardTemplate public class WizardGenerator extends NewWizardTemplate
{ {
List<WizardGeneratorPageKey> pagesList_; private String tagName_;
private String tagName_; private byte indent_;
public WizardGenerator(String title, String tagName) { public WizardGenerator(String title, String tagName, byte indent) {
SchemaParser.getInstance().parseSchema(false); SchemaParser.getInstance().parseSchema(false);
setWindowTitle(title); setWindowTitle(title);
Tag tagContent = SchemaParser.getInstance().getTags().get(tagName); Tag tagContent = SchemaParser.getInstance().getTags().get(tagName);
tagName_ = tagName; tagName_ = tagName;
indent_ = indent;
// keys section // keys section
int keysNr = tagContent.KeyChildren.size(); int keysNr = tagContent.KeyChildren.size();
@ -27,13 +29,14 @@ public class WizardGenerator extends NewWizardTemplate
for (int i = 0; i < pgsKey; i++) for (int i = 0; i < pgsKey; i++)
{ {
tempPageKey = new WizardGeneratorPageKey(tagName, tagContent.KeyChildren, startKey, tempPageKey = new WizardGeneratorPageKey(tagName, tagContent.KeyChildren, startKey,
startKey + WizardsConstants.MaxTextBoxesOnPage); startKey + WizardsConstants.MaxTextBoxesOnPage, (byte) (indent_ + 1));
startKey += WizardsConstants.MaxTextBoxesOnPage; startKey += WizardsConstants.MaxTextBoxesOnPage;
addPage(tempPageKey); addPage(tempPageKey);
} }
if (keysNr - 1 > 0) if (keysNr - 1 > 0)
{ {
tempPageKey = new WizardGeneratorPageKey(tagName, tagContent.KeyChildren, startKey, keysNr - 1); tempPageKey = new WizardGeneratorPageKey(tagName, tagContent.KeyChildren,
startKey, keysNr - 1, (byte) (indent_ + 1));
addPage(tempPageKey); addPage(tempPageKey);
} }
@ -44,13 +47,14 @@ public class WizardGenerator extends NewWizardTemplate
for (int i = 0; i < pgsTag; i++) for (int i = 0; i < pgsTag; i++)
{ {
tempPageTag = new WizardGeneratorPageTag(tagName, tagContent.TagChildren, startTag, tempPageTag = new WizardGeneratorPageTag(tagName, tagContent.TagChildren, startTag,
startTag + WizardsConstants.MaxGroupsOnPage); startTag + WizardsConstants.MaxGroupsOnPage, (byte) (indent_ + 1));
startTag += WizardsConstants.MaxTextBoxesOnPage; startTag += WizardsConstants.MaxTextBoxesOnPage;
addPage(tempPageTag); addPage(tempPageTag);
} }
if (tagsNr - 1 > 0) if (tagsNr - 1 > 0)
{ {
tempPageTag = new WizardGeneratorPageTag(tagName, tagContent.TagChildren, startTag, tagsNr - 1); tempPageTag = new WizardGeneratorPageTag(tagName, tagContent.TagChildren,
startTag, tagsNr - 1, (byte) (indent_ + 1));
addPage(tempPageTag); addPage(tempPageTag);
} }
@ -60,6 +64,11 @@ public class WizardGenerator extends NewWizardTemplate
} }
} }
public byte getIndent()
{
return indent_;
}
@Override @Override
public void addPages() public void addPages()
{ {
@ -70,13 +79,24 @@ public class WizardGenerator extends NewWizardTemplate
public boolean performFinish() public boolean performFinish()
{ {
// logic // logic
String result = StringUtils.multiples("\t", indent_) + "[" + tagName_ + "]\n";
data_ = "temp"; StringBuilder keys = new StringBuilder();
StringBuilder tags = new StringBuilder();
for (IWizardPage page : getPages())
{
if (page instanceof WizardGeneratorPageKey)
keys.append(((WizardGeneratorPageKey) page).getContent());
else if (page instanceof WizardGeneratorPageTag)
tags.append(((WizardGeneratorPageTag) page).getContent());
else
; // skip 404 pages
}
result += (keys.toString() + tags.toString());
result += (StringUtils.multiples("\t", indent_) + "[/" + tagName_ + "]\n");
data_ = result;
// for now let's just return tag's name // for now let's just return tag's name
objectName_ = tagName_; objectName_ = tagName_;
isFinished_ = true; isFinished_ = true;
return true; return true;
} }
} }

View File

@ -11,20 +11,27 @@ import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text; import org.eclipse.swt.widgets.Text;
import wesnoth_eclipse_plugin.utils.StringUtils;
public class WizardGeneratorPageKey extends WizardPage public class WizardGeneratorPageKey extends WizardPage
{ {
private List<TagKey> keys_; private List<TagKey> keys_;
private int startIndex_, endIndex_; private int startIndex_, endIndex_;
private Composite container_; private Composite container_;
private byte indent_;
public WizardGeneratorPageKey(String tagName, List<TagKey> keys, int startIndex, int endIndex) { public WizardGeneratorPageKey(String tagName, List<TagKey> keys,
int startIndex, int endIndex, byte indent) {
super("wizardPageKey" + startIndex); super("wizardPageKey" + startIndex);
setTitle(tagName + " new wizard"); setTitle(tagName + " new wizard");
//setDescription(String.format("page %d to %d out of %d", startIndex, endIndex, keys.size())); //setDescription(String.format("page %d to %d out of %d", startIndex, endIndex, keys.size()));
indent_ = indent;
startIndex_ = startIndex; startIndex_ = startIndex;
endIndex_ = endIndex; endIndex_ = endIndex;
keys_ = keys; keys_ = keys;
@ -56,4 +63,18 @@ public class WizardGeneratorPageKey extends WizardPage
} }
setPageComplete(true); setPageComplete(true);
} }
public String getContent()
{
StringBuilder result = new StringBuilder();
for (Control child : container_.getChildren())
{
if (!(child instanceof Text))
continue;
result.append(StringUtils.multiples("\t", indent_) +
child.getData("name") + "=" + ((Text) child).getText() + "\n");
}
return result.toString();
}
} }

View File

@ -6,6 +6,7 @@ package wesnoth_eclipse_plugin.wizards.generator;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map.Entry;
import org.eclipse.jface.wizard.WizardPage; import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT; import org.eclipse.swt.SWT;
@ -19,6 +20,8 @@ import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.List; import org.eclipse.swt.widgets.List;
import wesnoth_eclipse_plugin.utils.GUIUtils; import wesnoth_eclipse_plugin.utils.GUIUtils;
import wesnoth_eclipse_plugin.utils.ListUtils;
import wesnoth_eclipse_plugin.utils.StringUtils;
import wesnoth_eclipse_plugin.wizards.WizardUtils; import wesnoth_eclipse_plugin.wizards.WizardUtils;
public class WizardGeneratorPageTag extends WizardPage public class WizardGeneratorPageTag extends WizardPage
@ -27,12 +30,16 @@ public class WizardGeneratorPageTag extends WizardPage
private HashMap<String, java.util.List<String>> content_; private HashMap<String, java.util.List<String>> content_;
private int startIndex_, endIndex_; private int startIndex_, endIndex_;
private Composite container_; private Composite container_;
private byte indent_;
public WizardGeneratorPageTag(String tagName, java.util.List<Tag> tags, int startIndex, int endIndex) { public WizardGeneratorPageTag(String tagName, java.util.List<Tag> tags,
int startIndex, int endIndex, byte indent) {
super("wizardPageTag" + startIndex); super("wizardPageTag" + startIndex);
setTitle(tagName + " new wizard"); setTitle(tagName + " new wizard");
//setDescription(String.format("page %d to %d out of %d", startIndex, endIndex, tags.size())); //setDescription(String.format("page %d to %d out of %d", startIndex, endIndex, tags.size()));
indent_ = indent;
startIndex_ = startIndex; startIndex_ = startIndex;
endIndex_ = endIndex; endIndex_ = endIndex;
tags_ = tags; tags_ = tags;
@ -102,7 +109,8 @@ public class WizardGeneratorPageTag extends WizardPage
private void addNewItem(List targetList, String tagName) private void addNewItem(List targetList, String tagName)
{ {
//TODO: check for multiple addings //TODO: check for multiple addings
WizardGenerator wizard = new WizardGenerator("Create a new " + tagName, tagName); WizardGenerator wizard =
new WizardGenerator("Create a new " + tagName, tagName, (byte) (indent_ + 1));
WizardUtils.launchWizard(wizard, getShell(), null); WizardUtils.launchWizard(wizard, getShell(), null);
if (wizard.isFinished()) if (wizard.isFinished())
{ {
@ -122,4 +130,16 @@ public class WizardGeneratorPageTag extends WizardPage
content_.get(tagName).remove(targetList.getSelectionIndex()); content_.get(tagName).remove(targetList.getSelectionIndex());
targetList.remove(targetList.getSelectionIndex()); targetList.remove(targetList.getSelectionIndex());
} }
public String getContent()
{
StringBuilder result = new StringBuilder();
for (Entry<String, java.util.List<String>> tag : content_.entrySet())
{
result.append(StringUtils.multiples("\t", indent_) + "[" + tag.getKey() + "]\n");
result.append(ListUtils.concatenateList(tag.getValue(), "\n\t"));
result.append(StringUtils.multiples("\t", indent_) + "[/" + tag.getKey() + "]\n");
}
return result.toString();
}
} }

View File

@ -4,6 +4,31 @@
*/ */
package wesnoth_eclipse_plugin.wizards.generator; package wesnoth_eclipse_plugin.wizards.generator;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
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.jface.text.IDocument;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.texteditor.AbstractTextEditor;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.ITextEditor;
import wesnoth_eclipse_plugin.wizards.NewWizardTemplate; import wesnoth_eclipse_plugin.wizards.NewWizardTemplate;
import wesnoth_eclipse_plugin.wizards.WizardUtils; import wesnoth_eclipse_plugin.wizards.WizardUtils;
@ -11,6 +36,7 @@ public class WizardLauncher extends NewWizardTemplate
{ {
WizardLauncherPage0 page0_; WizardLauncherPage0 page0_;
WizardLauncherPage1 page1_; WizardLauncherPage1 page1_;
WizardGenerator wizard_;
public WizardLauncher() { public WizardLauncher() {
setWindowTitle("Wizard launcher"); setWindowTitle("Wizard launcher");
@ -32,9 +58,107 @@ public class WizardLauncher extends NewWizardTemplate
@Override @Override
public boolean performFinish() public boolean performFinish()
{ {
WizardGenerator wizard = new WizardGenerator(page1_.getTagDescription() + " new wizard", page1_.getTagName()); wizard_ = new WizardGenerator(page1_.getTagDescription() + " new wizard", page1_.getTagName(), (byte) 0);
WizardUtils.launchWizard(wizard, getShell(), selection_); WizardUtils.launchWizard(wizard_, getShell(), selection_);
return false; IRunnableWithProgress op = new IRunnableWithProgress() {
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException
{
try
{
doFinish(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(IProgressMonitor monitor) throws CoreException
{
// The file is opened in the editor -> just copy-paste the text
if (!(page0_.getIsTargetNewFile()))
{
try
{
IEditorPart part = page0_.getEditedFile();
if (!(part instanceof AbstractTextEditor))
return;
ITextEditor editor = (ITextEditor) part;
IDocumentProvider dp = editor.getDocumentProvider();
IDocument doc = dp.getDocument(editor.getEditorInput());
int offset = ((ITextSelection) editor.getSelectionProvider().getSelection()).getOffset();
doc.replace(offset, 0, wizard_.getData().toString());
} catch (Exception e)
{
e.printStackTrace();
}
return;
}
final String containerName = page0_.getDirectoryName();
final String fileName = page0_.getFileName();
// create the 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 = new ByteArrayInputStream(wizard_.getData().toString().getBytes());
if (file.exists())
{
file.setContents(stream, true, true, monitor);
}
else
{
file.create(stream, true, monitor);
}
stream.close();
} catch (Exception 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);
} }
} }

View File

@ -21,9 +21,7 @@ import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text; import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.dialogs.ContainerSelectionDialog; import org.eclipse.ui.dialogs.ContainerSelectionDialog;
import wesnoth_eclipse_plugin.Activator; import wesnoth_eclipse_plugin.Activator;
@ -201,34 +199,26 @@ public class WizardLauncherPage0 extends WizardPage
else else
{ {
// current file checking // current file checking
if (selection_ != null && selection_.isEmpty() == false && if (getEditedFile() != null)
selection_ instanceof IStructuredSelection && selection_.size() > 0)
{ {
try lblCurrentFileOpened.setText("File " + getEditedFile().getEditorInput().getName() + " opened.");
{ }
IEditorReference[] references = else
Activator.getDefault().getWorkbench().getActiveWorkbenchWindow().getPages()[0].getEditorReferences(); {
if (references.length > 0) lblCurrentFileOpened.setText("No file opened.");
{ setErrorMessage("No file opened.");
IEditorInput input = references[0].getEditorInput(); return;
lblCurrentFileOpened.setText("File " + input.getName() + " opened.");
}
else
{
lblCurrentFileOpened.setText("No file opened.");
setErrorMessage("No file opened.");
return;
}
} catch (PartInitException e)
{
e.printStackTrace();
}
} }
} }
setPageComplete(true); setPageComplete(true);
setErrorMessage(null); setErrorMessage(null);
} }
public IEditorPart getEditedFile()
{
return Activator.getDefault().getWorkbench().getActiveWorkbenchWindow().getPages()[0].getActiveEditor();
}
public void updateEnabledStatus() public void updateEnabledStatus()
{ {
// new file section // new file section
@ -277,7 +267,7 @@ public class WizardLauncherPage0 extends WizardPage
{ {
ContainerSelectionDialog dialog = ContainerSelectionDialog dialog =
new ContainerSelectionDialog(getShell(), ResourcesPlugin.getWorkspace().getRoot(), false, new ContainerSelectionDialog(getShell(), ResourcesPlugin.getWorkspace().getRoot(), false,
"Select a campaign project"); "Select a directory");
if (dialog.open() == ContainerSelectionDialog.OK) if (dialog.open() == ContainerSelectionDialog.OK)
{ {
Object[] result = dialog.getResult(); Object[] result = dialog.getResult();
@ -290,11 +280,16 @@ public class WizardLauncherPage0 extends WizardPage
public String getFileName() public String getFileName()
{ {
return radioNewFile.getSelection() == true ? txtFileName_.getText() : ""; return radioNewFile.getSelection() == true ? txtFileName_.getText() : getEditedFile().getEditorInput().getName();
} }
public String getDirectoryName() public String getDirectoryName()
{ {
return radioNewFile.getSelection() == true ? txtDirectory_.getText() : ""; return radioNewFile.getSelection() == true ? txtDirectory_.getText() : "";
} }
public boolean getIsTargetNewFile()
{
return radioNewFile.getSelection();
}
} }