Updated Branches: refs/heads/master 463f1379d -> b0118c114
show how to keep expansion state by id and in session Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/b0118c11 Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/b0118c11 Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/b0118c11 Branch: refs/heads/master Commit: b0118c11479d407800a4675c523d640017ddf541 Parents: 463f137 Author: Sven Meier <[email protected]> Authored: Fri Feb 10 15:23:23 2012 +0100 Committer: Sven Meier <[email protected]> Committed: Fri Feb 10 15:23:23 2012 +0100 ---------------------------------------------------------------------- .../apache/wicket/examples/tree/FooExpansion.java | 184 +++++++++++++++ .../apache/wicket/examples/tree/InverseSet.java | 143 ----------- .../org/apache/wicket/examples/tree/TreePage.java | 41 +--- .../wicket/examples/tree/InverseSetTest.java | 77 ------ 4 files changed, 196 insertions(+), 249 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/b0118c11/wicket-examples/src/main/java/org/apache/wicket/examples/tree/FooExpansion.java ---------------------------------------------------------------------- diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/tree/FooExpansion.java b/wicket-examples/src/main/java/org/apache/wicket/examples/tree/FooExpansion.java new file mode 100644 index 0000000..adbe4d7 --- /dev/null +++ b/wicket-examples/src/main/java/org/apache/wicket/examples/tree/FooExpansion.java @@ -0,0 +1,184 @@ +/* + * 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.wicket.examples.tree; + +import java.io.Serializable; +import java.util.Collection; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +import org.apache.wicket.MetaDataKey; +import org.apache.wicket.Session; + +/** + * Example of a custom expansion state: + * <ul> + * <li>expanded {@link Foo}s are identified by their id</li> + * <li>efficient expansion of all {@link Foo}</li> + * <li>state is stored in the session</li> + * </ul> + * + * @author svenmeier + */ +public class FooExpansion implements Set<Foo>, Serializable +{ + private static final long serialVersionUID = 1L; + + private static MetaDataKey<FooExpansion> KEY = new MetaDataKey<FooExpansion>() + { + private static final long serialVersionUID = 1L; + }; + + private Set<String> ids = new HashSet<String>(); + + private boolean inverse; + + public void expandAll() + { + ids.clear(); + + inverse = true; + } + + public void collapseAll() + { + ids.clear(); + + inverse = false; + } + + @Override + public boolean add(Foo foo) + { + if (inverse) + { + return ids.remove(foo.getId()); + } + else + { + return ids.add(foo.getId()); + } + } + + @Override + public boolean remove(Object o) + { + Foo foo = (Foo)o; + + if (inverse) + { + return ids.add(foo.getId()); + } + else + { + return ids.remove(foo.getId()); + } + } + + @Override + public boolean contains(Object o) + { + Foo foo = (Foo)o; + + if (inverse) + { + return !ids.contains(foo.getId()); + } + else + { + return ids.contains(foo.getId()); + } + } + + @Override + public void clear() + { + throw new UnsupportedOperationException(); + } + + @Override + public int size() + { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isEmpty() + { + throw new UnsupportedOperationException(); + } + + @Override + public <A> A[] toArray(A[] a) + { + throw new UnsupportedOperationException(); + } + + @Override + public Iterator<Foo> iterator() + { + throw new UnsupportedOperationException(); + } + + @Override + public Object[] toArray() + { + throw new UnsupportedOperationException(); + } + + @Override + public boolean containsAll(Collection<?> c) + { + throw new UnsupportedOperationException(); + } + + @Override + public boolean addAll(Collection<? extends Foo> c) + { + throw new UnsupportedOperationException(); + } + + @Override + public boolean retainAll(Collection<?> c) + { + throw new UnsupportedOperationException(); + } + + @Override + public boolean removeAll(Collection<?> c) + { + throw new UnsupportedOperationException(); + } + + /** + * Get the expansion for the session. + * + * @return expansion + */ + public static FooExpansion get() + { + FooExpansion expansion = Session.get().getMetaData(KEY); + if (expansion == null) + { + expansion = new FooExpansion(); + + Session.get().setMetaData(KEY, expansion); + } + return expansion; + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/b0118c11/wicket-examples/src/main/java/org/apache/wicket/examples/tree/InverseSet.java ---------------------------------------------------------------------- diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/tree/InverseSet.java b/wicket-examples/src/main/java/org/apache/wicket/examples/tree/InverseSet.java deleted file mode 100644 index 74f0258..0000000 --- a/wicket-examples/src/main/java/org/apache/wicket/examples/tree/InverseSet.java +++ /dev/null @@ -1,143 +0,0 @@ -/* - * 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.wicket.examples.tree; - -import java.util.Collection; -import java.util.Iterator; -import java.util.Set; - -import org.apache.wicket.model.IDetachable; - -/** - * An inverse set. - * - * @author svenmeier - */ -public class InverseSet<T> implements Set<T>, IDetachable -{ - - private static final long serialVersionUID = 1L; - - private Set<T> set; - - /** - * Create a full set. - * - * @param set - * the contained set - */ - public InverseSet(Set<T> set) - { - this.set = set; - } - - public void detach() - { - if (set instanceof IDetachable) - { - ((IDetachable)set).detach(); - } - } - - public boolean isEmpty() - { - return !set.isEmpty(); - } - - public boolean contains(Object o) - { - return !set.contains(o); - } - - public boolean add(T t) - { - return set.remove(t); - } - - @SuppressWarnings("unchecked") - public boolean remove(Object o) - { - return set.add((T)o); - } - - public boolean addAll(Collection<? extends T> ts) - { - boolean changed = false; - - for (T t : ts) - { - changed |= set.remove(t); - } - - return changed; - } - - public boolean containsAll(Collection<?> cs) - { - for (Object c : cs) - { - if (set.contains(c)) - { - return false; - } - } - return true; - } - - @SuppressWarnings("unchecked") - public boolean removeAll(Collection<?> cs) - { - boolean changed = false; - - for (Object c : cs) - { - changed |= set.add((T)c); - } - - return changed; - } - - public int size() - { - throw new UnsupportedOperationException(); - } - - public void clear() - { - throw new UnsupportedOperationException(); - } - - public Iterator<T> iterator() - { - throw new UnsupportedOperationException(); - } - - public boolean retainAll(Collection<?> c) - { - throw new UnsupportedOperationException(); - } - - public Object[] toArray() - { - throw new UnsupportedOperationException(); - } - - public <S> S[] toArray(S[] a) - { - throw new UnsupportedOperationException(); - } -} http://git-wip-us.apache.org/repos/asf/wicket/blob/b0118c11/wicket-examples/src/main/java/org/apache/wicket/examples/tree/TreePage.java ---------------------------------------------------------------------- diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/tree/TreePage.java b/wicket-examples/src/main/java/org/apache/wicket/examples/tree/TreePage.java index da6a7bf..b37863d 100644 --- a/wicket-examples/src/main/java/org/apache/wicket/examples/tree/TreePage.java +++ b/wicket-examples/src/main/java/org/apache/wicket/examples/tree/TreePage.java @@ -38,7 +38,6 @@ import org.apache.wicket.examples.tree.content.SelectableFolderContent; import org.apache.wicket.extensions.markup.html.repeater.tree.AbstractTree; import org.apache.wicket.extensions.markup.html.repeater.tree.theme.HumanTheme; import org.apache.wicket.extensions.markup.html.repeater.tree.theme.WindowsTheme; -import org.apache.wicket.extensions.markup.html.repeater.util.ProviderSubset; import org.apache.wicket.markup.ComponentTag; import org.apache.wicket.markup.head.IHeaderResponse; import org.apache.wicket.markup.html.form.Button; @@ -47,7 +46,6 @@ import org.apache.wicket.markup.html.form.DropDownChoice; import org.apache.wicket.markup.html.form.Form; import org.apache.wicket.markup.html.link.Link; import org.apache.wicket.model.AbstractReadOnlyModel; -import org.apache.wicket.model.IDetachable; import org.apache.wicket.model.IModel; import org.apache.wicket.model.PropertyModel; @@ -65,8 +63,6 @@ public abstract class TreePage extends WicketExamplePage private FooProvider provider = new FooProvider(); - private Set<Foo> state = new ProviderSubset<Foo>(provider); - private Content content; private List<Content> contents; @@ -83,7 +79,7 @@ public abstract class TreePage extends WicketExamplePage Form<Void> form = new Form<Void>("form"); add(form); - tree = createTree(provider, newStateModel()); + tree = createTree(provider, new FooExpansionModel()); tree.add(new Behavior() { private static final long serialVersionUID = 1L; @@ -134,8 +130,7 @@ public abstract class TreePage extends WicketExamplePage @Override public void onClick() { - ((IDetachable)state).detach(); - state = new InverseSet<Foo>(new ProviderSubset<Foo>(provider)); + FooExpansion.get().expandAll(); } }); @@ -146,8 +141,7 @@ public abstract class TreePage extends WicketExamplePage @Override public void onClick() { - ((IDetachable)state).detach(); - state = new ProviderSubset<Foo>(provider); + FooExpansion.get().collapseAll(); } }); @@ -162,26 +156,6 @@ public abstract class TreePage extends WicketExamplePage }); } - private IModel<Set<Foo>> newStateModel() - { - return new AbstractReadOnlyModel<Set<Foo>>() - { - private static final long serialVersionUID = 1L; - - @Override - public Set<Foo> getObject() - { - return state; - } - - @Override - public void detach() - { - ((IDetachable)state).detach(); - } - }; - } - protected abstract AbstractTree<Foo> createTree(FooProvider provider, IModel<Set<Foo>> state); private List<Content> initContents() @@ -232,4 +206,13 @@ public abstract class TreePage extends WicketExamplePage { return content.newContentComponent(id, tree, model); } + + private class FooExpansionModel extends AbstractReadOnlyModel<Set<Foo>> + { + @Override + public Set<Foo> getObject() + { + return FooExpansion.get(); + } + } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/wicket/blob/b0118c11/wicket-examples/src/test/java/org/apache/wicket/examples/tree/InverseSetTest.java ---------------------------------------------------------------------- diff --git a/wicket-examples/src/test/java/org/apache/wicket/examples/tree/InverseSetTest.java b/wicket-examples/src/test/java/org/apache/wicket/examples/tree/InverseSetTest.java deleted file mode 100644 index c0788a6..0000000 --- a/wicket-examples/src/test/java/org/apache/wicket/examples/tree/InverseSetTest.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * 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.wicket.examples.tree; - -import java.util.HashSet; - -import junit.framework.Assert; - -import org.apache.wicket.model.IDetachable; -import org.junit.Test; - -/** - * Test for {@link InverseSet}. - * - * @author svenmeier - */ -public class InverseSetTest extends Assert -{ - private TestSet set; - - /** - * Construct. - */ - public InverseSetTest() - { - set = new TestSet(); - set.add("A"); - } - - /** - * Test contains. - */ - @Test - public void contains() - { - InverseSet<String> inverse = new InverseSet<String>(set); - assertFalse(inverse.contains("A")); - assertTrue(inverse.contains("B")); - - inverse.remove("B"); - assertFalse(inverse.contains("A")); - assertFalse(inverse.contains("B")); - - inverse.add("A"); - assertTrue(inverse.contains("A")); - assertFalse(inverse.contains("B")); - - inverse.detach(); - assertTrue(set.detached); - } - - private class TestSet extends HashSet<String> implements IDetachable - { - private static final long serialVersionUID = 1L; - - boolean detached = false; - - public void detach() - { - detached = true; - } - } -} \ No newline at end of file
