[richfaces-svn-commits] JBoss Rich Faces SVN: r1038 - in trunk/sandbox/scrollable-grid/src/main: java/org/richfaces/model and 2 other directories.

richfaces-svn-commits at lists.jboss.org richfaces-svn-commits at lists.jboss.org
Wed Jun 6 08:04:15 EDT 2007


Author: maksimkaszynski
Date: 2007-06-06 08:04:15 -0400 (Wed, 06 Jun 2007)
New Revision: 1038

Added:
   trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/selection/ClientSelection.java
   trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/selection/ClientSelectionConverter.java
   trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/selection/SelectionRange.java
Modified:
   trunk/sandbox/scrollable-grid/src/main/config/component/scrollable-grid.xml
   trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/GridDataModel.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:
selection first steps

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 11:28:57 UTC (rev 1037)
+++ trunk/sandbox/scrollable-grid/src/main/config/component/scrollable-grid.xml	2007-06-06 12:04:15 UTC (rev 1038)
@@ -107,4 +107,9 @@
 		<template>org/richfaces/scrollable-grid-footer-cell.jspx</template>
 	</renderer>
 	
+	<converter generate="false">
+		<classname>org.richfaces.model.selection.ClientSelectionConverter</classname>
+		<forclass>org.richfaces.model.selection.ClientSelection</forclass>
+	</converter>
+	
 </components>		
\ No newline at end of file

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 11:28:57 UTC (rev 1037)
+++ trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/GridDataModel.java	2007-06-06 12:04:15 UTC (rev 1038)
@@ -27,6 +27,8 @@
 	
 	private Serializable rowKey;
 
+	private int rowIndex = Integer.MIN_VALUE;
+	
 	private Map mapping;
 	
 	public Object getRowKey() {

Added: 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	                        (rev 0)
+++ trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/selection/ClientSelection.java	2007-06-06 12:04:15 UTC (rev 1038)
@@ -0,0 +1,45 @@
+/**
+ * 
+ */
+package org.richfaces.model.selection;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * @author Maksim Kaszynski
+ *
+ */
+public class ClientSelection implements Serializable{
+	
+	private static final long serialVersionUID = 5855157282287053681L;
+
+	private List ranges = new ArrayList();
+	
+	public ClientSelection() {
+	}
+
+	
+	public void addRange(SelectionRange range) {
+		ranges.add(range);
+	}
+	
+	public boolean isSelected(int i) {
+		boolean result = false;
+		Iterator iterator = ranges.iterator();
+		while (iterator.hasNext() && !result) {
+			result |= ((SelectionRange) iterator.next()).within(i);
+		}
+		return result;
+	}
+	
+	public List getRanges() {
+		return ranges;
+	}
+	
+	public void addIndex(int i) {
+		
+	}
+}

Added: trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/selection/ClientSelectionConverter.java
===================================================================
--- trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/selection/ClientSelectionConverter.java	                        (rev 0)
+++ trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/selection/ClientSelectionConverter.java	2007-06-06 12:04:15 UTC (rev 1038)
@@ -0,0 +1,87 @@
+/**
+ * 
+ */
+package org.richfaces.model.selection;
+
+import java.util.Iterator;
+
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+import javax.faces.convert.Converter;
+import javax.faces.convert.ConverterException;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.richfaces.component.UIScrollableGrid;
+
+/**
+ * @author Maksim Kaszynski
+ *
+ */
+public class ClientSelectionConverter implements Converter {
+
+	public static final String CONVERTER_ID = 
+		ClientSelectionConverter.class.getName();
+	
+	private static final Log log = 
+		LogFactory.getLog(ClientSelectionConverter.class);
+	
+	public Object getAsObject(FacesContext context, UIComponent component, String value) {
+		return getAsSelection(context, (UIScrollableGrid) component, value);
+	}
+
+	public String getAsString(FacesContext context, UIComponent component, Object value) {
+		
+		StringBuffer buffer = new StringBuffer();
+		Iterator iterator = ((ClientSelection) value).getRanges().iterator();
+		
+		while(iterator.hasNext()) {
+			
+			SelectionRange selectionRange = 
+				(SelectionRange) iterator.next();
+			
+			buffer
+				.append(selectionRange.getStartIndex())
+				.append(",")
+				.append(selectionRange.getEndIndex())
+				.append(";");
+		}
+		
+		return buffer.toString();
+	}
+	
+	private ClientSelection getAsSelection(FacesContext context, UIScrollableGrid grid, String stringSelection) {
+		
+		final ClientSelection clientSelection = new ClientSelection();
+		
+		String [] selections = stringSelection.split(";");
+		
+		for (int i = 0; i < selections.length; i++) {
+			
+			String range = selections[i];
+			
+			if (range.length() == 0) {
+				continue;
+			}
+			String [] rng = range.split(",");
+			
+			try {
+				int fi = Integer.parseInt(rng[0]);
+				int il = Integer.parseInt(rng[1]);
+				
+				if (log.isDebugEnabled()) {
+					log.debug("Parsed range " + fi + " " + il);
+				}
+				
+				clientSelection.addRange(new SelectionRange(fi, il));
+				
+			} catch (NumberFormatException e) {
+				throw new ConverterException(e);
+			}
+		}
+
+		
+		return clientSelection;
+	}
+
+}

Added: 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	                        (rev 0)
+++ trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/selection/SelectionRange.java	2007-06-06 12:04:15 UTC (rev 1038)
@@ -0,0 +1,40 @@
+/**
+ * 
+ */
+package org.richfaces.model.selection;
+
+/**
+ * @author Maksim Kaszynski
+ *
+ */
+public class SelectionRange {
+	private int startIndex = -1;;
+	private int endIndex = -1;
+	
+	public SelectionRange(int startIndex, int endIndex) {
+		super();
+		this.startIndex = startIndex;
+		this.endIndex = endIndex;
+	}
+	
+	public int getStartIndex() {
+		return startIndex;
+	}
+	
+	public void setStartIndex(int startIndex) {
+		this.startIndex = startIndex;
+	}
+	
+	public int getEndIndex() {
+		return endIndex;
+	}
+	
+	public void setEndIndex(int endIndex) {
+		this.endIndex = endIndex;
+	}
+	
+	public boolean within(int index) {
+		return startIndex <= index && endIndex >= index;
+	}
+	
+}

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 11:28:57 UTC (rev 1037)
+++ trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/selection/SimpleSelection.java	2007-06-06 12:04:15 UTC (rev 1038)
@@ -22,6 +22,10 @@
 		return keys.add(rowKey);
 	}
 	
+	public boolean removeKey(Serializable rowKey) {
+		return keys.remove(rowKey);
+	}
+	
 	public Iterator getKeys() {
 		return keys.iterator();
 	}

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 11:28:57 UTC (rev 1037)
+++ trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/renderkit/html/ScrollableGridBaseRenderer.java	2007-06-06 12:04:15 UTC (rev 1038)
@@ -1,14 +1,18 @@
 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.component.UIOutput;
 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;
@@ -25,6 +29,10 @@
 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.ClientSelectionConverter;
+import org.richfaces.model.selection.Selection;
+import org.richfaces.model.selection.SimpleSelection;
 import org.richfaces.renderkit.CompositeRenderer;
 import org.richfaces.utils.TemplateLoader;
 
@@ -53,6 +61,9 @@
 	
 	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(){
@@ -170,7 +181,7 @@
 		}
 		
 	};
-	
+
 	private final DataVisitor rowsRenderer = new DataVisitor(){
 
 		public void process(FacesContext context, Object rowKey, Object argument) throws IOException {
@@ -486,9 +497,130 @@
 					grid.queueEvent(new AjaxEvent(grid));
 			}
 		}
+		
+		
 	}
 	
+	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;




More information about the richfaces-svn-commits mailing list