You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2018/05/08 11:32:43 UTC
[isis] branch master updated: ISIS-1944: Tree View: adds a model
for the collapse/expand state
This is an automated email from the ASF dual-hosted git repository.
ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git
The following commit(s) were added to refs/heads/master by this push:
new d0c124a ISIS-1944: Tree View: adds a model for the collapse/expand state
d0c124a is described below
commit d0c124aca88840935f1391313aa41102078f7cbe
Author: Andi Huber <ah...@apache.org>
AuthorDate: Tue May 8 13:32:36 2018 +0200
ISIS-1944: Tree View: adds a model for the collapse/expand state
Task-Url: https://issues.apache.org/jira/browse/ISIS-1944
---
.../org/apache/isis/applib/tree/LazyTreeNode.java | 36 +++++++---
.../java/org/apache/isis/applib/tree/TreeNode.java | 11 ++-
.../apache/isis/applib/tree/TreePath_Default.java | 41 +++++++----
.../org/apache/isis/applib/tree/TreeState.java | 32 +++++++++
.../apache/isis/applib/tree/TreeState_Default.java | 34 +++++++++
.../org/apache/isis/applib/tree/TreePathTest.java | 47 ++++++++++++
.../components/tree/IsisToWicketTreeAdapter.java | 84 ++++++++++++++++++++--
7 files changed, 254 insertions(+), 31 deletions(-)
diff --git a/core/applib/src/main/java/org/apache/isis/applib/tree/LazyTreeNode.java b/core/applib/src/main/java/org/apache/isis/applib/tree/LazyTreeNode.java
index 98ce49f..2446c21 100644
--- a/core/applib/src/main/java/org/apache/isis/applib/tree/LazyTreeNode.java
+++ b/core/applib/src/main/java/org/apache/isis/applib/tree/LazyTreeNode.java
@@ -29,18 +29,20 @@ import org.apache.isis.applib.internal.exceptions._Exceptions;
@Value(semanticsProviderName="org.apache.isis.core.metamodel.facets.value.treenode.TreeNodeValueSemanticsProvider")
public class LazyTreeNode<T> implements TreeNode<T> {
+ private final TreeState sharedState;
private final T value;
private final Class<? extends TreeAdapter<T>> treeAdapterClass;
private final _Lazy<TreeAdapter<T>> treeAdapter = _Lazy.of(this::newTreeAdapter);
private final _Lazy<TreePath> treePath = _Lazy.of(this::resolveTreePath);
- public static <T> TreeNode<T> of(T value, Class<? extends TreeAdapter<T>> treeAdapterClass) {
- return new LazyTreeNode<T>(value, treeAdapterClass);
+ public static <T> TreeNode<T> of(T value, Class<? extends TreeAdapter<T>> treeAdapterClass, TreeState sharedState) {
+ return new LazyTreeNode<T>(value, treeAdapterClass, sharedState);
}
- protected LazyTreeNode(T value, Class<? extends TreeAdapter<T>> treeAdapterClass) {
+ protected LazyTreeNode(T value, Class<? extends TreeAdapter<T>> treeAdapterClass, TreeState sharedState) {
this.value = Objects.requireNonNull(value);
this.treeAdapterClass = Objects.requireNonNull(treeAdapterClass);
+ this.sharedState = sharedState;
}
@Override
@@ -52,8 +54,7 @@ public class LazyTreeNode<T> implements TreeNode<T> {
public TreeNode<T> getParentIfAny() {
return treeAdapter().parentOf(getValue())
.map(this::toTreeNode)
- .orElse(null)
- ;
+ .orElse(null);
}
@Override
@@ -67,8 +68,7 @@ public class LazyTreeNode<T> implements TreeNode<T> {
return Stream.empty();
}
return treeAdapter().childrenOf(value)
- .map(this::toTreeNode)
- ;
+ .map(this::toTreeNode);
}
@Override
@@ -81,6 +81,23 @@ public class LazyTreeNode<T> implements TreeNode<T> {
return treePath.get();
}
+ @Override
+ public TreeState getTreeState() {
+ return sharedState;
+ }
+
+// @Override
+// public boolean isExpanded() {
+// // TODO Auto-generated method stub
+// return false;
+// }
+//
+// @Override
+// public void setExpanded(boolean expanded) {
+// // TODO Auto-generated method stub
+//
+// }
+
// -- HELPER
private TreeAdapter<T> newTreeAdapter() {
@@ -97,7 +114,7 @@ public class LazyTreeNode<T> implements TreeNode<T> {
}
private TreeNode<T> toTreeNode(T value){
- return of(value, getTreeAdapterClass());
+ return of(value, getTreeAdapterClass(), sharedState);
}
private TreePath resolveTreePath() {
@@ -116,8 +133,7 @@ public class LazyTreeNode<T> implements TreeNode<T> {
boolean found = parent.streamChildren()
.peek(__->indexOneBased.increment())
- .anyMatch(sibling->this.equals(sibling))
- ;
+ .anyMatch(sibling->this.equals(sibling));
if(!found) {
throw _Exceptions.unexpectedCodeReach();
diff --git a/core/applib/src/main/java/org/apache/isis/applib/tree/TreeNode.java b/core/applib/src/main/java/org/apache/isis/applib/tree/TreeNode.java
index 7ebb3ee..54f11d1 100644
--- a/core/applib/src/main/java/org/apache/isis/applib/tree/TreeNode.java
+++ b/core/applib/src/main/java/org/apache/isis/applib/tree/TreeNode.java
@@ -56,6 +56,14 @@ public interface TreeNode<T> {
public TreePath getPositionAsPath();
+ // -- COLLAPSE/EXPAND
+
+ public TreeState getTreeState();
+
+// public boolean isExpanded();
+//
+// public void setExpanded(boolean expanded);
+
// -- CONSTRUCTION
/**
@@ -65,7 +73,7 @@ public interface TreeNode<T> {
* @return new LazyTreeNode
*/
public static <T> TreeNode<T> lazy(T node, Class<? extends TreeAdapter<T>> treeAdapterClass) {
- return LazyTreeNode.of(node, treeAdapterClass);
+ return LazyTreeNode.of(node, treeAdapterClass, TreeState.rootCollapsed());
}
// -- PARENT NODE ITERATION
@@ -114,5 +122,6 @@ public interface TreeNode<T> {
// [ahuber] Implementation Note: a class rather than an instance, because otherwise
// the adapter would need to be serializable for Wicket's trees to work correctly.
public Class<? extends TreeAdapter<T>> getTreeAdapterClass();
+
}
diff --git a/core/applib/src/main/java/org/apache/isis/applib/tree/TreePath_Default.java b/core/applib/src/main/java/org/apache/isis/applib/tree/TreePath_Default.java
index 4851572..b692233 100644
--- a/core/applib/src/main/java/org/apache/isis/applib/tree/TreePath_Default.java
+++ b/core/applib/src/main/java/org/apache/isis/applib/tree/TreePath_Default.java
@@ -20,6 +20,8 @@ package org.apache.isis.applib.tree;
import java.util.Arrays;
import java.util.Objects;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
/**
* Package private mixin for TreePath.
@@ -28,6 +30,7 @@ class TreePath_Default implements TreePath {
private static final long serialVersionUID = 530511373409525896L;
private final int[] canonicalPath;
+ private final int hashCode;
TreePath_Default(int[] canonicalPath) {
Objects.requireNonNull(canonicalPath, "canonicalPath is required");
@@ -35,6 +38,7 @@ class TreePath_Default implements TreePath {
throw new IllegalArgumentException("canonicalPath must not be empty");
}
this.canonicalPath = canonicalPath;
+ this.hashCode = Arrays.hashCode(canonicalPath);
}
@Override
@@ -46,6 +50,23 @@ class TreePath_Default implements TreePath {
}
@Override
+ public TreePath getParentIfAny() {
+ if(isRoot()) {
+ return null;
+ }
+ final int[] newCanonicalPath = new int[canonicalPath.length-1];
+ System.arraycopy(canonicalPath, 0, newCanonicalPath, 0, canonicalPath.length-1);
+ return new TreePath_Default(newCanonicalPath);
+ }
+
+ @Override
+ public boolean isRoot() {
+ return canonicalPath.length==1;
+ }
+
+ // -- OBJECT CONTRACTS
+
+ @Override
public boolean equals(Object obj) {
if(obj instanceof TreePath_Default) {
final TreePath_Default other = (TreePath_Default) obj;
@@ -56,22 +77,14 @@ class TreePath_Default implements TreePath {
@Override
public int hashCode() {
- return canonicalPath.hashCode();
+ return hashCode;
}
-
- @Override
- public TreePath getParentIfAny() {
- if(isRoot()) {
- return null;
- }
- final int[] newCanonicalPath = new int[canonicalPath.length-1];
- System.arraycopy(canonicalPath, 0, newCanonicalPath, 0, canonicalPath.length-1);
- return new TreePath_Default(newCanonicalPath);
- }
-
+
@Override
- public boolean isRoot() {
- return canonicalPath.length==1;
+ public String toString() {
+ return "/" + IntStream.of(canonicalPath)
+ .mapToObj(i->""+i)
+ .collect(Collectors.joining("/"));
}
}
diff --git a/core/applib/src/main/java/org/apache/isis/applib/tree/TreeState.java b/core/applib/src/main/java/org/apache/isis/applib/tree/TreeState.java
new file mode 100644
index 0000000..1f194f0
--- /dev/null
+++ b/core/applib/src/main/java/org/apache/isis/applib/tree/TreeState.java
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.isis.applib.tree;
+
+import java.io.Serializable;
+import java.util.Set;
+
+public interface TreeState extends Serializable {
+
+ public static TreeState rootCollapsed() {
+ return new TreeState_Default();
+ }
+
+ public Set<TreePath> getExpandedNodePaths();
+
+}
diff --git a/core/applib/src/main/java/org/apache/isis/applib/tree/TreeState_Default.java b/core/applib/src/main/java/org/apache/isis/applib/tree/TreeState_Default.java
new file mode 100644
index 0000000..319d80c
--- /dev/null
+++ b/core/applib/src/main/java/org/apache/isis/applib/tree/TreeState_Default.java
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.isis.applib.tree;
+
+import java.util.HashSet;
+import java.util.Set;
+
+class TreeState_Default implements TreeState {
+ private static final long serialVersionUID = 7971539034663543462L;
+
+ private final Set<TreePath> expandedNodes = new HashSet<>();
+
+ @Override
+ public Set<TreePath> getExpandedNodePaths() {
+ return expandedNodes;
+ }
+
+}
diff --git a/core/applib/src/test/java/org/apache/isis/applib/tree/TreePathTest.java b/core/applib/src/test/java/org/apache/isis/applib/tree/TreePathTest.java
new file mode 100644
index 0000000..95edb9a
--- /dev/null
+++ b/core/applib/src/test/java/org/apache/isis/applib/tree/TreePathTest.java
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.isis.applib.tree;
+
+import static org.junit.Assert.*;
+
+import org.hamcrest.Matchers;
+import org.junit.Test;
+
+public class TreePathTest {
+
+ @Test
+ public void rootConstructor() {
+ final TreePath treePath = TreePath.root();
+ assertThat(treePath.isRoot(), Matchers.is(true));
+ assertThat(treePath.toString(), Matchers.is("/0"));
+ }
+
+ @Test
+ public void samePathsShouldBeEqual() {
+ final TreePath treePath1 = TreePath.of(0, 1, 2, 3);
+ final TreePath treePath2 = TreePath.of(0, 1, 2, 3);
+ assertEquals(treePath1, treePath2);
+ assertEquals(treePath1.hashCode(), treePath2.hashCode());
+ assertEquals(treePath1.isRoot(), treePath2.isRoot());
+ assertEquals(treePath1.toString(), treePath2.toString());
+
+ assertThat(treePath1.toString(), Matchers.is("/0/1/2/3"));
+ }
+
+}
diff --git a/core/viewer-wicket-ui/src/main/java/org/apache/isis/viewer/wicket/ui/components/tree/IsisToWicketTreeAdapter.java b/core/viewer-wicket-ui/src/main/java/org/apache/isis/viewer/wicket/ui/components/tree/IsisToWicketTreeAdapter.java
index 7aceaf2..a5ab062 100644
--- a/core/viewer-wicket-ui/src/main/java/org/apache/isis/viewer/wicket/ui/components/tree/IsisToWicketTreeAdapter.java
+++ b/core/viewer-wicket-ui/src/main/java/org/apache/isis/viewer/wicket/ui/components/tree/IsisToWicketTreeAdapter.java
@@ -5,7 +5,9 @@ import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;
+import java.util.Set;
import java.util.function.Function;
+import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.resource.spi.IllegalStateException;
@@ -15,6 +17,7 @@ import org.apache.isis.applib.internal.functions._Functions;
import org.apache.isis.applib.tree.TreeAdapter;
import org.apache.isis.applib.tree.TreeNode;
import org.apache.isis.applib.tree.TreePath;
+import org.apache.isis.applib.tree.TreeState;
import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
import org.apache.isis.core.metamodel.adapter.oid.RootOid;
import org.apache.isis.core.runtime.system.context.IsisContext;
@@ -32,17 +35,18 @@ import org.apache.wicket.extensions.markup.html.repeater.tree.ITreeProvider;
import org.apache.wicket.extensions.markup.html.repeater.tree.NestedTree;
import org.apache.wicket.extensions.markup.html.repeater.tree.Node;
import org.apache.wicket.extensions.markup.html.repeater.tree.theme.WindowsTheme;
+import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
class IsisToWicketTreeAdapter {
public static EntityTree adapt(String id, ValueModel valueModel) {
- return new EntityTree(id, toITreeProvider( valueModel ));
+ return new EntityTree(id, toITreeProvider(valueModel), toIModelRepresentingCollapseExpandState(valueModel));
}
-
+
public static EntityTree adapt(String id, ScalarModel scalarModel) {
- return new EntityTree(id, toITreeProvider( scalarModel ));
+ return new EntityTree(id, toITreeProvider(scalarModel), toIModelRepresentingCollapseExpandState(scalarModel));
}
// -- RENDERING
@@ -54,8 +58,11 @@ class IsisToWicketTreeAdapter {
private static final long serialVersionUID = 1L;
- public EntityTree(String id, ITreeProvider<TreeModel> provider) {
- super(id, provider);
+ public EntityTree(
+ String id,
+ ITreeProvider<TreeModel> provider,
+ TreeExpansionModel collapseExpandState) {
+ super(id, provider, collapseExpandState);
add(new WindowsTheme()); // TODO not required if Isis provides it's own css styles for tree-nodes
}
@@ -118,6 +125,16 @@ class IsisToWicketTreeAdapter {
}
+ /**
+ * To utilize the custom TreeExpansionModel for deciding a node's collapse/expand state we
+ * override this method.
+ */
+ @Override
+ public State getState(TreeModel t) {
+ final TreeExpansionModel treeExpansionModel = (TreeExpansionModel) getModel();
+ return treeExpansionModel.contains(t.getTreePath()) ? State.EXPANDED : State.COLLAPSED;
+ }
+
}
// -- HELPER
@@ -261,12 +278,67 @@ class IsisToWicketTreeAdapter {
private static ITreeProvider<TreeModel> toITreeProvider(ModelAbstract<ObjectAdapter> model) {
final TreeNode treeNode = (TreeNode) model.getObject().getObject();
- final TreeModelTreeAdapter wrappingTreeAdapter = new TreeModelTreeAdapter(treeNode.getTreeAdapterClass());
+ final Class<? extends TreeAdapter> treeAdapterClass = treeNode.getTreeAdapterClass();
+ final TreeModelTreeAdapter wrappingTreeAdapter = new TreeModelTreeAdapter(treeAdapterClass);
+
return new TreeModelTreeProvider(
wrappingTreeAdapter.wrap(treeNode.getValue(), treeNode.getPositionAsPath()),
wrappingTreeAdapter);
}
+ /**
+ *
+ * @param model
+ * @return Wicket's model for collapse/expand state
+ */
+ @SuppressWarnings({ "rawtypes" })
+ private static TreeExpansionModel toIModelRepresentingCollapseExpandState(ModelAbstract<ObjectAdapter> model) {
+
+ final TreeNode treeNode = (TreeNode) model.getObject().getObject();
+ final TreeState treeState = treeNode.getTreeState();
+ return TreeExpansionModel.of(treeState.getExpandedNodePaths());
+ }
+
+ /**
+ * Wicket's model for collapse/expand state
+ */
+ private static class TreeExpansionModel extends AbstractReadOnlyModel<Set<TreeModel>> {
+ private static final long serialVersionUID = 648152234030889164L;
+
+ public static TreeExpansionModel of(Set<TreePath> expandedTreePaths) {
+ return new TreeExpansionModel(expandedTreePaths);
+ }
+
+ public boolean contains(TreePath treePath) {
+ return expandedTreePaths.contains(treePath);
+ }
+
+ private final Set<TreePath> expandedTreePaths;
+ private final Set<TreeModel> expandedNodes; //TODO [ahuber] possibly not used within this readonly model
+
+ private TreeExpansionModel(Set<TreePath> expandedTreePaths) {
+ this.expandedTreePaths = expandedTreePaths;
+ this.expandedNodes = expandedTreePaths.stream()
+ .map(tPath->new TreeModel(null, tPath))
+ .collect(Collectors.toSet());
+ }
+
+ @Override
+ public Set<TreeModel> getObject() {
+ return expandedNodes;
+ }
+
+ @Override
+ public String toString() {
+ return "{" + expandedNodes.stream()
+ .map(TreeModel::getTreePath)
+ .map(TreePath::toString)
+ .collect(Collectors.joining(", ")) + "}";
+ }
+
+
+ }
+
private static class LoadableDetachableEntityModel extends LoadableDetachableModel<TreeModel> {
private static final long serialVersionUID = 1L;
--
To stop receiving notification emails like this one, please contact
ahuber@apache.org.