r1551 - trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/navigation
Author: tchemit Date: 2009-10-04 01:07:51 +0200 (Sun, 04 Oct 2009) New Revision: 1551 Added: trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/navigation/NavigationTreeContextHelper.java trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/navigation/NavigationTreeHelper.java Modified: trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/navigation/NavigationTreeHandler.java trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/navigation/NavigationTreeHandlerWithCardLayout.java trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/navigation/NavigationUtil.java Log: - deprecate none context safe navigation methods - add two helper classes which are context safe and more friendly Added: trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/navigation/NavigationTreeContextHelper.java =================================================================== --- trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/navigation/NavigationTreeContextHelper.java (rev 0) +++ trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/navigation/NavigationTreeContextHelper.java 2009-10-03 23:07:51 UTC (rev 1551) @@ -0,0 +1,133 @@ +package jaxx.runtime.swing.navigation; + +import jaxx.runtime.JAXXContext; +import jaxx.runtime.JAXXContextEntryDef; +import jaxx.runtime.swing.navigation.NavigationTreeModel.NavigationTreeNode; + +/** + * To help getting and setting navigation tree objects from a {@link JAXXContext}. + * <p/> + * There is four type of data which can be hold in a context : + * <ul> + * <li>tree model : the navigation tree model</li> + * <li>tree handler : the navigation tree handler</li> + * <li>selected path : the navigation path of the selected node</li> + * <li>selected node : the selected node</li> + * <li>selected bean : the selected bean</li> + * </ul> + * + * To make possible the use of more than one navigation tree system in a same + * context, we <b>MUST</b> distinguish the context entries definition. For this + * purpose, entries definition are normalized and prefixed by a unique {@link #prefix}. + * <p/> + * Here is the keys used : + * <ul> + * <li>tree model : {@code prefix + "-tree-model"}</li> + * <li>tree handler : {@code prefix + "-tree-handler"}</li> + * <li>selected path : {@code prefix + "-selected-path"}</li> + * <li>selected node : {@code prefix + "-selected-node"}</li> + * <li>selected bean : {@code prefix + "-selected-bean"}</li> + * </ul> + * + * @author chemit + * @since 1.7.2 + */ +public class NavigationTreeContextHelper { + + protected final String prefix; + protected JAXXContextEntryDef<String> selectedPathContextEntry; + protected JAXXContextEntryDef<Object> selectedBeanContextEntry; + protected JAXXContextEntryDef<NavigationTreeNode> selectedNodeContextEntry; + protected JAXXContextEntryDef<NavigationTreeModel> treeModelContextEntry; + protected JAXXContextEntryDef<NavigationTreeHandler> treeHandlerContextEntry; + + public NavigationTreeContextHelper(String contextPrefix) { + this.prefix = contextPrefix; + treeModelContextEntry = JAXXContextEntryDef.newDef(contextPrefix + "-tree-model", NavigationTreeModel.class); + treeHandlerContextEntry = JAXXContextEntryDef.newDef(contextPrefix + "-tree-handler", NavigationTreeHandler.class); + selectedBeanContextEntry = JAXXContextEntryDef.newDef(contextPrefix + "-selected-bean", Object.class); + selectedNodeContextEntry = JAXXContextEntryDef.newDef(contextPrefix + "-selected-node", NavigationTreeNode.class); + selectedPathContextEntry = JAXXContextEntryDef.newDef(contextPrefix + "-selected-path", String.class); + } + + public String getPrefix() { + return prefix; + } + + public NavigationTreeModel getTreeModel(JAXXContext context) { + NavigationTreeModel r = getTreeModelContextEntry().getContextValue(context); + return r; + } + + public NavigationTreeHandler getTreeHandler(JAXXContext context) { + NavigationTreeHandler r = getTreeHandlerContextEntry().getContextValue(context); + return r; + } + + public String getSelectedPath(JAXXContext context) { + String r = getSelectedPathContextEntry().getContextValue(context); + return r; + } + + public NavigationTreeNode getSelectedNode(JAXXContext context) { + NavigationTreeNode r = getSelectedNodeContextEntry().getContextValue(context); + return r; + } + + public Object getSelectedBean(JAXXContext context) { + Object r = getSelectedBeanContextEntry().getContextValue(context); + return r; + } + + public void setTreeModel(JAXXContext context, NavigationTreeModel model) { + getTreeModelContextEntry().setContextValue(context, model); + } + + public void setTreeHandler(JAXXContext context, NavigationTreeHandler handler) { + getTreeHandlerContextEntry().setContextValue(context, handler); + } + + public void setSelectedPath(JAXXContext context, String path) { + if (path == null) { + getSelectedPathContextEntry().removeContextValue(context); + } else { + getSelectedPathContextEntry().setContextValue(context, path); + } + } + + public void setSelectedNode(JAXXContext context, NavigationTreeNode node) { + if (node == null) { + getSelectedNodeContextEntry().removeContextValue(context); + } else { + getSelectedNodeContextEntry().setContextValue(context, node); + } + } + + public void setSelectedBean(JAXXContext context, Object bean) { + if (bean == null) { + getSelectedBeanContextEntry().removeContextValue(context); + } else { + getSelectedBeanContextEntry().setContextValue(context, bean); + } + } + + public JAXXContextEntryDef<NavigationTreeModel> getTreeModelContextEntry() { + return treeModelContextEntry; + } + + public JAXXContextEntryDef<NavigationTreeHandler> getTreeHandlerContextEntry() { + return treeHandlerContextEntry; + } + + public JAXXContextEntryDef<Object> getSelectedBeanContextEntry() { + return selectedBeanContextEntry; + } + + public JAXXContextEntryDef<NavigationTreeNode> getSelectedNodeContextEntry() { + return selectedNodeContextEntry; + } + + public JAXXContextEntryDef<String> getSelectedPathContextEntry() { + return selectedPathContextEntry; + } +} Property changes on: trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/navigation/NavigationTreeContextHelper.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL Modified: trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/navigation/NavigationTreeHandler.java =================================================================== --- trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/navigation/NavigationTreeHandler.java 2009-10-03 23:06:18 UTC (rev 1550) +++ trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/navigation/NavigationTreeHandler.java 2009-10-03 23:07:51 UTC (rev 1551) @@ -26,29 +26,69 @@ */ public abstract class NavigationTreeHandler extends DefaultTreeSelectionModel { + public static final String DEFAULT_CONTEXT_PREFIX = "navigation"; private static final long serialVersionUID = 1L; /** to use log facility, just put in your code: log.info(\"...\"); */ static private final Log log = LogFactory.getLog(NavigationTreeHandler.class); + /** + * @deprecated since 1.7.2, is no more used (prefer use the dynamic context entry defs) + */ + @Deprecated static public final String NAVIGATION_SELECTED_BEAN = "navigation-selected-bean"; + /** + * @deprecated since 1.7.2, is no more used (prefer use the dynamic context entry defs) + */ + @Deprecated static public final JAXXContextEntryDef<String> NAVIGATION_SELECTED_PATH_ENTRY_DEF = JAXXContextEntryDef.newDef("navigation-selected-path", String.class); + /** + * @deprecated since 1.7.2, is no more used (prefer use the dynamic context entry defs) + */ + @Deprecated static public final JAXXContextEntryDef<NavigationTreeNode> NAVIGATION_SELECTED_NODE_ENTRY_DEF = JAXXContextEntryDef.newDef("navigation-selected-node", NavigationTreeNode.class); - /** defined the stategy of instanciation of ui */ + /** + * Define the stategy of instanciation of ui + */ public enum Strategy { - /** instanciate a ui for a node */ + /** + * instanciate a ui for a node + */ PER_NODE, - /** instanciate only one a ui for a type,nodes will share the instanciation */ + /** + * instanciate only one a ui for a type,nodes will share the instanciation + */ PER_UI_TYPE } - /** la classe d'ui par defaut, associé à un noeud de l'arbe */ + /** + * default ui class to use if node does not define an ui class + */ protected Class<? extends JAXXObject> defaultUIClass; + /** + * [optional] default action class + */ protected Class<? extends JAXXAction> defaultUIHandlerClass; - /** l'ui contenant l'arbre de navigation */ + /** + * UI which contains navigation tree + */ protected JAXXObject context; + /** + * UI Instanciation strategy + */ protected Strategy strategy; + /** + * JAXXContext access helper. + * + * @since 1.7.2 + */ + protected NavigationTreeContextHelper contextHelper; protected NavigationTreeHandler(Class<? extends JAXXObject> defaultUIClass, Class<? extends JAXXAction> defaultUIHandlerClass, JAXXObject context, Strategy strategy) { + this(DEFAULT_CONTEXT_PREFIX, defaultUIClass, defaultUIHandlerClass, context, strategy); + } + + protected NavigationTreeHandler(String contextPrefix, Class<? extends JAXXObject> defaultUIClass, Class<? extends JAXXAction> defaultUIHandlerClass, JAXXObject context, Strategy strategy) { + this.contextHelper = new NavigationTreeContextHelper(contextPrefix); this.defaultUIClass = defaultUIClass; this.defaultUIHandlerClass = defaultUIHandlerClass; this.context = context; @@ -67,11 +107,14 @@ }); } + /** + * @return le modèle de navigation associé + */ protected abstract NavigationTreeModel getNavigationTreeModel(); /** - * @return le composent actuellement visible associé au noeud courant ou au noeud précédent - * lors d'un changement de noeud. + * @return le composent actuellement visible associé au noeud courant ou + * au noeud précédent lors d'un changement de noeud. */ protected abstract Component getCurrentUI(); @@ -97,6 +140,13 @@ */ protected abstract Component createUI(NavigationTreeNode node) throws Exception; + /** + * Prepare le context a utiliser pour initialiser une nouvelle ui. + * + * @param node le noeud associé à l'ui à créer + * @return le context à utiliser pour instancier l'ui + * @throws Exception if any + */ protected abstract JAXXContext createUIContext(NavigationTreeNode node) throws Exception; /** @@ -196,10 +246,12 @@ } // save in context current node context path - NAVIGATION_SELECTED_PATH_ENTRY_DEF.setContextValue(context, node.getContextPath()); + getContextHelper().setSelectedPath(context, node.getContextPath()); +// NAVIGATION_SELECTED_PATH_ENTRY_DEF.setContextValue(context, node.getContextPath()); // save in context current node - NAVIGATION_SELECTED_NODE_ENTRY_DEF.setContextValue(context, node); + getContextHelper().setSelectedNode(context, node); +// NAVIGATION_SELECTED_NODE_ENTRY_DEF.setContextValue(context, node); // really open the ui associated with the selected node openUI(newUI, node); @@ -218,10 +270,12 @@ log.debug("find data for contextPath <" + node.getContextPath() + "> : " + (data == null ? null : data.getClass())); } - context.removeContextValue(Object.class, NAVIGATION_SELECTED_BEAN); + getContextHelper().setSelectedBean(context, null); +// context.removeContextValue(Object.class, NAVIGATION_SELECTED_BEAN); if (data != null) { - context.setContextValue(data, NAVIGATION_SELECTED_BEAN); + getContextHelper().setSelectedBean(context, data); +// context.setContextValue(data, NAVIGATION_SELECTED_BEAN); //todo should we not use this to avoid conflict in context ? context.setContextValue(data); } @@ -246,4 +300,8 @@ JAXXAction action = jaxxActionClass.newInstance(); return action; } + + public NavigationTreeContextHelper getContextHelper() { + return contextHelper; + } } Modified: trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/navigation/NavigationTreeHandlerWithCardLayout.java =================================================================== --- trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/navigation/NavigationTreeHandlerWithCardLayout.java 2009-10-03 23:06:18 UTC (rev 1550) +++ trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/navigation/NavigationTreeHandlerWithCardLayout.java 2009-10-03 23:07:51 UTC (rev 1551) @@ -42,6 +42,16 @@ */ protected abstract CardLayout2 getContentLayout(); + public NavigationTreeHandlerWithCardLayout(String contextPrefix, Class<? extends JAXXObject> defaultUIClass, Class<? extends JAXXAction> defaultUIHandlerClass, JAXXObject context, Strategy strategy) { + super(contextPrefix, defaultUIClass, defaultUIHandlerClass, context, strategy); + if (getContentContainer() == null) { + throw new IllegalArgumentException("could not have a null 'contentContainer' in ui " + context); + } + if (getContentLayout() == null) { + throw new IllegalArgumentException("could not have a null 'contentLayout' in ui " + context); + } + } + public NavigationTreeHandlerWithCardLayout(Class<? extends JAXXObject> defaultUIClass, Class<? extends JAXXAction> defaultUIHandlerClass, JAXXObject context, Strategy strategy) { super(defaultUIClass, defaultUIHandlerClass, context, strategy); Added: trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/navigation/NavigationTreeHelper.java =================================================================== --- trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/navigation/NavigationTreeHelper.java (rev 0) +++ trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/navigation/NavigationTreeHelper.java 2009-10-03 23:07:51 UTC (rev 1551) @@ -0,0 +1,123 @@ +package jaxx.runtime.swing.navigation; + +import java.lang.reflect.InvocationTargetException; +import java.util.regex.Pattern; +import javax.swing.JTree; +import javax.swing.tree.TreePath; +import jaxx.runtime.JAXXContext; +import jaxx.runtime.JAXXObject; +import jaxx.runtime.swing.navigation.NavigationTreeModel.NavigationTreeNode; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * Helper object associated to a given navigation tree system. + * + * To helper is context safe (base on a {@link NavigationTreeContextHelper}. + * + * @author chemit + * @since 1.7.2 + * @see NavigationTreeCellRenderer + */ +public abstract class NavigationTreeHelper extends NavigationTreeContextHelper { + + static private final Log log = LogFactory.getLog(NavigationTreeHelper.class); + + public abstract NavigationTreeModel createTreeModel(JAXXContext context); + + public abstract NavigationTreeHandler createTreeHandler(JAXXObject context); + + public NavigationTreeHelper(String contextPrefix) { + super(contextPrefix); + } + + public Object getContextValue(JAXXContext context, String navigationPath) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException { + NavigationTreeModel treeModel = getSafeTreeModel(context); + return treeModel.getJAXXContextValue(context, navigationPath); + } + + public NavigationTreeNode findNode(JAXXContext context, String navigationPath) { + NavigationTreeModel treeModel = getSafeTreeModel(context); + return treeModel.findNode(navigationPath); + } + + public NavigationTreeNode findNode(JAXXContext context, String navigationPath, String regex) { + NavigationTreeModel treeModel = getSafeTreeModel(context); + return treeModel.findNode(navigationPath, regex); + } + + public NavigationTreeNode findNode(JAXXContext context, String navigationPath, Pattern regex) { + + NavigationTreeModel treeModel = getSafeTreeModel(context); + return treeModel.findNode(navigationPath, regex); + } + + public NavigationTreeNode findNode(JAXXContext context, String navigationPath, String regex, String suffix) { + + NavigationTreeModel treeModel = getSafeTreeModel(context); + + NavigationTreeNode navigationTreeNode = treeModel.findNode(navigationPath, regex); + if (navigationTreeNode != null && suffix != null) { + navigationTreeNode = treeModel.findNode(navigationTreeNode, suffix); + } + return navigationTreeNode; + } + + public NavigationTreeNode findNode(JAXXContext context, String navigationPath, Pattern regex, String suffix) { + + NavigationTreeModel treeModel = getSafeTreeModel(context); + + NavigationTreeNode navigationTreeNode = treeModel.findNode(navigationPath, regex); + if (navigationTreeNode != null && suffix != null) { + navigationTreeNode = treeModel.findNode(navigationTreeNode, suffix); + } + return navigationTreeNode; + } + + /** + * Sélection d'un noeud dans l'arbre de navigation à partir de son path. + * + * @param context le contexte applicatif + * @param tree l'arbre + * @param contextPath le path absolue du noeud dans l'arbre + */ + public void selectNode(JAXXContext context, JTree tree, String contextPath) { + NavigationTreeNode node = findNode(context, contextPath); + if (log.isDebugEnabled()) { + log.debug(contextPath + " :: " + node); + } + if (node != null) { + selectNode(context, tree, node); + } + } + + /** + * Sélection d'un noeud dans l'arbre de navigation. + * + * @param context le contexte applicatif + * @param tree l'arbre + * @param node le noeud à sélectionner dans l'arbre + */ + public void selectNode(JAXXContext context, JTree tree, NavigationTreeNode node) { + + NavigationTreeModel navigationModel = getSafeTreeModel(context); + if (log.isDebugEnabled()) { + log.debug(node); + } + TreePath path = new TreePath(navigationModel.getPathToRoot(node)); + tree.setSelectionPath(path); + tree.scrollPathToVisible(path); + if (!node.isLeaf() && !tree.isExpanded(path)) { + // expand the node to avoid a click :) + tree.expandPath(path); + } + } + + public NavigationTreeModel getSafeTreeModel(JAXXContext context) throws NullPointerException { + NavigationTreeModel treeModel = getTreeModel(context); + if (treeModel == null) { + throw new NullPointerException("could not find tree model with key " + getTreeModelContextEntry() + " in context " + context); + } + return treeModel; + } +} Property changes on: trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/navigation/NavigationTreeHelper.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL Modified: trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/navigation/NavigationUtil.java =================================================================== --- trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/navigation/NavigationUtil.java 2009-10-03 23:06:18 UTC (rev 1550) +++ trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/navigation/NavigationUtil.java 2009-10-03 23:07:51 UTC (rev 1551) @@ -13,27 +13,77 @@ /** * Usefull methods on {@link NavigationTreeModel} and others. * + * The class is deprecated since it is not safe context, and can not be used + * with more than one navigation tree in a same context. + * + * Now use the {@link NavigationTreeContextHelper} and {@link NavigationTreeHelper} + * which are context safe and more friendly. + * * @author chemit + * * @see jaxx.runtime.swing.navigation.NavigationTreeModel * @see jaxx.runtime.swing.navigation.NavigationTreeModel.NavigationTreeNode + * + * @deprecated since 1.7.2, prefer use a {@link NavigationTreeHelper} or + * {@link NavigationTreeContextHelper} to access data releated to a tree navigation */ +@Deprecated public class NavigationUtil { /** to use log facility, just put in your code: log.info(\"...\"); */ static private final Log log = LogFactory.getLog(NavigationUtil.class); + /** + * + * @param context + * @return + * @deprecated since 1.7.2, prefer use a {@link NavigationTreeContextHelper} + * to access data releated to a tree navigation + */ + @Deprecated public static String getCurrentNavigationNath(JAXXContext context) { return NavigationTreeSelectionAdapter.NAVIGATION_SELECTED_PATH_ENTRY_DEF.getContextValue(context); } + /** + * + * @param context + * @return + * @deprecated since 1.7.2, prefer use a {@link NavigationTreeContextHelper} + * to access data releated to a tree navigation + */ + @Deprecated public static NavigationTreeNode getSelectedNode(JAXXContext context) { return NavigationTreeSelectionAdapter.NAVIGATION_SELECTED_NODE_ENTRY_DEF.getContextValue(context); } + /** + * + * @param <O> + * @param context + * @param clazz + * @return + * @deprecated since 1.7.2, prefer use a {@link NavigationTreeContextHelper} + * to access data releated to a tree navigation + */ + @Deprecated public static <O> O getSelectedBean(JAXXContext context, Class<O> clazz) { return context.getContextValue(clazz, NavigationTreeSelectionAdapter.NAVIGATION_SELECTED_BEAN); } + /** + * + * @param context + * @param contextKey + * @param navigationPath + * @return + * @throws InvocationTargetException + * @throws NoSuchMethodException + * @throws IllegalAccessException + * @deprecated since 1.7.2, prefer use a {@link NavigationTreeHelper} + * to access data releated to a tree navigation + */ + @Deprecated public static Object getContextValue(JAXXContext context, String contextKey, String navigationPath) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException { NavigationTreeModel navigationModel = context.getContextValue(NavigationTreeModel.class, contextKey); @@ -41,6 +91,16 @@ return navigationModel.getJAXXContextValue(context, navigationPath); } + /** + * + * @param context + * @param contextKey + * @param navigationPath + * @return + * @deprecated since 1.7.2, prefer use a {@link NavigationTreeHelper} + * to access data releated to a tree navigation + */ + @Deprecated public static NavigationTreeNode findNode(JAXXContext context, String contextKey, String navigationPath) { NavigationTreeModel navigationModel = context.getContextValue(NavigationTreeModel.class, contextKey); @@ -48,6 +108,17 @@ return navigationModel.findNode(navigationPath); } + /** + * + * @param context + * @param contextKey + * @param navigationPath + * @param regex + * @return + * @deprecated since 1.7.2, prefer use a {@link NavigationTreeHelper} + * to access data releated to a tree navigation + */ + @Deprecated public static NavigationTreeNode findNode(JAXXContext context, String contextKey, String navigationPath, String regex) { NavigationTreeModel navigationModel = context.getContextValue(NavigationTreeModel.class, contextKey); @@ -55,7 +126,17 @@ return navigationModel.findNode(navigationPath, regex); } - + /** + * + * @param context + * @param contextKey + * @param navigationPath + * @param regex + * @return + * @deprecated since 1.7.2, prefer use a {@link NavigationTreeHelper} + * to access data releated to a tree navigation + */ + @Deprecated public static NavigationTreeNode findNode(JAXXContext context, String contextKey, String navigationPath, Pattern regex) { NavigationTreeModel navigationModel = context.getContextValue(NavigationTreeModel.class, contextKey); @@ -63,6 +144,18 @@ return navigationModel.findNode(navigationPath, regex); } + /** + * + * @param context + * @param contextKey + * @param navigationPath + * @param regex + * @param suffix + * @return + * @deprecated since 1.7.2, prefer use a {@link NavigationTreeHelper} + * to access data releated to a tree navigation + */ + @Deprecated public static NavigationTreeNode findNode(JAXXContext context, String contextKey, String navigationPath, String regex, String suffix) { NavigationTreeModel navigationModel = context.getContextValue(NavigationTreeModel.class, contextKey); @@ -74,6 +167,18 @@ return navigationTreeNode; } + /** + * + * @param context + * @param contextKey + * @param navigationPath + * @param regex + * @param suffix + * @return + * @deprecated since 1.7.2, prefer use a {@link NavigationTreeHelper} + * to access data releated to a tree navigation + */ + @Deprecated public static NavigationTreeNode findNode(JAXXContext context, String contextKey, String navigationPath, Pattern regex, String suffix) { NavigationTreeModel navigationModel = context.getContextValue(NavigationTreeModel.class, contextKey); @@ -96,7 +201,7 @@ protected Class<?> internalClass; protected String rendererCachedValue; - + private static final long serialVersionUID = -1238962588426200861L; public NodeRenderer(String libelle) {
participants (1)
-
tchemit@users.nuiton.org