private static int getVisibleRowCount(final JTree tree) { final Rectangle visible = tree.getVisibleRect(); int count = 0; for (int i = 0; i < tree.getRowCount(); i++) { final Rectangle bounds = tree.getRowBounds(i); if (visible.y <= bounds.y && visible.y + visible.height >= bounds.y + bounds.height) { count++; } } return count; }
private static int getFirstVisibleRow(final JTree tree) { final Rectangle visible = tree.getVisibleRect(); int row = -1; for (int i = 0; i < tree.getRowCount(); i++) { final Rectangle bounds = tree.getRowBounds(i); if (visible.y <= bounds.y && visible.y + visible.height >= bounds.y + bounds.height) { row = i; break; } } return row; }
@NotNull @Override public RelativePoint guessBestPopupLocation(@NotNull final JComponent component) { Point popupMenuPoint = null; final Rectangle visibleRect = component.getVisibleRect(); if (component instanceof JList) { // JList JList list = (JList) component; int firstVisibleIndex = list.getFirstVisibleIndex(); int lastVisibleIndex = list.getLastVisibleIndex(); int[] selectedIndices = list.getSelectedIndices(); for (int index : selectedIndices) { if (firstVisibleIndex <= index && index <= lastVisibleIndex) { Rectangle cellBounds = list.getCellBounds(index, index); popupMenuPoint = new Point(visibleRect.x + visibleRect.width / 4, cellBounds.y + cellBounds.height); break; } } } else if (component instanceof JTree) { // JTree JTree tree = (JTree) component; int[] selectionRows = tree.getSelectionRows(); if (selectionRows != null) { Arrays.sort(selectionRows); for (int i = 0; i < selectionRows.length; i++) { int row = selectionRows[i]; Rectangle rowBounds = tree.getRowBounds(row); if (visibleRect.contains(rowBounds)) { popupMenuPoint = new Point(rowBounds.x + 2, rowBounds.y + rowBounds.height - 1); break; } } if (popupMenuPoint == null) { // All selected rows are out of visible rect Point visibleCenter = new Point( visibleRect.x + visibleRect.width / 2, visibleRect.y + visibleRect.height / 2); double minDistance = Double.POSITIVE_INFINITY; int bestRow = -1; Point rowCenter; double distance; for (int i = 0; i < selectionRows.length; i++) { int row = selectionRows[i]; Rectangle rowBounds = tree.getRowBounds(row); rowCenter = new Point(rowBounds.x + rowBounds.width / 2, rowBounds.y + rowBounds.height / 2); distance = visibleCenter.distance(rowCenter); if (minDistance > distance) { minDistance = distance; bestRow = row; } } if (bestRow != -1) { Rectangle rowBounds = tree.getRowBounds(bestRow); tree.scrollRectToVisible( new Rectangle( rowBounds.x, rowBounds.y, Math.min(visibleRect.width, rowBounds.width), rowBounds.height)); popupMenuPoint = new Point(rowBounds.x + 2, rowBounds.y + rowBounds.height - 1); } } } } else if (component instanceof JTable) { JTable table = (JTable) component; int column = table.getColumnModel().getSelectionModel().getLeadSelectionIndex(); int row = Math.max( table.getSelectionModel().getLeadSelectionIndex(), table.getSelectionModel().getAnchorSelectionIndex()); Rectangle rect = table.getCellRect(row, column, false); if (!visibleRect.intersects(rect)) { table.scrollRectToVisible(rect); } popupMenuPoint = new Point(rect.x, rect.y + rect.height); } else if (component instanceof PopupOwner) { popupMenuPoint = ((PopupOwner) component).getBestPopupPosition(); } if (popupMenuPoint == null) { popupMenuPoint = new Point(visibleRect.x + visibleRect.width / 2, visibleRect.y + visibleRect.height / 2); } return new RelativePoint(component, popupMenuPoint); }
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; }