Author: maksimkaszynski
Date: 2007-06-12 14:23:59 -0400 (Tue, 12 Jun 2007)
New Revision: 1148
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/SimpleSelection.java
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/renderkit/html/SelectionRendererContributor.java
trunk/sandbox/scrollable-grid/src/main/javascript/ClientUI/common/utils/Utils.js
trunk/sandbox/scrollable-grid/src/test/java/org/richfaces/component/renderkit/html/SelectionRendererContributorTest.java
trunk/sandbox/scrollable-grid/src/test/java/org/richfaces/model/selection/ClientSelectionTest.java
Log:
selection renderer contributor now supports reset and all instructions
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-12
18:08:46 UTC (rev 1147)
+++
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/selection/ClientSelection.java 2007-06-12
18:23:59 UTC (rev 1148)
@@ -16,6 +16,10 @@
private static final long serialVersionUID = 5855157282287053681L;
+ public static final String FLAG_RESET = "x";
+
+ public static final String FLAG_ALL = "a";
+
private String selectionFlag;
private List ranges = new ArrayList();
@@ -94,7 +98,17 @@
}
+ private boolean reset = false;
+ public boolean isReset() {
+ return reset;
+ }
+
+ private boolean selectAll = false;
+ public boolean isSelectAll() {
+ return selectAll;
+ }
+
public String getSelectionFlag() {
return selectionFlag;
}
@@ -102,5 +116,14 @@
public void setSelectionFlag(String selectionFlag) {
this.selectionFlag = selectionFlag;
+
+ reset = false;
+ selectAll = false;
+
+ if (FLAG_ALL.equals(selectionFlag)) {
+ selectAll = true;
+ } else if (FLAG_RESET.equals(selectionFlag)) {
+ reset = true;
+ }
}
}
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-12
18:08:46 UTC (rev 1147)
+++
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/selection/SimpleSelection.java 2007-06-12
18:23:59 UTC (rev 1148)
@@ -37,4 +37,7 @@
return keys.contains(rowKey);
}
+ public void clear() {
+ keys.clear();
+ }
}
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-12
18:08:46 UTC (rev 1147)
+++
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/renderkit/html/SelectionRendererContributor.java 2007-06-12
18:23:59 UTC (rev 1148)
@@ -75,8 +75,15 @@
state.setRowIndex(GridUtils.getClientRowIndex(grid));
- final SimpleSelection simpleSelection = grid.getSelection() == null ? new
SimpleSelection() : (SimpleSelection) grid.getSelection();
+ final SimpleSelection simpleSelection = grid.getSelection() == null ? new
SimpleSelection()
+ : (SimpleSelection) grid.getSelection();
+
+
+ if (clientSelection.isReset() || clientSelection.isSelectAll()) {
+ simpleSelection.clear();
+ }
+
try {
grid.walk(context,
new DataVisitor() {
@@ -169,13 +176,15 @@
//Decide whether to add new row to selection based on comparison with old one
- private boolean shouldAddToSelection(int i, ClientSelection oldSelection,
ClientSelection newSelection) {
- return newSelection.isSelected(i) && !oldSelection.isSelected(i);
+ public boolean shouldAddToSelection(int i, ClientSelection oldSelection, ClientSelection
newSelection) {
+
+ return newSelection.isSelectAll() ||
+ (newSelection.isSelected(i) && (!oldSelection.isSelected(i) ||
newSelection.isReset())) ;
}
//Decide whether to remove new row to selection based on comparison with old one
- private boolean shouldRemoveFromSelection(int i, ClientSelection oldSelection,
ClientSelection newSelection) {
- return !newSelection.isSelected(i) && oldSelection.isSelected(i);
+ public boolean shouldRemoveFromSelection(int i, ClientSelection oldSelection,
ClientSelection newSelection) {
+ return !newSelection.isReset() && (!newSelection.isSelectAll() &&
(!newSelection.isSelected(i) && oldSelection.isSelected(i)));
}
Modified:
trunk/sandbox/scrollable-grid/src/main/javascript/ClientUI/common/utils/Utils.js
===================================================================
---
trunk/sandbox/scrollable-grid/src/main/javascript/ClientUI/common/utils/Utils.js 2007-06-12
18:08:46 UTC (rev 1147)
+++
trunk/sandbox/scrollable-grid/src/main/javascript/ClientUI/common/utils/Utils.js 2007-06-12
18:23:59 UTC (rev 1148)
@@ -1,45 +1,8 @@
var Utils = {
DOM: {
-
- importNode: function(node, bKids) {
-
- var nodeName = node.nodeName.toUpperCase();
-
- //LOG.debug("Importing node " + Utils.DOM._formatNode(node));
-
- switch(nodeName) {
- case "TBODY":
- case "TR":
- case "TD":
- {
- //LOG.debug("Creating new node ");
- var imported = document.createElement(nodeName);
-
- this.copyAttributes(imported, node);
-
- if (bKids) {
-
- for(var kid = node.firstChild; kid;
- kid = kid.nextSibling) {
-
- var importedKid = this.importNode(kid, true);
-
- imported.insertBefore(importedKid, null);
- }
- }
- return imported;
- }
- default: {
- var imported = document.createElement(nodeName);
- imported.outerHTML = node.xml;
- return imported;
- }
- }
- },
-
copyAttributes : function(target, source, opts) {
- //LOG.debug("Copying attributes from " + Utils.DOM._formatNode(source) +
" to " + Utils.DOM._formatNode(target) );
+ //LOG.debug("copyAttributes");
var attrs = source.attributes;
var exclusions = (opts && opts.exclude) ? opts.exclude : [];
@@ -50,7 +13,16 @@
var nodeName = attributeNode.nodeName;
var nodeValue = attributeNode.nodeValue;
- if(attributeNode.specified && nodeValue && nodeValue.length > 0
&& exclusions.indexOf(nodeName) < 0) {
+ var shouldCheckAttribute =
+ nodeValue &&
+ nodeValue.length > 0 &&
+ exclusions.indexOf(nodeName) < 0;
+
+ if (ClientUILib.isIE) {
+ shouldCheckAttribute &= attributeNode.specified;
+ }
+
+ if(shouldCheckAttribute) {
//LOG.debug("Copying attribute " + attributeNode.nodeName + "="
+ attributeNode.nodeValue);
@@ -62,7 +34,7 @@
}
}
-
+ //LOG.debug("/copyAttributes");
},
replaceNode : function(id, request) {
@@ -71,12 +43,12 @@
Utils.DOM.Event.removeListeners(target);
-
if (ClientUILib.isIE) {
var theDoc = document;
var createEl = theDoc.createElement;
var row = target.cloneNode(false);
+ this._clearAttributes(row);
this.copyAttributes(row, src);
for (var td = src.firstChild; td; td = td.nextSibling) {
@@ -102,22 +74,26 @@
} else {
target.innerHTML = src.innerHTML;
+ this._clearAttributes(target);
+ this.copyAttributes(target, src);
src = target;
- //src = document.importNode(src, true);
}
-
- //var importProvider = ClientUILib.isIE ? this : document;
-
- //src = importProvider.importNode(src, true);
-
-
-
-
return src;
},
+ _clearAttributes : function(node) {
+ var attrs = node.attributes;
+ if (node.clearAttributes) {
+ node.clearAttributes();
+ } else {
+ while(node.attributes.length > 0) {
+ node.removeAttributeNode(node.attributes[0]);
+ }
+ }
+ },
+
_formatNode : function(node) {
var sb = new StringBuilder();
Modified:
trunk/sandbox/scrollable-grid/src/test/java/org/richfaces/component/renderkit/html/SelectionRendererContributorTest.java
===================================================================
---
trunk/sandbox/scrollable-grid/src/test/java/org/richfaces/component/renderkit/html/SelectionRendererContributorTest.java 2007-06-12
18:08:46 UTC (rev 1147)
+++
trunk/sandbox/scrollable-grid/src/test/java/org/richfaces/component/renderkit/html/SelectionRendererContributorTest.java 2007-06-12
18:23:59 UTC (rev 1148)
@@ -12,6 +12,7 @@
import org.apache.shale.test.mock.MockExternalContext;
import org.richfaces.component.UIScrollableGrid;
import org.richfaces.model.selection.ClientSelection;
+import org.richfaces.model.selection.SelectionRange;
import org.richfaces.renderkit.html.ScrollableGridBaseRenderer;
import org.richfaces.renderkit.html.SelectionRendererContributor;
@@ -71,5 +72,126 @@
Object selection = component.getSelection();
}
+
+ public void testShouldAddToSelection() {
+
+ ClientSelection oldSelection = new ClientSelection();
+ oldSelection.addRange(new SelectionRange(10, 15));
+ oldSelection.addRange(new SelectionRange(16, 20));
+
+ ClientSelection newSelection = new ClientSelection();
+ newSelection.addRange(new SelectionRange(20, 40));
+
+ newSelection.setSelectionFlag(ClientSelection.FLAG_ALL);
+
+ for(int i = 0; i < 100; i++) {
+ assertTrue(
+ "Contributor was supposed to add " + i,
+ contributor.shouldAddToSelection(i, oldSelection, newSelection)
+ );
+ }
+
+
+ newSelection.setSelectionFlag(null);
+
+ for (int i = 0; i < 21; i++) {
+ assertFalse(
+ "Contributor wasn't supposed to add " + i,
+ contributor.shouldAddToSelection(i, oldSelection, newSelection)
+ );
+ }
+
+ for (int i = 21; i < 41; i++) {
+ assertTrue(
+ "Contributor was supposed to add " + i,
+ contributor.shouldAddToSelection(i, oldSelection, newSelection)
+ );
+ }
+
+ for (int i = 41; i < 100; i++) {
+ assertFalse(
+ "Contributor wasn't supposed to add " + i,
+ contributor.shouldAddToSelection(i, oldSelection, newSelection)
+ );
+ }
+
+
+ newSelection.setSelectionFlag(ClientSelection.FLAG_RESET);
+
+
+ for (int i = 0; i < 20; i++) {
+ assertFalse(
+ "Contributor wasn't supposed to add " + i,
+ contributor.shouldAddToSelection(i, oldSelection, newSelection)
+ );
+ }
+
+ for (int i = 21; i < 41; i++) {
+ assertTrue(
+ "Contributor was supposed to add " + i,
+ contributor.shouldAddToSelection(i, oldSelection, newSelection)
+ );
+ }
+
+ for (int i = 41; i < 100; i++) {
+ assertFalse(
+ "Contributor wasn't supposed to add " + i,
+ contributor.shouldAddToSelection(i, oldSelection, newSelection)
+ );
+ }
+
+
+
+ }
+
+
+ public void testShouldRemoveFromSelection() {
+ ClientSelection oldSelection = new ClientSelection();
+ oldSelection.addRange(new SelectionRange(10, 15));
+ oldSelection.addRange(new SelectionRange(16, 20));
+
+ ClientSelection newSelection = new ClientSelection();
+ newSelection.addRange(new SelectionRange(20, 40));
+
+ for (int i = 0; i < 10; i++) {
+ assertFalse(
+ "Contributor wasn't supposed to remove " + i,
+ contributor.shouldRemoveFromSelection(i, oldSelection, newSelection)
+ );
+ }
+ for (int i = 10; i < 20; i++) {
+ assertTrue(
+ "Contributor was supposed to remove " + i,
+ contributor.shouldRemoveFromSelection(i, oldSelection, newSelection)
+ );
+ }
+
+ for (int i = 21; i < 100; i++) {
+ assertFalse(
+ "Contributor wasn't supposed to remove " + i,
+ contributor.shouldRemoveFromSelection(i, oldSelection, newSelection)
+ );
+ }
+
+
+ newSelection.setSelectionFlag(ClientSelection.FLAG_ALL);
+
+ for (int i = 0; i < 100; i++) {
+ assertFalse(
+ "Contributor wasn't supposed to remove " + i,
+ contributor.shouldRemoveFromSelection(i, oldSelection, newSelection)
+ );
+ }
+
+ newSelection.setSelectionFlag(ClientSelection.FLAG_RESET);
+
+ for (int i = 0; i < 100; i++) {
+ assertFalse(
+ "Contributor wasn't supposed to remove " + i,
+ contributor.shouldRemoveFromSelection(i, oldSelection, newSelection)
+ );
+ }
+ }
+
}
Modified:
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 2007-06-12
18:08:46 UTC (rev 1147)
+++
trunk/sandbox/scrollable-grid/src/test/java/org/richfaces/model/selection/ClientSelectionTest.java 2007-06-12
18:23:59 UTC (rev 1148)
@@ -70,4 +70,24 @@
}
+ public void testSetSelectionFlag() {
+ clientSelection.setSelectionFlag(null);
+ assertFalse(clientSelection.isReset());
+ assertFalse(clientSelection.isSelectAll());
+
+ clientSelection.setSelectionFlag(ClientSelection.FLAG_ALL);
+ assertFalse(clientSelection.isReset());
+ assertTrue(clientSelection.isSelectAll());
+
+ clientSelection.setSelectionFlag(ClientSelection.FLAG_RESET);
+ assertTrue(clientSelection.isReset());
+ assertFalse(clientSelection.isSelectAll());
+
+ clientSelection.setSelectionFlag("zzzzzz");
+ assertFalse(clientSelection.isReset());
+ assertFalse(clientSelection.isSelectAll());
+
+ }
+
+
}