Search in sources :

Example 1 with SimpleChoiceSet

use of pcgen.cdom.choiceset.SimpleChoiceSet in project pcgen by PCGen.

the class RegionLst method parseTokenWithSeparator.

@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, CDOMObject obj, String value) {
    if (obj instanceof Ungranted) {
        return new ParseResult.Fail("Cannot use " + getTokenName() + " on an Ungranted object type: " + obj.getClass().getSimpleName(), context);
    }
    if (obj instanceof NonInteractive) {
        return new ParseResult.Fail("Cannot use " + getTokenName() + " on an Non-Interactive object type: " + obj.getClass().getSimpleName(), context);
    }
    StringTokenizer tok = new StringTokenizer(value, Constants.PIPE);
    String item = tok.nextToken();
    Formula count = FormulaFactory.getFormulaFor(item);
    if (!count.isValid()) {
        return new ParseResult.Fail("Count in " + getTokenName() + " was not valid: " + count.toString(), context);
    }
    if (count.isStatic()) {
        if (!tok.hasMoreTokens()) {
            return new ParseResult.Fail(getTokenName() + " cannot have only a count: " + value, context);
        }
        item = tok.nextToken();
        if (count.resolveStatic().intValue() <= 0) {
            return new ParseResult.Fail("Count in " + getTokenName() + " must be > 0: " + value, context);
        }
    } else {
        count = FormulaFactory.ONE;
    }
    List<Region> regions = new ArrayList<>();
    while (true) {
        regions.add(Region.getConstant(item));
        if (!tok.hasMoreTokens()) {
            break;
        }
        item = tok.nextToken();
    }
    SimpleChoiceSet<Region> rcs = new SimpleChoiceSet<>(regions);
    ChoiceSet<Region> cs = new ChoiceSet<>(getTokenName(), rcs);
    cs.setTitle("Region Selection");
    TransitionChoice<Region> tc = new ConcreteTransitionChoice<>(cs, count);
    context.getObjectContext().put(obj, ObjectKey.REGION_CHOICE, tc);
    tc.setRequired(false);
    tc.setChoiceActor(this);
    return ParseResult.SUCCESS;
}
Also used : ConcreteTransitionChoice(pcgen.cdom.base.ConcreteTransitionChoice) SimpleChoiceSet(pcgen.cdom.choiceset.SimpleChoiceSet) ChoiceSet(pcgen.cdom.base.ChoiceSet) ArrayList(java.util.ArrayList) Ungranted(pcgen.cdom.base.Ungranted) Formula(pcgen.base.formula.Formula) StringTokenizer(java.util.StringTokenizer) SimpleChoiceSet(pcgen.cdom.choiceset.SimpleChoiceSet) NonInteractive(pcgen.cdom.base.NonInteractive) Region(pcgen.cdom.enumeration.Region)

Example 2 with SimpleChoiceSet

use of pcgen.cdom.choiceset.SimpleChoiceSet in project pcgen by PCGen.

the class StringToken method parseToken.

@Override
public ParseResult parseToken(LoadContext context, CDOMObject obj, String value) {
    if (value == null || value.isEmpty()) {
        return new ParseResult.Fail("CHOOSE:" + getTokenName() + " must have arguments", context);
    }
    if (value.indexOf(',') != -1) {
        return new ParseResult.Fail("CHOOSE:" + getTokenName() + " arguments may not contain , : " + value, context);
    }
    if (value.indexOf('[') != -1) {
        return new ParseResult.Fail("CHOOSE:" + getTokenName() + " arguments may not contain [] : " + value, context);
    }
    if (value.charAt(0) == '|') {
        return new ParseResult.Fail("CHOOSE:" + getTokenName() + " arguments may not start with | : " + value, context);
    }
    if (value.charAt(value.length() - 1) == '|') {
        return new ParseResult.Fail("CHOOSE:" + getTokenName() + " arguments may not end with | : " + value, context);
    }
    if (value.indexOf("||") != -1) {
        return new ParseResult.Fail("CHOOSE:" + getTokenName() + " arguments uses double separator || : " + value, context);
    }
    StringTokenizer tok = new StringTokenizer(value, Constants.PIPE);
    Set<String> set = new HashSet<>();
    while (tok.hasMoreTokens()) {
        String tokString = tok.nextToken();
        set.add(tokString);
    }
    SimpleChoiceSet<String> scs = new SimpleChoiceSet<>(set, Constants.PIPE);
    BasicChooseInformation<String> tc = new BasicChooseInformation<>(getTokenName(), scs);
    tc.setTitle("Choose an Item");
    tc.setChoiceActor(this);
    context.getObjectContext().put(obj, ObjectKey.CHOOSE_INFO, tc);
    return ParseResult.SUCCESS;
}
Also used : StringTokenizer(java.util.StringTokenizer) BasicChooseInformation(pcgen.cdom.base.BasicChooseInformation) SimpleChoiceSet(pcgen.cdom.choiceset.SimpleChoiceSet) HashSet(java.util.HashSet)

Aggregations

StringTokenizer (java.util.StringTokenizer)2 SimpleChoiceSet (pcgen.cdom.choiceset.SimpleChoiceSet)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Formula (pcgen.base.formula.Formula)1 BasicChooseInformation (pcgen.cdom.base.BasicChooseInformation)1 ChoiceSet (pcgen.cdom.base.ChoiceSet)1 ConcreteTransitionChoice (pcgen.cdom.base.ConcreteTransitionChoice)1 NonInteractive (pcgen.cdom.base.NonInteractive)1 Ungranted (pcgen.cdom.base.Ungranted)1 Region (pcgen.cdom.enumeration.Region)1