Author: maksimkaszynski
Date: 2007-06-06 13:00:43 -0400 (Wed, 06 Jun 2007)
New Revision: 1056
Added:
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/component/Selectable.java
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/renderkit/html/HTMLEncodingContributor.java
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/renderkit/html/SelectionRendererContributor.java
Modified:
trunk/sandbox-samples/scrollable-grid-demo/src/main/java/org/richfaces/demo/datagrid/bean/ChannelDataModel2.java
trunk/sandbox/scrollable-grid/src/main/config/component/scrollable-grid.xml
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/component/UIScrollableGrid.java
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/Entity.java
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/GridDataModel.java
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/selection/Selection.java
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/selection/SimpleSelection.java
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/renderkit/html/ScrollableGridBaseRenderer.java
Log:
added selection renderer contributor
Modified: trunk/sandbox/scrollable-grid/src/main/config/component/scrollable-grid.xml
===================================================================
--- trunk/sandbox/scrollable-grid/src/main/config/component/scrollable-grid.xml 2007-06-06
16:32:32 UTC (rev 1055)
+++ trunk/sandbox/scrollable-grid/src/main/config/component/scrollable-grid.xml 2007-06-06
17:00:43 UTC (rev 1056)
@@ -52,11 +52,14 @@
</property>
- <property attachedstate="true">
+ <property attachedstate="true" hidden="true">
<name>sortOrder</name>
<classname>org.richfaces.model.SortOrder</classname>
</property>
-
+ <property attachedstate="true" hidden="true">
+ <name>selection</name>
+ <classname>org.richfaces.model.selection.Selection</classname>
+ </property>
&ui_component_attributes;
</component>
Added:
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/component/Selectable.java
===================================================================
--- trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/component/Selectable.java
(rev 0)
+++
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/component/Selectable.java 2007-06-06
17:00:43 UTC (rev 1056)
@@ -0,0 +1,15 @@
+/**
+ *
+ */
+package org.richfaces.component;
+
+import org.richfaces.model.selection.Selection;
+
+/**
+ * @author Maksim Kaszynski
+ *
+ */
+public interface Selectable {
+ public Selection getSelection();
+ public void setSelection (Selection selection);
+}
Modified:
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/component/UIScrollableGrid.java
===================================================================
---
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/component/UIScrollableGrid.java 2007-06-06
16:32:32 UTC (rev 1055)
+++
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/component/UIScrollableGrid.java 2007-06-06
17:00:43 UTC (rev 1056)
@@ -41,7 +41,7 @@
*
*/
-public abstract class UIScrollableGrid extends UIRepeat implements AjaxComponent,
Sortable{
+public abstract class UIScrollableGrid extends UIRepeat implements AjaxComponent,
Sortable, Selectable{
private transient Collection partialUpdateChildren;
Modified: trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/Entity.java
===================================================================
--- trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/Entity.java 2007-06-06
16:32:32 UTC (rev 1055)
+++ trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/Entity.java 2007-06-06
17:00:43 UTC (rev 1056)
@@ -3,12 +3,11 @@
*/
package org.richfaces.model;
-import java.io.Serializable;
/**
* @author Maksim Kaszynski
*
*/
public interface Entity {
- public Serializable getId();
+ public Object getId();
}
Modified:
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/GridDataModel.java
===================================================================
---
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/GridDataModel.java 2007-06-06
16:32:32 UTC (rev 1055)
+++
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/GridDataModel.java 2007-06-06
17:00:43 UTC (rev 1056)
@@ -4,7 +4,6 @@
package org.richfaces.model;
import java.io.IOException;
-import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -25,10 +24,8 @@
private static final Log log = LogFactory.getLog(GridDataModel.class);
- private Serializable rowKey;
+ private Object rowKey;
- private int rowIndex = Integer.MIN_VALUE;
-
private Map mapping;
public Object getRowKey() {
@@ -36,7 +33,7 @@
}
public void setRowKey(Object key) {
- rowKey = (Serializable) key;
+ rowKey = key;
}
public void walk(FacesContext context, DataVisitor visitor, Range range,
@@ -78,9 +75,9 @@
public abstract List loadData(int startRow, int endRow, SortOrder sortOrder);
- public abstract Object getObjectById(Serializable id);
+ public abstract Object getObjectById(Object id);
- private Object loadAndMap(Serializable id) {
+ private Object loadAndMap(Object id) {
if (log.isTraceEnabled()) {
log.trace("loadAndMap " + id);
Modified:
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/selection/Selection.java
===================================================================
---
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/selection/Selection.java 2007-06-06
16:32:32 UTC (rev 1055)
+++
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/selection/Selection.java 2007-06-06
17:00:43 UTC (rev 1056)
@@ -16,5 +16,5 @@
public int size();
- public boolean isSelected(Serializable rowKey);
+ public boolean isSelected(Object rowKey);
}
Modified:
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/selection/SimpleSelection.java
===================================================================
---
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/selection/SimpleSelection.java 2007-06-06
16:32:32 UTC (rev 1055)
+++
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/selection/SimpleSelection.java 2007-06-06
17:00:43 UTC (rev 1056)
@@ -18,11 +18,11 @@
private Set keys = new HashSet();
- public boolean addKey(Serializable rowKey) {
+ public boolean addKey(Object rowKey) {
return keys.add(rowKey);
}
- public boolean removeKey(Serializable rowKey) {
+ public boolean removeKey(Object rowKey) {
return keys.remove(rowKey);
}
@@ -34,7 +34,7 @@
return keys.size();
}
- public boolean isSelected(Serializable rowKey) {
+ public boolean isSelected(Object rowKey) {
return keys.contains(rowKey);
}
Added:
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/renderkit/html/HTMLEncodingContributor.java
===================================================================
---
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/renderkit/html/HTMLEncodingContributor.java
(rev 0)
+++
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/renderkit/html/HTMLEncodingContributor.java 2007-06-06
17:00:43 UTC (rev 1056)
@@ -0,0 +1,11 @@
+package org.richfaces.renderkit.html;
+
+import java.io.IOException;
+
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+
+public interface HTMLEncodingContributor {
+ public void encode(FacesContext context, UIComponent component)
+ throws IOException;
+}
Modified:
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/renderkit/html/ScrollableGridBaseRenderer.java
===================================================================
---
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/renderkit/html/ScrollableGridBaseRenderer.java 2007-06-06
16:32:32 UTC (rev 1055)
+++
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/renderkit/html/ScrollableGridBaseRenderer.java 2007-06-06
17:00:43 UTC (rev 1056)
@@ -1,17 +1,14 @@
package org.richfaces.renderkit.html;
import java.io.IOException;
-import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
-import javax.faces.application.Application;
import javax.faces.component.UIComponent;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
-import javax.faces.convert.Converter;
import org.ajax4jsf.ajax.repeat.DataVisitor;
import org.ajax4jsf.framework.ajax.AjaxContext;
@@ -28,10 +25,8 @@
import org.richfaces.component.UIScrollableGrid;
import org.richfaces.component.UIScrollableGridColumn;
import org.richfaces.event.sort.SortEvent;
-import org.richfaces.model.selection.ClientSelection;
-import org.richfaces.model.selection.Selection;
-import org.richfaces.model.selection.SimpleSelection;
import org.richfaces.renderkit.CompositeRenderer;
+import org.richfaces.renderkit.RendererContributor;
import org.richfaces.renderkit.html.response.GridScrollSettings;
import org.richfaces.utils.TemplateLoader;
@@ -60,8 +55,6 @@
private RendererBase footerCellTemplate = null;
- private static final String CLIENT_SELECTION = "clientSelection";
-
private final Log log = LogFactory.getLog(ScrollableGridBaseRenderer.class);
private final ColumnVisitor columnsWidthCounter = new ColumnVisitor(){
@@ -245,11 +238,11 @@
state.setFrozenColumnCount(((Integer)grid.getAttributes().get("frozenColCount")).intValue());
String row_id = null;
-
+ /*
System.out.println("row data index: " + grid.getRowIndex());
System.out.println("rows count " + grid.getRows());
System.out.println("local rows " + state.getRowIndex());
-
+ */
for (Iterator iter = grid.getChildren().iterator(); iter.hasNext(); ) {
UIComponent kid = (UIComponent) iter.next();
@@ -301,7 +294,6 @@
}
};
-
public String getJavaScriptVarName(FacesContext context, UIScrollableGrid grid) {
String id = grid.getBaseClientId(context);
return "Richfaces_ScrollableGrid_" + id.replaceAll("[^A-Za-z0-9_]",
"_");
@@ -497,126 +489,6 @@
}
- private void decodeSelection(FacesContext context, UIScrollableGrid grid)
- throws IOException{
-
- String clientId = grid.getClientId(context);
- String id = clientId + "_selection";
- ExternalContext externalContext = context.getExternalContext();
- Map requestParamMap = externalContext.getRequestParameterMap();
- Application application = context.getApplication();
-
- String value = (String) requestParamMap.get(id);
-
- Converter converter = application.createConverter(ClientSelection.class);
-
- ClientSelection _oldClientSelection =
- (ClientSelection) grid.getAttributes().get(CLIENT_SELECTION);
-
- final ClientSelection oldClientSelection =
- _oldClientSelection == null ?
- new ClientSelection() :
- _oldClientSelection;
-
- final ClientSelection clientSelection =
- (ClientSelection) converter.getAsObject(context, grid, value);
-
-
-
- //FIXME: Obscure code. Hope Anton will make it more clear
- final GridRendererState state = GridRendererState.createState(context, grid);
-
- final SimpleSelection simpleSelection = new SimpleSelection();
-
- grid.walk(context,
- new DataVisitor() {
- public void process(FacesContext context, Object rowKey,
- Object argument) throws IOException {
-
- int i = state.getRowIndex();
-
- if (shouldAddToSelection(i, oldClientSelection, clientSelection)) {
-
- simpleSelection.addKey((Serializable)rowKey);
-
- } else if (shouldRemoveFromSelection(i, oldClientSelection, clientSelection)){
-
- simpleSelection.removeKey((Serializable) rowKey);
-
- }
-
- }
- },
- state);
-
-
- GridRendererState.restoreState(context);
-
- //FIXME:
-
-
- }
-
- //Decide whether to add new row to selection based on comparison with old one
- private boolean shouldAddToSelection(int i, ClientSelection oldSelection,
ClientSelection newSelection) {
- return false;
- }
-
- //Decide whether to remove new row to selection based on comparison with old one
- private boolean shouldRemoveFromSelection(int i, ClientSelection oldSelection,
ClientSelection newSelection) {
- return false;
- }
-
-
- private void encodeSelection(FacesContext context, UIScrollableGrid grid) throws
IOException {
- final GridRendererState state = GridRendererState.createState(context, grid);
-
- final Selection gridSelection = new SimpleSelection();
- final ClientSelection clientSelection = new ClientSelection();
-
- grid.walk(context,
- new DataVisitor() {
- public void process(FacesContext context, Object rowKey,
- Object argument) throws IOException {
-
- if (gridSelection.isSelected((Serializable) rowKey)) {
-
- int i = state.getRowIndex();
-
- clientSelection.addIndex(i);
- }
-
-
-
-
- }
- },
- state);
-
-
- GridRendererState.restoreState(context);
-
- grid.getAttributes().put(CLIENT_SELECTION, clientSelection);
- }
-
- public void writeSelection(FacesContext context, UIScrollableGrid grid)
- throws IOException {
-
- Application application = context.getApplication();
-
- Converter converter =
- application.createConverter(ClientSelection.class);
-
- String string =
- converter.getAsString(context, grid, grid.getAttributes().get(CLIENT_SELECTION));
-
- if (string == null) {
- string = "";
- }
-
- }
-
-
private void decodeScrolling(String submitedState, UIScrollableGrid grid){
boolean isEmpty = true;
@@ -681,7 +553,7 @@
private GridScrollSettings createOptions(UIScrollableGrid grid){
- int index = grid.getRowIndex();
+ int index = grid.getFirst();
int startRow = ((Integer)grid.getAttributes().get(CLIENT_ROW_KEY)).intValue();
int count = grid.getRows();
@@ -760,4 +632,18 @@
return footerCellTemplate;
}
+ public void contributorsEncodeHere(FacesContext context, UIScrollableGrid grid) throws
IOException {
+ RendererContributor [] contribs = getContributors();
+
+ if (contribs != null) {
+ for (int i = 0; i < contribs.length; i++) {
+ RendererContributor rendererContributor = contribs[i];
+
+ if (rendererContributor instanceof HTMLEncodingContributor) {
+ ((HTMLEncodingContributor) rendererContributor).encode(context, grid);
+ }
+ }
+ }
+ }
+
}
Added:
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/renderkit/html/SelectionRendererContributor.java
===================================================================
---
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/renderkit/html/SelectionRendererContributor.java
(rev 0)
+++
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/renderkit/html/SelectionRendererContributor.java 2007-06-06
17:00:43 UTC (rev 1056)
@@ -0,0 +1,236 @@
+/**
+ *
+ */
+package org.richfaces.renderkit.html;
+
+import java.io.IOException;
+import java.util.Map;
+
+import javax.faces.FacesException;
+import javax.faces.application.Application;
+import javax.faces.component.UIComponent;
+import javax.faces.context.ExternalContext;
+import javax.faces.context.FacesContext;
+import javax.faces.context.ResponseWriter;
+import javax.faces.convert.Converter;
+
+import org.ajax4jsf.ajax.repeat.DataVisitor;
+import org.ajax4jsf.framework.ajax.AjaxContext;
+import org.ajax4jsf.framework.renderer.RendererUtils.HTML;
+import org.richfaces.component.UIScrollableGrid;
+import org.richfaces.model.selection.ClientSelection;
+import org.richfaces.model.selection.Selection;
+import org.richfaces.model.selection.SimpleSelection;
+import org.richfaces.renderkit.CompositeRenderer;
+import org.richfaces.renderkit.RendererContributor;
+import org.richfaces.renderkit.ScriptOptions;
+
+/**
+ * @author Maksim Kaszynski
+ *
+ */
+public class SelectionRendererContributor implements RendererContributor {
+
+
+ public static final String CLIENT_SELECTION = "clientSelection";
+
+ public static final String getSelectionInputName(FacesContext context,
+ UIScrollableGrid grid) {
+ String id = grid.getBaseClientId(context) + "_sel";
+
+ return id;
+ }
+
+ public void decode(FacesContext context, UIComponent component,
+ CompositeRenderer compositeRenderer) {
+
+ UIScrollableGrid grid = (UIScrollableGrid) component;
+
+ ExternalContext externalContext = context.getExternalContext();
+ Map requestParamMap = externalContext.getRequestParameterMap();
+ Application application = context.getApplication();
+
+ String id = getSelectionInputName(context, grid);
+
+ String value = (String) requestParamMap.get(id);
+
+ Converter converter = application.createConverter(ClientSelection.class);
+
+ ClientSelection _oldClientSelection =
+ (ClientSelection) grid.getAttributes().get(CLIENT_SELECTION);
+
+ final ClientSelection oldClientSelection =
+ _oldClientSelection == null ?
+ new ClientSelection() :
+ _oldClientSelection;
+
+ final ClientSelection clientSelection =
+ (ClientSelection) converter.getAsObject(context, grid, value);
+
+
+
+ final GridRendererState state =
+ GridRendererState.createState(context, grid);
+
+ final SimpleSelection simpleSelection = new SimpleSelection();
+
+ try {
+ grid.walk(context,
+ new DataVisitor() {
+ public void process(FacesContext context, Object rowKey,
+ Object argument) throws IOException {
+
+ int i = state.getRowIndex();
+
+ if (shouldAddToSelection(i, oldClientSelection, clientSelection)) {
+
+ simpleSelection.addKey(rowKey);
+
+ } else if (shouldRemoveFromSelection(i, oldClientSelection, clientSelection)){
+
+ simpleSelection.removeKey(rowKey);
+
+ }
+
+ }
+ },
+ state);
+ } catch(IOException e) {
+ throw new FacesException(e);
+ }
+
+
+ GridRendererState.restoreState(context);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.richfaces.renderkit.RendererContributor#getAcceptableClass()
+ */
+ public Class getAcceptableClass() {
+ return UIScrollableGrid.class;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
org.richfaces.renderkit.RendererContributor#getScriptContribution(javax.faces.context.FacesContext,
+ * javax.faces.component.UIComponent)
+ */
+ public String getScriptContribution(FacesContext context,
+ UIComponent component) {
+ return null;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.richfaces.renderkit.RendererContributor#getScriptDependencies()
+ */
+ public String[] getScriptDependencies() {
+ return null;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.richfaces.renderkit.RendererContributor#getStyleDependencies()
+ */
+ public String[] getStyleDependencies() {
+ return null;
+ }
+
+ public ScriptOptions buildOptions(FacesContext context,
+ UIComponent component) {
+ ScriptOptions scriptOptions = new ScriptOptions(component);
+ scriptOptions.addOption("selectionInput", getSelectionInputName(
+ context, (UIScrollableGrid) component));
+ return null;
+ }
+
+ //Decide whether to add new row to selection based on comparison with old one
+ private boolean shouldAddToSelection(int i, ClientSelection oldSelection,
ClientSelection newSelection) {
+ return false;
+ }
+
+ //Decide whether to remove new row to selection based on comparison with old one
+ private boolean shouldRemoveFromSelection(int i, ClientSelection oldSelection,
ClientSelection newSelection) {
+ return false;
+ }
+
+
+ private void encodeSelection(FacesContext context, UIScrollableGrid grid) throws
IOException {
+ final GridRendererState state = GridRendererState.createState(context, grid);
+
+ final Selection gridSelection = new SimpleSelection();
+ final ClientSelection clientSelection = new ClientSelection();
+
+ grid.walk(context,
+ new DataVisitor() {
+ public void process(FacesContext context, Object rowKey,
+ Object argument) throws IOException {
+
+ if (gridSelection.isSelected(rowKey)) {
+
+ int i = state.getRowIndex();
+
+ clientSelection.addIndex(i);
+ }
+
+
+
+
+ }
+ },
+ state);
+
+
+ GridRendererState.restoreState(context);
+
+ grid.getAttributes().put(CLIENT_SELECTION, clientSelection);
+ }
+
+ /**
+ * Get client selection from the component, transform it into string form,
+ * and write it as hidden input
+ * @param context
+ * @param grid
+ * @throws IOException
+ */
+ public void writeSelection(FacesContext context, UIScrollableGrid grid)
+ throws IOException {
+
+ Application application = context.getApplication();
+
+ Converter converter =
+ application.createConverter(ClientSelection.class);
+
+ String string =
+ converter.getAsString(context, grid, grid.getAttributes().get(CLIENT_SELECTION));
+
+ if (string == null) {
+ string = "";
+ }
+
+ String id = grid.getBaseClientId(context) + "_s";
+
+
+ ResponseWriter writer = context.getResponseWriter();
+ writer.startElement(HTML.INPUT_ELEM, grid);
+ writer.writeAttribute(HTML.TYPE_ATTR, "hidden", null);
+ writer.writeAttribute(HTML.id_ATTRIBUTE, id, null);
+ writer.writeAttribute(HTML.NAME_ATTRIBUTE, id, null);
+ writer.writeAttribute(HTML.value_ATTRIBUTE, string, null);
+ writer.endElement(HTML.INPUT_ELEM);
+
+ AjaxContext ajaxContext = AjaxContext.getCurrentInstance(context);
+
+ if (ajaxContext.isAjaxRequest()) {
+ ajaxContext.addRenderedArea(id);
+ }
+
+ }
+
+
+}
Modified:
trunk/sandbox-samples/scrollable-grid-demo/src/main/java/org/richfaces/demo/datagrid/bean/ChannelDataModel2.java
===================================================================
---
trunk/sandbox-samples/scrollable-grid-demo/src/main/java/org/richfaces/demo/datagrid/bean/ChannelDataModel2.java 2007-06-06
16:32:32 UTC (rev 1055)
+++
trunk/sandbox-samples/scrollable-grid-demo/src/main/java/org/richfaces/demo/datagrid/bean/ChannelDataModel2.java 2007-06-06
17:00:43 UTC (rev 1056)
@@ -7,6 +7,7 @@
import java.util.List;
import org.richfaces.demo.datagrid.model.Channel;
+import org.richfaces.demo.datagrid.model.Issue;
import org.richfaces.model.GridDataModel;
import org.richfaces.model.SortOrder;
@@ -22,7 +23,7 @@
* @see org.richfaces.model.GridDataModel#getObjectById(java.io.Serializable)
*/
@Override
- public Object getObjectById(Serializable id) {
+ public Object getObjectById(Object id) {
return channel.findById((Integer) id);
}
@@ -30,7 +31,7 @@
* @see org.richfaces.model.GridDataModel#loadData(int, int,
org.richfaces.model.SortOrder)
*/
@Override
- public List loadData(int startRow, int endRow, SortOrder sortOrder) {
+ public List <Issue> loadData(int startRow, int endRow, SortOrder sortOrder) {
return channel.executeQuery(startRow, endRow, sortOrder);
}