Author: nbelaevski
Date: 2010-11-01 15:09:23 -0400 (Mon, 01 Nov 2010)
New Revision: 19858
Added:
trunk/ui/iteration/api/src/main/java/org/richfaces/model/TreeDataModel.java
trunk/ui/iteration/ui/src/main/java/org/richfaces/model/ExtendedTreeDataModelImpl.java
trunk/ui/iteration/ui/src/main/java/org/richfaces/model/SwingTreeNodeDataModelImpl.java
Removed:
trunk/ui/iteration/ui/src/main/java/org/richfaces/model/TreeDataModelImpl.java
Modified:
trunk/ui/iteration/ui/src/main/java/org/richfaces/component/AbstractTree.java
trunk/ui/iteration/ui/src/main/java/org/richfaces/renderkit/TreeEncoderBase.java
trunk/ui/iteration/ui/src/main/java/org/richfaces/renderkit/TreeEncoderFull.java
Log:
https://jira.jboss.org/browse/RF-9609
Added: trunk/ui/iteration/api/src/main/java/org/richfaces/model/TreeDataModel.java
===================================================================
--- trunk/ui/iteration/api/src/main/java/org/richfaces/model/TreeDataModel.java
(rev 0)
+++ trunk/ui/iteration/api/src/main/java/org/richfaces/model/TreeDataModel.java 2010-11-01
19:09:23 UTC (rev 19858)
@@ -0,0 +1,49 @@
+/*
+ * 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.model;
+
+import java.util.Iterator;
+
+/**
+ * @author Nick Belaevski
+ *
+ */
+//TODO - add necessary methods for tree adaptors support
+public interface TreeDataModel<E> {
+
+ public Object getRowKey();
+
+ public void setRowKey(Object rowKey);
+
+ public boolean isDataAvailable();
+
+ public E getData();
+
+ public Iterator<Object> getChildrenRowKeysIterator(Object rowKey);
+
+ public Object getParentRowKey(Object rowKey);
+
+ public Object getWrappedData();
+
+ public void setWrappedData(Object data);
+
+}
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
18:02:02 UTC (rev 19857)
+++
trunk/ui/iteration/ui/src/main/java/org/richfaces/component/AbstractTree.java 2010-11-01
19:09:23 UTC (rev 19858)
@@ -43,6 +43,7 @@
import javax.faces.event.ExceptionQueuedEventContext;
import javax.faces.event.FacesEvent;
import javax.faces.event.PhaseId;
+import javax.swing.tree.TreeNode;
import org.ajax4jsf.model.DataComponentState;
import org.ajax4jsf.model.ExtendedDataModel;
@@ -62,7 +63,9 @@
import org.richfaces.event.TreeSelectionListener;
import org.richfaces.event.TreeToggleEvent;
import org.richfaces.event.TreeToggleListener;
-import org.richfaces.model.TreeDataModelImpl;
+import org.richfaces.model.ExtendedTreeDataModelImpl;
+import org.richfaces.model.SwingTreeNodeDataModelImpl;
+import org.richfaces.model.TreeDataModel;
import org.richfaces.renderkit.MetaComponentRenderer;
import com.google.common.base.Predicate;
@@ -210,12 +213,9 @@
getStateHelper().put(PropertyKeys.expanded, this.getClientId(getFacesContext()),
newValue);
}
- /* (non-Javadoc)
- * @see org.richfaces.component.UIDataAdaptor#createExtendedDataModel()
- */
@Override
protected ExtendedDataModel<?> createExtendedDataModel() {
- TreeDataModelImpl model = new TreeDataModelImpl();
+ ExtendedTreeDataModelImpl<?> model = new
ExtendedTreeDataModelImpl<TreeNode>(new SwingTreeNodeDataModelImpl());
model.setWrappedData(getValue());
return model;
}
@@ -235,8 +235,9 @@
return converter;
}
- public Iterator<Object> getChildrenIterator(FacesContext faces, Object rowKey)
{
- return ((TreeDataModelImpl) getExtendedDataModel()).getChildrenIterator(faces,
rowKey);
+ public Iterator<Object> getChildrenRowKeysIterator(FacesContext faces, Object
rowKey) {
+ TreeDataModel<?> dataModel = (TreeDataModel<?>)
getExtendedDataModel();
+ return dataModel.getChildrenRowKeysIterator(rowKey);
}
public AbstractTreeNode getTreeNodeComponent() {
Copied:
trunk/ui/iteration/ui/src/main/java/org/richfaces/model/ExtendedTreeDataModelImpl.java
(from rev 19848,
trunk/ui/iteration/ui/src/main/java/org/richfaces/model/TreeDataModelImpl.java)
===================================================================
---
trunk/ui/iteration/ui/src/main/java/org/richfaces/model/ExtendedTreeDataModelImpl.java
(rev 0)
+++
trunk/ui/iteration/ui/src/main/java/org/richfaces/model/ExtendedTreeDataModelImpl.java 2010-11-01
19:09:23 UTC (rev 19858)
@@ -0,0 +1,131 @@
+/*
+ * 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.model;
+
+import java.util.Iterator;
+
+import javax.faces.context.FacesContext;
+
+import org.ajax4jsf.model.DataVisitResult;
+import org.ajax4jsf.model.DataVisitor;
+import org.ajax4jsf.model.ExtendedDataModel;
+import org.ajax4jsf.model.Range;
+import org.richfaces.component.TreeRange;
+
+/**
+ * @author Nick Belaevski
+ *
+ */
+public class ExtendedTreeDataModelImpl<E> extends ExtendedDataModel<E>
implements TreeDataModel<E> {
+
+ private TreeDataModel<E> wrappedModel;
+
+ public ExtendedTreeDataModelImpl(TreeDataModel<E> wrappedModel) {
+ super();
+ this.wrappedModel = wrappedModel;
+ }
+
+ public boolean isDataAvailable() {
+ return wrappedModel.isDataAvailable();
+ }
+
+ public E getData() {
+ return wrappedModel.getData();
+ }
+
+ public Iterator<Object> getChildrenRowKeysIterator(Object rowKey) {
+ return wrappedModel.getChildrenRowKeysIterator(rowKey);
+ }
+
+ public Object getParentRowKey(Object rowKey) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void setRowKey(Object key) {
+ wrappedModel.setRowKey(key);
+ }
+
+ @Override
+ public Object getRowKey() {
+ return wrappedModel.getRowKey();
+ }
+
+ protected void walk(FacesContext context, DataVisitor visitor, Range range, Object
argument, Iterator<Object> keysIterator) {
+ while (keysIterator.hasNext()) {
+ Object object = (Object) keysIterator.next();
+
+ DataVisitResult visitResult = visitor.process(context, object, argument);
+ if (visitResult == DataVisitResult.CONTINUE) {
+ if (((TreeRange) range).shouldIterateChildren(object)) {
+ Iterator<Object> childrenIterator =
getChildrenRowKeysIterator(object);
+ walk(context, visitor, range, argument, childrenIterator);
+ }
+ }
+ }
+ }
+
+ @Override
+ public void walk(FacesContext context, DataVisitor visitor, Range range, Object
argument) {
+ TreeRange treeRange = (TreeRange) range;
+ if (treeRange.shouldIterateChildren(null)) {
+ Iterator<Object> iterator = getChildrenRowKeysIterator(null);
+ walk(context, visitor, range, argument, iterator);
+ }
+ }
+
+ @Override
+ public boolean isRowAvailable() {
+ return wrappedModel.isDataAvailable();
+ }
+
+ @Override
+ public int getRowCount() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public E getRowData() {
+ return wrappedModel.getData();
+ }
+
+ @Override
+ public int getRowIndex() {
+ throw new UnsupportedOperationException();
+ }
+
+ public void setRowIndex(int rowIndex) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Object getWrappedData() {
+ return wrappedModel.getWrappedData();
+ }
+
+ @Override
+ public void setWrappedData(Object data) {
+ wrappedModel.setWrappedData(data);
+ }
+
+
+}
Added:
trunk/ui/iteration/ui/src/main/java/org/richfaces/model/SwingTreeNodeDataModelImpl.java
===================================================================
---
trunk/ui/iteration/ui/src/main/java/org/richfaces/model/SwingTreeNodeDataModelImpl.java
(rev 0)
+++
trunk/ui/iteration/ui/src/main/java/org/richfaces/model/SwingTreeNodeDataModelImpl.java 2010-11-01
19:09:23 UTC (rev 19858)
@@ -0,0 +1,179 @@
+/*
+ * 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.model;
+
+import java.util.Collection;
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+import javax.swing.tree.TreeNode;
+
+import com.google.common.base.Predicates;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Iterators;
+
+/**
+ * @author Nick Belaevski
+ *
+ */
+public class SwingTreeNodeDataModelImpl implements TreeDataModel<TreeNode> {
+
+ /**
+ * @author Nick Belaevski
+ *
+ */
+ private final class FakeRootNode implements TreeNode {
+ public boolean isLeaf() {
+ return !wrappedData.isEmpty();
+ }
+
+ public TreeNode getParent() {
+ return null;
+ }
+
+ public int getIndex(TreeNode node) {
+ if (wrappedData == null) {
+ return -1;
+ }
+
+ return Iterables.indexOf(wrappedData, Predicates.equalTo(node));
+ }
+
+ public int getChildCount() {
+ if (wrappedData == null) {
+ return 0;
+ }
+
+ return wrappedData.size();
+ }
+
+ public TreeNode getChildAt(int childIndex) {
+ if (wrappedData == null) {
+ throw new NoSuchElementException(String.valueOf(childIndex));
+ }
+
+ return Iterables.get(wrappedData, childIndex);
+ }
+
+ public boolean getAllowsChildren() {
+ return true;
+ }
+
+ public Enumeration<?> children() {
+ if (wrappedData == null) {
+ return Iterators.asEnumeration(Iterators.emptyIterator());
+ }
+
+ return Iterators.asEnumeration(wrappedData.iterator());
+ }
+ }
+
+ private static final SequenceRowKey<Integer> EMPTY_SEQUENCE_ROW_KEY = new
SequenceRowKey<Integer>();
+
+ private Collection<TreeNode> wrappedData = null;
+
+ private TreeNode fakeRootNode = new FakeRootNode();
+
+ private TreeNode selectedNode;
+
+ private SequenceRowKey<Integer> selectedRowKey;
+
+ private Iterator<TreeNode> findChildren(SequenceRowKey<Integer>
compositeKey) {
+ TreeNode treeNode = findNode(compositeKey);
+
+ if (treeNode == null) {
+ return Iterators.emptyIterator();
+ }
+
+ return Iterators.forEnumeration((Enumeration<TreeNode>)
treeNode.children());
+ }
+
+ private TreeNode findNode(SequenceRowKey<Integer> compositeKey) {
+ if (compositeKey == null) {
+ return null;
+ }
+
+ TreeNode result = fakeRootNode;
+
+ for (Integer simpleKey : compositeKey.getSimpleKeys()) {
+ int idx = simpleKey.intValue();
+
+ if (idx < result.getChildCount()) {
+ result = result.getChildAt(idx);
+ } else {
+ result = null;
+ break;
+ }
+ }
+
+ return result;
+ }
+
+ public void setRowKey(Object key) {
+ this.selectedRowKey = (SequenceRowKey<Integer>) key;
+ this.selectedNode = findNode(selectedRowKey);
+ }
+
+ public Object getRowKey() {
+ return selectedRowKey;
+ }
+
+ private SequenceRowKey<Integer> castKeyAndWrapNull(Object rowKey) {
+ if (rowKey == null) {
+ return EMPTY_SEQUENCE_ROW_KEY;
+ }
+
+ return (SequenceRowKey<Integer>) rowKey;
+ }
+
+ public Iterator<Object> getChildrenRowKeysIterator(Object rowKey) {
+ SequenceRowKey<Integer> sequenceKey = castKeyAndWrapNull(rowKey);
+ Iterator<TreeNode> itr = findChildren(sequenceKey);
+
+ return new SequenceRowKeyIterator<TreeNode>(sequenceKey, itr);
+ }
+
+ public TreeNode getData() {
+ if (!isDataAvailable()) {
+ throw new IllegalArgumentException();
+ }
+
+ return selectedNode;
+ }
+
+ public Object getParentRowKey(Object rowKey) {
+ throw new UnsupportedOperationException();
+ }
+
+ public boolean isDataAvailable() {
+ return selectedNode != null;
+ }
+
+ public Object getWrappedData() {
+ return wrappedData;
+ }
+
+ public void setWrappedData(Object wrappedData) {
+ this.wrappedData = (Collection<TreeNode>) wrappedData;
+ }
+}
Deleted: trunk/ui/iteration/ui/src/main/java/org/richfaces/model/TreeDataModelImpl.java
===================================================================
---
trunk/ui/iteration/ui/src/main/java/org/richfaces/model/TreeDataModelImpl.java 2010-11-01
18:02:02 UTC (rev 19857)
+++
trunk/ui/iteration/ui/src/main/java/org/richfaces/model/TreeDataModelImpl.java 2010-11-01
19:09:23 UTC (rev 19858)
@@ -1,184 +0,0 @@
-/*
- * 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.model;
-
-import java.util.Enumeration;
-import java.util.Iterator;
-import java.util.List;
-
-import javax.faces.context.FacesContext;
-import javax.swing.tree.TreeNode;
-
-import org.ajax4jsf.model.DataVisitResult;
-import org.ajax4jsf.model.DataVisitor;
-import org.ajax4jsf.model.ExtendedDataModel;
-import org.ajax4jsf.model.Range;
-import org.richfaces.component.TreeRange;
-
-import com.google.common.collect.Iterators;
-
-/**
- * @author Nick Belaevski
- *
- */
-public class TreeDataModelImpl extends ExtendedDataModel<TreeNode> {
-
- private static final SequenceRowKey<Integer> EMPTY_SEQUENCE_ROW_KEY = new
SequenceRowKey<Integer>();
-
- private SwingTreeNodeImpl<?> rootNode;
-
- private TreeNode selectedNode;
-
- private SequenceRowKey<Integer> selectedRowKey;
-
- private Iterator<TreeNode> findChildren(SequenceRowKey<Integer>
compositeKey) {
- TreeNode treeNode = findNode(compositeKey);
-
- if (treeNode == null) {
- return Iterators.emptyIterator();
- }
-
- return Iterators.forEnumeration((Enumeration<TreeNode>)
treeNode.children());
- }
-
- private TreeNode findNode(SequenceRowKey<Integer> compositeKey) {
- if (compositeKey == null) {
- return null;
- }
-
- TreeNode result = rootNode;
-
- for (Integer simpleKey : compositeKey.getSimpleKeys()) {
- int idx = simpleKey.intValue();
-
- if (idx < result.getChildCount()) {
- result = result.getChildAt(idx);
- } else {
- result = null;
- break;
- }
- }
-
- return result;
- }
-
- @Override
- public void setRowKey(Object key) {
- this.selectedRowKey = (SequenceRowKey<Integer>) key;
- this.selectedNode = findNode(selectedRowKey);
- }
-
- @Override
- public Object getRowKey() {
- return selectedRowKey;
- }
-
- @Override
- public boolean isRowAvailable() {
- return selectedNode != null;
- }
-
- /* (non-Javadoc)
- * @see javax.faces.model.DataModel#getRowCount()
- */
- @Override
- public int getRowCount() {
- // TODO Auto-generated method stub
- return 0;
- }
-
- @Override
- public TreeNode getRowData() {
- return selectedNode;
- }
-
- /* (non-Javadoc)
- * @see javax.faces.model.DataModel#getRowIndex()
- */
- @Override
- public int getRowIndex() {
- // TODO Auto-generated method stub
- return 0;
- }
-
- /* (non-Javadoc)
- * @see javax.faces.model.DataModel#setRowIndex(int)
- */
- @Override
- public void setRowIndex(int rowIndex) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public Object getWrappedData() {
- return rootNode.getChildrenList();
- }
-
- @Override
- public void setWrappedData(Object data) {
- this.rootNode = new SwingTreeNodeImpl((List<TreeNode>) data);
- }
-
- private SequenceRowKey<Integer> castKeyAndWrapNull(Object rowKey) {
- if (rowKey == null) {
- return EMPTY_SEQUENCE_ROW_KEY;
- }
-
- return (SequenceRowKey<Integer>) rowKey;
- }
-
- public Iterator<Object> getChildrenIterator(FacesContext faces, Object rowKey)
{
- SequenceRowKey<Integer> sequenceKey = castKeyAndWrapNull(rowKey);
- Iterator<TreeNode> itr = findChildren(sequenceKey);
-
- return new SequenceRowKeyIterator<TreeNode>(sequenceKey, itr);
- }
-
- protected void walk(FacesContext context, DataVisitor visitor, Range range, Object
argument, Iterator<Object> keysIterator) {
- while (keysIterator.hasNext()) {
- Object object = (Object) keysIterator.next();
-
- DataVisitResult visitResult = visitor.process(context, object, argument);
- if (visitResult == DataVisitResult.CONTINUE) {
- if (((TreeRange) range).shouldIterateChildren(object)) {
- Iterator<Object> childrenIterator =
getChildrenIterator(context, object);
- walk(context, visitor, range, argument, childrenIterator);
- }
- }
- }
- }
-
- /* (non-Javadoc)
- * @see org.ajax4jsf.model.ExtendedDataModel#walk(javax.faces.context.FacesContext,
org.ajax4jsf.model.DataVisitor, org.ajax4jsf.model.Range, java.lang.Object)
- */
- @Override
- public void walk(FacesContext context, DataVisitor visitor, Range range, Object
argument) {
- // TODO Auto-generated method stub
-
- if (((TreeRange) range).shouldIterateChildren(null)) {
- Iterator<Object> iterator = getChildrenIterator(context, null);
- walk(context, visitor, range, argument, iterator);
- }
- }
-
-}
Modified:
trunk/ui/iteration/ui/src/main/java/org/richfaces/renderkit/TreeEncoderBase.java
===================================================================
---
trunk/ui/iteration/ui/src/main/java/org/richfaces/renderkit/TreeEncoderBase.java 2010-11-01
18:02:02 UTC (rev 19857)
+++
trunk/ui/iteration/ui/src/main/java/org/richfaces/renderkit/TreeEncoderBase.java 2010-11-01
19:09:23 UTC (rev 19858)
@@ -104,7 +104,7 @@
boolean iterateChildren = treeRange.shouldIterateChildren(rowKey);
if (iterateChildren) {
- encodeTree(tree.getChildrenIterator(context, rowKey));
+ encodeTree(tree.getChildrenRowKeysIterator(context, rowKey));
}
QueuedData data = queuedData.removeLast();
Modified:
trunk/ui/iteration/ui/src/main/java/org/richfaces/renderkit/TreeEncoderFull.java
===================================================================
---
trunk/ui/iteration/ui/src/main/java/org/richfaces/renderkit/TreeEncoderFull.java 2010-11-01
18:02:02 UTC (rev 19857)
+++
trunk/ui/iteration/ui/src/main/java/org/richfaces/renderkit/TreeEncoderFull.java 2010-11-01
19:09:23 UTC (rev 19858)
@@ -38,7 +38,7 @@
public void encode() throws java.io.IOException {
Object initialRowKey = tree.getRowKey();
try {
- encodeTree(tree.getChildrenIterator(context, null));
+ encodeTree(tree.getChildrenRowKeysIterator(context, null));
} finally {
try {
tree.setRowKey(context, initialRowKey);