Skip to content
This repository has been archived by the owner on Jan 10, 2019. It is now read-only.

Commit

Permalink
Merge pull request #26 from dmitrygusev/master
Browse files Browse the repository at this point in the history
Support for BeanDisplay and Alerts
  • Loading branch information
trsvax committed Aug 28, 2012
2 parents c8df7ca + 19eb2c6 commit 16b0d42
Show file tree
Hide file tree
Showing 10 changed files with 388 additions and 12 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ table to be formated by Bootstrap
as with Grid an fwtype parameter will format the form with Bootstrap.
based on [Forms](http://twitter.github.com/bootstrap/base-css.html#forms)

## Using BeanDisplay
Add fwtype parameter to the BeanDisplay. If the value is null the BeanDisplat looks as before.
Use values "dl" or "dl-horizontal" to apply mixin.
Any other CSS classes can be additionally specified here, i.e., bootstrap "well".

## Using Alerts
Add fwtype parameter to the Alerts. If the value is null the Alerts looks as before.
Value fwtype="alert" changes look and feel according to [bootstrap alerts](http://twitter.github.com/bootstrap/components.html#alerts).

Until [TAP5-1996](https://issues.apache.org/jira/browse/TAP5-1996) fixed Tap5 alerts will use this mapping:
Tap5 'info' -> 'alert-success'
Tap5 'warn' -> 'alert'
Tap5 'error' -> 'alert-error'


## Customizing Bootstrap:
There are two Tapestry configurations that allow you to customize Bootstrap. First the EnvironmentSetup.class defines
a set of mapped objects that set defaults for most of the components/mixins. For example the following sets the default
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.trsvax.bootstrap.environment;


public interface AlertsEnvironment extends FWEnvironment {

}
41 changes: 41 additions & 0 deletions src/main/java/com/trsvax/bootstrap/environment/AlertsValues.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.trsvax.bootstrap.environment;

import com.trsvax.bootstrap.FrameworkMixin;

public class AlertsValues implements AlertsEnvironment {
private boolean isInstrumented;
private String type;
private String prefix = "alert";

public AlertsValues(AlertsEnvironment values) {
if ( values != null ) {
this.type = values.getType(null);
}
}

public boolean isInstrumented() {
return isInstrumented;
}

public void withInstrumented(boolean value) {
isInstrumented = value;
}

public String getType(FrameworkMixin mixin) {
if ( mixin == null ) {
return type;
}
return mixin.getType() == null ? type : mixin.getType();
}

public AlertsValues withType(String type) {
this.type = type;
return this;
}

public String getPrefix() {
return prefix;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.trsvax.bootstrap.environment;


public interface BeanDisplayEnvironment extends FWEnvironment {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.trsvax.bootstrap.environment;

import com.trsvax.bootstrap.FrameworkMixin;

public class BeanDisplayValues implements BeanDisplayEnvironment {
private boolean isInstrumented;
private String type;
private String prefix = "dl";

public BeanDisplayValues(BeanDisplayEnvironment values) {
if ( values != null ) {
this.type = values.getType(null);
}
}

public boolean isInstrumented() {
return isInstrumented;
}

public void withInstrumented(boolean value) {
isInstrumented = value;
}

public String getType(FrameworkMixin mixin) {
if ( mixin == null ) {
return type;
}
return mixin.getType() == null ? type : mixin.getType();
}

public BeanDisplayValues withType(String type) {
this.type = type;
return this;
}

public String getPrefix() {
return prefix;
}

}
36 changes: 24 additions & 12 deletions src/main/java/com/trsvax/bootstrap/services/BootstrapModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
import com.trsvax.bootstrap.BootstrapProvider;
import com.trsvax.bootstrap.FrameworkProvider;
import com.trsvax.bootstrap.FrameworkVisitor;
import com.trsvax.bootstrap.environment.AlertsEnvironment;
import com.trsvax.bootstrap.environment.AlertsValues;
import com.trsvax.bootstrap.environment.BeanDisplayEnvironment;
import com.trsvax.bootstrap.environment.BeanDisplayValues;
import com.trsvax.bootstrap.environment.BreadcrumbEnvironment;
import com.trsvax.bootstrap.environment.BreadcrumbValues;
import com.trsvax.bootstrap.environment.ButtonEnvironment;
Expand All @@ -53,6 +57,8 @@
import com.trsvax.bootstrap.environment.NavValues;
import com.trsvax.bootstrap.environment.TableEnvironment;
import com.trsvax.bootstrap.environment.TableValues;
import com.trsvax.bootstrap.services.bootstrapprovider.AlertsProvider;
import com.trsvax.bootstrap.services.bootstrapprovider.BeanDisplayProvider;
import com.trsvax.bootstrap.services.bootstrapprovider.BootstrapFrameworkVisitor;
import com.trsvax.bootstrap.services.bootstrapprovider.BootstrapVisitor;
import com.trsvax.bootstrap.services.bootstrapprovider.BreadcrumbProvider;
Expand Down Expand Up @@ -84,7 +90,6 @@ public static void bind(ServiceBinder binder) {
binder.bind(ExcludeVisitor.class,ExcludeVisitorImpl.class);
binder.bind(EnvironmentSetup.class, EnvironmentSetupImpl.class);


binder.bind(BootstrapProvider.class,BreadcrumbProvider.class).withId("BootstrapBreadcrumb");
binder.bind(BootstrapProvider.class,ButtonProvider.class).withId("BootstrapButton");
binder.bind(BootstrapProvider.class,ButtonGroupProvider.class).withId("BootstrapButtonGroup");
Expand All @@ -95,7 +100,8 @@ public static void bind(ServiceBinder binder) {
binder.bind(BootstrapProvider.class,NavBarProvider.class).withId("BootstrapNavBar");
binder.bind(BootstrapProvider.class,PaginationProvider.class).withId("BootstrapPagination");
binder.bind(BootstrapProvider.class,TableProvider.class).withId("BootstrapTable");

binder.bind(BootstrapProvider.class, BeanDisplayProvider.class).withId("BootstrapBeanDisplay");
binder.bind(BootstrapProvider.class, AlertsProvider.class).withId("BootstrapAlerts");


binder.bind(FrameworkProvider.class,FrameworkProviderImpl.class).withId("FrameworkProvider");
Expand All @@ -107,12 +113,12 @@ public static void contributeComponentClassResolver(Configuration<LibraryMapping
}

public static void contributeJavaScriptStackSource(MappedConfiguration<String, JavaScriptStack> configuration,
@Symbol(JQuerySymbolConstants.SUPPRESS_PROTOTYPE) boolean suppressPrototype) {
@Symbol(JQuerySymbolConstants.SUPPRESS_PROTOTYPE) boolean suppressPrototype) {
if ( suppressPrototype ) {
configuration.overrideInstance(BootstrapFormStack.STACK_ID, BootstrapFormStack.class);
}
}
}

public static void contributeBindingSource(MappedConfiguration<String, BindingFactory> configuration,
@InjectService("SessionBindingFactory") BindingFactory sessionBindingFactory,
Expand All @@ -132,7 +138,10 @@ public static void contributeBootstrapProvider(OrderedConfiguration<BootstrapPro
@InjectService("BootstrapNav") BootstrapProvider navProvider,
@InjectService("BootstrapNavBar") BootstrapProvider navBarProvider,
@InjectService("BootstrapPagination") BootstrapProvider paginationProvider,
@InjectService("BootstrapTable") BootstrapProvider tableProvider) {
@InjectService("BootstrapTable") BootstrapProvider tableProvider,
@InjectService("BootstrapBeanDisplay") BootstrapProvider beanDisplayProvider,
@InjectService("BootstrapAlerts") BootstrapProvider alertsProvider)
{
configuration.add("Breadcrumb",breadcrumbProvider);
configuration.add("Button", buttonProvider);
configuration.add("ButtonGroup", buttonGroupProvider);
Expand All @@ -143,7 +152,8 @@ public static void contributeBootstrapProvider(OrderedConfiguration<BootstrapPro
configuration.add("NavBar", navBarProvider);
configuration.add("Pagination", paginationProvider);
configuration.add("Table", tableProvider);

configuration.add("BeanDisplay", beanDisplayProvider);
configuration.add("Alerts", alertsProvider);
}

@Marker(Primary.class)
Expand All @@ -161,7 +171,7 @@ public static void provideFrameworks(MappedConfiguration<String, FrameworkProvid
*/

@Contribute(ComponentClassTransformWorker2.class)
public static void provideWorkers(OrderedConfiguration<ComponentClassTransformWorker2> workers) {
public static void provideWorkers(OrderedConfiguration<ComponentClassTransformWorker2> workers) {
workers.addInstance("ConnectWorker", ConnectWorker.class);
workers.addInstance("ExcludeWorker", ExcludeWorker.class);
workers.addInstance("FrameworkMixinWorker", FrameworkMixinWorker.class);
Expand All @@ -176,6 +186,8 @@ public static void provideEnvironmentSetup(MappedConfiguration<Class, Object> co
configuration.add(NavEnvironment.class, new NavValues(null));
configuration.add(GridPagerEnvironment.class, new GridPagerValues(null));
configuration.add(TableEnvironment.class, new TableValues(null));
configuration.add(BeanDisplayEnvironment.class, new BeanDisplayValues(null));
configuration.add(AlertsEnvironment.class, new AlertsValues(null));
}

public void contributeMarkupRenderer(OrderedConfiguration<MarkupRendererFilter> configuration,
Expand Down Expand Up @@ -282,12 +294,12 @@ public static void provideDefaultBeanBlocks(Configuration<BeanBlockContribution>
}

private static void addEditBlock(Configuration<BeanBlockContribution> configuration, String dataType) {
addEditBlock(configuration, dataType, dataType);
}
addEditBlock(configuration, dataType, dataType);
}

private static void addEditBlock(Configuration<BeanBlockContribution> configuration, String dataType, String blockId) {
configuration.add(new EditBlockContribution(dataType, "tb/AppPropertyEditBlocks", blockId));
}
configuration.add(new EditBlockContribution(dataType, "tb/AppPropertyEditBlocks", blockId));
}

private static void addDisplayBlock(Configuration<BeanBlockContribution> configuration, String dataType)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.trsvax.bootstrap.services.bootstrapprovider;

import java.util.HashSet;
import java.util.Set;

import org.apache.tapestry5.Asset;
import org.apache.tapestry5.MarkupWriter;
import org.apache.tapestry5.annotations.Path;
import org.apache.tapestry5.corelib.components.Alerts;
import org.apache.tapestry5.dom.Element;
import org.apache.tapestry5.dom.Visitor;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.json.JSONArray;
import org.apache.tapestry5.services.Environment;
import org.apache.tapestry5.services.javascript.InitializationPriority;
import org.apache.tapestry5.services.javascript.JavaScriptSupport;
import org.slf4j.Logger;

import com.trsvax.bootstrap.AbstractFrameworkProvider;
import com.trsvax.bootstrap.BootstrapProvider;
import com.trsvax.bootstrap.FrameworkMixin;
import com.trsvax.bootstrap.environment.AlertsEnvironment;

public class AlertsProvider extends AbstractFrameworkProvider implements BootstrapProvider {

public class AlertsVisitor implements Visitor {

Element controls;
final String type;
final Set<Element> pop;
final FrameworkMixin mixin;

public AlertsVisitor(final FrameworkMixin mixin, final String type, final Set<Element> pop) {
this.type = type;
this.pop = pop;
this.mixin = mixin;
}

public void visit(Element element) {
if (!hasName("div", element)) {
return;
}

javaScriptSupport.importStylesheet(alertsCss);
javaScriptSupport.importJavaScriptLibrary(alertsJs);
javaScriptSupport.addInitializerCall(InitializationPriority.EARLY, "bootstrapAlerts", new JSONArray());
}

}

private final Class<?>[] handles = {Alerts.class};
private final Class<AlertsEnvironment> environmentClass = AlertsEnvironment.class;
private final Environment environment;
@SuppressWarnings("unused")
private final Logger logger;

@Inject @Path("classpath:com/trsvax/bootstrap/t5-bootstrap-alerts.js")
private Asset alertsJs;

@Inject @Path("classpath:com/trsvax/bootstrap/t5-bootstrap-alerts.css")
private Asset alertsCss;
private JavaScriptSupport javaScriptSupport;

public AlertsProvider(Environment environment, Logger logger, JavaScriptSupport javaScriptSupport) {
this.environment = environment;
this.logger = logger;
this.javaScriptSupport = javaScriptSupport;
}

public boolean instrument(FrameworkMixin mixin) {
return instrument(mixin, environment.peekRequired(environmentClass), handles);
}

boolean handle(FrameworkMixin mixin) {
if ( Alerts.class.getCanonicalName().equals(mixin.getComponentClassName())) {
return true;
}

return false;
}

public boolean cleanupRender(FrameworkMixin mixin, MarkupWriter writer) {

if ( ! handle(mixin)) {
return false;
}

final AlertsEnvironment alertsEnvironment = environment.peekRequired(environmentClass);
final String type = alertsEnvironment.getType(mixin);
if ( type == null ) {
return false;
}

final Set<Element> pop = new HashSet<Element>();
mixin.getRoot().visit( new AlertsVisitor(mixin,type,pop));
for ( Element element : pop ) {
element.pop();
}
return true;
}
}
Loading

0 comments on commit 16b0d42

Please sign in to comment.