All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jaxx.runtime.swing.navigation.NavigationTreeContextHelper Maven / Gradle / Ivy

/*
 * *##% 
 * JAXX Runtime
 * Copyright (C) 2008 - 2009 CodeLutin
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 *
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * ##%*
 */
package jaxx.runtime.swing.navigation;

import jaxx.runtime.JAXXContext;
import jaxx.runtime.JAXXUtil;
import jaxx.runtime.context.JAXXContextEntryDef;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdesktop.swingx.JXTreeTable;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JTree;

/**
 * To help getting and setting navigation tree objects from a {@link JAXXContext}.
 * 

* There is seven types of data which can be hold in a context : *

    *
  • tree : the tree
  • *
  • tree-tablr : the jx tree table
  • *
  • tree model : the navigation tree model
  • *
  • tree handler : the navigation tree handler
  • *
  • selected path : the navigation path of the selected node
  • *
  • selected node : the selected node
  • *
  • selected bean : the selected bean
  • *
*

* To make possible the use of more than one navigation tree system in a same * context, we MUST distinguish the context entries definition. For this * purpose, entries definition are normalized and prefixed by a unique {@link #prefix}. *

* Here is the keys mapping : *

    *
  • tree : {@code prefix + "-tree"}
  • *
  • tree-table : {@code prefix + "-tree-table"}
  • *
  • tree model : {@code prefix + "-tree-model"}
  • *
  • tree handler : {@code prefix + "-tree-handler"}
  • *
  • selected path : {@code prefix + "-selected-path"}
  • *
  • selected node : {@code prefix + "-selected-node"}
  • *
  • selected bean : {@code prefix + "-selected-bean"}
  • *
* * @author chemit * @since 1.7.2 */ public class NavigationTreeContextHelper { /** * Logger */ static private final Log log = LogFactory.getLog(NavigationTreeContextHelper.class); protected final String prefix; protected JAXXContextEntryDef> selectedPathsContextEntry; protected JAXXContextEntryDef> selectedBeansContextEntry; protected JAXXContextEntryDef> selectedNodesContextEntry; protected JAXXContextEntryDef modelContextEntry; protected JAXXContextEntryDef handlerContextEntry; protected JAXXContextEntryDef treeContextEntry; protected JAXXContextEntryDef treeTableContextEntry; public NavigationTreeContextHelper(String prefix) { this.prefix = prefix; treeContextEntry = JAXXUtil.newContextEntryDef(prefix + "-tree", JTree.class); treeTableContextEntry = JAXXUtil.newContextEntryDef(prefix + "-tree-table", JXTreeTable.class); modelContextEntry = JAXXUtil.newContextEntryDef(prefix + "-model", NavigationModel.class); handlerContextEntry = JAXXUtil.newContextEntryDef(prefix + "-handler", NavigationTreeHandler.class); selectedBeansContextEntry = JAXXUtil.newListContextEntryDef(prefix + "-selected-beans"); selectedNodesContextEntry = JAXXUtil.newListContextEntryDef(prefix + "-selected-nodes"); selectedPathsContextEntry = JAXXUtil.newListContextEntryDef(prefix + "-selected-paths"); } public String getPrefix() { return prefix; } public JTree getTree(JAXXContext context) { JTree r = getTreeContextEntry().getContextValue(context); return r; } public JXTreeTable getTreeTable(JAXXContext context) { JXTreeTable r = getTreeTableContextEntry().getContextValue(context); return r; } public NavigationModel getModel(JAXXContext context) { NavigationModel r = getModelContextEntry().getContextValue(context); return r; } /** * @param context where to find model * @deprecated since 2.0 please use {@link #getModel(JAXXContext)}, will * be remove soon * @return the specific tree model */ @Deprecated public NavigationTreeModel getTreeModel(JAXXContext context) { NavigationModel r = getModel(context); if (r instanceof NavigationTreeModel) { return (NavigationTreeModel) r; } return null; } public NavigationTreeHandler getTreeHandler(JAXXContext context) { NavigationTreeHandler r = getTreeHandlerContextEntry().getContextValue(context); return r; } public String getSelectedPath(JAXXContext context) { String result = getSelectedValue(getSelectedPathContextEntry(),context); return result; } public List getSelectedPaths(JAXXContext context) { return getSelectedPathContextEntry().getContextValue(context); } public NavigationTreeNode getSelectedNode(JAXXContext context) { NavigationTreeNode result = getSelectedValue(getSelectedNodeContextEntry(),context); return result; } public List getSelectedNodes(JAXXContext context) { return getSelectedNodeContextEntry().getContextValue(context); } public Object getSelectedBean(JAXXContext context) { Object result = getSelectedValue(getSelectedBeanContextEntry(),context); return result; } public List getSelectedBeans(JAXXContext context) { return getSelectedBeanContextEntry().getContextValue(context); } /** * @param context where to store model * @param model model to store * @deprecated since 2.0 please use {@link #setModel(JAXXContext, NavigationModel)}, * will be remove soon */ @Deprecated public void setTreeModel(JAXXContext context, NavigationModel model) { setModel(context, model); } public void setModel(JAXXContext context, NavigationModel model) { getModelContextEntry().setContextValue(context, model); } public void setTree(JAXXContext context, JTree tree) { getTreeContextEntry().setContextValue(context, tree); } public void setTreeTable(JAXXContext context, JXTreeTable treeTable) { getTreeTableContextEntry().setContextValue(context, treeTable); } public void setTreeHandler(JAXXContext context, NavigationTreeHandler handler) { getTreeHandlerContextEntry().setContextValue(context, handler); } public void setSelectedPath(JAXXContext context, String path) { setSelectedValue(getSelectedPathContextEntry(), context, path); } public void setSelectedPaths(JAXXContext context, List paths) { setSelectedValues(getSelectedPathContextEntry(), context, paths); } public void setSelectedNode(JAXXContext context, NavigationTreeNode node) { setSelectedValue(getSelectedNodeContextEntry(), context, node); } public void setSelectedNodes(JAXXContext context, List nodes) { setSelectedValues(getSelectedNodeContextEntry(), context, nodes); } public void setSelectedBean(JAXXContext context, Object bean) { setSelectedValue(getSelectedBeanContextEntry(), context, bean); } public void setSelectedBeans(JAXXContext context, List beans) { setSelectedValues(getSelectedBeanContextEntry(), context, beans); } protected JAXXContextEntryDef getModelContextEntry() { return modelContextEntry; } protected JAXXContextEntryDef getTreeHandlerContextEntry() { return handlerContextEntry; } protected JAXXContextEntryDef> getSelectedBeanContextEntry() { return selectedBeansContextEntry; } protected JAXXContextEntryDef> getSelectedNodeContextEntry() { return selectedNodesContextEntry; } protected JAXXContextEntryDef> getSelectedPathContextEntry() { return selectedPathsContextEntry; } protected JAXXContextEntryDef getTreeContextEntry() { return treeContextEntry; } protected JAXXContextEntryDef getTreeTableContextEntry() { return treeTableContextEntry; } protected T getSelectedValue(JAXXContextEntryDef> entry, JAXXContext context) { List values = entry.getContextValue(context); T result = null; if (values != null && !values.isEmpty()) { if (values.size() > 1) { if (log.isWarnEnabled()) { log.warn("There is " + values.size() + " values selected, will return first one"); } } result = values.get(0); } return result; } protected void setSelectedValue(JAXXContextEntryDef> entry, JAXXContext context, T value) { if (value == null) { entry.removeContextValue(context); } else { List selecteds = new ArrayList(); selecteds.add(value); entry.setContextValue(context, selecteds); } } protected void setSelectedValues(JAXXContextEntryDef> entry, JAXXContext context, List values) { if (values == null || values.isEmpty()) { entry.removeContextValue(context); } else { entry.setContextValue(context, values); } } }