r1035 - in lutinjaxx/trunk/jaxx-core/src: main/java/jaxx/runtime/swing/navigation test/java/jaxx/runtime/swing/navigation
Author: chemit Date: 2008-11-26 01:06:42 +0000 (Wed, 26 Nov 2008) New Revision: 1035 Modified: lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/runtime/swing/navigation/NavigationTreeModel.java lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/runtime/swing/navigation/NavigationTreeModelBuilder.java lutinjaxx/trunk/jaxx-core/src/test/java/jaxx/runtime/swing/navigation/NavigationTreeModelTest.java Log: improve and refactor navigation model to make findNode more efficent + can now defined the navigation path separator. Modified: lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/runtime/swing/navigation/NavigationTreeModel.java =================================================================== --- lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/runtime/swing/navigation/NavigationTreeModel.java 2008-11-25 09:42:25 UTC (rev 1034) +++ lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/runtime/swing/navigation/NavigationTreeModel.java 2008-11-26 01:06:42 UTC (rev 1035) @@ -13,8 +13,7 @@ import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreeNode; -import java.lang.reflect.InvocationTargetException; -import java.util.Stack; +import java.util.StringTokenizer; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -30,17 +29,181 @@ /** to use log facility, just put in your code: log.info(\"...\"); */ static private final Log log = LogFactory.getLog(NavigationTreeModel.class); - private static final long serialVersionUID = 1L; + static private final long serialVersionUID = 1L; - /** la représentation d'un noeud dans le model {@link NavigationTreeModel} */ - public static class NavigationTreeNode extends DefaultMutableTreeNode { + /** the separator char used to produce the navigation path of a node. */ + protected final String navigationPathSeparator; + public NavigationTreeModel(TreeNode root, String navigationPathSeparator) { + super(root); + this.navigationPathSeparator = navigationPathSeparator; + } + + @Override + public NavigationTreeNode getRoot() { + return (NavigationTreeNode) super.getRoot(); + } + + /** + * Search from the root node a node named by his fully path (concatenation of nodes + * {@link NavigationTreeNode#navigationPath} valued separated by dot. + * <p/> + * Example : + * <p/> + * <pre>$root.child1.leaf1</pre> + * + * @param path the fully path of the searched node. + * @return the node matching the fully context from the root node, or <code>null</code> if not find. + */ + public NavigationTreeNode findNode(String path) { + return findNode(getRoot(), path, (Pattern) null); + } + + /** + * Apply first the regex pattern to obtain the searched node fi the given <code>regex</code> is not null. + * <p/> + * Search then from the root node a node named by his fully path (concatenation of nodes + * {@link NavigationTreeNode#navigationPath} valued separated by {@link #navigationPathSeparator}. + * <p/> + * <p/> + * Example : + * <p/> + * <pre>$root.child1.leaf1</pre> + * + * @param path the fully path of the searched node. + * @param regex a optional regex to apply to path before searching + * @return the node matching the fully context from the root node, or <code>null</code> if not found. + */ + public NavigationTreeNode findNode(String path, String regex) { + return findNode(getRoot(), path, regex); + } + + /** + * Apply first the regex pattern to obtain the searched node. + * <p/> + * Search then from the root node a node named by his fully path (concatenation of nodes + * {@link NavigationTreeNode#navigationPath} valued separated by {@link #navigationPathSeparator}. + * <p/> + * Example : + * <p/> + * <pre>$root.child1.leaf1</pre> + * + * @param path the fully path of the searched node. + * @param regex a optional regex to apply to path before searching + * @return the node matching the fully context from the root node, or <code>null</code> if not found. + */ + public NavigationTreeNode findNode(String path, Pattern regex) { + return findNode(getRoot(), path, regex); + } + + + /** + * Search from a given root node a node named by his fully path (concatenation of nodes + * {@link NavigationTreeNode#navigationPath} valued separated by {@link #navigationPathSeparator}. + * + * @param root root node to be used + * @param path the fully path of the searched node. + * @return the node matching the fully context from the given root node, or <code>null</code> if not found. + */ + public NavigationTreeNode findNode(NavigationTreeNode root, String path) { + return findNode(root, path, (Pattern) null); + } + + /** + * Apply first the regex pattern to obtain the searched node. + * <p/> + * Search then from a given root node a node named by his fully path (concatenation of nodes) + * {@link NavigationTreeNode#navigationPath} valued separated by {@link #navigationPathSeparator}. + * + * @param root root node to be used + * @param path the fully path of the searched node. + * @param regex a previous regex to apply to path : must have a matches + * @return the node matching the fully context from the given root node, or <code>null</code> if not found. + */ + public NavigationTreeNode findNode(NavigationTreeNode root, String path, String regex) { + return findNode(root, path, regex == null ? null : Pattern.compile(regex)); + } + + /** + * Apply first the regex pattern to obtain the searched node. + * <p/> + * Search then from a given root node a node named by his fully path (concatenation of nodes + * {@link NavigationTreeNode#navigationPath} valued separated by {@link #navigationPathSeparator}. + * + * @param root root node to be used + * @param path the fully path of the searched node. + * @param regex a previous regex to apply to path : must have a matches + * @return the node matching the fully context from the given root node, or <code>null</code> if not found. + */ + public NavigationTreeNode findNode(NavigationTreeNode root, String path, Pattern regex) { + if (regex != null) { + Matcher matcher = regex.matcher(path); + if (!matcher.matches() || matcher.groupCount() < 1) { + log.warn("no matching regex " + regex + " to " + path); + return null; + } + path = matcher.group(1); + log.info("matching regex " + regex + " : " + path); + } + StringTokenizer stk = new StringTokenizer(path, navigationPathSeparator); + NavigationTreeNode result = root; + // pas the first token (matches the root node) + if (root.isRoot() && stk.hasMoreTokens()) { + String rootPath = stk.nextToken(); + if (!rootPath.equals(root.getNavigationPath())) { + return null; + } + } + while (stk.hasMoreTokens()) { + result = result.getChild(stk.nextToken()); + } + return result; + } + + + /** + * Obtain the associated bean value from context corresponding to node from given navigation path. + * + * @param context the context where to seek value + * @param navigationPath the current context path of the node + * @return the value associated in context with the given navigation path + */ + public Object getJAXXContextValue(JAXXContext context, String navigationPath) { + Object result; + NavigationTreeNode node = findNode(navigationPath, (Pattern) null); + result = getJAXXContextValue(context, node); + return result; + } + + /** + * Obtain the associated bean value from context corresponding to node + * + * @param context the context where to seek value + * @param node the current node + * @return the value associated in context with the given node. + */ + public Object getJAXXContextValue(JAXXContext context, NavigationTreeNode node) { + if (node == null) { + return null; + //fixme should throw a NPE exception + //throw new NullPointerException("node can not be null"); + } + return node.getJAXXContextValue(context); + } + + /** + * la représentation d'un noeud dans le modele {@link NavigationTreeModel} + * + * @author chemit + */ + public class NavigationTreeNode extends DefaultMutableTreeNode { + private static final long serialVersionUID = 1L; /** pour representer le context du noeud. */ - protected String context; + protected String navigationPath; - /** the JAXXObject class associated with this node */ + /** the JAXXObject class associated with this node (can be null) */ protected Class<? extends JAXXObject> jaxxClass; /** the JAXXAction class associated with this node and will be put in ui context */ @@ -49,13 +212,15 @@ /** the definition of the JAXXContext entry associated to this node, if null will seek in parent */ protected JAXXContextEntryDef jaxxContextEntryDef; + /** jxPath to process to obtain real value associated from context with the node (can be null) */ protected String jaxxContextEntryPath; + /** renderer of the node */ protected NodeRenderer renderer; public NavigationTreeNode(Object renderer, Object jaxxContextEntryDef, - String context, + String navigationPath, Class<? extends JAXXObject> jaxxClass, Class<? extends JAXXAction> jaxxActionClass) { super(renderer); @@ -64,13 +229,11 @@ ((NodeRenderer) renderer).setNode(this); } else if (renderer instanceof String) { // nothing special to be done - } else { - if (renderer != null) { - // wrong renderer type - throw new IllegalArgumentException("to define a renderer, must be a String (simple libelle) or a " + NodeRenderer.class + ", but was " + renderer); - } + } else if (renderer != null) { + // wrong renderer type + throw new IllegalArgumentException("to define a renderer, must be a String (simple libelle) or a " + NodeRenderer.class + ", but was " + renderer); } - this.context = context; + this.navigationPath = navigationPath; this.jaxxClass = jaxxClass; this.jaxxActionClass = jaxxActionClass; @@ -78,18 +241,16 @@ this.jaxxContextEntryDef = ((JAXXContextEntryDef) jaxxContextEntryDef); } else if (jaxxContextEntryDef instanceof String) { this.jaxxContextEntryPath = (String) jaxxContextEntryDef; - } else { - if (jaxxContextEntryDef != null) { - // wront context definition type - throw new IllegalArgumentException("to define a context link, must be a String (jxpath) or a " + JAXXContextEntryDef.class + ", but was " + jaxxContextEntryDef); - } + } else if (jaxxContextEntryDef != null) { + // wrong context definition type + throw new IllegalArgumentException("to define a context link, must be a String (jxpath) or a " + JAXXContextEntryDef.class + ", but was " + jaxxContextEntryDef); } } public NavigationTreeNode(Object renderer, JAXXContextEntryDef jaxxContextEntryDef, String jaxxContextEntryPath, - String context, + String navigationPath, Class<? extends JAXXObject> jaxxClass, Class<? extends JAXXAction> jaxxActionClass) { super(renderer); @@ -98,25 +259,23 @@ ((NodeRenderer) renderer).setNode(this); } else if (renderer instanceof String) { // nothing special to be done - } else { - if (renderer != null) { - // wrong renderer type - throw new IllegalArgumentException("to define a renderer, must be a String (simple libelle) or a " + NodeRenderer.class + ", but was " + renderer); - } + } else if (renderer != null) { + // wrong renderer type + throw new IllegalArgumentException("to define a renderer, must be a String (simple libelle) or a " + NodeRenderer.class + ", but was " + renderer); } - this.context = context; + this.navigationPath = navigationPath; this.jaxxClass = jaxxClass; this.jaxxActionClass = jaxxActionClass; this.jaxxContextEntryDef = jaxxContextEntryDef; this.jaxxContextEntryPath = jaxxContextEntryPath; } - public String getContext() { - return context; + public String getNavigationPath() { + return navigationPath; } - public void setContext(String context) { - this.context = context; + public void setNavigationPath(String navigationPath) { + this.navigationPath = navigationPath; } public Class<? extends JAXXObject> getJaxxClass() { @@ -143,13 +302,21 @@ this.jaxxContextEntryDef = jaxxContextEntryDef; } + public String getJaxxContextEntryPath() { + return jaxxContextEntryPath; + } + + public void setJaxxContextEntryPath(String jaxxContextEntryPath) { + this.jaxxContextEntryPath = jaxxContextEntryPath; + } + /** @return the fully context pathof the node from the root node to this. */ public String getContextPath() { TreeNode[] path = getPath(); StringBuilder sb = new StringBuilder(); for (TreeNode treeNode : path) { NavigationTreeNode myNode = (NavigationTreeNode) treeNode; - sb.append('.').append(myNode.getContext()); + sb.append(navigationPathSeparator).append(myNode.getNavigationPath()); } return sb.substring(1); } @@ -165,13 +332,13 @@ } /** - * @param context the name of the {@link #context} to be matched in the cild of this node. - * @return the child of this node with given {@link #context} value. + * @param navigationPath the name of the {@link #navigationPath} to be matched in the cild of this node. + * @return the child of this node with given {@link # navigationPath} value. */ - public NavigationTreeNode getChild(String context) { + public NavigationTreeNode getChild(String navigationPath) { for (int i = 0, max = getChildCount(); i < max; i++) { NavigationTreeNode son = getChildAt(i); - if (context.equals(son.getContext())) { + if (navigationPath.equals(son.getNavigationPath())) { return son; } } @@ -179,48 +346,16 @@ } /** - * @return the first ancestor with a none null {@link #jaxxContextEntryDef} - * or <code>null</code> if none find.. - */ - protected NavigationTreeNode getFirstAncestorWithDef() { - if (jaxxContextEntryDef != null) { - return this; - } - return getParent() == null ? null : getParent().getFirstAncestorWithDef(); - } - - protected NavigationTreeNode findNode(Stack<String> stack) { - if (log.isDebugEnabled()) { - log.debug(context + " : enter with " + stack); - } - if (stack.isEmpty()) { - return this; - } - // next context to search - String currentPath = stack.pop(); - if (getContext().equals(currentPath) && currentPath.startsWith("$")) { - // this node matchs (only do this for root node) - return findNode(stack); - } - // find the next matching son - NavigationTreeNode son = getChild(currentPath); - return son == null ? null : son.findNode(stack); - } - - /** * Obtain the associated bean value from context corresponding to node * * @param context the context to seek * @return the value associated in context with the given context path - * @throws InvocationTargetException todo - * @throws NoSuchMethodException todo - * @throws IllegalAccessException todo */ - public Object getJAXXContextValue(JAXXContext context) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException { + public Object getJAXXContextValue(JAXXContext context) { Object result; if (getJaxxContextEntryDef() != null && jaxxContextEntryPath == null) { - // the node maps directly a value in context, with no jxpath resolving + // the node maps directly a value in context, with no jxpath resolving result = getJaxxContextEntryDef().getContextValue(context); return result; } @@ -234,7 +369,6 @@ } Object parentBean = parentNode.getJaxxContextEntryDef().getContextValue(context); - //Object parentBean = parentNode.getJAXXContextValue(context); if (parentBean == null) { // must be an error no bean found @@ -281,6 +415,17 @@ return result; } + /** + * @return the first ancestor with a none null {@link #jaxxContextEntryDef} + * or <code>null</code> if none find.. + */ + protected NavigationTreeNode getFirstAncestorWithDef() { + if (jaxxContextEntryDef != null) { + return this; + } + return getParent() == null ? null : getParent().getFirstAncestorWithDef(); + } + protected String computeJXPath(String expr, NavigationTreeNode parentNode) { if (parentNode == this) { // reach the parent limit node, return the expr computed @@ -337,162 +482,4 @@ } } - @Override - public NavigationTreeNode getRoot() { - return (NavigationTreeNode) super.getRoot(); - } - - - public NavigationTreeModel(TreeNode root) { - super(root); - } - - public NavigationTreeModel(TreeNode root, boolean asksAllowsChildren) { - super(root, asksAllowsChildren); - } - - /** - * Search from the root node a node named by his fully path (concatenation of nodes - * {@link NavigationTreeNode#context} valued separated by dot. - * <p/> - * Example : - * <p/> - * <pre>$root.child1.leaf1</pre> - * - * @param path the fully path of the searched node. - * @return the node matching the fully context from the root node, or <code>null</code> if not find. - */ - public NavigationTreeNode findNode(String path) { - return findNode(getRoot(), path, (Pattern) null); - } - - /** - * Apply first the regex pattern to obtain the searched node. - * <p/> - * Search then from the root node a node named by his fully path (concatenation of nodes - * {@link NavigationTreeNode#context} valued separated by dot. - * <p/> - * <p/> - * Example : - * <p/> - * <pre>$root.child1.leaf1</pre> - * - * @param path the fully path of the searched node. - * @param regex a optional regex to apply to path before searching - * @return the node matching the fully context from the root node, or <code>null</code> if not find. - */ - public NavigationTreeNode findNode(String path, String regex) { - return findNode(getRoot(), path, regex); - } - - /** - * Apply first the regex pattern to obtain the searched node. - * <p/> - * Search then from the root node a node named by his fully path (concatenation of nodes - * {@link NavigationTreeNode#context} valued separated by dot. - * <p/> - * Example : - * <p/> - * <pre>$root.child1.leaf1</pre> - * - * @param path the fully path of the searched node. - * @param regex a optional regex to apply to path before searching - * @return the node matching the fully context from the root node, or <code>null</code> if not find. - */ - public NavigationTreeNode findNode(String path, Pattern regex) { - return findNode(getRoot(), path, regex); - } - - - /** - * Search from a given root node a node named by his fully path (concatenation of nodes - * {@link NavigationTreeNode#context} valued separated by dot. - * - * @param root root node to be used - * @param path the fully path of the searched node. - * @return the node matching the fully context from the given root node, or <code>null</code> if not find. - */ - public NavigationTreeNode findNode(NavigationTreeNode root, String path) { - return findNode(root, path, (Pattern) null); - } - - /** - * Apply first the regex pattern to obtain the searched node. - * <p/> - * Search then from a given root node a node named by his fully path (concatenation of nodes - * {@link NavigationTreeNode#context} valued separated by dot. - * - * @param root root node to be used - * @param path the fully path of the searched node. - * @param regex a previous regex to apply to path : must have a matches - * @return the node matching the fully context from the given root node, or <code>null</code> if not find. - */ - public NavigationTreeNode findNode(NavigationTreeNode root, String path, String regex) { - return findNode(root, path, regex == null ? null : Pattern.compile(regex)); - } - - /** - * Apply first the regex pattern to obtain the searched node. - * <p/> - * Search then from a given root node a node named by his fully path (concatenation of nodes - * {@link NavigationTreeNode#context} valued separated by dot. - * - * @param root root node to be used - * @param path the fully path of the searched node. - * @param regex a previous regex to apply to path : must have a matches - * @return the node matching the fully context from the given root node, or <code>null</code> if not find. - */ - public NavigationTreeNode findNode(NavigationTreeNode root, String path, Pattern regex) { - if (regex != null) { - Matcher matcher = regex.matcher(path); - if (!matcher.matches() || matcher.groupCount() < 1) { - log.warn("no matching regex " + regex + " to " + path); - return null; - } - path = matcher.group(1); - log.info("matching regex " + regex + " : " + path); - } - String[] paths = path.split("\\."); - Stack<String> stack = new Stack<String>(); - for (int i = paths.length - 1; i > -1; i--) { - stack.push(paths[i]); - } - return root.findNode(stack); - } - - - /** - * Obtain the associated bean value from context corresponding to node from given contextPath. - * - * @param context the context to seek - * @param contextPath the current context path of the node - * @return the value associated in context with the given context path - * @throws InvocationTargetException todo - * @throws NoSuchMethodException todo - * @throws IllegalAccessException todo - */ - public Object getJAXXContextValue(JAXXContext context, String contextPath) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException { - Object result; - NavigationTreeNode node = findNode(contextPath, (Pattern) null); - result = getJAXXContextValue(context, node); - return result; - } - - /** - * Obtain the associated bean value from context corresponding to node - * - * @param context the context to seek - * @param node the current node - * @return the value associated in context with the given context path - * @throws InvocationTargetException todo - * @throws NoSuchMethodException todo - * @throws IllegalAccessException todo - */ - public Object getJAXXContextValue(JAXXContext context, NavigationTreeNode node) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException { - //Object result; - if (node == null) { - return null; - } - return node.getJAXXContextValue(context); - } } Modified: lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/runtime/swing/navigation/NavigationTreeModelBuilder.java =================================================================== --- lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/runtime/swing/navigation/NavigationTreeModelBuilder.java 2008-11-25 09:42:25 UTC (rev 1034) +++ lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/runtime/swing/navigation/NavigationTreeModelBuilder.java 2008-11-26 01:06:42 UTC (rev 1035) @@ -12,7 +12,8 @@ protected NavigationTreeModel model; - public NavigationTreeModelBuilder() { + public NavigationTreeModelBuilder(String navigationSeparator) { + model = new NavigationTreeModel(null, navigationSeparator); } public NavigationTreeModel getModel() { @@ -25,7 +26,7 @@ String contextName, Class<? extends JAXXObject> uiClass, Class<? extends JAXXAction> actionClass) { - NavigationTreeNode node = new NavigationTreeNode(new NodeRenderer(libelle), entryDef, entryPath, contextName, uiClass, actionClass); + NavigationTreeNode node = model.new NavigationTreeNode(new NodeRenderer(libelle), entryDef, entryPath, contextName, uiClass, actionClass); return addChildNode(parentNode, node); } @@ -34,7 +35,7 @@ String contextName, Class<? extends JAXXObject> uiClass, Class<? extends JAXXAction> actionClass) { - NavigationTreeNode node = new NavigationTreeNode(new NodeRenderer(libelle), entryDef, contextName, uiClass, actionClass); + NavigationTreeNode node = model.new NavigationTreeNode(new NodeRenderer(libelle), entryDef, contextName, uiClass, actionClass); return addChildNode(parentNode, node); } @@ -43,7 +44,7 @@ String contextName, Class<? extends JAXXObject> uiClass, Class<? extends JAXXAction> actionClass) { - NavigationTreeNode node = new NavigationTreeNode(new NodeRenderer(libelle), entryPath, contextName, uiClass, actionClass); + NavigationTreeNode node = model.new NavigationTreeNode(new NodeRenderer(libelle), entryPath, contextName, uiClass, actionClass); return addChildNode(parentNode, node); } @@ -53,7 +54,7 @@ String contextName, Class<? extends JAXXObject> uiClass, Class<? extends JAXXAction> actionClass) { - NavigationTreeNode node = new NavigationTreeNode(new NodeRenderer(decorator), entryDef, entryPath, contextName, uiClass, actionClass); + NavigationTreeNode node = model.new NavigationTreeNode(new NodeRenderer(decorator), entryDef, entryPath, contextName, uiClass, actionClass); return addChildNode(parentNode, node); } @@ -62,7 +63,7 @@ String contextName, Class<? extends JAXXObject> uiClass, Class<? extends JAXXAction> actionClass) { - NavigationTreeNode node = new NavigationTreeNode(new NodeRenderer(decorator), entryDef, contextName, uiClass, actionClass); + NavigationTreeNode node = model.new NavigationTreeNode(new NodeRenderer(decorator), entryDef, contextName, uiClass, actionClass); return addChildNode(parentNode, node); } @@ -71,72 +72,27 @@ String contextName, Class<? extends JAXXObject> uiClass, Class<? extends JAXXAction> actionClass) { - NavigationTreeNode node = new NavigationTreeNode(new NodeRenderer(decorator), entryPath, contextName, uiClass, actionClass); + NavigationTreeNode node = model.new NavigationTreeNode(new NodeRenderer(decorator), entryPath, contextName, uiClass, actionClass); return addChildNode(parentNode, node); } - public void setRootNode(NavigationTreeNode rootNode) { - if (model == null) { - // create model with the root node - model = new NavigationTreeModel(rootNode); - } else { - model.setRoot(rootNode); - } - } - protected NavigationTreeNode addChildNode(NavigationTreeNode parentNode, NavigationTreeNode node) { - if (parentNode != null) { + if (parentNode == null) { + model.setRoot(node); + } else { parentNode.add(node); } + model.nodeStructureChanged(parentNode); return node; } - public NavigationTreeNode removeChildNode(NavigationTreeNode parentNode, NavigationTreeNode node) { - if (parentNode != null) { + public NavigationTreeNode removeChildNode(NavigationTreeNode node) { + NavigationTreeNode parentNode = node.getParent(); + /*if (parentNode != null) { parentNode.remove(node); - } - return node; + }*/ + model.removeNodeFromParent(node); + return parentNode; } - /* - public NavigationTreeNode build(NavigationTreeNode parentNode, String libelle, JAXXContextEntryDef entryDef, - String contextName, - Class<? extends JAXXObject> uiClass) { - return build(parentNode, libelle, entryDef, contextName, uiClass, null); - } - - public NavigationTreeNode build(NavigationTreeNode parentNode, String libelle, JAXXContextEntryDef entryDef, - String contextName) { - return build(parentNode, libelle, entryDef, contextName, null, null); - } - - public NavigationTreeNode build(NavigationTreeNode parentNode, String libelle, String contextName, - Class<? extends JAXXObject> uiClass) { - return build(parentNode, libelle, (String) null, contextName, uiClass, null); - } - - public NavigationTreeNode build(NavigationTreeNode parentNode, String libelle, String contextName) { - return build(parentNode, libelle, (String) null, contextName, null, null); - } - - public NavigationTreeNode build(NavigationTreeNode parentNode, Decorator<Object> decorator, JAXXContextEntryDef entryDef, - String contextName, Class<? extends JAXXObject> uiClass) { - return build(parentNode, decorator, entryDef, contextName, uiClass, null); - } - - public NavigationTreeNode build(NavigationTreeNode parentNode, Decorator<Object> decorator, JAXXContextEntryDef entryDef, - String contextName) { - return build(parentNode, decorator, entryDef, contextName, null, null); - } - - public NavigationTreeNode build(NavigationTreeNode parentNode, Decorator<Object> decorator, - String contextName, Class<? extends JAXXObject> uiClass) { - return build(parentNode, decorator, (String) null, contextName, uiClass, null); - } - - public NavigationTreeNode build(NavigationTreeNode parentNode, Decorator<Object> decorator, - String contextName) { - return build(parentNode, decorator, (String) null, contextName, null, null); - }*/ - } Modified: lutinjaxx/trunk/jaxx-core/src/test/java/jaxx/runtime/swing/navigation/NavigationTreeModelTest.java =================================================================== --- lutinjaxx/trunk/jaxx-core/src/test/java/jaxx/runtime/swing/navigation/NavigationTreeModelTest.java 2008-11-25 09:42:25 UTC (rev 1034) +++ lutinjaxx/trunk/jaxx-core/src/test/java/jaxx/runtime/swing/navigation/NavigationTreeModelTest.java 2008-11-26 01:06:42 UTC (rev 1035) @@ -7,7 +7,6 @@ import org.junit.Assert; import org.junit.Test; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -20,29 +19,29 @@ */ public class NavigationTreeModelTest { - private static final String ROOT_CONTEXT = "$root"; private static final String FAKE = "-fake"; - private static final String DOT = "."; + private static final String separator = "/"; + @Test public void testFindNode() throws Exception { - NavigationTreeNode rootNode = new NavigationTreeNode(null, null, ROOT_CONTEXT, null, null); + NavigationTreeModelBuilder builder = new NavigationTreeModelBuilder(separator); + NavigationTreeNode rootNode = builder.build(null, (String) null, (String) null, ROOT_CONTEXT, null, null); + for (int i = 0; i < 4; i++) { - NavigationTreeNode sonNode = new NavigationTreeNode(null, null, getNodeContext(i), null, null); - rootNode.insert(sonNode, i); + NavigationTreeNode sonNode = builder.build(rootNode, (String) null, (String) null, getNodeContext(i), null, null); for (int j = 0; j < 4; j++) { - NavigationTreeNode sonSonNode = new NavigationTreeNode(null, null, getNodeContext(i, j), null, null); - sonNode.insert(sonSonNode, j); + NavigationTreeNode sonSonNode = builder.build(sonNode, (String) null, (String) null, getNodeContext(i, j), null, null); for (int k = 0; k < 4; k++) { - sonSonNode.insert(new NavigationTreeNode(null, null, getNodeContext(i, j, k), null, null), k); + builder.build(sonSonNode, (String) null, (String) null, getNodeContext(i, j, k), null, null); } } } - NavigationTreeModel model = new NavigationTreeModel(rootNode); + NavigationTreeModel model = builder.getModel(); NavigationTreeNode node; String contextPath; @@ -57,28 +56,28 @@ for (int i = 0; i < 4; i++) { currentNode = getNodeContext(i); - contextPath = ROOT_CONTEXT + DOT + currentNode; + contextPath = ROOT_CONTEXT + separator + currentNode; node = model.findNode(contextPath); assertNodeEquals(contextPath, currentNode, 1, node, false); for (int j = 0; j < 4; j++) { currentNode = getNodeContext(i, j); - contextPath = ROOT_CONTEXT + DOT + getNodeContext(i) + DOT + currentNode; + contextPath = ROOT_CONTEXT + separator + getNodeContext(i) + separator + currentNode; node = model.findNode(contextPath); assertNodeEquals(contextPath, currentNode, 2, node, false); for (int k = 0; k < 4; k++) { currentNode = getNodeContext(i, j, k); - contextPath = ROOT_CONTEXT + DOT + getNodeContext(i) + DOT + getNodeContext(i, j) + DOT + currentNode; + contextPath = ROOT_CONTEXT + separator + getNodeContext(i) + separator + getNodeContext(i, j) + separator + currentNode; node = model.findNode(contextPath); assertNodeEquals(contextPath, currentNode, 3, node, false); } - node = model.findNode(ROOT_CONTEXT + DOT + getNodeContext(i) + DOT + getNodeContext(i, j) + DOT + currentNode + FAKE); + node = model.findNode(ROOT_CONTEXT + separator + getNodeContext(i) + separator + getNodeContext(i, j) + separator + currentNode + FAKE); Assert.assertNull(node); } - node = model.findNode(ROOT_CONTEXT + DOT + getNodeContext(i) + DOT + currentNode + FAKE); + node = model.findNode(ROOT_CONTEXT + separator + getNodeContext(i) + separator + currentNode + FAKE); Assert.assertNull(node); } @@ -128,52 +127,44 @@ @Test public void testGetJAXXContextValue() throws Exception { - NavigationTreeNode rootNode = new NavigationTreeNode(null, null, ROOT_CONTEXT, null, null); + NavigationTreeModelBuilder builder = new NavigationTreeModelBuilder(separator); + + NavigationTreeNode rootNode = builder.build(null, (String) null, (String) null, ROOT_CONTEXT, null, null); + NavigationTreeNode sonNode; NavigationTreeNode sonSonNode; NavigationTreeNode sonSonSonNode; - sonNode = new NavigationTreeNode(null, JAXXContextEntryDef.newDef("name", String.class), "name", null, null); + builder.build(rootNode, (String) null, JAXXContextEntryDef.newDef("name", String.class), "name", null, null); + builder.build(rootNode, (String) null, JAXXContextEntryDef.newDef("name2", String.class), "name2", null, null); - rootNode.insert(sonNode, 0); + sonNode = builder.build(rootNode, (String) null, JAXXContextEntryDef.newDef(Model.class), "model", null, null); - sonNode = new NavigationTreeNode(null, JAXXContextEntryDef.newDef("name2", String.class), "name2", null, null); + builder.build(sonNode, (String) null, "../name", "name", null, null); + builder.build(sonNode, (String) null, "../integerValue", "integerValue", null, null); - rootNode.insert(sonNode, 1); + sonSonNode = builder.build(sonNode, (String) null, "../sons", "sons", null, null); - sonNode = new NavigationTreeNode(null, JAXXContextEntryDef.newDef(Model.class), "model", null, null); + sonSonSonNode = builder.build(sonSonNode, (String) null, "..[1]", 0 + "", null, null); - rootNode.insert(sonNode, 2); + builder.build(sonSonSonNode, (String) null, "../name", "name", null, null); + builder.build(sonSonSonNode, (String) null, "../integerValue", "integerValue", null, null); + builder.build(sonSonSonNode, (String) null, "../sons", "sons", null, null); - sonSonNode = new NavigationTreeNode(null, "../name", "name", null, null); - sonNode.insert(sonSonNode, 0); + sonSonSonNode = builder.build(sonSonNode, (String) null, "..[2]", 1 + "", null, null); - sonSonNode = new NavigationTreeNode(null, "../integerValue", "integerValue", null, null); - sonNode.insert(sonSonNode, 1); + builder.build(sonSonSonNode, (String) null, "../name", "name", null, null); + builder.build(sonSonSonNode, (String) null, "../integerValue", "integerValue", null, null); + builder.build(sonSonSonNode, (String) null, "../sons", "sons", null, null); - sonSonNode = new NavigationTreeNode(null, "../sons", "sons", null, null); - sonNode.insert(sonSonNode, 2); - - sonSonSonNode = new NavigationTreeNode(null, "..[1]", 0 + "", null, null); - sonSonNode.insert(sonSonSonNode, 0); - sonSonSonNode.insert(new NavigationTreeNode(null, "../name", "name", null, null), 0); - sonSonSonNode.insert(new NavigationTreeNode(null, "../integerValue", "integerValue", null, null), 1); - sonSonSonNode.insert(new NavigationTreeNode(null, "../sons", "sons", null, null), 2); - - sonSonSonNode = new NavigationTreeNode(null, "..[2]", 1 + "", null, null); - sonSonNode.insert(sonSonSonNode, 1); - sonSonSonNode.insert(new NavigationTreeNode(null, "../name", "name", null, null), 0); - sonSonSonNode.insert(new NavigationTreeNode(null, "../integerValue", "integerValue", null, null), 1); - sonSonSonNode.insert(new NavigationTreeNode(null, "../sons", "sons", null, null), 2); - - sonSonSonNode = new NavigationTreeNode(null, null, 2 + "", null, null); - //sonSonSonNode = new NavigationTreeNode(null, "..[3]", 2 + "", null, null); + sonSonSonNode = builder.build(sonSonNode, (String) null, (String) null, 2 + "", null, null); + //sonSonSonNode = model.new NavigationTreeNode(null, "..[3]", 2 + "", null, null); sonSonNode.insert(sonSonSonNode, 2); - sonSonSonNode.insert(new NavigationTreeNode(null, "../..[3]/name", "name", null, null), 0); - sonSonSonNode.insert(new NavigationTreeNode(null, "../..[3]/integerValue", "integerValue", null, null), 1); - sonSonSonNode.insert(new NavigationTreeNode(null, "../..[3]/sons", "sons", null, null), 2); + builder.build(sonSonSonNode, (String) null, "../..[3]/name", "name", null, null); + builder.build(sonSonSonNode, (String) null, "../..[3]/integerValue", "integerValue", null, null); + builder.build(sonSonSonNode, (String) null, "../..[3]/sons", "sons", null, null); - NavigationTreeModel model = new NavigationTreeModel(rootNode); + NavigationTreeModel model = builder.getModel(); JAXXContext context = new DefaultJAXXContext(); context.setContextValue("the name", "name"); @@ -192,28 +183,28 @@ Assert.assertNull(model.getJAXXContextValue(context, "$root.name" + FAKE)); - testBinding(model, context, "$root.name", context.getContextValue(String.class, "name")); - testBinding(model, context, "$root.name2", context.getContextValue(String.class, "name2")); + testBinding(model, context, "$root/name", context.getContextValue(String.class, "name")); + testBinding(model, context, "$root/name2", context.getContextValue(String.class, "name2")); Model bean = context.getContextValue(Model.class); - testBinding(model, context, "$root.model", bean); - testBinding(model, context, "$root.model.name", bean.getName()); - testBinding(model, context, "$root.model.integerValue", bean.getIntegerValue()); - testBinding(model, context, "$root.model.sons", bean.getSons()); + testBinding(model, context, "$root/model", bean); + testBinding(model, context, "$root/model/name", bean.getName()); + testBinding(model, context, "$root/model/integerValue", bean.getIntegerValue()); + testBinding(model, context, "$root/model/sons", bean.getSons()); - testBinding(model, context, "$root.model.sons.0.name", bean.getSons().get(0).getName()); - testBinding(model, context, "$root.model.sons.0.integerValue", bean.getSons().get(0).getIntegerValue()); - testBinding(model, context, "$root.model.sons.0.sons", bean.getSons().get(0).getSons()); + testBinding(model, context, "$root/model/sons/0/name", bean.getSons().get(0).getName()); + testBinding(model, context, "$root/model/sons/0/integerValue", bean.getSons().get(0).getIntegerValue()); + testBinding(model, context, "$root/model/sons/0/sons", bean.getSons().get(0).getSons()); - testBinding(model, context, "$root.model.sons.1.name", bean.getSons().get(1).getName()); - testBinding(model, context, "$root.model.sons.1.integerValue", bean.getSons().get(1).getIntegerValue()); - testBinding(model, context, "$root.model.sons.1.sons", bean.getSons().get(1).getSons()); + testBinding(model, context, "$root/model/sons/1/name", bean.getSons().get(1).getName()); + testBinding(model, context, "$root/model/sons/1/integerValue", bean.getSons().get(1).getIntegerValue()); + testBinding(model, context, "$root/model/sons/1/sons", bean.getSons().get(1).getSons()); - testBinding(model, context, "$root.model.sons.2.name", bean.getSons().get(2).getName()); - testBinding(model, context, "$root.model.sons.2.integerValue", bean.getSons().get(2).getIntegerValue()); - testBinding(model, context, "$root.model.sons.2.sons", bean.getSons().get(2).getSons()); + testBinding(model, context, "$root/model/sons/2/name", bean.getSons().get(2).getName()); + testBinding(model, context, "$root/model/sons/2/integerValue", bean.getSons().get(2).getIntegerValue()); + testBinding(model, context, "$root/model/sons/2/sons", bean.getSons().get(2).getSons()); } /** @@ -252,90 +243,93 @@ @Test public void testGetJAXXContextValueFromList() throws Exception { - NavigationTreeNode rootNode = new NavigationTreeNode(null, null, ROOT_CONTEXT, null, null); + NavigationTreeModelBuilder builder = new NavigationTreeModelBuilder(separator); + + NavigationTreeNode rootNode = builder.build(null, (String) null, (String) null, ROOT_CONTEXT, null, null); + NavigationTreeNode sonNode; NavigationTreeNode sonSonNode; NavigationTreeNode sonSonSonNode; // first son is a list of models - sonNode = new NavigationTreeNode(null, JAXXContextEntryDef.newListDef("models"), "models", null, null); - rootNode.insert(sonNode, 0); + sonNode = builder.build(rootNode, (String) null, JAXXContextEntryDef.newListDef("models"), "models", null, null); // first son son is a model - sonSonNode = new NavigationTreeNode(null, "..[1]", "0", null, null); - sonNode.insert(sonSonNode, 0); + sonSonNode = builder.build(sonNode, (String) null, "..[1]", "0", null, null); - sonSonNode.insert(new NavigationTreeNode(null, "../name", "name", null, null), 0); - sonSonNode.insert(new NavigationTreeNode(null, "../integerValue", "integerValue", null, null), 1); - sonSonNode.insert(sonSonSonNode = new NavigationTreeNode(null, "../sons", "sons", null, null), 2); + builder.build(sonSonNode, (String) null, "../name", "name", null, null); + builder.build(sonSonNode, (String) null, "../integerValue", "integerValue", null, null); + sonSonNode = builder.build(sonSonNode, (String) null, "../sons", "sons", null, null); - sonSonSonNode.insert(sonSonSonNode = new NavigationTreeNode(null, "..[1]", "0", null, null), 0); - sonSonSonNode.insert(new NavigationTreeNode(null, "../name", "name", null, null), 0); + sonSonSonNode = builder.build(sonSonNode, (String) null, "..[1]", "0", null, null); + builder.build(sonSonSonNode, (String) null, "../name", "name", null, null); // second son son is a model - sonSonNode = new NavigationTreeNode(null, "..[2]", "1", null, null); - sonNode.insert(sonSonNode, 1); - sonSonNode.insert(new NavigationTreeNode(null, "../name", "name", null, null), 0); - sonSonNode.insert(new NavigationTreeNode(null, "../integerValue", "integerValue", null, null), 1); - sonSonNode.insert(new NavigationTreeNode(null, "../sons", "sons", null, null), 2); + sonSonNode = builder.build(sonNode, (String) null, "..[2]", "1", null, null); + builder.build(sonSonNode, (String) null, "../name", "name", null, null); + builder.build(sonSonNode, (String) null, "../integerValue", "integerValue", null, null); + builder.build(sonSonNode, (String) null, "../sons", "sons", null, null); + // third son son is a model - sonSonNode = new NavigationTreeNode(null, "..[3]", "2", null, null); - sonNode.insert(sonSonNode, 2); - sonSonNode.insert(new NavigationTreeNode(null, "../name", "name", null, null), 0); - sonSonNode.insert(new NavigationTreeNode(null, "../integerValue", "integerValue", null, null), 1); - sonSonNode.insert(new NavigationTreeNode(null, "../sons", "sons", null, null), 2); + sonSonNode = builder.build(sonNode, (String) null, "..[3]", "2", null, null); - NavigationTreeModel model = new NavigationTreeModel(rootNode); + builder.build(sonSonNode, (String) null, "../name", "name", null, null); + builder.build(sonSonNode, (String) null, "../integerValue", "integerValue", null, null); + builder.build(sonSonNode, (String) null, "../sons", "sons", null, null); - JAXXContext context = new DefaultJAXXContext(); - List<Model> list = new ArrayList<Model>(); - list.add(new Model("entryOne", 10, - Arrays.asList( - new Model("one", 1, Collections.<Model>emptyList()), - new Model("two", 2, Collections.<Model>emptyList()), - new Model("three", 3, Collections.<Model>emptyList()) + NavigationTreeModel model = builder.getModel(); + + + List<Model> list = Arrays.asList( + new Model("entryOne", 10, + Arrays.asList( + new Model("one", 1, Collections.<Model>emptyList()), + new Model("two", 2, Collections.<Model>emptyList()), + new Model("three", 3, Collections.<Model>emptyList()) + ) + ), + new Model("entryTwo", 20, + Arrays.asList( + new Model("2one", 1, Collections.<Model>emptyList()), + new Model("2two", 2, Collections.<Model>emptyList()), + new Model("2three", 3, Collections.<Model>emptyList()) + ) + ), + new Model("entryThree", 30, + Arrays.asList( + new Model("3one", 1, Collections.<Model>emptyList()), + new Model("3two", 2, Collections.<Model>emptyList()), + new Model("3three", 3, Collections.<Model>emptyList()) + ) ) - )); - list.add(new Model("entryTwo", 20, - Arrays.asList( - new Model("2one", 1, Collections.<Model>emptyList()), - new Model("2two", 2, Collections.<Model>emptyList()), - new Model("2three", 3, Collections.<Model>emptyList()) - ) - )); - list.add(new Model("entryThree", 30, - Arrays.asList( - new Model("3one", 1, Collections.<Model>emptyList()), - new Model("3two", 2, Collections.<Model>emptyList()), - new Model("3three", 3, Collections.<Model>emptyList()) - ) - )); + ); + JAXXContext context = new DefaultJAXXContext(); context.setContextValue(list, "models"); Model bean; - testBinding(model, context, "$root.models", list); + testBinding(model, context, "$root/models", list); bean = list.get(0); - testBinding(model, context, "$root.models.0", bean); - testBinding(model, context, "$root.models.0.name", bean.getName()); - testBinding(model, context, "$root.models.0.integerValue", bean.getIntegerValue()); - testBinding(model, context, "$root.models.0.sons", bean.getSons()); - testBinding(model, context, "$root.models.0.sons.0", bean.getSons().get(0)); - testBinding(model, context, "$root.models.0.sons.0.name", bean.getSons().get(0).getName()); + testBinding(model, context, "$root/models/0", bean); + testBinding(model, context, "$root/models/0/name", bean.getName()); + testBinding(model, context, "$root/models/0/integerValue", bean.getIntegerValue()); + testBinding(model, context, "$root/models/0/sons", bean.getSons()); + testBinding(model, context, "$root/models/0/sons/0", bean.getSons().get(0)); + testBinding(model, context, "$root/models/0/sons/0/name", bean.getSons().get(0).getName()); bean = list.get(1); - testBinding(model, context, "$root.models.1", bean); - testBinding(model, context, "$root.models.1.name", bean.getName()); - testBinding(model, context, "$root.models.1.integerValue", bean.getIntegerValue()); - testBinding(model, context, "$root.models.1.sons", bean.getSons()); + testBinding(model, context, "$root/models/1", bean); + testBinding(model, context, "$root/models/1/name", bean.getName()); + testBinding(model, context, "$root/models/1/integerValue", bean.getIntegerValue()); + testBinding(model, context, "$root/models/1/sons", bean.getSons()); bean = list.get(2); - testBinding(model, context, "$root.models.2", bean); - testBinding(model, context, "$root.models.2.name", bean.getName()); - testBinding(model, context, "$root.models.2.integerValue", bean.getIntegerValue()); - testBinding(model, context, "$root.models.2.sons", bean.getSons()); + testBinding(model, context, "$root/models/2", bean); + testBinding(model, context, "$root/models/2/name", bean.getName()); + testBinding(model, context, "$root/models/2/integerValue", bean.getIntegerValue()); + testBinding(model, context, "$root/models/2/sons", bean.getSons()); } @@ -347,7 +341,7 @@ Assert.assertEquals(expected, value); } - protected static String getNodeContext(int... context) { + protected String getNodeContext(int... context) { String result = ""; for (int i : context) { result += i; @@ -360,7 +354,7 @@ Assert.assertNotNull(node); Assert.assertEquals(root, node.isRoot()); Assert.assertEquals(level, node.getLevel()); - Assert.assertEquals(nodeContext, node.getContext()); + Assert.assertEquals(nodeContext, node.getNavigationPath()); Assert.assertEquals(contextPath, node.getContextPath()); }
participants (1)
-
chemit@users.labs.libre-entreprise.org