ini.trakem2.tree
Class DNDTree

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.JTree
                  extended by ini.trakem2.tree.DNDTree
All Implemented Interfaces:
java.awt.event.KeyListener, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, java.util.EventListener, javax.accessibility.Accessible, javax.swing.event.TreeExpansionListener, javax.swing.Scrollable
Direct Known Subclasses:
LayerTree, ProjectTree, TemplateTree

public abstract class DNDTree
extends javax.swing.JTree
implements javax.swing.event.TreeExpansionListener, java.awt.event.KeyListener

A JTree which has a built-in drag and drop feature. Adapted from freely available code by DeuDeu from http://forum.java.sun.com/thread.jspa?threadID=296255&start=0&tstart=0

See Also:
Serialized Form

Nested Class Summary
protected  class DNDTree.NodeRenderer
           
 
Nested classes/interfaces inherited from class javax.swing.JTree
javax.swing.JTree.AccessibleJTree, javax.swing.JTree.DropLocation, javax.swing.JTree.DynamicUtilTreeNode, javax.swing.JTree.EmptySelectionModel, javax.swing.JTree.TreeModelHandler, javax.swing.JTree.TreeSelectionRedirector
 
Nested classes/interfaces inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BaselineResizeBehavior, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
protected  java.awt.Color background
           
protected  Dispatcher dispatcher
           
protected  Project project
           
 
Fields inherited from class javax.swing.JTree
ANCHOR_SELECTION_PATH_PROPERTY, CELL_EDITOR_PROPERTY, CELL_RENDERER_PROPERTY, cellEditor, cellRenderer, editable, EDITABLE_PROPERTY, EXPANDS_SELECTED_PATHS_PROPERTY, INVOKES_STOP_CELL_EDITING_PROPERTY, invokesStopCellEditing, LARGE_MODEL_PROPERTY, largeModel, LEAD_SELECTION_PATH_PROPERTY, ROOT_VISIBLE_PROPERTY, rootVisible, ROW_HEIGHT_PROPERTY, rowHeight, SCROLLS_ON_EXPAND_PROPERTY, scrollsOnExpand, SELECTION_MODEL_PROPERTY, selectionModel, selectionRedirector, SHOWS_ROOT_HANDLES_PROPERTY, showsRootHandles, TOGGLE_CLICK_COUNT_PROPERTY, toggleClickCount, TREE_MODEL_PROPERTY, treeModel, treeModelListener, VISIBLE_ROW_COUNT_PROPERTY, visibleRowCount
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
DNDTree(Project project, javax.swing.tree.DefaultMutableTreeNode root, java.awt.Color background)
           
 
Method Summary
protected  javax.swing.tree.DefaultMutableTreeNode addChild(Thing child, javax.swing.tree.DefaultMutableTreeNode parent_node)
          Appends at the end of the parent_node child list, and waits until the tree's UI is updated.
 void addLeafs(java.util.List<? extends Thing> leafs, java.lang.Runnable after)
          Will add only those for which a node doesn't exist already.
 void autoscroll(java.awt.Point cursorLocation)
           
protected  DNDTree.NodeRenderer createNodeRenderer()
          Subclasses should override this method to return a subclass of DNDTree.NodeRenderer
 void destroy()
           
 Thing duplicate(java.util.HashMap<Thing,java.lang.Boolean> expanded_state)
          Shallow copy of the tree: returns a clone of the root node and cloned children, recursively, with all Thing cloned as well, but the Thing object is the same.
static void expandAllNodes(DNDTree tree, javax.swing.tree.DefaultMutableTreeNode root_node)
          Convenient method.
static void expandAllNodes(javax.swing.JTree tree, javax.swing.tree.TreePath path)
           
static boolean expandNode(DNDTree tree, javax.swing.tree.DefaultMutableTreeNode node)
           
static javax.swing.tree.DefaultMutableTreeNode findNode(java.lang.Object thing_ob, javax.swing.JTree tree)
          Find the node in the tree that contains the given Thing.
static javax.swing.tree.DefaultMutableTreeNode findNode2(java.lang.Object project_ob, javax.swing.JTree tree)
          Find the node in the tree that contains a Thing which contains the given project_ob.
 java.awt.Insets getAutoscrollInsets()
           
 java.util.HashMap<Thing,java.lang.Boolean> getExpandedStates()
          Get the map of Thing vs.
 java.lang.String getInfo()
           
 javax.swing.tree.DefaultMutableTreeNode getRoot()
           
 javax.swing.tree.DefaultMutableTreeNode getRootNode()
           
protected abstract  Thing getRootThing()
           
 boolean isExpanded(javax.swing.tree.DefaultMutableTreeNode node)
           
 boolean isExpanded(Thing thing)
          Check if there is a node holding the given Thing, and whether such node is expanded.
 void keyPressed(java.awt.event.KeyEvent ke)
           
 void keyReleased(java.awt.event.KeyEvent ke)
           
 void keyTyped(java.awt.event.KeyEvent ke)
           
static javax.swing.tree.DefaultMutableTreeNode makeNode(Thing thing)
           
static javax.swing.tree.DefaultMutableTreeNode makeNode(Thing thing, boolean childless_nested)
          Returns a DefaultMutableTreeNode with all its children.
protected  boolean processKeyBinding(javax.swing.KeyStroke ks, java.awt.event.KeyEvent e, int condition, boolean pressed)
          Prevent processing.
protected  void processKeyEvent(java.awt.event.KeyEvent ke)
          Removing all KeyListener and ActionMap is not enough: one must override this method to stop the JTree from reacting to keys.
 void rebuild()
          Rebuilds the entire tree, starting at the root Thing object.
 void rebuild(javax.swing.tree.DefaultMutableTreeNode node, boolean repaint)
          Rebuilds the entire tree, from the given node downward.
 void rebuild(Thing thing)
          Rebuilds the entire tree, starting at the given Thing object.
protected  boolean removeNode(javax.swing.tree.DefaultMutableTreeNode node)
           
 void reset(java.util.HashMap<Thing,java.lang.Boolean> expanded_state)
          Set the root Thing, and the expanded state of all nodes if @param expanded_state is not null.
static void selectNode(java.lang.Object ob, DNDTree tree)
          Deselects whatever node is selected in the tree, and tries to select the one that contains the given object.
protected  void set(javax.swing.tree.DefaultMutableTreeNode root, Thing root_thing, java.util.HashMap<Thing,java.lang.Boolean> expanded_state)
          Recursive
 void setExpandedSilently(javax.swing.tree.DefaultMutableTreeNode node, boolean b)
           
 void setExpandedSilently(Thing thing, boolean b)
          Does not incur in firing a TreeExpansion event, and affects the node only, not any of its parents.
 void treeCollapsed(javax.swing.event.TreeExpansionEvent tee)
          Sense node expansion events (the method name is missleading).
 void treeExpanded(javax.swing.event.TreeExpansionEvent tee)
           
 void updateList(javax.swing.tree.DefaultMutableTreeNode node)
          Rebuilds the part of the tree under the given node, one level deep only, for reordering purposes.
 void updateList(Thing thing)
          Rebuilds the part of the tree under the given node, one level deep only, for reordering purposes.
 void updateUILater()
          Overriding to fix synchronization issues: the path changes while the multithreaded swing attempts to repaint it, so we "invoke later".
 
Methods inherited from class javax.swing.JTree
addSelectionInterval, addSelectionPath, addSelectionPaths, addSelectionRow, addSelectionRows, addTreeExpansionListener, addTreeSelectionListener, addTreeWillExpandListener, cancelEditing, clearSelection, clearToggledPaths, collapsePath, collapseRow, convertValueToText, createTreeModel, createTreeModelListener, expandPath, expandRow, fireTreeCollapsed, fireTreeExpanded, fireTreeWillCollapse, fireTreeWillExpand, fireValueChanged, getAccessibleContext, getAnchorSelectionPath, getCellEditor, getCellRenderer, getClosestPathForLocation, getClosestRowForLocation, getDefaultTreeModel, getDescendantToggledPaths, getDragEnabled, getDropLocation, getDropMode, getEditingPath, getExpandedDescendants, getExpandsSelectedPaths, getInvokesStopCellEditing, getLastSelectedPathComponent, getLeadSelectionPath, getLeadSelectionRow, getMaxSelectionRow, getMinSelectionRow, getModel, getNextMatch, getPathBetweenRows, getPathBounds, getPathForLocation, getPathForRow, getPreferredScrollableViewportSize, getRowBounds, getRowCount, getRowForLocation, getRowForPath, getRowHeight, getScrollableBlockIncrement, getScrollableTracksViewportHeight, getScrollableTracksViewportWidth, getScrollableUnitIncrement, getScrollsOnExpand, getSelectionCount, getSelectionModel, getSelectionPath, getSelectionPaths, getSelectionRows, getShowsRootHandles, getToggleClickCount, getToolTipText, getTreeExpansionListeners, getTreeSelectionListeners, getTreeWillExpandListeners, getUI, getUIClassID, getVisibleRowCount, hasBeenExpanded, isCollapsed, isCollapsed, isEditable, isEditing, isExpanded, isExpanded, isFixedRowHeight, isLargeModel, isPathEditable, isPathSelected, isRootVisible, isRowSelected, isSelectionEmpty, isVisible, makeVisible, paramString, removeDescendantSelectedPaths, removeDescendantToggledPaths, removeSelectionInterval, removeSelectionPath, removeSelectionPaths, removeSelectionRow, removeSelectionRows, removeTreeExpansionListener, removeTreeSelectionListener, removeTreeWillExpandListener, scrollPathToVisible, scrollRowToVisible, setAnchorSelectionPath, setCellEditor, setCellRenderer, setDragEnabled, setDropMode, setEditable, setExpandedState, setExpandsSelectedPaths, setInvokesStopCellEditing, setLargeModel, setLeadSelectionPath, setModel, setRootVisible, setRowHeight, setScrollsOnExpand, setSelectionInterval, setSelectionModel, setSelectionPath, setSelectionPaths, setSelectionRow, setSelectionRows, setShowsRootHandles, setToggleClickCount, setUI, setVisibleRowCount, startEditingAtPath, stopEditing, treeDidChange, updateUI
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

dispatcher

protected final Dispatcher dispatcher

project

protected final Project project

background

protected final java.awt.Color background
Constructor Detail

DNDTree

public DNDTree(Project project,
               javax.swing.tree.DefaultMutableTreeNode root,
               java.awt.Color background)
Method Detail

processKeyEvent

protected void processKeyEvent(java.awt.event.KeyEvent ke)
Removing all KeyListener and ActionMap is not enough: one must override this method to stop the JTree from reacting to keys.

Overrides:
processKeyEvent in class javax.swing.JComponent

processKeyBinding

protected boolean processKeyBinding(javax.swing.KeyStroke ks,
                                    java.awt.event.KeyEvent e,
                                    int condition,
                                    boolean pressed)
Prevent processing.

Overrides:
processKeyBinding in class javax.swing.JComponent

createNodeRenderer

protected DNDTree.NodeRenderer createNodeRenderer()
Subclasses should override this method to return a subclass of DNDTree.NodeRenderer


autoscroll

public void autoscroll(java.awt.Point cursorLocation)

getAutoscrollInsets

public java.awt.Insets getAutoscrollInsets()

expandAllNodes

public static void expandAllNodes(javax.swing.JTree tree,
                                  javax.swing.tree.TreePath path)

expandNode

public static boolean expandNode(DNDTree tree,
                                 javax.swing.tree.DefaultMutableTreeNode node)

expandAllNodes

public static void expandAllNodes(DNDTree tree,
                                  javax.swing.tree.DefaultMutableTreeNode root_node)
Convenient method.


makeNode

public static javax.swing.tree.DefaultMutableTreeNode makeNode(Thing thing)

makeNode

public static javax.swing.tree.DefaultMutableTreeNode makeNode(Thing thing,
                                                               boolean childless_nested)
Returns a DefaultMutableTreeNode with all its children. If this is called on a root node, it will fill in the whole tree. The Attribute nodes are only added it their value is non-null, and at the top of the list.


findNode

public static javax.swing.tree.DefaultMutableTreeNode findNode(java.lang.Object thing_ob,
                                                               javax.swing.JTree tree)
Find the node in the tree that contains the given Thing.


findNode2

public static javax.swing.tree.DefaultMutableTreeNode findNode2(java.lang.Object project_ob,
                                                                javax.swing.JTree tree)
Find the node in the tree that contains a Thing which contains the given project_ob.


selectNode

public static void selectNode(java.lang.Object ob,
                              DNDTree tree)
Deselects whatever node is selected in the tree, and tries to select the one that contains the given object.


destroy

public void destroy()

updateUILater

public void updateUILater()
Overriding to fix synchronization issues: the path changes while the multithreaded swing attempts to repaint it, so we "invoke later". Hilarious.


rebuild

public void rebuild()
Rebuilds the entire tree, starting at the root Thing object.


rebuild

public void rebuild(Thing thing)
Rebuilds the entire tree, starting at the given Thing object.


rebuild

public void rebuild(javax.swing.tree.DefaultMutableTreeNode node,
                    boolean repaint)
Rebuilds the entire tree, from the given node downward.


updateList

public void updateList(Thing thing)
Rebuilds the part of the tree under the given node, one level deep only, for reordering purposes.


updateList

public void updateList(javax.swing.tree.DefaultMutableTreeNode node)
Rebuilds the part of the tree under the given node, one level deep only, for reordering purposes.


getRootNode

public javax.swing.tree.DefaultMutableTreeNode getRootNode()

setExpandedSilently

public void setExpandedSilently(Thing thing,
                                boolean b)
Does not incur in firing a TreeExpansion event, and affects the node only, not any of its parents.


setExpandedSilently

public void setExpandedSilently(javax.swing.tree.DefaultMutableTreeNode node,
                                boolean b)

getExpandedStates

public java.util.HashMap<Thing,java.lang.Boolean> getExpandedStates()
Get the map of Thing vs. expanded state for all nodes that have children.


isExpanded

public boolean isExpanded(Thing thing)
Check if there is a node holding the given Thing, and whether such node is expanded.


isExpanded

public boolean isExpanded(javax.swing.tree.DefaultMutableTreeNode node)

treeCollapsed

public void treeCollapsed(javax.swing.event.TreeExpansionEvent tee)
Sense node expansion events (the method name is missleading).

Specified by:
treeCollapsed in interface javax.swing.event.TreeExpansionListener

treeExpanded

public void treeExpanded(javax.swing.event.TreeExpansionEvent tee)
Specified by:
treeExpanded in interface javax.swing.event.TreeExpansionListener

getInfo

public java.lang.String getInfo()

getRoot

public final javax.swing.tree.DefaultMutableTreeNode getRoot()

addChild

protected javax.swing.tree.DefaultMutableTreeNode addChild(Thing child,
                                                           javax.swing.tree.DefaultMutableTreeNode parent_node)
Appends at the end of the parent_node child list, and waits until the tree's UI is updated.


addLeafs

public void addLeafs(java.util.List<? extends Thing> leafs,
                     java.lang.Runnable after)
Will add only those for which a node doesn't exist already.


removeNode

protected boolean removeNode(javax.swing.tree.DefaultMutableTreeNode node)

duplicate

public Thing duplicate(java.util.HashMap<Thing,java.lang.Boolean> expanded_state)
Shallow copy of the tree: returns a clone of the root node and cloned children, recursively, with all Thing cloned as well, but the Thing object is the same.


reset

public void reset(java.util.HashMap<Thing,java.lang.Boolean> expanded_state)
Set the root Thing, and the expanded state of all nodes if @param expanded_state is not null. Used for restoring purposes from an undo step.


getRootThing

protected abstract Thing getRootThing()

set

protected void set(javax.swing.tree.DefaultMutableTreeNode root,
                   Thing root_thing,
                   java.util.HashMap<Thing,java.lang.Boolean> expanded_state)
Recursive


keyPressed

public void keyPressed(java.awt.event.KeyEvent ke)
Specified by:
keyPressed in interface java.awt.event.KeyListener

keyReleased

public void keyReleased(java.awt.event.KeyEvent ke)
Specified by:
keyReleased in interface java.awt.event.KeyListener

keyTyped

public void keyTyped(java.awt.event.KeyEvent ke)
Specified by:
keyTyped in interface java.awt.event.KeyListener