private static void drawSelection(JTree tree, Graphics g, final int width) {
   int y = 0;
   final int[] rows = tree.getSelectionRows();
   final int height = tree.getRowHeight();
   for (int row : rows) {
     final TreeCellRenderer renderer = tree.getCellRenderer();
     final Object value = tree.getPathForRow(row).getLastPathComponent();
     if (value == null) continue;
     final Component component =
         renderer.getTreeCellRendererComponent(tree, value, false, false, false, row, false);
     if (component.getFont() == null) {
       component.setFont(tree.getFont());
     }
     g.translate(0, y);
     component.setBounds(0, 0, width, height);
     boolean wasOpaque = false;
     if (component instanceof JComponent) {
       final JComponent j = (JComponent) component;
       if (j.isOpaque()) wasOpaque = true;
       j.setOpaque(false);
     }
     component.paint(g);
     if (wasOpaque) {
       ((JComponent) component).setOpaque(true);
     }
     y += height;
     g.translate(0, -y);
   }
 }
  public Component getTreeCellRendererComponent(
      JTree tree,
      Object value,
      boolean selected,
      boolean expanded,
      boolean leaf,
      int row,
      boolean hasFocus) {
    Component renderer =
        delegate.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);

    TreePath path = tree.getPathForRow(row);
    if (path != null) {
      if (selectionModel.isPathSelected(path, true))
        checkBox.setState(gui_TristateCheckBox.SELECTED); // checkBox.setState(Boolean.TRUE);
      else {
        if (selectionModel.isPartiallySelected(path))
          checkBox.setState(gui_TristateCheckBox.DONT_CARE);
        else checkBox.setState(gui_TristateCheckBox.NOT_SELECTED);
        // checkBox.setState(selectionModel.isPartiallySelected(path) ? null : Boolean.FALSE);
      }
    }
    removeAll();
    add(checkBox, BorderLayout.WEST);
    add(renderer, BorderLayout.CENTER);
    return this;
  }
Пример #3
0
  public Component getTreeCellRendererComponent(
      JTree tree,
      Object value,
      boolean selected,
      boolean expanded,
      boolean leaf,
      int row,
      boolean hasFocus) {
    Component renderer =
        delegate.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
    if (!showRootNodeCheckBox && tree.getModel().getRoot() == value) return renderer;

    TreePath path = tree.getPathForRow(row);
    if (path != null) {
      if (checkBoxCustomer != null && !checkBoxCustomer.showCheckBox(path)) return renderer;
      if (selectionModel.isPathSelected(path, selectionModel.isDigged()))
        checkBox.getTristateModel().setState(TristateState.SELECTED);
      else
        checkBox
            .getTristateModel()
            .setState(
                selectionModel.isDigged() && selectionModel.isPartiallySelected(path)
                    ? TristateState.INDETERMINATE
                    : TristateState.DESELECTED);
    }
    removeAll();
    add(checkBox, BorderLayout.WEST);
    add(renderer, BorderLayout.CENTER);
    return this;
  }
Пример #4
0
  private void selectCompanion(CompanionFacade compFacade) {
    TreeTableModel treeTableModel = companionsTable.getTreeTableModel();
    treeTableModel.getRoot();
    TreePath path = null;

    JTree tree = companionsTable.getTree();
    String companionType = compFacade.getCompanionType();
    for (int i = 0; i < tree.getRowCount(); i++) {
      TreePath pathForRow = tree.getPathForRow(i);
      Object lastPathComponent = pathForRow.getLastPathComponent();
      if (lastPathComponent.toString().startsWith(companionType)) {
        tree.expandRow(i);
      } else if (lastPathComponent
          instanceof pcgen.gui2.tabs.CompanionInfoTab.CompanionsModel.CompanionNode) {
        CompanionFacade rowComp =
            (CompanionFacade)
                ((pcgen.gui2.tabs.CompanionInfoTab.CompanionsModel.CompanionNode) lastPathComponent)
                    .getValueAt(0);

        if (rowComp != null
            && rowComp.getFileRef().getReference() == compFacade.getFileRef().getReference()
            && rowComp.getNameRef().getReference() == compFacade.getNameRef().getReference()
            && rowComp.getRaceRef().getReference() == compFacade.getRaceRef().getReference()) {
          path = pathForRow;
        }
      }
    }
    if (path != null) {
      companionsTable.getTree().setSelectionPath(path);
      companionsTable.getTree().scrollPathToVisible(path);
    }
  }
Пример #5
0
  public static void ensureSelection(JTree tree) {
    final TreePath[] paths = tree.getSelectionPaths();

    if (paths != null) {
      for (TreePath each : paths) {
        if (tree.getRowForPath(each) >= 0 && tree.isVisible(each)) {
          return;
        }
      }
    }

    for (int eachRow = 0; eachRow < tree.getRowCount(); eachRow++) {
      TreePath eachPath = tree.getPathForRow(eachRow);
      if (eachPath != null && tree.isVisible(eachPath)) {
        tree.setSelectionPath(eachPath);
        break;
      }
    }
  }
Пример #6
0
 /**
  * Mouse Click Event.
  *
  * @param e MouseEvent Object.
  */
 public void mouseClicked(MouseEvent e) {
   int x = e.getX();
   int y = e.getY();
   int row = tree.getRowForLocation(x, y);
   TreePath path = tree.getPathForRow(row);
   if (path != null) {
     CheckNode node = (CheckNode) path.getLastPathComponent();
     boolean isSelected = !(node.isSelected());
     node.setSelected(isSelected);
     if (node.getSelectionMode() == CheckNode.DIG_IN_SELECTION) {
       if (isSelected) {
         tree.expandPath(path);
       } else {
         tree.collapsePath(path);
       }
     }
     ((DefaultTreeModel) tree.getModel()).nodeChanged(node);
     if (row == 0) {
       tree.revalidate();
       tree.repaint();
     }
   }
 }
Пример #7
0
 public static RelativePoint getPointForRow(JTree aTree, int aRow) {
   return getPointForPath(aTree, aTree.getPathForRow(aRow));
 }
Пример #8
0
  public static ActionCallback showAndSelect(
      final JTree tree,
      int top,
      int bottom,
      final int row,
      final int previous,
      boolean addToSelection,
      final boolean scroll) {
    final TreePath path = tree.getPathForRow(row);

    if (path == null) return new ActionCallback.Done();

    final int size = tree.getRowCount();
    if (size == 0) {
      tree.clearSelection();
      return new ActionCallback.Done();
    }
    if (top < 0) {
      top = 0;
    }
    if (bottom >= size) {
      bottom = size - 1;
    }

    if (row >= tree.getRowCount()) return new ActionCallback.Done();

    if (!tree.isValid()) {
      tree.validate();
    }

    final Rectangle rowBounds = tree.getRowBounds(row);
    if (rowBounds == null) return new ActionCallback.Done();

    Rectangle topBounds = tree.getRowBounds(top);
    if (topBounds == null) {
      topBounds = rowBounds;
    }

    Rectangle bottomBounds = tree.getRowBounds(bottom);
    if (bottomBounds == null) {
      bottomBounds = rowBounds;
    }

    Rectangle bounds = topBounds.union(bottomBounds);
    bounds.x = rowBounds.x;
    bounds.width = rowBounds.width;

    final Rectangle visible = tree.getVisibleRect();
    if (visible.contains(bounds)) {
      bounds = null;
    } else {
      final Component comp =
          tree.getCellRenderer()
              .getTreeCellRendererComponent(
                  tree, path.getLastPathComponent(), true, true, false, row, false);

      if (comp instanceof SimpleColoredComponent) {
        final SimpleColoredComponent renderer = ((SimpleColoredComponent) comp);
        final Dimension scrollableSize = renderer.computePreferredSize(true);
        bounds.width = scrollableSize.width;
      }
    }

    final ActionCallback callback = new ActionCallback();

    if (!tree.isRowSelected(row)) {
      if (addToSelection) {
        tree.getSelectionModel().addSelectionPath(tree.getPathForRow(row));
      } else {
        tree.setSelectionRow(row);
      }
    }

    if (bounds != null) {
      final Range<Integer> range = getExpandControlRange(tree, path);
      if (range != null) {
        int delta = bounds.x - range.getFrom().intValue();
        bounds.x -= delta;
        bounds.width -= delta;
      }

      if (visible.width < bounds.width) {
        bounds.width = visible.width;
      }

      final Rectangle b1 = bounds;
      final Runnable runnable =
          new Runnable() {
            public void run() {
              if (scroll) {
                tree.scrollRectToVisible(b1);
              }
              callback.setDone();
            }
          };

      if (ApplicationManager.getApplication().isUnitTestMode()) {
        runnable.run();
      } else {
        SwingUtilities.invokeLater(runnable);
      }
    } else {
      callback.setDone();
    }

    return callback;
  }