Created
March 30, 2013 14:32
-
-
Save dmitrygusev/5276919 to your computer and use it in GitHub Desktop.
Tapestry5 Skicky Forms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
public class AppModule | |
{ | |
public static void bind(ServiceBinder binder) throws ClassNotFoundException | |
{ | |
binder.bind(StickyFormSource.class, StickyFormSourceImpl.class); | |
} | |
@Contribute(StickyFormSource.class) | |
public void contributeStickyForms(Configuration<StickyFormConfig> configuration) | |
{ | |
configuration.add(new StickyFormConfig(Register.class, "registrationform.regform")); | |
configuration.add(new StickyFormConfig(SignIn.class, "signinform.signinform")); | |
} | |
public void contributeMarkupRenderer(OrderedConfiguration<MarkupRendererFilter> configuration, | |
final Environment environment, final StickyFormSource stickyFormSource) | |
{ | |
// Copy-pasted from TapestryModule | |
MarkupRendererFilter clientBehaviorSupport = new MarkupRendererFilter() | |
{ | |
public void renderMarkup(MarkupWriter writer, MarkupRenderer renderer) | |
{ | |
JavaScriptSupport javascriptSupport = environment.peekRequired(JavaScriptSupport.class); | |
ClientBehaviorSupportImpl clientBehaviorSupport = | |
new ClientBehaviorSupportImpl(javascriptSupport, environment) | |
{ | |
@Override | |
public void linkZone(String linkId, String elementId, Link eventLink) | |
{ | |
StickyFormConfig config = stickyFormSource.getConfiguration(linkId); | |
if (config != null) | |
{ | |
eventLink = stickyFormSource.getFormActionLink(config); | |
} | |
super.linkZone(linkId, elementId, eventLink); | |
} | |
}; | |
environment.push(ClientBehaviorSupport.class, clientBehaviorSupport); | |
renderer.renderMarkup(writer); | |
environment.pop(ClientBehaviorSupport.class); | |
clientBehaviorSupport.commit(); | |
} | |
}; | |
configuration.override("ClientBehaviorSupport", clientBehaviorSupport); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class StickyFormConfig | |
{ | |
private final Class<?> containingPage; | |
private final String nestedId; | |
public StickyFormConfig(Class<?> containingPage, String nestedId) | |
{ | |
this.nestedId = nestedId; | |
this.containingPage = containingPage; | |
} | |
public String getFormCompleteId() | |
{ | |
return containingPage.getSimpleName() + ":" + nestedId; | |
} | |
public Class<?> getContainingPage() | |
{ | |
return containingPage; | |
} | |
public String getNestedId() | |
{ | |
return nestedId; | |
} | |
public String getFormId() | |
{ | |
return nestedId.substring(nestedId.lastIndexOf('.') + 1); | |
} | |
public Object[] getContext() | |
{ | |
return new Object[0]; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.tapestry5.ComponentResources; | |
import org.apache.tapestry5.MarkupWriter; | |
import org.apache.tapestry5.annotations.MixinAfter; | |
import org.apache.tapestry5.dom.Element; | |
import org.apache.tapestry5.ioc.annotations.Inject; | |
@MixinAfter | |
public class StickyFormMixin | |
{ | |
@Inject | |
private ComponentResources resources; | |
@Inject | |
private StickyFormSource stickyFormSource; | |
void beginRender(MarkupWriter writer) | |
{ | |
String componentId = resources.getContainer().getComponentResources().getCompleteId(); | |
StickyFormConfig config = stickyFormSource.getConfiguration(componentId); | |
if (config == null) | |
{ | |
throw new IllegalStateException("Can't apply mixin to " + componentId + ". " | |
+ getClass().getSimpleName() + " requires corresponding configuration to be provided for " | |
+ componentId + " via " + StickyFormSource.class.getSimpleName()); | |
} | |
Element element = writer.getElement(); | |
if (!"form".equals(element.getName())) | |
{ | |
throw new IllegalStateException(getClass().getSimpleName() + " can only be appied to form components"); | |
} | |
element.forceAttributes("action", stickyFormSource.getFormActionLink(config).toURI()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.tapestry5.Link; | |
public interface StickyFormSource | |
{ | |
StickyFormConfig getConfiguration(String formId); | |
Link getFormActionLink(StickyFormConfig config); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Collection; | |
import java.util.HashMap; | |
import java.util.Map; | |
import org.apache.tapestry5.EventConstants; | |
import org.apache.tapestry5.Link; | |
import org.apache.tapestry5.internal.services.LinkSource; | |
import org.apache.tapestry5.internal.services.PageRenderQueue; | |
import org.apache.tapestry5.internal.services.PageSource; | |
import org.apache.tapestry5.internal.structure.Page; | |
public class StickyFormSourceImpl implements StickyFormSource | |
{ | |
private final Map<String, StickyFormConfig> configuration; | |
private final LinkSource linkSource; | |
private final PageSource pageSource; | |
private final PageRenderQueue pageRenderQueue; | |
public StickyFormSourceImpl(Collection<StickyFormConfig> configuration, | |
LinkSource linkSource, PageSource pageSource, | |
PageRenderQueue pageRenderQueue) | |
{ | |
this.configuration = new HashMap<String, StickyFormConfig>(); | |
for (StickyFormConfig config : configuration) | |
{ | |
this.configuration.put(config.getFormId().toLowerCase(), config); | |
this.configuration.put(config.getFormCompleteId().toLowerCase(), config); | |
} | |
this.linkSource = linkSource; | |
this.pageRenderQueue = pageRenderQueue; | |
this.pageSource = pageSource; | |
} | |
@Override | |
public StickyFormConfig getConfiguration(String formId) | |
{ | |
return configuration.get(formId.toLowerCase()); | |
} | |
@Override | |
public Link getFormActionLink(StickyFormConfig config) | |
{ | |
Page renderingPage = pageRenderQueue.getRenderingPage(); | |
try | |
{ | |
Page targetPage = pageSource.getPage(config.getContainingPage().getSimpleName().toLowerCase()); | |
pageRenderQueue.setRenderingPage(targetPage); | |
return linkSource.createComponentEventLink( | |
targetPage, config.getNestedId(), | |
EventConstants.ACTION, true, config.getContext()); | |
} | |
finally | |
{ | |
pageRenderQueue.setRenderingPage(renderingPage); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment