Author: maksimkaszynski
Date: 2007-06-07 09:20:36 -0400 (Thu, 07 Jun 2007)
New Revision: 1074
Added:
trunk/sandbox/scrollable-grid/src/test/java/org/richfaces/model/selection/ClientSelectionTest.java
Modified:
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/selection/ClientSelection.java
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/selection/SelectionRange.java
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/renderkit/html/ScrollableGridBaseRenderer.java
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/renderkit/html/SelectionRendererContributor.java
Log:
selection contributor integration
Modified:
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/selection/ClientSelection.java
===================================================================
---
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/selection/ClientSelection.java 2007-06-07
12:56:29 UTC (rev 1073)
+++
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/selection/ClientSelection.java 2007-06-07
13:20:36 UTC (rev 1074)
@@ -39,7 +39,56 @@
return ranges;
}
- public void addIndex(int i) {
+ public void addIndex(int j) {
+ if(this.isSelected(j)) return;
+
+
+ SelectionRange firstRange = null;
+
+ int s = ranges.size();
+
+ int insertPosition = 0;
+
+ for(int i = 0; i < s && insertPosition >= 0 ; i++) {
+
+ firstRange = (SelectionRange) ranges.get(i);
+
+ if (firstRange.getStartIndex() == j + 1) {
+
+ firstRange.setStartIndex(j);
+ insertPosition = -1;
+
+ } else if (firstRange.getEndIndex() == j - 1) {
+
+ firstRange.setEndIndex(j);
+
+ if (i + 1 < s) {
+ SelectionRange range2 = (SelectionRange) ranges.get(i + 1);
+
+ if (range2.getStartIndex() == j || range2.getStartIndex() == j + 1) {
+
+ ranges.remove(i + 1);
+
+ firstRange.setEndIndex(range2.getEndIndex());
+ }
+ }
+
+ insertPosition = -1;
+
+ } else if (firstRange.getStartIndex() > j) {
+ insertPosition = i;
+ }
+
+ }
+
+ if (insertPosition >= 0) {
+ firstRange = new SelectionRange(j,j);
+ ranges.add(insertPosition, firstRange);
+
+ return;
+ }
+
+
}
}
Modified:
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/selection/SelectionRange.java
===================================================================
---
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/selection/SelectionRange.java 2007-06-07
12:56:29 UTC (rev 1073)
+++
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/selection/SelectionRange.java 2007-06-07
13:20:36 UTC (rev 1074)
@@ -36,5 +36,29 @@
public boolean within(int index) {
return startIndex <= index && endIndex >= index;
}
+
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + endIndex;
+ result = prime * result + startIndex;
+ return result;
+ }
+
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ final SelectionRange other = (SelectionRange) obj;
+ if (endIndex != other.endIndex)
+ return false;
+ if (startIndex != other.startIndex)
+ return false;
+ return true;
+ }
+
}
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-07
12:56:29 UTC (rev 1073)
+++
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/renderkit/html/ScrollableGridBaseRenderer.java 2007-06-07
13:20:36 UTC (rev 1074)
@@ -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;
@@ -29,9 +26,6 @@
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.ScriptOptions;
@@ -64,8 +58,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(){
@@ -343,6 +335,8 @@
options.addOption("onSortAjaxUpdate", functionDefinition);
+ mergeScriptOptions(options, context, grid);
+
return function.toScript();
}
@@ -536,126 +530,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;
Modified:
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 2007-06-07
12:56:29 UTC (rev 1073)
+++
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/renderkit/html/SelectionRendererContributor.java 2007-06-07
13:20:36 UTC (rev 1074)
@@ -147,7 +147,7 @@
ScriptOptions scriptOptions = new ScriptOptions(component);
scriptOptions.addOption("selectionInput", getSelectionInputName(
context, (UIScrollableGrid) component));
- return null;
+ return scriptOptions;
}
Added:
trunk/sandbox/scrollable-grid/src/test/java/org/richfaces/model/selection/ClientSelectionTest.java
===================================================================
---
trunk/sandbox/scrollable-grid/src/test/java/org/richfaces/model/selection/ClientSelectionTest.java
(rev 0)
+++
trunk/sandbox/scrollable-grid/src/test/java/org/richfaces/model/selection/ClientSelectionTest.java 2007-06-07
13:20:36 UTC (rev 1074)
@@ -0,0 +1,73 @@
+package org.richfaces.model.selection;
+
+import junit.framework.TestCase;
+
+public class ClientSelectionTest extends TestCase {
+
+
+ private ClientSelection clientSelection;
+
+ public ClientSelectionTest(String name) {
+ super(name);
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ clientSelection = new ClientSelection();
+ }
+
+ protected void tearDown() throws Exception {
+ clientSelection = null;
+ super.tearDown();
+ }
+
+ public void testAddRange() {
+ SelectionRange range = new SelectionRange(10, 20);
+ clientSelection.addRange(range);
+ assertTrue(clientSelection.isSelected(10));
+ assertTrue(clientSelection.isSelected(11));
+ assertTrue(clientSelection.isSelected(15));
+ assertTrue(clientSelection.isSelected(20));
+ assertFalse(clientSelection.isSelected(0));
+ assertFalse(clientSelection.isSelected(30));
+ }
+
+ public void testIsSelected() {
+ SelectionRange range = new SelectionRange(10, 20);
+ clientSelection.addRange(range);
+ assertTrue(clientSelection.isSelected(10));
+ assertTrue(clientSelection.isSelected(11));
+ assertTrue(clientSelection.isSelected(15));
+ assertTrue(clientSelection.isSelected(20));
+ assertFalse(clientSelection.isSelected(0));
+ assertFalse(clientSelection.isSelected(30));
+ }
+
+ public void testGetRanges() {
+ SelectionRange range = new SelectionRange(10, 20);
+ clientSelection.addRange(range);
+ range = new SelectionRange(30, 40);
+ clientSelection.addRange(range);
+
+ assertNotNull(clientSelection.getRanges());
+ assertEquals(2, clientSelection.getRanges().size());
+ }
+
+ public void testAddIndex() {
+ clientSelection.addRange(new SelectionRange(10, 20));
+ clientSelection.addIndex(21);
+ assertEquals(1, clientSelection.getRanges().size());
+ assertTrue(clientSelection.isSelected(21));
+ clientSelection.addRange(new SelectionRange(23, 30));
+ assertEquals(2, clientSelection.getRanges().size());
+ assertFalse(clientSelection.isSelected(22));
+ assertTrue(clientSelection.isSelected(23));
+
+ clientSelection.addIndex(22);
+ assertTrue(clientSelection.isSelected(22));
+ assertEquals(1, clientSelection.getRanges().size());
+
+
+ }
+
+}