Skip to content

Commit

Permalink
FORGE-1150: Shell now supports multi-level UIWizards
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Sep 2, 2013
1 parent 1939c2b commit 6801a2c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
Expand Up @@ -20,7 +20,6 @@
import org.jboss.forge.addon.ui.UICommand;
import org.jboss.forge.addon.ui.util.Commands;
import org.jboss.forge.addon.ui.wizard.UIWizard;
import org.jboss.forge.addon.ui.wizard.UIWizardStep;
import org.jboss.forge.furnace.addons.AddonRegistry;
import org.jboss.forge.furnace.services.Imported;

Expand All @@ -43,7 +42,7 @@ public CommandManager(final AddonRegistry addonRegistry)
this.addonRegistry = addonRegistry;
}

public UIWizardStep lookup(Class<? extends UIWizardStep> type)
public UICommand lookup(Class<? extends UICommand> type)
{
return addonRegistry.getServices(type).get();
}
Expand Down
Expand Up @@ -23,11 +23,11 @@
import org.jboss.forge.addon.shell.CommandManager;
import org.jboss.forge.addon.shell.ui.ShellContext;
import org.jboss.forge.addon.shell.ui.ShellValidationContext;
import org.jboss.forge.addon.ui.UICommand;
import org.jboss.forge.addon.ui.input.InputComponent;
import org.jboss.forge.addon.ui.result.NavigationResult;
import org.jboss.forge.addon.ui.result.Result;
import org.jboss.forge.addon.ui.wizard.UIWizard;
import org.jboss.forge.addon.ui.wizard.UIWizardStep;
import org.jboss.forge.furnace.util.Strings;

/**
Expand All @@ -38,6 +38,7 @@
public class ShellWizard extends AbstractShellInteraction
{
private LinkedList<ShellWizardStep> steps = new LinkedList<ShellWizardStep>();
private LinkedList<Class<? extends UICommand>> subflows = new LinkedList<Class<? extends UICommand>>();
private CommandManager commandManager;
private CommandLineParser fullCommandLineParser;

Expand Down Expand Up @@ -139,8 +140,7 @@ public ParsedCompleteObject parseCompleteObject(String line) throws CommandLineP
return new CommandLineCompletionParser(fullCommandLineParser).findCompleteObject(line);
}

@SuppressWarnings("unchecked")
private CommandLineParser populate(UIWizard root, UIWizard current, String line, boolean lenient) throws Exception
private CommandLineParser populate(UICommand root, UICommand current, String line, boolean lenient) throws Exception
{
addWizardStep(current);
Map<String, InputComponent<?, Object>> inputs = getInputs();
Expand All @@ -150,21 +150,42 @@ private CommandLineParser populate(UIWizard root, UIWizard current, String line,
List<String> errors = validate();
if (errors.isEmpty())
{
NavigationResult next = current.next(getContext());
// Proceed to next input
if (next != null && next.getNext() != null)
if (current instanceof UIWizard)
{
// It should always be a UIWizardStep
Class<? extends UIWizardStep>[] nextWizardStep = (Class<? extends UIWizardStep>[]) next.getNext();
// TODO: Change this
UIWizardStep step = commandManager.lookup(nextWizardStep[0]);
parser = populate(root, step, line, lenient);
NavigationResult next = ((UIWizard) current).next(getContext());
final Class<? extends UICommand> successor;
// Proceed to next input
if (next != null && next.getNext() != null)
{
Class<? extends UICommand>[] successors = next.getNext();
successor = successors[0];
for (int i = 1; i < successors.length; i++)
{
if (successors[i] != null)
{
subflows.push(successors[i]);
}
}
}
else if (!subflows.isEmpty())
{
successor = subflows.pop();
}
else
{
successor = null;
}
if (successor != null)
{
UICommand step = commandManager.lookup(successor);
parser = populate(root, step, line, lenient);
}
}
}
return parser;
}

private ShellWizardStep addWizardStep(final UIWizard step)
private ShellWizardStep addWizardStep(final UICommand step)
{
ShellWizardStep cmdStep = new ShellWizardStep(step, buildInputs(step));
steps.add(cmdStep);
Expand Down Expand Up @@ -199,10 +220,10 @@ public List<String> validate()

private static class ShellWizardStep
{
public final UIWizard command;
public final UICommand command;
public final Map<String, InputComponent<?, Object>> inputs;

public ShellWizardStep(UIWizard command, Map<String, InputComponent<?, Object>> inputs)
public ShellWizardStep(UICommand command, Map<String, InputComponent<?, Object>> inputs)
{
this.command = command;
this.inputs = inputs;
Expand Down

0 comments on commit 6801a2c

Please sign in to comment.