Skip to content

Commit

Permalink
FORGE-1046: Introduced InputComponentFactory to allow dynamic creatio…
Browse files Browse the repository at this point in the history
…n of InputComponents without injection
  • Loading branch information
gastaldi committed Jul 26, 2013
1 parent de0171a commit b09f81d
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2013 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.jboss.forge.addon.ui;

import org.jboss.forge.addon.ui.input.UIInput;
import org.jboss.forge.addon.ui.input.UIInputMany;
import org.jboss.forge.addon.ui.input.UISelectMany;
import org.jboss.forge.addon.ui.input.UISelectOne;
import org.jboss.forge.furnace.services.Exported;

/**
* Allows creation of input components without injection
*
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*
*/
@Exported
public interface InputComponentFactory
{
/**
* Creates an {@link UIInput} component
*/
public abstract <T> UIInput<T> createInput(String name, Class<T> valueType);

/**
* Creates an {@link UIInputMany} component
*/
public abstract <T> UIInputMany<T> createInputMany(String name, Class<T> valueType);

/**
* Creates an {@link UISelectOne} component
*/
public abstract <T> UISelectOne<T> createSelectOne(String name, Class<T> valueType);

/**
* Creates an {@link UISelectMany} component
*/
public abstract <T> UISelectMany<T> createSelectMany(String name, Class<T> valueType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.jboss.forge.addon.convert.ConverterFactory;
import org.jboss.forge.addon.environment.Environment;
import org.jboss.forge.addon.ui.InputComponentFactory;
import org.jboss.forge.addon.ui.facets.HintsFacet;
import org.jboss.forge.addon.ui.hints.InputType;
import org.jboss.forge.addon.ui.impl.facets.HintsFacetImpl;
Expand All @@ -41,13 +42,13 @@
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*
*/
public class InputComponentFactory
public class InputComponentProducer implements InputComponentFactory
{
private Environment environment;
private AddonRegistry addonRegistry;

@Inject
public InputComponentFactory(Environment environment, AddonRegistry addonRegistry)
public InputComponentProducer(Environment environment, AddonRegistry addonRegistry)
{
this.environment = environment;
this.addonRegistry = addonRegistry;
Expand All @@ -67,7 +68,9 @@ public <T> UISelectOne<T> produceSelectOne(InjectionPoint injectionPoint)
Type[] typeArguments = parameterizedType.getActualTypeArguments();
Class<T> valueType = (Class<T>) typeArguments[0];
WithAttributes withAttributes = injectionPoint.getAnnotated().getAnnotation(WithAttributes.class);
return createSelectOne(name, valueType, withAttributes);
UISelectOne<T> input = createSelectOne(name, valueType);
preconfigureInput(input, withAttributes);
return input;
}
else
{
Expand All @@ -90,7 +93,9 @@ public <T> UISelectMany<T> produceSelectMany(InjectionPoint injectionPoint)
Type[] typeArguments = parameterizedType.getActualTypeArguments();
Class<T> valueType = (Class<T>) typeArguments[0];
WithAttributes withAttributes = injectionPoint.getAnnotated().getAnnotation(WithAttributes.class);
return createSelectMany(name, valueType, withAttributes);
UISelectMany<T> input = createSelectMany(name, valueType);
preconfigureInput(input, withAttributes);
return input;
}
else
{
Expand All @@ -113,7 +118,9 @@ public <T> UIInput<T> produceInput(InjectionPoint injectionPoint)
Type[] typeArguments = parameterizedType.getActualTypeArguments();
Class<T> valueType = (Class<T>) typeArguments[0];
WithAttributes withAttributes = injectionPoint.getAnnotated().getAnnotation(WithAttributes.class);
return createInput(name, valueType, withAttributes);
UIInput<T> input = createInput(name, valueType);
preconfigureInput(input, withAttributes);
return input;
}
else
{
Expand All @@ -136,7 +143,9 @@ public <T> UIInputMany<T> produceInputMany(InjectionPoint injectionPoint)
Type[] typeArguments = parameterizedType.getActualTypeArguments();
Class<T> valueType = (Class<T>) typeArguments[0];
WithAttributes withAttributes = injectionPoint.getAnnotated().getAnnotation(WithAttributes.class);
return createInputMany(name, valueType, withAttributes);
UIInputMany<T> input = createInputMany(name, valueType);
preconfigureInput(input, withAttributes);
return input;
}
else
{
Expand All @@ -145,40 +154,28 @@ public <T> UIInputMany<T> produceInputMany(InjectionPoint injectionPoint)
}
}

public <T> UIInput<T> createInput(String name, Class<T> valueType, WithAttributes withAttributes)
@Override
public <T> UIInput<T> createInput(String name, Class<T> valueType)
{
UIInputImpl<T> result = new UIInputImpl<T>(name, valueType);
HintsFacetImpl hintsFacet = new HintsFacetImpl(result, environment);
result.install(hintsFacet);
preconfigureInput(result, withAttributes);
return result;
return new UIInputImpl<T>(name, valueType);
}

public <T> UIInputMany<T> createInputMany(String name, Class<T> valueType, WithAttributes withAttributes)
@Override
public <T> UIInputMany<T> createInputMany(String name, Class<T> valueType)
{
UIInputManyImpl<T> result = new UIInputManyImpl<T>(name, valueType);
HintsFacetImpl hintsFacet = new HintsFacetImpl(result, environment);
result.install(hintsFacet);
preconfigureInput(result, withAttributes);
return result;
return new UIInputManyImpl<T>(name, valueType);
}

public <T> UISelectOne<T> createSelectOne(String name, Class<T> valueType, WithAttributes withAttributes)
@Override
public <T> UISelectOne<T> createSelectOne(String name, Class<T> valueType)
{
UISelectOne<T> result = new UISelectOneImpl<T>(name, valueType);
HintsFacetImpl hintsFacet = new HintsFacetImpl(result, environment);
result.install(hintsFacet);
preconfigureInput(result, withAttributes);
return result;
return new UISelectOneImpl<T>(name, valueType);
}

public <T> UISelectMany<T> createSelectMany(String name, Class<T> valueType, WithAttributes withAttributes)
@Override
public <T> UISelectMany<T> createSelectMany(String name, Class<T> valueType)
{
UISelectMany<T> result = new UISelectManyImpl<T>(name, valueType);
HintsFacetImpl hintsFacet = new HintsFacetImpl(result, environment);
result.install(hintsFacet);
preconfigureInput(result, withAttributes);
return result;
return new UISelectManyImpl<T>(name, valueType);
}

/**
Expand All @@ -187,6 +184,8 @@ public <T> UISelectMany<T> createSelectMany(String name, Class<T> valueType, Wit
@SuppressWarnings({ "unchecked", "rawtypes" })
private void preconfigureInput(InputComponent<?, ?> input, WithAttributes atts)
{
HintsFacetImpl hintsFacet = new HintsFacetImpl(input, environment);
input.install(hintsFacet);
if (atts != null)
{
input.setEnabled(atts.enabled());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2012 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.jboss.forge.addon.ui.impl;

import javax.inject.Inject;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.forge.addon.ui.InputComponentFactory;
import org.jboss.forge.addon.ui.input.UIInput;
import org.jboss.forge.addon.ui.input.UIInputMany;
import org.jboss.forge.addon.ui.input.UISelectMany;
import org.jboss.forge.addon.ui.input.UISelectOne;
import org.jboss.forge.arquillian.AddonDependency;
import org.jboss.forge.arquillian.Dependencies;
import org.jboss.forge.arquillian.archive.ForgeArchive;
import org.jboss.forge.furnace.repositories.AddonDependencyEntry;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(Arquillian.class)
public class InputComponentFactoryTest
{
@Deployment
@Dependencies({ @AddonDependency(name = "org.jboss.forge.addon:ui", version = "2.0.0-SNAPSHOT"),
@AddonDependency(name = "org.jboss.forge.furnace:container-cdi", version = "2.0.0-SNAPSHOT") })
public static ForgeArchive getDeployment()
{
ForgeArchive archive = ShrinkWrap
.create(ForgeArchive.class)
.addBeansXML()
.addAsAddonDependencies(
AddonDependencyEntry.create("org.jboss.forge.addon:ui", "2.0.0-SNAPSHOT"),
AddonDependencyEntry.create("org.jboss.forge.furnace:container-cdi", "2.0.0-SNAPSHOT"));

return archive;
}

@Inject
InputComponentFactory factory;

@Test
public void testCreateUIInput() throws Exception
{
UIInput<String> input = factory.createInput("foo", String.class);
Assert.assertNotNull(input);
}

@Test
public void testCreateUIInputMany() throws Exception
{
UIInputMany<String> input = factory.createInputMany("foo", String.class);
Assert.assertNotNull(input);
}

@Test
public void testCreateUISelectMany() throws Exception
{
UISelectMany<String> input = factory.createSelectMany("foo", String.class);
Assert.assertNotNull(input);
}

@Test
public void testCreateUISelectOne() throws Exception
{
UISelectOne<String> input = factory.createSelectOne("foo", String.class);
Assert.assertNotNull(input);
}
}

0 comments on commit b09f81d

Please sign in to comment.