Author: abelevich
Date: 2009-11-20 12:14:19 -0500 (Fri, 20 Nov 2009)
New Revision: 15948
Added:
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/Expandable.java
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/event/
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/event/ToggleEvent.java
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/event/ToggleListener.java
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/taglib/
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/taglib/SubTableHandler.java
Modified:
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/UISubTable.java
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/UIToggleControl.java
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/html/HtmlSubTable.java
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/renderkit/DataTableRenderer.java
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/renderkit/ToggleControlRendererBase.java
root/ui-sandbox/trunk/components/tables/ui/src/main/resources/META-INF/resources/toggler.js
root/ui-sandbox/trunk/components/tables/ui/src/main/resources/META-INF/tables.taglib.xml
Log:
add toggleEvent support, toggleListener attribute for th subTable
Added:
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/Expandable.java
===================================================================
---
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/Expandable.java
(rev 0)
+++
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/Expandable.java 2009-11-20
17:14:19 UTC (rev 15948)
@@ -0,0 +1,21 @@
+package org.richfaces.component;
+
+import javax.el.MethodExpression;
+
+import org.richfaces.event.ToggleListener;
+
+public interface Expandable {
+
+ public boolean isExpanded();
+
+ public void addToggleListener(ToggleListener listener);
+
+ public void removeToggleListener(ToggleListener listener);
+
+ public ToggleListener [] getToggleListeners();
+
+ public void setToggleListener(MethodExpression toggleExpression);
+
+ public MethodExpression getToggleListener();
+
+}
Modified:
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/UISubTable.java
===================================================================
---
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/UISubTable.java 2009-11-20
17:02:34 UTC (rev 15947)
+++
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/UISubTable.java 2009-11-20
17:14:19 UTC (rev 15948)
@@ -23,17 +23,23 @@
import java.util.Iterator;
+import javax.el.MethodExpression;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
+import javax.faces.event.AbortProcessingException;
+import javax.faces.event.FacesEvent;
+import javax.faces.event.PhaseId;
import org.ajax4jsf.model.DataVisitor;
+import org.richfaces.event.ToggleEvent;
+import org.richfaces.event.ToggleListener;
/**
* JSF component class
*
*/
-public abstract class UISubTable extends UIDataTableBase implements Row, Column {
+public abstract class UISubTable extends UIDataTableBase implements Row, Column,
Expandable{
public static final String COMPONENT_TYPE = "org.richfaces.SubTable";
@@ -47,7 +53,7 @@
enum PropertyKeys {
- switchType, expanded
+ switchType, expanded, toggleExpression
}
@Override
@@ -69,8 +75,7 @@
}
public void setSortExpression(String sortExpression) {
- // Do nothing - subtable is not sortable element;
- //TODO nick - throw exception
+ throw new IllegalArgumentException("subtable is not sortable element");
}
@Override
@@ -91,11 +96,53 @@
getStateHelper().put(PropertyKeys.expanded, expanded);
}
- public boolean getExpanded() {
+ public boolean isExpanded() {
return (Boolean) getStateHelper().eval(PropertyKeys.expanded, Boolean.TRUE);
}
+ public void setToggleListener(MethodExpression toggleExpression) {
+ getStateHelper().put(PropertyKeys.toggleExpression, toggleExpression);
+ }
+
+ public MethodExpression getToggleListener() {
+ return (MethodExpression) getStateHelper().eval(PropertyKeys.toggleExpression);
+ }
+
@Override
+ public void queueEvent(FacesEvent event) {
+ if(event instanceof ToggleEvent) {
+ event.setPhaseId(PhaseId.UPDATE_MODEL_VALUES);
+ }
+ super.queueEvent(event);
+ }
+
+ @Override
+ public void broadcast(FacesEvent event) throws AbortProcessingException {
+ super.broadcast(event);
+
+ if(event instanceof ToggleEvent) {
+ ToggleEvent toggleEvent = (ToggleEvent)event;
+
+ MethodExpression expression = getToggleListener();
+ if(expression != null) {
+ expression.invoke(getFacesContext().getELContext(), new Object[]{toggleEvent});
+ }
+ }
+ }
+
+ public void addToggleListener(ToggleListener listener) {
+ addFacesListener(listener);
+ }
+
+ public void removeToggleListener(ToggleListener listener) {
+ removeFacesListener(listener);
+ }
+
+ public ToggleListener [] getToggleListeners() {
+ return (ToggleListener[]) getFacesListeners(ToggleListener.class);
+ }
+
+ @Override
public boolean getRendersChildren() {
//TODO nick - why "false"?
return false;
@@ -157,5 +204,5 @@
public String getColumnFooterSkinClass() {
return "rich-subtable-subfooter-cell";
}
-
+
}
Modified:
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/UIToggleControl.java
===================================================================
---
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/UIToggleControl.java 2009-11-20
17:02:34 UTC (rev 15947)
+++
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/UIToggleControl.java 2009-11-20
17:14:19 UTC (rev 15948)
@@ -1,8 +1,8 @@
package org.richfaces.component;
-import javax.faces.component.UICommand;
+import javax.faces.component.UIComponentBase;
-public abstract class UIToggleControl extends UICommand{
+public abstract class UIToggleControl extends UIComponentBase {
public static final String COMPONENT_TYPE = "org.richfaces.ToggleControl";
@@ -50,5 +50,4 @@
public void setEvent(String event) {
getStateHelper().put(PropertyKeys.event, event);
}
-
}
Modified:
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/html/HtmlSubTable.java
===================================================================
---
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/html/HtmlSubTable.java 2009-11-20
17:02:34 UTC (rev 15947)
+++
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/html/HtmlSubTable.java 2009-11-20
17:14:19 UTC (rev 15948)
@@ -3,6 +3,7 @@
import javax.el.MethodExpression;
import org.richfaces.component.UISubTable;
+import org.richfaces.event.ToggleListener;
import org.richfaces.model.Ordering;
public class HtmlSubTable extends UISubTable{
Added:
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/event/ToggleEvent.java
===================================================================
---
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/event/ToggleEvent.java
(rev 0)
+++
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/event/ToggleEvent.java 2009-11-20
17:14:19 UTC (rev 15948)
@@ -0,0 +1,32 @@
+package org.richfaces.event;
+
+import javax.faces.component.UIComponent;
+import javax.faces.event.FacesEvent;
+import javax.faces.event.FacesListener;
+
+public class ToggleEvent extends FacesEvent {
+
+ private static final long serialVersionUID = 2916560585918250885L;
+
+ private String state;
+
+
+ public ToggleEvent(UIComponent target, String state) {
+ super(target);
+ this.state = state;
+ }
+
+ public String getState() {
+ return this.state;
+ }
+
+ @Override
+ public boolean isAppropriateListener(FacesListener listener) {
+ return (listener instanceof ToggleListener);
+ }
+
+ @Override
+ public void processListener(FacesListener listener) {
+ ((ToggleListener)listener).processToggle(this);
+ }
+}
Added:
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/event/ToggleListener.java
===================================================================
---
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/event/ToggleListener.java
(rev 0)
+++
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/event/ToggleListener.java 2009-11-20
17:14:19 UTC (rev 15948)
@@ -0,0 +1,9 @@
+package org.richfaces.event;
+
+import javax.faces.event.FacesListener;
+
+public interface ToggleListener extends FacesListener {
+
+ public void processToggle(ToggleEvent event);
+
+}
Modified:
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/renderkit/DataTableRenderer.java
===================================================================
---
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/renderkit/DataTableRenderer.java 2009-11-20
17:02:34 UTC (rev 15947)
+++
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/renderkit/DataTableRenderer.java 2009-11-20
17:14:19 UTC (rev 15948)
@@ -17,7 +17,7 @@
import org.richfaces.component.UISubTable;
@ResourceDependencies( {
- @ResourceDependency(library = "javax.faces", name = "jsf.js"),
+ @ResourceDependency(library = "javax.faces", name =
"jsf-uncompressed.js"),
@ResourceDependency(name = "jquery.js"),
@ResourceDependency(name = "richfaces.js"),
@ResourceDependency(name = "datatable.js"),
@@ -25,6 +25,10 @@
})
public class DataTableRenderer extends AbstractTableRenderer implements
ElementEncodeListener{
+ @Override
+ protected void doDecode(FacesContext context, UIComponent component) {
+ System.out.println("DataTableRenderer.doDecode()");
+ }
protected void encodeTableStructure(ResponseWriter writer, FacesContext context,
UIDataTableBase dataTable) throws IOException {
@@ -133,9 +137,10 @@
if(child instanceof UISubTable) {
UISubTable subTable = (UISubTable)child;
encodeFooter(writer, context, subTable);
-
+// encodeHiddenInput(context, writer, subTable);
+
encodeTableBodyEnd(writer, context, subTable);
-
+
bodyStart = false;
if(iterator.hasNext()) {
encodeTableBodyStart(writer, context, (UIDataTableBase)row);
@@ -182,6 +187,17 @@
encodeTableBodyEnd(writer, context, (UIDataTableBase) row);
}
}
+/*
+ public void encodeHiddenInput(FacesContext context, ResponseWriter writer,
UIDataTableBase dataTable) throws IOException {
+
+ String hiddenId = dataTable.getClientId(context) + ":" + "hidden";
+
+ writer.startElement(HTML.INPUT_ELEM, dataTable);
+ writer.writeAttribute(HTML.ID_ATTRIBUTE, hiddenId, null);
+ writer.writeAttribute(HTML.TYPE_ATTR,"hidden", null);
+ writer.endElement(HTML.INPUT_ELEM);
+
+ } */
@Override
protected void doEncodeBegin(ResponseWriter writer, FacesContext context, UIComponent
component) throws IOException {
Modified:
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/renderkit/ToggleControlRendererBase.java
===================================================================
---
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/renderkit/ToggleControlRendererBase.java 2009-11-20
17:02:34 UTC (rev 15947)
+++
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/renderkit/ToggleControlRendererBase.java 2009-11-20
17:14:19 UTC (rev 15948)
@@ -2,6 +2,7 @@
import java.io.IOException;
import java.text.MessageFormat;
+import java.util.Map;
import javax.faces.application.ResourceDependencies;
import javax.faces.application.ResourceDependency;
@@ -11,8 +12,10 @@
import org.ajax4jsf.renderkit.RendererBase;
import org.ajax4jsf.renderkit.RendererUtils.HTML;
+import org.richfaces.component.Expandable;
import org.richfaces.component.UISubTable;
import org.richfaces.component.UIToggleControl;
+import org.richfaces.event.ToggleEvent;
@ResourceDependencies({
@ResourceDependency(name = "toggler.js")
@@ -33,30 +36,66 @@
private static final String COLLAPSE_STATE = "collapse";
+ private static final String HIDDEN = ":hidden";
+
+ @Override
+ protected void doDecode(FacesContext context, UIComponent component) {
+ if(!(component instanceof UIToggleControl)) {
+ return;
+ }
+
+ UIToggleControl toggle = (UIToggleControl)component;
+
+ Map<String, String> params =
context.getExternalContext().getRequestParameterMap();
+
+ String hiddenId = toggle.getClientId() + HIDDEN;
+ String state = params.get(hiddenId);
+
+ if(state != null && state.trim().length()> 0) {
+ UIComponent processed = findComponent(context, toggle);
+ if(processed instanceof Expandable) {
+
+ String prevState = getState(((Expandable)processed).isExpanded());
+ if(!state.equals(prevState)) {
+ new ToggleEvent(processed, state).queue();
+ }
+
+ }
+
+ }
+ }
+
protected void encodeControl(FacesContext context, UIComponent component) throws
IOException{
- UIToggleControl toggleControl = (UIToggleControl) component;
- UISubTable subTable = findComponent(context, toggleControl);
- if(subTable != null) {
- ResponseWriter writer = context.getResponseWriter();
-
- String toggleId = toggleControl.getClientId(context);
- String subTableId = subTable.getClientId(context);
- boolean expanded = subTable.getExpanded();
- String initialState = expanded ? EXPAND_STATE : COLLAPSE_STATE;
+
+ if(component instanceof UIToggleControl) {
+ UIToggleControl toggleControl = (UIToggleControl)component;
- String registerScript = MessageFormat.format(MANAGER_SCRIPT, toggleId, subTableId,
initialState);
- writer.write(registerScript);
-
- String switchType = subTable.getSwichType();
- encodeControl(context, writer, toggleControl, switchType, expanded, false);
- encodeControl(context, writer, toggleControl, switchType, !expanded, true);
+ UISubTable subTable = findComponent(context, toggleControl);
+ if(subTable != null) {
+ ResponseWriter writer = context.getResponseWriter();
+
+ String toggleId = toggleControl.getClientId(context);
+ String subTableId = subTable.getClientId(context);
+ boolean expanded = subTable.isExpanded();
+ String initialState = getState(expanded);
+
+ String registerScript = MessageFormat.format(MANAGER_SCRIPT, toggleId, subTableId,
initialState);
+ writer.write(registerScript);
+
+ String switchType = subTable.getSwichType();
+ encodeControl(context, writer, toggleControl, switchType, expanded, false);
+ encodeControl(context, writer, toggleControl, switchType, !expanded, true);
+ encodeHiddenInput(context, writer, toggleControl);
+
+ }
}
}
protected void encodeControl(FacesContext context, ResponseWriter writer,
UIToggleControl control, String switchType, boolean expanded, boolean visible) throws
IOException {
- String state = expanded ? EXPAND_STATE: COLLAPSE_STATE;
+ String state = getState(expanded);
+
String image = expanded ? control.getExpandControl() : control.getCollapseControl();
String script = getScript(control, switchType, state);
@@ -88,6 +127,18 @@
writer.endElement(HTML.SPAN_ELEM);
}
+ public void encodeHiddenInput(FacesContext context, ResponseWriter writer, UIComponent
component) throws IOException {
+
+ String hiddenId = component.getClientId(context) +HIDDEN;
+
+ writer.startElement(HTML.INPUT_ELEM, component);
+ writer.writeAttribute(HTML.ID_ATTRIBUTE, hiddenId, null);
+ writer.writeAttribute(HTML.NAME_ATTRIBUTE, hiddenId, null);
+ writer.writeAttribute(HTML.TYPE_ATTR,"hidden", null);
+ writer.endElement(HTML.INPUT_ELEM);
+
+ }
+
public String getStyleClass(FacesContext context, UIToggleControl control) {
return null;
}
@@ -127,4 +178,8 @@
return script;
}
+ protected String getState(boolean expand) {
+ return expand ? EXPAND_STATE: COLLAPSE_STATE;
+ }
+
}
Added:
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/taglib/SubTableHandler.java
===================================================================
---
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/taglib/SubTableHandler.java
(rev 0)
+++
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/taglib/SubTableHandler.java 2009-11-20
17:14:19 UTC (rev 15948)
@@ -0,0 +1,58 @@
+package org.richfaces.taglib;
+
+import javax.el.MethodExpression;
+import javax.faces.view.facelets.ComponentConfig;
+import javax.faces.view.facelets.ComponentHandler;
+import javax.faces.view.facelets.FaceletContext;
+import javax.faces.view.facelets.MetaRule;
+import javax.faces.view.facelets.MetaRuleset;
+import javax.faces.view.facelets.Metadata;
+import javax.faces.view.facelets.MetadataTarget;
+import javax.faces.view.facelets.TagAttribute;
+
+import org.richfaces.component.UISubTable;
+
+public class SubTableHandler extends ComponentHandler{
+
+ private static final SubTableHandlerMetaRule metaRule = new SubTableHandlerMetaRule();
+
+ public SubTableHandler(ComponentConfig config) {
+ super(config);
+ }
+
+ @Override
+ protected MetaRuleset createMetaRuleset(Class type) {
+ MetaRuleset m = super.createMetaRuleset(type);
+ m.addRule(metaRule);
+ return m;
+ }
+
+ static class SubTableHandlerMetaRule extends MetaRule {
+
+ @Override
+ public Metadata applyRule(String name, TagAttribute attribute, MetadataTarget meta) {
+ if(meta.isTargetInstanceOf(UISubTable.class) &&
"toggleListener".equals(name)) {
+ return new SubTableMapper(attribute);
+ }
+ return null;
+ }
+
+ }
+
+ static class SubTableMapper extends Metadata {
+
+ private static final Class[] SIGNATURE = new Class[] {
org.richfaces.event.ToggleEvent.class };
+
+ private final TagAttribute _attribute;
+
+ public SubTableMapper(TagAttribute attribute) {
+ this._attribute = attribute;
+ }
+
+ @Override
+ public void applyMetadata(FaceletContext ctx, Object instance) {
+ MethodExpression methodExpression = _attribute.getMethodExpression(ctx, null,
SIGNATURE);
+ ((UISubTable)instance).setToggleListener(methodExpression);
+ }
+ }
+}
Modified:
root/ui-sandbox/trunk/components/tables/ui/src/main/resources/META-INF/resources/toggler.js
===================================================================
---
root/ui-sandbox/trunk/components/tables/ui/src/main/resources/META-INF/resources/toggler.js 2009-11-20
17:02:34 UTC (rev 15947)
+++
root/ui-sandbox/trunk/components/tables/ui/src/main/resources/META-INF/resources/toggler.js 2009-11-20
17:14:19 UTC (rev 15948)
@@ -2,6 +2,7 @@
this.id = id;
this.forId = forId;
this.state = initialState;
+ this.hidden = id + ":hidden";
}
Toggle.prototype.convertId = function(id) {
@@ -28,9 +29,18 @@
this.command("show", elements);
}
this.state = toState;
+ this.saveState(this.state);
}
}
+Toggle.prototype.saveState= function(state) {
+ var id = this.hidden;
+ var element = this.getElement(id);
+ if(element) {
+ element[0].attr('value', state);
+ }
+}
+
Toggle.prototype.serverToggle = function(toState) {
}
Modified:
root/ui-sandbox/trunk/components/tables/ui/src/main/resources/META-INF/tables.taglib.xml
===================================================================
---
root/ui-sandbox/trunk/components/tables/ui/src/main/resources/META-INF/tables.taglib.xml 2009-11-20
17:02:34 UTC (rev 15947)
+++
root/ui-sandbox/trunk/components/tables/ui/src/main/resources/META-INF/tables.taglib.xml 2009-11-20
17:14:19 UTC (rev 15948)
@@ -22,6 +22,7 @@
<component>
<component-type>org.richfaces.Column</component-type>
</component>
+
</tag>
<tag>
<tag-name>colGroup</tag-name>
@@ -34,7 +35,8 @@
<tag-name>subTable</tag-name>
<component>
<component-type>org.richfaces.SubTable</component-type>
- <!--
renderer-type>org.richfaces.renderkit.SubTableRenderer</renderer-type-->
+ <!--
renderer-type>org.richfaces.renderkit.SubTableRenderer</renderer-type-->
+ <!-- handler-class>org.richfaces.taglib.SubTableHandler</handler-class
-->
</component>
</tag>
<tag>