Author: chemit Date: 2008-11-16 21:19:26 +0000 (Sun, 16 Nov 2008) New Revision: 1018 Modified: lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/runtime/swing/NavigationTreeModel.java Log: javadoc +add convinient methods in NavizationTreeModel Modified: lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/runtime/swing/NavigationTreeModel.java =================================================================== --- lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/runtime/swing/NavigationTreeModel.java 2008-11-16 17:44:15 UTC (rev 1017) +++ lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/runtime/swing/NavigationTreeModel.java 2008-11-16 21:19:26 UTC (rev 1018) @@ -16,6 +16,8 @@ import java.lang.reflect.InvocationTargetException; import java.util.List; import java.util.Stack; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * Le modele utilisé pour un arbre de navigation. @@ -27,8 +29,10 @@ public class NavigationTreeModel extends DefaultTreeModel { /** to use log facility, just put in your code: log.info(\"...\"); */ - static private Log log = LogFactory.getLog(NavigationTreeModel.class); + static private final Log log = LogFactory.getLog(NavigationTreeModel.class); + private static final long serialVersionUID = 1L; + /** la représentation d'un noeud dans le model {@link NavigationTreeModel} */ public static class NavigationTreeNode extends DefaultMutableTreeNode { @@ -101,6 +105,7 @@ this.jaxxContextEntryDef = jaxxContextEntryDef; } + /** @return the fully context pathof the node from the root node to this. */ public String getContextPath() { TreeNode[] path = getPath(); StringBuilder sb = new StringBuilder(); @@ -116,6 +121,15 @@ return (NavigationTreeNode) super.getChildAt(index); } + @Override + public NavigationTreeNode getParent() { + return (NavigationTreeNode) super.getParent(); + } + + /** + * @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. + */ public NavigationTreeNode getChild(String context) { for (int i = 0, max = getChildCount(); i < max; i++) { NavigationTreeNode son = getChildAt(i); @@ -126,7 +140,18 @@ return null; } - public NavigationTreeNode findNode(Stack<String> stack) { + /** + * @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); } @@ -144,15 +169,6 @@ return son == null ? null : son.findNode(stack); } - public NavigationTreeNode getFirstAncestorWithDef() { - if (jaxxContextEntryDef != null) { - return this; - } - if (getParent() == null) { - return null; - } - return ((NavigationTreeNode) getParent()).getFirstAncestorWithDef(); - } } @Override @@ -160,9 +176,7 @@ return (NavigationTreeNode) super.getRoot(); } - private static final long serialVersionUID = 1L; - public NavigationTreeModel(TreeNode root) { super(root); } @@ -171,17 +185,119 @@ 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--) { - String s = paths[i]; - stack.push(s); + stack.push(paths[i]); } - return getRoot().findNode(stack); + 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 @@ -191,11 +307,21 @@ */ public Object getJAXXContextValue(JAXXContext context, String contextPath) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException { Object result; - NavigationTreeNode node = findNode(contextPath); + 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) {