eclipse plugin: schema-based wizards - step 7...

...implement constraints checkers and added comboboxes for "enum"
value types
This commit is contained in:
Timotei Dolean 2010-07-09 10:34:41 +00:00
parent 8cfb9385be
commit 358612dbb5
5 changed files with 108 additions and 22 deletions

View file

@ -2,6 +2,11 @@
[schema] [schema]
identifier="re ^[a-zA-Z0-9_ ]+$" identifier="re ^[a-zA-Z0-9_ ]+$"
identifierlist="re ^([a-zA-Z0-9_ ]+,)*[a-zA-Z0-9_ ]+$" identifierlist="re ^([a-zA-Z0-9_ ]+,)*[a-zA-Z0-9_ ]+$"
string="re ^"[\d\w\s]*"$"
tstring="re ^_"[\d\w\s]*"$"
integer="re ^\d+$"
float="re ^\d+.\d+$"
boolean="enum true,false"
# slash-separated filenames # slash-separated filenames
path="re ^([a-zA-Z0-9_\-.+]+/)*[a-zA-Z0-9_\-.+]+(~(TC|RC|PAL|FL|GS|CS|CROP|SCALE|BL|O|R|G|B|NOP|RIGHT)\(.*\))*$" path="re ^([a-zA-Z0-9_\-.+]+/)*[a-zA-Z0-9_\-.+]+(~(TC|RC|PAL|FL|GS|CS|CROP|SCALE|BL|O|R|G|B|NOP|RIGHT)\(.*\))*$"
#TODO: imagepath, remove imagepathfunctions from path #TODO: imagepath, remove imagepathfunctions from path

View file

@ -8,6 +8,7 @@ import java.io.File;
import java.util.HashMap; import java.util.HashMap;
import java.util.Stack; import java.util.Stack;
import wesnoth_eclipse_plugin.Logger;
import wesnoth_eclipse_plugin.preferences.PreferenceConstants; import wesnoth_eclipse_plugin.preferences.PreferenceConstants;
import wesnoth_eclipse_plugin.preferences.PreferenceInitializer; import wesnoth_eclipse_plugin.preferences.PreferenceInitializer;
import wesnoth_eclipse_plugin.utils.ResourceUtils; import wesnoth_eclipse_plugin.utils.ResourceUtils;
@ -34,16 +35,20 @@ public class SchemaParser
if (parsingDone_ && !force) if (parsingDone_ && !force)
return; return;
parsingDone_ = false;
if (force)
{
primitives_.clear();
tags_.clear();
}
Logger.print("parsing schema " + (force == true ? "forced" : ""));
File schemaFile = new File(PreferenceInitializer.getString( File schemaFile = new File(PreferenceInitializer.getString(
PreferenceConstants.P_WESNOTH_WORKING_DIR) + "/data/schema.cfg"); PreferenceConstants.P_WESNOTH_WORKING_DIR) + "/data/schema.cfg");
String res = ResourceUtils.getFileContents(schemaFile); String res = ResourceUtils.getFileContents(schemaFile);
String[] lines = StringUtils.getLines(res); String[] lines = StringUtils.getLines(res);
Stack<String> tagStack = new Stack<String>(); Stack<String> tagStack = new Stack<String>();
// temporarly add 'string' and 'tstring' primitives
primitives_.put("string", "\"[\\d\\w\\s]\"");
primitives_.put("tstring", "_\"[\\d\\w\\s]\"");
Tag currentTag = null; Tag currentTag = null;
for (int index = 0; index < lines.length; index++) for (int index = 0; index < lines.length; index++)
{ {
@ -144,7 +149,7 @@ public class SchemaParser
continue; //return; continue; //return;
} }
if (currentTag != null)//tags.containsKey(tagStack.peek())) if (currentTag != null)
{ {
if (tokens[0].startsWith("_")) // reference to another tag if (tokens[0].startsWith("_")) // reference to another tag
{ {
@ -166,7 +171,9 @@ public class SchemaParser
{ {
if (!(primitives_.containsKey(value[1]))) if (!(primitives_.containsKey(value[1])))
currentTag.NeedsExpanding = true; currentTag.NeedsExpanding = true;
currentTag.addKey(tokens[0], value[1], getCardinality(value[0])); if (primitives_.get(value[1]) == null)
System.err.println("Undefined primitive type in schema.cfg for: " + value[1]);
currentTag.addKey(tokens[0], primitives_.get(value[1]), getCardinality(value[0]));
} }
} }
else else
@ -214,7 +221,7 @@ public class SchemaParser
String res = indent + "[" + tag.Name + "]\n"; String res = indent + "[" + tag.Name + "]\n";
for (TagKey key : tag.KeyChildren) for (TagKey key : tag.KeyChildren)
{ {
res += (indent + "\t" + key.Name + "=" + key.Regex + "\n"); res += (indent + "\t" + key.Name + "=" + key.ValueType + "\n");
} }
for (Tag tmpTag : tag.TagChildren) for (Tag tmpTag : tag.TagChildren)
{ {

View file

@ -49,8 +49,8 @@ class Tag
KeyChildren.add(key); KeyChildren.add(key);
} }
public void addKey(String name, String regex, char cardinality) public void addKey(String name, String valueType, char cardinality)
{ {
addKey(new TagKey(name, cardinality, regex)); addKey(new TagKey(name, cardinality, valueType));
} }
} }

View file

@ -15,11 +15,18 @@ public class TagKey
* - = forbidden * - = forbidden
*/ */
public char Cardinality; public char Cardinality;
public String Regex; public String ValueType;
public boolean IsEnum;
public TagKey(String name, char cardinality, String regex) { public TagKey(String name, char cardinality, String valueType) {
Name = name; Name = name;
Cardinality = cardinality; Cardinality = cardinality;
Regex = regex;
if (valueType == null)
{
return;
}
IsEnum = valueType.substring(1, valueType.indexOf(" ")).equals("enum");
ValueType = valueType.substring(valueType.indexOf(" ") + 1, valueType.length() - 1); // remove the " "
} }
} }

View file

@ -8,8 +8,11 @@ import java.util.List;
import org.eclipse.jface.wizard.WizardPage; import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT; import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
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.Combo;
import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Label;
@ -47,21 +50,81 @@ public class WizardGeneratorPageKey extends WizardPage
for (int i = startIndex_; i <= endIndex_; i++) for (int i = startIndex_; i <= endIndex_; i++)
{ {
TagKey key = keys_.get(i); TagKey key = keys_.get(i);
Label label = new Label(container_, SWT.NONE); Label label = new Label(container_, SWT.NONE);
label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1)); label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
// add star to required items // add star to required items
label.setText(key.Name + (key.Cardinality == '1' ? "*" : "") + ":"); label.setText(key.Name + (key.Cardinality == '1' ? "*" : "") + ":");
Text textBox = new Text(container_, SWT.BORDER); // if the is an enum create a combobox instead of textbox
textBox.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1)); if (key.IsEnum)
//textBox.setText(key.Regex); {
textBox.setData("name", key.Name); Combo combo = new Combo(container_, SWT.READ_ONLY);
textBox.setData("regex", key.Regex); combo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
textBox.setData("card", key.Cardinality); combo.setData("name", key.Name);
String[] items = key.ValueType.split(",");
combo.setItems(items);
combo.select(0);
}
else
{
Text textBox = new Text(container_, SWT.BORDER);
textBox.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
//TODO: check for regex/cardinality textBox.setData("name", key.Name);
textBox.setData("valType", key.ValueType);
textBox.setData("card", key.Cardinality);
if (key.Cardinality == '1')
textBox.setData("comp", false); // is textbox complete
textBox.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent e)
{
if (!(e.getSource() instanceof Text))
return;
Text txt = ((Text) e.getSource());
if (txt.getData("comp") == null)
return;
if ((txt.getText().isEmpty() && (txt.getData("card").toString().equals("1"))) || // cardinality
!txt.getText().matches(txt.getData("valType").toString()) // regex
)
txt.setData("comp", false);
else
txt.setData("comp", true);
updatePageIsComplete();
}
});
}
} }
updatePageIsComplete();
}
private void updatePageIsComplete()
{
setPageComplete(false);
for (Control child : container_.getChildren())
{
if (!(child instanceof Text)) // don't check comboboxes
continue;
if (child.getData("comp") == null)
continue;
if (child.getData("comp").toString().equals("false"))
{
setErrorMessage(child.getData("name") +
" is empty or does not match required value\n"
+ child.getData("valType"));
return;
}
}
setPageComplete(true); setPageComplete(true);
setErrorMessage(null);
} }
public String getContent() public String getContent()
@ -69,11 +132,15 @@ public class WizardGeneratorPageKey extends WizardPage
StringBuilder result = new StringBuilder(); StringBuilder result = new StringBuilder();
for (Control child : container_.getChildren()) for (Control child : container_.getChildren())
{ {
if (!(child instanceof Text)) if (!(child instanceof Text || child instanceof Combo))
continue; continue;
String text = "";
if (child instanceof Text)
text = ((Text) child).getText();
else
text = ((Combo) child).getText();
result.append(StringUtils.multiples("\t", indent_) + result.append(StringUtils.multiples("\t", indent_) +
child.getData("name") + "=" + ((Text) child).getText() + "\n"); child.getData("name") + "=" + text + "\n");
} }
return result.toString(); return result.toString();
} }