Skip to content

Commit

Permalink
FORGE-2428: Added UIBuilder.getInputComponentFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Aug 7, 2015
1 parent b5c0387 commit f56d3b0
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.jboss.forge.addon.ui.controller.CommandControllerFactory;
import org.jboss.forge.addon.ui.controller.SingleCommandController;
import org.jboss.forge.addon.ui.controller.WizardCommandController;
import org.jboss.forge.addon.ui.input.InputComponentFactory;
import org.jboss.forge.furnace.Furnace;
import org.jboss.forge.furnace.addons.AddonRegistry;
import org.jboss.forge.furnace.exception.ContainerException;
Expand Down Expand Up @@ -65,9 +66,10 @@ public ForgeCommandRegistry(Furnace furnace, ShellImpl shell, AddonRegistry addo
this.commandFactory = addonRegistry.getServices(CommandFactory.class).get();
this.commandControllerFactory = addonRegistry.getServices(CommandControllerFactory.class).get();
ConverterFactory converterFactory = addonRegistry.getServices(ConverterFactory.class).get();
InputComponentFactory inputComponentFactory = addonRegistry.getServices(InputComponentFactory.class).get();

// Use Aesh commands
Man manCommand = new Man(new ForgeManProvider(shell, commandFactory, converterFactory));
Man manCommand = new Man(new ForgeManProvider(shell, commandFactory, converterFactory, inputComponentFactory));
this.aeshCommandRegistry = new AeshCommandRegistryBuilder()
.command(Grep.class)
.command(Less.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.jboss.forge.addon.ui.context.UIBuilder;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.input.InputComponent;
import org.jboss.forge.addon.ui.input.InputComponentFactory;
import org.jboss.forge.addon.ui.input.ManyValued;
import org.jboss.forge.addon.ui.input.SelectComponent;
import org.jboss.forge.addon.ui.util.InputComponents;
Expand All @@ -49,6 +50,7 @@ public class ForgeManProvider implements ManProvider
private final ShellImpl shell;
private final CommandFactory manager;
private final ConverterFactory converterFactory;
private final InputComponentFactory inputComponentFactory;
private final Comparator<? super InputComponent<?, ?>> SHORTNAME_COMPARATOR = new Comparator<InputComponent<?, ?>>()
{
@Override
Expand All @@ -67,11 +69,13 @@ public int compare(InputComponent<?, ?> left, InputComponent<?, ?> right)
}
};

public ForgeManProvider(ShellImpl shell, CommandFactory manager, ConverterFactory converterFactory)
public ForgeManProvider(ShellImpl shell, CommandFactory manager, ConverterFactory converterFactory,
InputComponentFactory inputComponentFactory)
{
this.shell = shell;
this.manager = manager;
this.converterFactory = converterFactory;
this.inputComponentFactory = inputComponentFactory;
}

@Override
Expand Down Expand Up @@ -130,6 +134,12 @@ public UIBuilder add(InputComponent<?, ?> input)
inputs.add(input);
return this;
}

@Override
public InputComponentFactory getInputComponentFactory()
{
return inputComponentFactory;
}
});

result = result.replaceAll("%name%", manager.getCommandName(context, cmd));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import org.jboss.forge.addon.ui.command.UICommand;
import org.jboss.forge.addon.ui.input.InputComponent;
import org.jboss.forge.addon.ui.input.InputComponentFactory;

/**
* Allows {@link UICommand} objects to specify the order of the displayed fields
Expand All @@ -24,7 +25,14 @@ public interface UIBuilder extends UIContextProvider
* Implementations should call this method in order to make the {@link InputComponent} available in the UI
*
* @param input
* @return
* @return this same instance, to allow method chaining
*/
UIBuilder add(InputComponent<?, ?> input);

/**
* Returns the {@link InputComponentFactory} service to allow creation of inputs ad-hoc.
*
* @return {@link InputComponentFactory} service
*/
InputComponentFactory getInputComponentFactory();
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.jboss.forge.addon.ui.context.UIBuilder;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.input.InputComponent;
import org.jboss.forge.addon.ui.input.InputComponentFactory;

/**
* Implementation of the {@link UIBuilder} interface
Expand All @@ -21,13 +22,14 @@
*/
public class UIBuilderImpl implements UIBuilder
{

private final UIContext context;
private final InputComponentFactory inputComponentFactory;
private Map<String, InputComponent<?, ?>> inputs = new LinkedHashMap<>();

public UIBuilderImpl(UIContext context)
public UIBuilderImpl(UIContext context, InputComponentFactory inputComponentFactory)
{
this.context = context;
this.inputComponentFactory = inputComponentFactory;
}

@Override
Expand All @@ -47,4 +49,10 @@ public UIBuilder add(InputComponent<?, ?> input)
{
return inputs;
}

@Override
public InputComponentFactory getInputComponentFactory()
{
return inputComponentFactory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.jboss.forge.addon.ui.impl.context.UIExecutionContextImpl;
import org.jboss.forge.addon.ui.impl.context.UIValidationContextImpl;
import org.jboss.forge.addon.ui.input.InputComponent;
import org.jboss.forge.addon.ui.input.InputComponentFactory;
import org.jboss.forge.addon.ui.input.UIPrompt;
import org.jboss.forge.addon.ui.metadata.UICommandMetadata;
import org.jboss.forge.addon.ui.output.UIMessage;
Expand All @@ -40,20 +41,22 @@
class SingleCommandControllerImpl extends AbstractCommandController implements SingleCommandController
{
private UIBuilderImpl uiBuilder;
private ConverterFactory converterFactory;
private final ConverterFactory converterFactory;
private final InputComponentFactory inputComponentFactory;

SingleCommandControllerImpl(AddonRegistry addonRegistry, UIRuntime runtime, UICommand command, UIContext context)
{
super(addonRegistry, runtime, command, context);
this.converterFactory = addonRegistry.getServices(ConverterFactory.class).get();
this.inputComponentFactory = addonRegistry.getServices(InputComponentFactory.class).get();
}

@Override
public void initialize() throws Exception
{
if (!isInitialized())
{
uiBuilder = new UIBuilderImpl(context);
uiBuilder = new UIBuilderImpl(context, inputComponentFactory);
initialCommand.initializeUI(uiBuilder);
}
}
Expand Down Expand Up @@ -123,7 +126,6 @@ public boolean hasInput(String inputName)
return getInputs().containsKey(inputName);
}

@SuppressWarnings("unchecked")
@Override
public CommandController setValueFor(String inputName, Object value)
{
Expand All @@ -132,7 +134,7 @@ public CommandController setValueFor(String inputName, Object value)
{
throw new IllegalArgumentException("Input named '" + inputName + "' does not exist");
}
InputComponents.setValueFor(getConverterFactory(), (InputComponent<?, Object>) input, value);
InputComponents.setValueFor(getConverterFactory(), input, value);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ public void testWizardCanMoveToNextStep() throws Exception
}
}

@SuppressWarnings("resource")
@Test
public void testPreStepsCommand() throws Exception
{
Expand Down

1 comment on commit f56d3b0

@lincolnthree
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GOOD IDEA!!!

Please sign in to comment.