[Patch][gui] javax.swing.JTree

Michael Koch konqueror@gmx.de
Wed Dec 1 14:24:00 GMT 2004


Hi list,


I just commited the attached patch which addes much missing stuff to
javax.swing.JTree.


Michael


2004-12-01  Michael Koch  <konqueror@gmx.de>

	* javax/swing/JTree.java
	(ANCHOR_SELECTION_PATH_PROPERTY): Added @since tag.
	(LEAD_SELECTION_PATH_PROPERTY): Likewise.
	(EXPANDS_SELECTED_PATHS_PROPERTY): Likewise.
	(EXPANDED): New constant.
	(COLLAPSED): Likewise.
	(nodeStates): New field.
	(cellEditor): New method.
	(cellRenderer): Likewise.
	(selectionModel): Likewise.
	(treeModel): Likewise.
	(scrollPathToVisible): Likewise.
	(scrollRowToVisible): Likewise.
	(isCollapsed): Likewise.
	(isExpanded): Likewise.
	(clearToggledPaths): Likewise.
	(getDescendantToggledPaths): Likewise.
	(hasBeenExpanded): Likewise.
	(isVisible): Likewise.
	(isPathEditable): Likewise.

-------------- next part --------------
Index: javax/swing/JTree.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/javax/swing/JTree.java,v
retrieving revision 1.3.2.11
diff -u -r1.3.2.11 JTree.java
--- javax/swing/JTree.java	26 Nov 2004 20:45:10 -0000	1.3.2.11
+++ javax/swing/JTree.java	1 Dec 2004 13:45:22 -0000
@@ -70,14 +70,11 @@
 {
   private static final long serialVersionUID = 7559816092864483649L;
 
-  public static final String ANCHOR_SELECTION_PATH_PROPERTY = "anchorSelectionPath";
   public static final String CELL_EDITOR_PROPERTY = "cellEditor";
   public static final String CELL_RENDERER_PROPERTY = "cellRenderer";
   public static final String EDITABLE_PROPERTY = "editable";
-  public static final String EXPANDS_SELECTED_PATHS_PROPERTY = "expandsSelectedPaths";
   public static final String INVOKES_STOP_CELL_EDITING_PROPERTY = "invokesStopCellEditing";
   public static final String LARGE_MODEL_PROPERTY = "largeModel";
-  public static final String LEAD_SELECTION_PATH_PROPERTY = "leadSelectionPath";
   public static final String ROOT_VISIBLE_PROPERTY = "rootVisible";
   public static final String ROW_HEIGHT_PROPERTY = "rowHeight";
   public static final String SCROLLS_ON_EXPAND_PROPERTY = "scrollsOnExpand";
@@ -87,23 +84,40 @@
   public static final String TREE_MODEL_PROPERTY = "model";
   public static final String VISIBLE_ROW_COUNT_PROPERTY = "visibleRowCount";
 
+  /** @since 1.3 */
+  public static final String ANCHOR_SELECTION_PATH_PROPERTY = "anchorSelectionPath";
+  /** @since 1.3 */
+  public static final String LEAD_SELECTION_PATH_PROPERTY = "leadSelectionPath";
+  /** @since 1.3 */
+  public static final String EXPANDS_SELECTED_PATHS_PROPERTY = "expandsSelectedPaths";
+
+  private static final Object EXPANDED = new Object();
+  private static final Object COLLAPSED = new Object();
+  
   private boolean dragEnabled;
   private boolean expandsSelectedPaths;
   private TreePath anchorSelectionPath;
   private TreePath leadSelectionPath;
 
-  protected TreeCellEditor cellEditor;
-  protected TreeCellRenderer cellRenderer;
+  /*
+   * This contains the state of all nodes in the tree. Al/ entries map the
+   * TreePath of a note to to its state. Valid states are EXPANDED and
+   * COLLAPSED.  Nodes not in this Hashtable are assumed state COLLAPSED.
+   */
+  private Hashtable nodeStates;
+
+  protected transient TreeCellEditor cellEditor;
+  protected transient TreeCellRenderer cellRenderer;
   protected boolean editable;
   protected boolean invokesStopCellEditing;
   protected boolean largeModel;
   protected boolean rootVisible;
   protected int rowHeight;
   protected boolean scrollsOnExpand;
-  protected TreeSelectionModel selectionModel;
+  protected transient TreeSelectionModel selectionModel;
   protected boolean showsRootHandles;
   protected int toggleClickCount;
-  protected TreeModel treeModel;
+  protected transient TreeModel treeModel;
   protected int visibleRowCount;
 
   /**
@@ -757,6 +771,24 @@
     firePropertyChange(TOGGLE_CLICK_COUNT_PROPERTY, oldValue, count);
   }
 
+  public void scrollPathToVisible(TreePath path)
+  {
+    if (path == null)
+      return;
+
+    Rectangle rect = getPathBounds(path);
+
+    if (rect == null)
+      return;
+
+    scrollRectToVisible(rect);
+  }
+
+  public void scrollRowToVisible(int row)
+  {
+    scrollPathToVisible(getPathForRow(row));
+  }
+  
   public boolean getScrollsOnExpand()
   {
     return scrollsOnExpand;
@@ -1020,6 +1052,55 @@
     return 0;
   }
 
+  public boolean isCollapsed(TreePath path)
+  {
+    return ! isExpanded(path);
+  }
+
+  public boolean isCollapsed(int row)
+  {
+    if (row < 0 || row >= getRowCount())
+      return false;
+
+    TreePath path = getPathForRow(row);
+
+    if (path != null)
+      return isCollapsed(path);
+
+    return false;
+  }
+
+  public boolean isExpanded(TreePath path)
+  {
+    if (path == null)
+      return false;
+
+    Object state = nodeStates.get(path);
+
+    if ((state == null) || (state != EXPANDED))
+      return false;
+
+    TreePath parent = path.getParentPath();
+
+    if (parent != null)
+      return isExpanded(parent);
+    
+    return true;
+  }
+  
+  public boolean isExpanded(int row)
+  {
+    if (row < 0 || row >= getRowCount())
+      return false;
+
+    TreePath path = getPathForRow(row);
+
+    if (path != null)
+      return isExpanded(path);
+
+    return false;
+  }
+
   /**
    * @since 1.3
    */
@@ -1161,4 +1242,54 @@
 
     return null;
   }
+
+  protected void clearToggledPaths()
+  {
+    nodeStates.clear();
+  }
+
+  protected Enumeration getDescendantToggledPaths(TreePath parent)
+  {
+    if (parent == null)
+      return null;
+
+    Enumeration nodes = nodeStates.keys();
+    Vector result = new Vector();
+
+    while (nodes.hasMoreElements())
+      {
+        TreePath path = (TreePath) nodes.nextElement();
+
+        if (path.isDescendant(parent))
+          result.addElement(path);
+      }
+    
+    return result.elements();
+  }
+
+  public boolean hasBeenExpanded(TreePath path)
+  {
+    if (path == null)
+      return false;
+
+    return nodeStates.get(path) != null;
+  }
+
+  public boolean isVisible(TreePath path)
+  {
+    if (path == null)
+      return false;
+
+    TreePath parent = path.getParentPath();
+
+    if (parent == null)
+      return true; // Is root node.
+
+    return isExpanded(parent);
+  }
+
+  public boolean isPathEditable(TreePath path)
+  {    
+    return isEditable();
+  }
 }


More information about the Java-patches mailing list