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"));
}
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)
{
//TODO: sort tags's keys by cardinality (required first) ??
if (parsingDone_ && !force)
return;

View file

@ -4,21 +4,23 @@
*/
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.WizardsConstants;
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);
setWindowTitle(title);
Tag tagContent = SchemaParser.getInstance().getTags().get(tagName);
tagName_ = tagName;
indent_ = indent;
// keys section
int keysNr = tagContent.KeyChildren.size();
@ -27,13 +29,14 @@ public class WizardGenerator extends NewWizardTemplate
for (int i = 0; i < pgsKey; i++)
{
tempPageKey = new WizardGeneratorPageKey(tagName, tagContent.KeyChildren, startKey,
startKey + WizardsConstants.MaxTextBoxesOnPage);
startKey + WizardsConstants.MaxTextBoxesOnPage, (byte) (indent_ + 1));
startKey += WizardsConstants.MaxTextBoxesOnPage;
addPage(tempPageKey);
}
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);
}
@ -44,13 +47,14 @@ public class WizardGenerator extends NewWizardTemplate
for (int i = 0; i < pgsTag; i++)
{
tempPageTag = new WizardGeneratorPageTag(tagName, tagContent.TagChildren, startTag,
startTag + WizardsConstants.MaxGroupsOnPage);
startTag + WizardsConstants.MaxGroupsOnPage, (byte) (indent_ + 1));
startTag += WizardsConstants.MaxTextBoxesOnPage;
addPage(tempPageTag);
}
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);
}
@ -60,6 +64,11 @@ public class WizardGenerator extends NewWizardTemplate
}
}
public byte getIndent()
{
return indent_;
}
@Override
public void addPages()
{
@ -70,13 +79,24 @@ public class WizardGenerator extends NewWizardTemplate
public boolean performFinish()
{
// logic
data_ = "temp";
String result = StringUtils.multiples("\t", indent_) + "[" + tagName_ + "]\n";
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
objectName_ = tagName_;
isFinished_ = 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.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import wesnoth_eclipse_plugin.utils.StringUtils;
public class WizardGeneratorPageKey extends WizardPage
{
private List<TagKey> keys_;
private int startIndex_, endIndex_;
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);
setTitle(tagName + " new wizard");
//setDescription(String.format("page %d to %d out of %d", startIndex, endIndex, keys.size()));
indent_ = indent;
startIndex_ = startIndex;
endIndex_ = endIndex;
keys_ = keys;
@ -56,4 +63,18 @@ public class WizardGeneratorPageKey extends WizardPage
}
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.HashMap;
import java.util.Map.Entry;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
@ -19,6 +20,8 @@ import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.List;
import wesnoth_eclipse_plugin.utils.GUIUtils;
import wesnoth_eclipse_plugin.utils.ListUtils;
import wesnoth_eclipse_plugin.utils.StringUtils;
import wesnoth_eclipse_plugin.wizards.WizardUtils;
public class WizardGeneratorPageTag extends WizardPage
@ -27,12 +30,16 @@ public class WizardGeneratorPageTag extends WizardPage
private HashMap<String, java.util.List<String>> content_;
private int startIndex_, endIndex_;
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);
setTitle(tagName + " new wizard");
//setDescription(String.format("page %d to %d out of %d", startIndex, endIndex, tags.size()));
indent_ = indent;
startIndex_ = startIndex;
endIndex_ = endIndex;
tags_ = tags;
@ -102,7 +109,8 @@ public class WizardGeneratorPageTag extends WizardPage
private void addNewItem(List targetList, String tagName)
{
//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);
if (wizard.isFinished())
{
@ -122,4 +130,16 @@ public class WizardGeneratorPageTag extends WizardPage
content_.get(tagName).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;
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.WizardUtils;
@ -11,6 +36,7 @@ public class WizardLauncher extends NewWizardTemplate
{
WizardLauncherPage0 page0_;
WizardLauncherPage1 page1_;
WizardGenerator wizard_;
public WizardLauncher() {
setWindowTitle("Wizard launcher");
@ -32,9 +58,107 @@ public class WizardLauncher extends NewWizardTemplate
@Override
public boolean performFinish()
{
WizardGenerator wizard = new WizardGenerator(page1_.getTagDescription() + " new wizard", page1_.getTagName());
WizardUtils.launchWizard(wizard, getShell(), selection_);
wizard_ = new WizardGenerator(page1_.getTagDescription() + " new wizard", page1_.getTagName(), (byte) 0);
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.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.dialogs.ContainerSelectionDialog;
import wesnoth_eclipse_plugin.Activator;
@ -201,34 +199,26 @@ public class WizardLauncherPage0 extends WizardPage
else
{
// current file checking
if (selection_ != null && selection_.isEmpty() == false &&
selection_ instanceof IStructuredSelection && selection_.size() > 0)
if (getEditedFile() != null)
{
try
{
IEditorReference[] references =
Activator.getDefault().getWorkbench().getActiveWorkbenchWindow().getPages()[0].getEditorReferences();
if (references.length > 0)
{
IEditorInput input = references[0].getEditorInput();
lblCurrentFileOpened.setText("File " + input.getName() + " opened.");
}
else
{
lblCurrentFileOpened.setText("No file opened.");
setErrorMessage("No file opened.");
return;
}
} catch (PartInitException e)
{
e.printStackTrace();
}
lblCurrentFileOpened.setText("File " + getEditedFile().getEditorInput().getName() + " opened.");
}
else
{
lblCurrentFileOpened.setText("No file opened.");
setErrorMessage("No file opened.");
return;
}
}
setPageComplete(true);
setErrorMessage(null);
}
public IEditorPart getEditedFile()
{
return Activator.getDefault().getWorkbench().getActiveWorkbenchWindow().getPages()[0].getActiveEditor();
}
public void updateEnabledStatus()
{
// new file section
@ -277,7 +267,7 @@ public class WizardLauncherPage0 extends WizardPage
{
ContainerSelectionDialog dialog =
new ContainerSelectionDialog(getShell(), ResourcesPlugin.getWorkspace().getRoot(), false,
"Select a campaign project");
"Select a directory");
if (dialog.open() == ContainerSelectionDialog.OK)
{
Object[] result = dialog.getResult();
@ -290,11 +280,16 @@ public class WizardLauncherPage0 extends WizardPage
public String getFileName()
{
return radioNewFile.getSelection() == true ? txtFileName_.getText() : "";
return radioNewFile.getSelection() == true ? txtFileName_.getText() : getEditedFile().getEditorInput().getName();
}
public String getDirectoryName()
{
return radioNewFile.getSelection() == true ? txtDirectory_.getText() : "";
}
public boolean getIsTargetNewFile()
{
return radioNewFile.getSelection();
}
}