Author: nbelaevski
Date: 2010-11-01 12:42:07 -0400 (Mon, 01 Nov 2010)
New Revision: 19854
Added:
trunk/examples/iteration-demo/src/main/java/org/richfaces/demo/TracingSet.java
Modified:
trunk/examples/iteration-demo/src/main/java/org/richfaces/demo/TreeBean.java
trunk/examples/iteration-demo/src/main/webapp/tree.xhtml
trunk/ui/iteration/api/src/main/java/org/richfaces/event/TreeSelectionEvent.java
trunk/ui/iteration/ui/src/main/java/org/richfaces/component/AbstractTree.java
trunk/ui/iteration/ui/src/main/java/org/richfaces/model/SequenceRowKey.java
trunk/ui/iteration/ui/src/main/java/org/richfaces/renderkit/TreeRendererBase.java
Log:
https://jira.jboss.org/browse/RF-9315
- selection refactoring
Added: trunk/examples/iteration-demo/src/main/java/org/richfaces/demo/TracingSet.java
===================================================================
--- trunk/examples/iteration-demo/src/main/java/org/richfaces/demo/TracingSet.java
(rev 0)
+++
trunk/examples/iteration-demo/src/main/java/org/richfaces/demo/TracingSet.java 2010-11-01
16:42:07 UTC (rev 19854)
@@ -0,0 +1,117 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.richfaces.demo;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+import java.util.Set;
+
+import org.richfaces.log.LogFactory;
+import org.richfaces.log.Logger;
+
+import com.google.common.collect.ForwardingIterator;
+import com.google.common.collect.ForwardingSet;
+
+/**
+ * @author Nick Belaevski
+ *
+ */
+public class TracingSet<E> extends ForwardingSet<E> implements Serializable
{
+
+ private static final long serialVersionUID = 267329344963751893L;
+
+ private static final Logger LOGGER = LogFactory.getLogger(TracingSet.class);
+
+ private class TracingIterator extends ForwardingIterator<E> {
+
+ private final Iterator<E> itr = backingCollection.iterator();
+
+ private E lastObject;
+
+ @Override
+ protected Iterator<E> delegate() {
+ return itr;
+ }
+
+ @Override
+ public E next() {
+ try {
+ lastObject = super.next();
+ return lastObject;
+ } catch (NoSuchElementException e) {
+ lastObject = null;
+ throw e;
+ }
+ }
+
+ @Override
+ public void remove() {
+ LOGGER.info("TracingSet.TracingIterator.remove() " + lastObject);
+ super.remove();
+ }
+ }
+
+ private Set<E> backingCollection = new HashSet<E>();
+
+ @Override
+ protected Set<E> delegate() {
+ return backingCollection;
+ }
+
+ @Override
+ public boolean removeAll(Collection<?> collection) {
+ LOGGER.info("TracingSet.removeAll() " + collection);
+ return super.removeAll(collection);
+ }
+
+ @Override
+ public boolean add(E element) {
+ LOGGER.info("TracingSet.add() " + element);
+ return super.add(element);
+ }
+
+ @Override
+ public boolean remove(Object object) {
+ LOGGER.info("TracingSet.remove() " + object);
+ return super.remove(object);
+ }
+
+ @Override
+ public boolean addAll(Collection<? extends E> collection) {
+ LOGGER.info("TracingSet.addAll() " + collection);
+ return super.addAll(collection);
+ }
+
+ @Override
+ public void clear() {
+ LOGGER.info("TracingSet.clear()");
+ super.clear();
+ }
+
+ @Override
+ public Iterator<E> iterator() {
+ return new TracingIterator();
+ }
+}
Modified: trunk/examples/iteration-demo/src/main/java/org/richfaces/demo/TreeBean.java
===================================================================
---
trunk/examples/iteration-demo/src/main/java/org/richfaces/demo/TreeBean.java 2010-11-01
16:23:04 UTC (rev 19853)
+++
trunk/examples/iteration-demo/src/main/java/org/richfaces/demo/TreeBean.java 2010-11-01
16:42:07 UTC (rev 19854)
@@ -56,7 +56,7 @@
private Object nodeData;
- private Collection<Object> selection;
+ private Collection<Object> selection = new TracingSet<Object>();
@PostConstruct
public void init() {
Modified: trunk/examples/iteration-demo/src/main/webapp/tree.xhtml
===================================================================
--- trunk/examples/iteration-demo/src/main/webapp/tree.xhtml 2010-11-01 16:23:04 UTC (rev
19853)
+++ trunk/examples/iteration-demo/src/main/webapp/tree.xhtml 2010-11-01 16:42:07 UTC (rev
19854)
@@ -24,7 +24,8 @@
</h:form>
<h:form id="form">
- <it:tree id="tree" nodeType="#{node.parent == null ?
'rootNode': 'childNode'}" var="node"
value="#{treeBean.rootNodes}"
selectionType="#{treeBean.selectionType}"
toggleType="#{treeBean.toggleType}">
+ <it:tree id="tree" nodeType="#{node.parent == null ?
'rootNode': 'childNode'}" var="node"
value="#{treeBean.rootNodes}"
+ selectionType="#{treeBean.selectionType}"
toggleType="#{treeBean.toggleType}"
selection="#{treeBean.selection}">
<it:treeNode type="rootNode">
<h:panelGroup id="rootNodeGroup">
Root node: #{node.data} -
Modified:
trunk/ui/iteration/api/src/main/java/org/richfaces/event/TreeSelectionEvent.java
===================================================================
---
trunk/ui/iteration/api/src/main/java/org/richfaces/event/TreeSelectionEvent.java 2010-11-01
16:23:04 UTC (rev 19853)
+++
trunk/ui/iteration/api/src/main/java/org/richfaces/event/TreeSelectionEvent.java 2010-11-01
16:42:07 UTC (rev 19854)
@@ -22,7 +22,6 @@
package org.richfaces.event;
import java.util.Collection;
-import java.util.HashSet;
import javax.faces.component.UIComponent;
import javax.faces.event.FacesEvent;
@@ -36,15 +35,15 @@
private static final long serialVersionUID = 6292604445872458007L;
- private Collection<Object> addedKeys = new HashSet<Object>();
+ private Collection<Object> oldSelection;
- private Collection<Object> removedKeys = new HashSet<Object>();
+ private Collection<Object> newSelection;
- public TreeSelectionEvent(UIComponent component, Collection<Object> addedKeys,
Collection<Object> removedKeys) {
+ public TreeSelectionEvent(UIComponent component, Collection<Object>
oldSelection, Collection<Object> newSelection) {
super(component);
-
- this.addedKeys = addedKeys;
- this.removedKeys = removedKeys;
+
+ this.oldSelection = oldSelection;
+ this.newSelection = newSelection;
}
@Override
@@ -57,11 +56,12 @@
((TreeSelectionListener) listener).processSelection(this);
}
- public Collection<Object> getAddedKeys() {
- return addedKeys;
+ public Collection<Object> getOldSelection() {
+ return oldSelection;
}
- public Collection<Object> getRemovedKeys() {
- return removedKeys;
+ public Collection<Object> getNewSelection() {
+ return newSelection;
}
+
}
Modified: trunk/ui/iteration/ui/src/main/java/org/richfaces/component/AbstractTree.java
===================================================================
---
trunk/ui/iteration/ui/src/main/java/org/richfaces/component/AbstractTree.java 2010-11-01
16:23:04 UTC (rev 19853)
+++
trunk/ui/iteration/ui/src/main/java/org/richfaces/component/AbstractTree.java 2010-11-01
16:42:07 UTC (rev 19854)
@@ -66,6 +66,7 @@
import org.richfaces.renderkit.MetaComponentRenderer;
import com.google.common.base.Predicate;
+import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;
/**
@@ -313,14 +314,18 @@
} else if (event instanceof TreeSelectionEvent) {
TreeSelectionEvent selectionEvent = (TreeSelectionEvent) event;
- Collection<Object> selection = getSelection();
+ final Collection<Object> newSelection =
selectionEvent.getNewSelection();
+
+ Collection<Object> selectionCollection = getSelection();
- for (Object addedKey: selectionEvent.getAddedKeys()) {
- selection.add(addedKey);
- }
+ Iterables.removeIf(selectionCollection, new Predicate<Object>() {
+ public boolean apply(Object input) {
+ return !newSelection.contains(input);
+ };
+ });
- for (Object removedKey: selectionEvent.getRemovedKeys()) {
- selection.remove(removedKey);
+ if (!newSelection.isEmpty()) {
+ Iterables.addAll(selectionCollection, newSelection);
}
}
}
Modified: trunk/ui/iteration/ui/src/main/java/org/richfaces/model/SequenceRowKey.java
===================================================================
--- trunk/ui/iteration/ui/src/main/java/org/richfaces/model/SequenceRowKey.java 2010-11-01
16:23:04 UTC (rev 19853)
+++ trunk/ui/iteration/ui/src/main/java/org/richfaces/model/SequenceRowKey.java 2010-11-01
16:42:07 UTC (rev 19854)
@@ -74,4 +74,9 @@
}
return true;
}
+
+ @Override
+ public String toString() {
+ return getClass().getName() + Arrays.toString(simpleKeys);
+ }
}
Modified:
trunk/ui/iteration/ui/src/main/java/org/richfaces/renderkit/TreeRendererBase.java
===================================================================
---
trunk/ui/iteration/ui/src/main/java/org/richfaces/renderkit/TreeRendererBase.java 2010-11-01
16:23:04 UTC (rev 19853)
+++
trunk/ui/iteration/ui/src/main/java/org/richfaces/renderkit/TreeRendererBase.java 2010-11-01
16:42:07 UTC (rev 19854)
@@ -29,10 +29,9 @@
import java.io.IOException;
import java.util.Collection;
-import java.util.HashSet;
+import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
-import java.util.Set;
import javax.faces.component.UIComponent;
import javax.faces.component.UINamingContainer;
@@ -54,6 +53,7 @@
import org.richfaces.log.RichfacesLogger;
import com.google.common.base.Strings;
+import com.google.common.collect.Sets;
/**
@@ -304,18 +304,20 @@
Collection<Object> selection = tree.getSelection();
- Set<Object> addedKeys = new HashSet<Object>(2);
- Set<Object> removedKeys = new HashSet<Object>(2);
-
+ Collection<Object> newSelection = null;
+
if (selectionRowKey == null) {
- removedKeys.addAll(selection);
- } else if (!selection.contains(selectionRowKey)) {
- addedKeys.add(selectionRowKey);
- removedKeys.addAll(selection);
+ if (!selection.isEmpty()) {
+ newSelection = Collections.emptySet();
+ }
+ } else {
+ if (!selection.contains(selectionRowKey)) {
+ newSelection = Collections.singleton(selectionRowKey);
+ }
}
-
- if (!removedKeys.isEmpty() || !addedKeys.isEmpty()) {
- new TreeSelectionEvent(component, addedKeys, removedKeys).queue();
+
+ if (newSelection != null) {
+ new TreeSelectionEvent(component, Sets.newHashSet(selection),
newSelection).queue();
}
PartialViewContext pvc = context.getPartialViewContext();