Beispiel #1
0
 @Override
 public String toString() {
   StringBuilder result = new StringBuilder();
   if (root != null) {
     Deque<Pair<TreeNode, TreePath>> toPrint = new LinkedList<>();
     toPrint.push(new Pair<>(root, TreePath.EMPTY));
     while (!toPrint.isEmpty()) {
       Pair<TreeNode, TreePath> entry = toPrint.pop();
       TreeNode node = entry.getFirst();
       TreePath path = entry.getSecond();
       int pathLength = path.length();
       for (int i = 0; i < pathLength; i++) {
         if (i == pathLength - 1) {
           result.append(" +-");
         } else {
           result.append(path.isLeftAt(i) ? " | " : "   ");
         }
       }
       result.append(node).append('\n');
       if (node != null && !node.isTerminal()) {
         DecisionNode decisionNode = (DecisionNode) node;
         toPrint.push(new Pair<>(decisionNode.getRight(), path.extendRight()));
         toPrint.push(new Pair<>(decisionNode.getLeft(), path.extendLeft()));
       }
     }
   }
   return result.toString();
 }
 private Object internalGetElement(Object elementOrPath) {
   if (elementOrPath instanceof TreePath) {
     TreePath tp = (TreePath) elementOrPath;
     return tp.getLastSegment();
   }
   return elementOrPath;
 }
  /**
   * Invoked after the tree has drastically changed structure from a given node down. If the path
   * returned by e.getPath() is of length one and the first element does not identify the current
   * root node the first element should become the new root of the tree.
   *
   * <p>e.path() holds the path to the node.
   *
   * <p>e.childIndices() returns null.
   */
  public void treeStructureChanged(TreeModelEvent e) {
    if (e != null) {
      TreePath changedPath = SwingUtilities2.getTreePath(e, getModel());
      FHTreeStateNode changedNode = getNodeForPath(changedPath, false, false);

      // Check if root has changed, either to a null root, or
      // to an entirely new root.
      if (changedNode == root
          || (changedNode == null
              && ((changedPath == null && treeModel != null && treeModel.getRoot() == null)
                  || (changedPath != null && changedPath.getPathCount() <= 1)))) {
        rebuild(true);
      } else if (changedNode != null) {
        boolean wasExpanded, wasVisible;
        FHTreeStateNode parent = (FHTreeStateNode) changedNode.getParent();

        wasExpanded = changedNode.isExpanded();
        wasVisible = changedNode.isVisible();

        int index = parent.getIndex(changedNode);
        changedNode.collapse(false);
        parent.remove(index);

        if (wasVisible && wasExpanded) {
          int row = changedNode.getRow();
          parent.resetChildrenRowsFrom(row, index, changedNode.getChildIndex());
          changedNode = getNodeForPath(changedPath, false, true);
          changedNode.expand();
        }
        if (treeSelectionModel != null && wasVisible && wasExpanded)
          treeSelectionModel.resetRowSelection();
        if (wasVisible) this.visibleNodesChanged();
      }
    }
  }
Beispiel #4
0
  public Object getNode(TreePath path) {
    if (path == TreePath.ROOT_PATH) {
      return model.getRoot();
    }

    Object parent = getNode(path.getParentPath());
    if (parent == null) {
      return null;
    }

    return model.getChild(parent, path.getLastKey());
  }
  /**
   * Messages getTreeNodeForPage(path, onlyIfVisible, shouldCreate, path.length) as long as path is
   * non-null and the length is {@literal >} 0. Otherwise returns null.
   */
  private FHTreeStateNode getNodeForPath(
      TreePath path, boolean onlyIfVisible, boolean shouldCreate) {
    if (path != null) {
      FHTreeStateNode node;

      node = getMapping(path);
      if (node != null) {
        if (onlyIfVisible && !node.isVisible()) return null;
        return node;
      }
      if (onlyIfVisible) return null;

      // Check all the parent paths, until a match is found.
      Stack<TreePath> paths;

      if (tempStacks.size() == 0) {
        paths = new Stack<TreePath>();
      } else {
        paths = tempStacks.pop();
      }

      try {
        paths.push(path);
        path = path.getParentPath();
        node = null;
        while (path != null) {
          node = getMapping(path);
          if (node != null) {
            // Found a match, create entries for all paths in
            // paths.
            while (node != null && paths.size() > 0) {
              path = paths.pop();
              node = node.createChildFor(path.getLastPathComponent());
            }
            return node;
          }
          paths.push(path);
          path = path.getParentPath();
        }
      } finally {
        paths.removeAllElements();
        tempStacks.push(paths);
      }
      // If we get here it means they share a different root!
      return null;
    }
    return null;
  }
 private Object[] internalGetChildren(
     ISynchronizationContext context, Object parent, Object[] children) {
   List result = new ArrayList(children.length);
   for (int i = 0; i < children.length; i++) {
     Object object = children[i];
     // If the parent is a TreePath then the subclass is
     // TreePath aware and we can send a TrePath to the
     // isVisible method
     if (parent instanceof TreePath) {
       TreePath tp = (TreePath) parent;
       object = tp.createChildPath(object);
     }
     if (isVisible(context, object)) result.add(internalGetElement(object));
   }
   return result.toArray(new Object[result.size()]);
 }
  /**
   * Returns the row that the last item identified in path is visible at. Will return -1 if any of
   * the elements in path are not currently visible.
   */
  public int getRowForPath(TreePath path) {
    if (path == null || root == null) return -1;

    FHTreeStateNode node = getNodeForPath(path, true, false);

    if (node != null) return node.getRow();

    TreePath parentPath = path.getParentPath();

    node = getNodeForPath(parentPath, true, false);
    if (node != null && node.isExpanded()) {
      return node.getRowToModelIndex(
          treeModel.getIndexOfChild(
              parentPath.getLastPathComponent(), path.getLastPathComponent()));
    }
    return -1;
  }
 public void setNodeExpanded(TreePath keyPath, boolean expanded) {
   if (expanded) {
     if (!isNodeExpanded(keyPath))
       throw new UnsupportedOperationException(
           "SeveralLevelsExpanded can't expand a node on level "
               + level
               + " or deper: "
               + keyPath.getLevel());
   } else {
     if (isNodeExpanded(keyPath))
       throw new UnsupportedOperationException(
           "SeveralLevelsExpanded can't collapse a node on top "
               + level
               + " levels: "
               + keyPath.getLevel());
   }
 }
Beispiel #9
0
 public void makeVisible(TreePath path) {
   if (path != null) {
     TreePath parent = path.getParentPath();
     if (parent != null) {
       expandPath(parent);
     }
   }
 }
 /** Recreates the receivers path, and all its children's paths. */
 protected void resetChildrenPaths(TreePath parentPath) {
   removeMapping(this);
   if (parentPath == null) path = new TreePath(getUserObject());
   else path = parentPath.pathByAddingChild(getUserObject());
   addMapping(this);
   for (int counter = getChildCount() - 1; counter >= 0; counter--)
     ((FHTreeStateNode) getChildAt(counter)).resetChildrenPaths(path);
 }
Beispiel #11
0
 public boolean isExpanded(TreePath path) {
   if (this.expandedPaths.contains(path)) {
     // Ensure all parents are expanded
     TreePath parent = path.getParentPath();
     return parent == null ? true : isExpanded(parent);
   } else {
     return false;
   }
 }
Beispiel #12
0
 public void removeSelectionPath(TreePath path) {
   if (this.selectedPaths.remove(path)) {
     // Need to redisplay the parent
     markForRefresh(path.getParentPath());
     if (this.selectionListener != null) {
       this.selectionListener.selectionChanged(new TreeSelectionEvent(this, path, false));
     }
   }
 }
Beispiel #13
0
  /**
   * Creates a DocTreePath for a root node.
   *
   * @param treePath the TreePath from which the root node was created.
   * @param t the DocCommentTree to create the path for.
   */
  public DocTreePath(TreePath treePath, DocCommentTree t) {
    treePath.getClass();
    t.getClass();

    this.treePath = treePath;
    this.docComment = t;
    this.parent = null;
    this.leaf = t;
  }
  /**
   * Returns an Enumerator that increments over the visible paths starting at the passed in
   * location. The ordering of the enumeration is based on how the paths are displayed.
   */
  public Enumeration<TreePath> getVisiblePathsFrom(TreePath path) {
    if (path == null) return null;

    FHTreeStateNode node = getNodeForPath(path, true, false);

    if (node != null) {
      return new VisibleFHTreeStateNodeEnumeration(node);
    }
    TreePath parentPath = path.getParentPath();

    node = getNodeForPath(parentPath, true, false);
    if (node != null && node.isExpanded()) {
      return new VisibleFHTreeStateNodeEnumeration(
          node,
          treeModel.getIndexOfChild(
              parentPath.getLastPathComponent(), path.getLastPathComponent()));
    }
    return null;
  }
  /**
   * Returns a rectangle giving the bounds needed to draw path.
   *
   * @param path a TreePath specifying a node
   * @param placeIn a Rectangle object giving the available space
   * @return a Rectangle object specifying the space to be used
   */
  public Rectangle getBounds(TreePath path, Rectangle placeIn) {
    if (path == null) return null;

    FHTreeStateNode node = getNodeForPath(path, true, false);

    if (node != null) return getBounds(node, -1, placeIn);

    // node hasn't been created yet.
    TreePath parentPath = path.getParentPath();

    node = getNodeForPath(parentPath, true, false);
    if (node != null && node.isExpanded()) {
      int childIndex =
          treeModel.getIndexOfChild(parentPath.getLastPathComponent(), path.getLastPathComponent());

      if (childIndex != -1) return getBounds(node, childIndex, placeIn);
    }
    return null;
  }
  /**
   * Ensures that all the path components in path are expanded, accept for the last component which
   * will only be expanded if expandLast is true. Returns true if succesful in finding the path.
   */
  private boolean ensurePathIsExpanded(TreePath aPath, boolean expandLast) {
    if (aPath != null) {
      // Make sure the last entry isn't a leaf.
      if (treeModel.isLeaf(aPath.getLastPathComponent())) {
        aPath = aPath.getParentPath();
        expandLast = true;
      }
      if (aPath != null) {
        FHTreeStateNode lastNode = getNodeForPath(aPath, false, true);

        if (lastNode != null) {
          lastNode.makeVisible();
          if (expandLast) lastNode.expand();
          return true;
        }
      }
    }
    return false;
  }
Beispiel #17
0
  /** {@inheritDoc} */
  public TreeNode getNode(TreePath path) {
    TreeNode node = this.getModel().getRoot();
    if (!node.getText().equals(path.get(0))) // Test root node
    return null;

    Iterator<String> iterator = path.iterator();
    iterator.next(); // Skip root node, we already tested it above
    while (iterator.hasNext()) {
      String nodeText = iterator.next();
      boolean foundMatch = false;
      for (TreeNode child : node.getChildren()) {
        if (child.getText().equals(nodeText)) {
          node = child;
          foundMatch = true;
          break;
        }
      }
      if (!foundMatch) return null;
    }
    return node;
  }
Beispiel #18
0
 /**
  * Returns true if the value identified by path is currently viewable, which means it is either
  * the root or all of its parents are expanded. Otherwise, this method returns false.
  *
  * @return true if the node is viewable, otherwise false
  */
 public boolean isVisible(TreePath path) {
   if (path == TreePath.ROOT_PATH) {
     return true;
   }
   if (path != null) {
     TreePath parent = path.getParentPath();
     if (parent != null) {
       return isExpanded(parent);
     } else {
       // root node
       return true;
     }
   } else {
     return false;
   }
 }
  /** Marks the path <code>path</code> expanded state to <code>isExpanded</code>. */
  public void setExpandedState(TreePath path, boolean isExpanded) {
    if (isExpanded) ensurePathIsExpanded(path, true);
    else if (path != null) {
      TreePath parentPath = path.getParentPath();

      // YECK! Make the parent expanded.
      if (parentPath != null) {
        FHTreeStateNode parentNode = getNodeForPath(parentPath, false, true);
        if (parentNode != null) parentNode.makeVisible();
      }
      // And collapse the child.
      FHTreeStateNode childNode = getNodeForPath(path, true, false);

      if (childNode != null) childNode.collapse(true);
    }
  }
 public boolean isNodeExpanded(TreePath keyPath) {
   return keyPath.getLevel() < level;
 }
Beispiel #21
0
  public void readFromRequest(FormContext formContext) {
    // TODO: crawl open nodes, calling their widget's readFromRequest

    Request req = formContext.getRequest();
    String paramName = getRequestParameterName();

    // ---------------------------------------------------------------------
    // Handle node selection using checkboxes named <id>$select
    // ---------------------------------------------------------------------
    String[] selectValues = req.getParameterValues(paramName + ":select");
    if (selectValues != null) {
      // Create the set of paths given by the request
      Set newSelection = new HashSet();
      for (int i = 0; i < selectValues.length; i++) {
        newSelection.add(TreePath.valueOf(selectValues[i]));
      }

      // Check if all visible selections are in the new selection
      TreePath[] currentSelection =
          (TreePath[]) this.selectedPaths.toArray(new TreePath[this.selectedPaths.size()]);
      for (int i = 0; i < currentSelection.length; i++) {
        TreePath p = currentSelection[i];
        if (!newSelection.contains(p) && isVisible(p)) {
          removeSelectionPath(p);
        }
      }

      // Now add the currently selected items (may be selected already)
      Iterator iter = newSelection.iterator();
      while (iter.hasNext()) {
        addSelectionPath((TreePath) iter.next());
      }
    }

    // ---------------------------------------------------------------------
    // Handle tree actions:
    // - action is in <name>$action
    // - path is in <name>$path
    // ---------------------------------------------------------------------
    String action = req.getParameter(paramName + ":action");

    if (action == null || action.length() == 0) {
      // Nothing more to do here
      return;
    }

    getForm().setSubmitWidget(this);
    String pathValue = req.getParameter(paramName + ":path");

    if (pathValue == null || pathValue.length() == 0) {
      // this.treeDef.getLogger().warn("No tree path given");
      return;
    }

    // Parse the path
    TreePath path = TreePath.valueOf(pathValue);

    if ("expand".equals(action)) {
      this.expandPath(path);
    } else if ("collapse".equals(action)) {
      this.collapsePath(path);
    } else if ("toggle-collapse".equals(action)) {
      if (this.isExpanded(path)) {
        this.collapsePath(path);
      } else {
        this.expandPath(path);
      }
    } else if ("select".equals(action)) {
      this.addSelectionPath(path);
    } else if ("unselect".equals(action)) {
      this.removeSelectionPath(path);
    } else if ("toggle-select".equals(action)) {
      if (this.isPathSelected(path)) {
        this.removeSelectionPath(path);
      } else {
        this.addSelectionPath(path);
      }
    } else {
      // Unknown action
      // this.treeDef.getLogger().warn("Unknown action " + action);
    }
  }