Class OntologyBrowser

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible

public class OntologyBrowser extends JPanel
A reusable panel that presents one or more hierarchical ontologies as searchable checkbox trees. Users pick terms and selections are serialized as "::"-delimited hierarchical strings (e.g., "Isocortex::Visual areas::VISp").

Ontology tabs can be added from existing DefaultTreeModels (e.g., Allen CCF via AllenUtils.getTreeModel(boolean)), or from simple nested Maps for user-defined term hierarchies.

Author:
Tiago Ferreira
See Also:
  • Field Details

    • SEPARATOR

      public static final String SEPARATOR
      Separator used for serializing hierarchical paths.
      See Also:
    • LEAF_ONLY_FORMATTER

      public static final Function<TreePath,String> LEAF_ONLY_FORMATTER
      A predefined path formatter that returns only the leaf node's name. For BrainAnnotation user objects, returns the acronym (if available and different from the name) prefixed to the name, e.g., "PB: protocerebral bridge". Otherwise falls back to toString(). Useful when the full hierarchical path would be too long for display.
    • ACRONYM_ONLY_FORMATTER

      public static final Function<TreePath,String> ACRONYM_ONLY_FORMATTER
      A predefined path formatter that builds a "::"-delimited string using only acronyms. For each node in the path (excluding the root), the BrainAnnotation.acronym() is used if available, otherwise the name. E.g., "CX::PB" instead of "adult central complex (CX)::protocerebral bridge (PB) (3669)".
  • Constructor Details

    • OntologyBrowser

      public OntologyBrowser()
      Creates an empty OntologyBrowser with multi-selection enabled. Add ontology tabs via addOntology(String, DefaultTreeModel) or convenience methods.
    • OntologyBrowser

      public OntologyBrowser(boolean singleSelection)
      Creates an empty OntologyBrowser.
      Parameters:
      singleSelection - if true, only one term can be checked at a time across all tabs (radio-group behavior)
  • Method Details

    • addOntology

      public OntologyBrowser.OntologyTab addOntology(String tabLabel, DefaultTreeModel model)
      Adds an ontology tab backed by an existing DefaultTreeModel.
      Parameters:
      tabLabel - the tab title
      model - the tree model (nodes should have meaningful toString() on their user objects)
      Returns:
      the created OntologyBrowser.OntologyTab for further customization
    • addAllenCCFOntology

      public OntologyBrowser.OntologyTab addAllenCCFOntology()
      Convenience method to add the Allen CCF ontology as a tab, including all compartments.
      Returns:
      the created OntologyBrowser.OntologyTab
    • addAllenCCFOntology

      public OntologyBrowser.OntologyTab addAllenCCFOntology(boolean meshesOnly)
      Adds the Allen CCF ontology as a tab.
      Parameters:
      meshesOnly - if true, only compartments with available meshes are included in the tree
      Returns:
      the created OntologyBrowser.OntologyTab
    • addAllOntologies

      public void addAllOntologies()
      Adds all the default ontologies as tabs.
    • addDrosophilaOntology

      public OntologyBrowser.OntologyTab addDrosophilaOntology()
      Adds the Drosophila adult brain ontology (FBbt) as a tab.
      Returns:
      the created OntologyBrowser.OntologyTab
    • addOntology

      public OntologyBrowser.OntologyTab addOntology(String tabLabel, Map<String,List<String>> hierarchy)
      Adds a custom ontology tab from a nested map. Each key is a parent term and the value is the list of child terms. A null or empty list means the key is a leaf. The root of the tree is not shown.

      Example:

       Map<String, List<String>> map = new LinkedHashMap<>();
       map.put("Cortex", List.of("L1", "L2/3", "L4", "L5", "L6"));
       map.put("White Matter", List.of("cc", "ic"));
       browser.addOntology("Histological Landmarks", map);
       
      Parameters:
      tabLabel - the tab title
      hierarchy - a map of parent term to child terms (single-level)
      Returns:
      the created OntologyBrowser.OntologyTab
    • addDeepOntology

      public OntologyBrowser.OntologyTab addDeepOntology(String tabLabel, Map<String,Object> tree)
      Adds a custom ontology tab built from a deeply nested map. Each key maps to either another map (subtree) or null (leaf).
      Parameters:
      tabLabel - the tab title
      tree - a recursively nested map representing the ontology
      Returns:
      the created OntologyBrowser.OntologyTab
    • getSelectedPaths

      public List<String> getSelectedPaths()
      Returns the checked selections from all tabs as "::"- delimited hierarchical paths. The root node is excluded from the path.
      Returns:
      list of serialized paths, e.g., ["Isocortex::Visual areas::VISp"]
    • getSelectedLeaves

      public List<String> getSelectedLeaves()
      Returns only the leaf-level names of checked selections (without ancestor prefixes).
      Returns:
      list of leaf names
    • getCheckedUserObjects

      public <T> List<T> getCheckedUserObjects(Class<T> type)
      Returns the checked user objects from all tabs, cast to the given type.
      Type Parameters:
      T - the expected user-object type
      Parameters:
      type - the class to cast to
      Returns:
      list of checked user objects
    • addSelectionListener

      public void addSelectionListener(Consumer<List<String>> listener)
      Registers a listener that is notified whenever the checked selection changes in any tab.
      Parameters:
      listener - the selection listener
    • clearSelection

      public void clearSelection()
      Clears all checked selections across all tabs.
    • setPathFormatter

      public void setPathFormatter(Function<TreePath,String> formatter)
      Sets a custom formatter for converting checked TreePaths into strings. By default, paths are serialized as "::"- delimited hierarchical strings via serializePath(TreePath).
      Parameters:
      formatter - the function to apply to each checked tree path
      See Also:
    • getTabCount

      public int getTabCount()
      Returns the number of ontology tabs.
    • getTab

      public OntologyBrowser.OntologyTab getTab(int index)
      Returns the tab at the given index.
      Parameters:
      index - the tab index
      Returns:
      the ontology tab
    • showDialog

      public List<String> showDialog(Component parent, String title)
      Shows this browser in a modal dialog and returns the selected paths, or null if the user cancelled.
      Parameters:
      parent - the parent component for dialog positioning
      title - the dialog title
      Returns:
      the selected hierarchical paths, or null if cancelled
    • showModelessDialog

      public JDialog showModelessDialog(Component parent, String title)
      Shows this browser in a modeless (non-modal) dialog for reference browsing. The dialog stays open while the user works in other windows. Includes an info button and a copy-to-clipboard button.
      Parameters:
      parent - the parent component for initial positioning
      title - the dialog title
      Returns:
      the modeless JDialog
    • showSelectionInfo

      public void showSelectionInfo(Component parent)
      Displays an HTML info table for the currently checked compartments. Works with any BrainAnnotation user objects (Allen CCF, Drosophila FBbt, etc.).
      Parameters:
      parent - the parent component for the info dialog
    • copySelectionToClipboard

      public void copySelectionToClipboard()
      Copies the checked terms to the system clipboard. Each term is placed on its own line, formatted as "acronym (id): name".
    • serializePath

      public static String serializePath(TreePath treePath)
      Serializes a TreePath as a "::"-delimited string, excluding the root node.
      Parameters:
      treePath - the tree path
      Returns:
      the serialized hierarchical string