Author: abelevich
Date: 2010-04-06 12:08:35 -0400 (Tue, 06 Apr 2010)
New Revision: 16736
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/renderkit/ToggleControlRendererBase.java
Log:
refactor existing toggleControl functionality
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 2010-04-06
16:07:37 UTC (rev 16735)
+++
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/UIToggleControl.java 2010-04-06
16:08:35 UTC (rev 16736)
@@ -1,21 +1,18 @@
package org.richfaces.component;
-import java.util.Collection;
-import java.util.LinkedHashSet;
-
import javax.faces.component.UIComponentBase;
-public abstract class UIToggleControl extends UIComponentBase {
+public class UIToggleControl extends UIComponentBase {
public static final String COMPONENT_TYPE = "org.richfaces.ToggleControl";
public static final String COMPONENT_FAMILY =
"org.richfaces.ToggleControl";
-
enum PropertyKeys {
- expandControl, collapseControl, forId, event, execute, render
+ expandControl, collapseControl, forId, switchType, event
}
+
@Override
public String getFamily() {
return COMPONENT_FAMILY;
@@ -37,14 +34,14 @@
getStateHelper().put(PropertyKeys.collapseControl, collapseControl);
}
- public String getForId() {
+ public String getFor() {
return (String) getStateHelper().eval(PropertyKeys.forId, null);
}
- public void setForId(String forId) {
+ public void setFor(String forId) {
getStateHelper().put(PropertyKeys.forId, forId);
}
-
+
public String getEvent() {
return (String) getStateHelper().eval(PropertyKeys.event, "onclick");
@@ -53,38 +50,5 @@
public void setEvent(String event) {
getStateHelper().put(PropertyKeys.event, event);
}
- public Collection<String> getExecute() {
- Collection<String> execute =
(Collection<String>)getStateHelper().get(PropertyKeys.execute);
- if(execute == null) {
- execute = getDefaultIds();
- getStateHelper().put(PropertyKeys.execute, execute);
- }
- return execute;
- }
-
- public void addExecuteId(String id){
- //TODO nick - if user has provided collection in bean it shouldn't be
modified by the component - create a copy
- getExecute().add(id);
- }
-
- public Collection<String> getRender() {
- Collection<String> render =
(Collection<String>)getStateHelper().get(PropertyKeys.render);
- if(render == null) {
- render = getDefaultIds();
- getStateHelper().put(PropertyKeys.render, render);
- }
- return render;
- }
-
- public void addRenderId(String id){
- //TODO nick - if user has provided collection in bean it shouldn't be
modified by the component - create a copy
- getRender().add(id);
- }
-
- private Collection<String> getDefaultIds() {
- Collection<String> ids = new LinkedHashSet<String>();
- ids.add("@this");
- return ids;
- }
-
+
}
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 2010-04-06
16:07:37 UTC (rev 16735)
+++
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/renderkit/ToggleControlRendererBase.java 2010-04-06
16:08:35 UTC (rev 16736)
@@ -13,7 +13,6 @@
import javax.faces.context.ResponseWriter;
import org.ajax4jsf.javascript.ScriptUtils;
-import org.ajax4jsf.renderkit.AjaxRendererUtils;
import org.ajax4jsf.renderkit.RendererBase;
import org.ajax4jsf.renderkit.RendererUtils.HTML;
import org.richfaces.component.Expandable;
@@ -46,12 +45,9 @@
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();
@@ -60,91 +56,93 @@
String state = params.get(hiddenId);
if (state != null && state.trim().length() > 0) {
- boolean newState = Boolean.valueOf(state).booleanValue();
+
UIComponent processed = findComponent(context, toggle);
if (processed instanceof Expandable) {
-
+
+ boolean newState = Boolean.valueOf(state).booleanValue();
boolean prevState = ((Expandable) processed).isExpanded();
+
if (newState != prevState) {
new ToggleEvent(toggle, processed, newState).queue();
}
-
-//
toggle.addExecuteId((String)processed.getAttributes().get("clientId"));
-//
toggle.addRenderId((String)processed.getAttributes().get("clientId"));
+
}
}
}
protected void encodeControl(FacesContext context, UIComponent component) throws
IOException {
+ UIToggleControl toggleControl = (UIToggleControl) component;
- if (component instanceof UIToggleControl) {
- UIToggleControl toggleControl = (UIToggleControl) component;
+ UISubTable subTable = findComponent(context, toggleControl);
+ if (subTable != null) {
+ ResponseWriter writer = context.getResponseWriter();
+
+ String toggleId = toggleControl.getClientId(context);
+ String options = encodeOptions(context, toggleControl, subTable);
+ String switchType = subTable.getExpandMode();
+
+ boolean expanded = subTable.isExpanded();
- UISubTable subTable = findComponent(context, toggleControl);
- //TODO nick - remove the next line
- subTable.getAttributes().get("expanded");
- if (subTable != null) {
- ResponseWriter writer = context.getResponseWriter();
+ encodeControl(context, writer, toggleControl, switchType, expanded, false);
+ encodeControl(context, writer, toggleControl, switchType, !expanded, true);
- String toggleId = toggleControl.getClientId(context);
- String options = encodeOptions(context, toggleControl, subTable);
-
- String switchType = null;//subTable.getSwichType();
-// boolean expanded = subTable.isExpanded();
-
-// encodeControl(context, writer, toggleControl, switchType, expanded,
false);
-// encodeControl(context, writer, toggleControl, switchType, !expanded,
true);
-
- encodeHiddenInput(context, writer, toggleControl);
-
- writer.startElement(HTML.SCRIPT_ELEM, subTable);
- String registerScript = MessageFormat.format(MANAGER_SCRIPT, toggleId,
options);
- writer.writeText(registerScript, null);
- writer.endElement(HTML.SCRIPT_ELEM);
- }
+ encodeHiddenInput(context, writer, toggleControl);
+
+ String registerScript = MessageFormat.format(MANAGER_SCRIPT, toggleId,
options);
+ writer.startElement(HTML.SCRIPT_ELEM, subTable);
+ writer.writeText(registerScript, null);
+ writer.endElement(HTML.SCRIPT_ELEM);
}
+
}
protected void encodeControl(FacesContext context, ResponseWriter writer,
UIToggleControl control,
- String switchType, boolean expanded, boolean visible) throws IOException {
-
+ String switchType, boolean expanded, boolean visible)
throws IOException {
String state = getState(expanded);
-
- String image = expanded ? control.getExpandControl() :
control.getCollapseControl();
-
String script = getScript(control, switchType, expanded);
- String style = getStyle(context, control);
String styleClass = getStyleClass(context, control);
+ String style = getStyle(context, control);
writer.startElement(HTML.SPAN_ELEM, control);
writer.writeAttribute(HTML.CLASS_ATTRIBUTE, styleClass, null);
writer.writeAttribute(HTML.STYLE_ATTRIBUTE, style, null);
-
- writer.startElement(HTML.IMG_ELEMENT, control);
- writer.writeAttribute(HTML.ID_ATTRIBUTE, control.getClientId() + ":" +
state, null);
-
- if (!visible) {
- writer.writeAttribute(HTML.STYLE_ATTRIBUTE, DISPLAY_NONE, null);
+
+ UIComponent controlFacet = control.getFacet(state);
+ if(controlFacet != null && controlFacet.isRendered()) {
+ if(!visible) {
+ String facetStyle =
(String)controlFacet.getAttributes().get("style");
+ facetStyle = facetStyle != null ? facetStyle + "; display:
none" : "; display: none";
+ controlFacet.getAttributes().put("style", facetStyle);
+ }
+ controlFacet.encodeAll(context);
+ } else {
+
+ String image = expanded ? control.getExpandControl() :
control.getCollapseControl();
+ if (image != null && image.trim().length() > 0) {
+
+ writer.startElement(HTML.IMG_ELEMENT, control);
+ writer.writeAttribute(HTML.ID_ATTRIBUTE, control.getClientId() +
":" + state, null);
+ writer.writeAttribute(HTML.SRC_ATTRIBUTE, image, null);
+ writer.writeAttribute(HTML.ALT_ATTRIBUTE, "", null);
+
+ if (!visible) {
+ writer.writeAttribute(HTML.STYLE_ATTRIBUTE, DISPLAY_NONE, null);
+ }
+
+ //TODO: anton - remove inline script !
+ if (script != null && script.length() > 0) {
+ String eventName = control.getEvent();
+ writer.writeAttribute(eventName, script, null);
+ }
+ writer.endElement(HTML.IMG_ELEMENT);
+ }
}
-
- if (script != null && script.length() > 0) {
- String eventName = control.getEvent();
- writer.writeAttribute(eventName, script, null);
- }
-
- //TODO nick - if image is empty, then no tag should be encoded
- //TODO nick - "alt" attribute is required for <IMG>
- if (image != null && image.trim().length() > 0) {
- writer.writeAttribute(HTML.SRC_ATTRIBUTE, image, null);
- }
-
- writer.endElement(HTML.IMG_ELEMENT);
writer.endElement(HTML.SPAN_ELEM);
}
public String encodeOptions(FacesContext context, UIToggleControl toggleControl,
UISubTable subTable) {
-
UIForm nestingForm = getUtils().getNestingForm(context, toggleControl);
String formId = nestingForm.getClientId(context);
@@ -163,8 +161,7 @@
}
public void encodeHiddenInput(FacesContext context, ResponseWriter writer,
UIComponent component)
- throws IOException {
-
+ throws IOException {
String hiddenId = component.getClientId(context) + HIDDEN;
writer.startElement(HTML.INPUT_ELEM, component);
@@ -184,19 +181,19 @@
}
protected UISubTable findComponent(FacesContext context, UIToggleControl
toggleControl) {
- String forId = toggleControl.getForId();
-
+ String forId = toggleControl.getFor();
if (forId != null && forId.length() > 0) {
+
UIComponent subTable = getUtils().findComponentFor(context, toggleControl,
forId);
if (subTable instanceof UISubTable) {
return (UISubTable) subTable;
}
+
}
return null;
}
public String getScript(UIToggleControl toggleControl, String switchType, boolean
isExpanded) {
-
String pattern = null;
/*
if (switchType.equals(UISubTable.SWITCH_TYPE_CLIENT)) {
@@ -208,11 +205,10 @@
}*/
String toggleId = toggleControl.getClientId();
- return MessageFormat.format(pattern, toggleId, isExpanded);
+ return ""; //MessageFormat.format(pattern, toggleId, isExpanded);
}
protected String getState(boolean expand) {
return expand ? EXPAND_STATE : COLLAPSE_STATE;
}
-
}