/* * Handles the checkbox selection process. Uses the bounds property of the * check box within the selected cell to determine whether the checkbox should * be selected or not **/ public void mouseReleased(MouseEvent e) { if (list == null || list.getSelectedIndex() == -1 || !isEnabled(list.locationToIndex(e.getPoint()))) { return; } int[] indices = list.getSelectedIndices(); // get the current relative position of the check box // rect = box.getBounds(rect); for (int i = 0; i < indices.length; i++) { // get the current relative position of the check box int loc = list.locationToIndex(e.getPoint()); rect = list.getCellBounds(loc, loc); // ensure the point clicked in within the checkBox /*if(e.getX() < (rect.getX() + 20) ) { selState[indices[i]] = !selState[indices[i]]; setSelStateList(selState); } */ } list.revalidate(); list.repaint(); }
public void selectField(String fieldName) { int idx = listModel.indexOf(fieldName); if (idx >= 0) list.setSelectedIndex(idx); // Make sure it is visible: JViewport viewport = sp.getViewport(); viewport.scrollRectToVisible(list.getCellBounds(idx, idx)); }
public void listClicked(MouseEvent e) { Point p = new Point(e.getX(), e.getY()); int index = list.locationToIndex(p); Rectangle inside = list.getCellBounds(index, index); if (inside.contains(p) && (!newSelection)) { list.clearSelection(); } newSelection = false; }
private void hovered(final MouseEvent e, final boolean entered) { hoveredIndex = myList.locationToIndex(e.getPoint()); if (!entered || hoveredIndex != -1 && !myList.getCellBounds(hoveredIndex, hoveredIndex).contains(e.getPoint())) hoveredIndex = -1; myList.repaint(); }
@Override public void mousePressed(MouseEvent e) { JList list = (JList) e.getComponent(); Point pt = e.getPoint(); int index = list.locationToIndex(pt); if (index >= 0) { JButton button = getButton(list, pt, index); if (Objects.nonNull(button)) { listRepaint(list, list.getCellBounds(index, index)); } } }
@Override public void mouseMoved(MouseEvent e) { JList list = (JList) e.getComponent(); Point pt = e.getPoint(); int index = list.locationToIndex(pt); if (!list.getCellBounds(index, index).contains(pt)) { if (prevIndex >= 0) { Rectangle r = list.getCellBounds(prevIndex, prevIndex); listRepaint(list, r); } index = -1; prevButton = null; return; } if (index >= 0) { JButton button = getButton(list, pt, index); ButtonsRenderer renderer = (ButtonsRenderer) list.getCellRenderer(); if (Objects.nonNull(button)) { renderer.rolloverIndex = index; if (!button.equals(prevButton)) { Rectangle r = list.getCellBounds(prevIndex, index); listRepaint(list, r); } } else { renderer.rolloverIndex = -1; Rectangle r = null; if (prevIndex == index) { if (prevIndex >= 0 && Objects.nonNull(prevButton)) { r = list.getCellBounds(prevIndex, prevIndex); } } else { r = list.getCellBounds(index, index); } listRepaint(list, r); prevIndex = -1; } prevButton = button; } prevIndex = index; }
private static JButton getButton(JList<String> list, Point pt, int index) { Component c = list.getCellRenderer().getListCellRendererComponent(list, "", index, false, false); Rectangle r = list.getCellBounds(index, index); c.setBounds(r); // c.doLayout(); //may be needed for mone LayoutManager pt.translate(-r.x, -r.y); Component b = SwingUtilities.getDeepestComponentAt(c, pt.x, pt.y); if (b instanceof JButton) { return (JButton) b; } else { return null; } }
/** Adapts the selection. */ private void adaptSelection() { int[] indexes = list.getSelectedIndices(); if (indexes.length > 1) { boolean selected = false; for (int i = 0; i < indexes.length; i++) { int index = indexes[i]; IElementNode elementNode = (IElementNode) list.getModel().getElementAt(index); if (i == 0) { selected = elementNode.isSelected(); } elementNode.setSelected(selected); Rectangle rectangle = list.getCellBounds(index, index); list.repaint(rectangle); } } else { int index = list.getSelectedIndex(); if (index > -1) { IElementNode elementNode = (IElementNode) list.getModel().getElementAt(index); elementNode.setSelected(!elementNode.isSelected()); Rectangle rectangle = list.getCellBounds(index, index); list.repaint(rectangle); } } }
@Override public void mousePressed(MouseEvent e) { // JList list = (JList) e.getComponent(); Point pt = e.getPoint(); int index = list.locationToIndex(pt); if (index >= 0) { JButton button = getButton(list, pt, index); if (Objects.nonNull(button)) { ButtonsRenderer renderer = (ButtonsRenderer) list.getCellRenderer(); renderer.pressedIndex = index; renderer.button = button; listRepaint(list, list.getCellBounds(index, index)); } } }
// PENDING JW: this isn't aware of sorting/filtering - fix! private static boolean pointIsInActualBounds(JList list, int index, Point point) { ListCellRenderer renderer = list.getCellRenderer(); ListModel model = list.getModel(); Object element = model.getElementAt(index); Component comp = renderer.getListCellRendererComponent(list, element, index, false, false); Dimension prefSize = comp.getPreferredSize(); Rectangle cellBounds = list.getCellBounds(index, index); if (!(comp.getComponentOrientation().isLeftToRight())) { cellBounds.x += cellBounds.width - prefSize.width; } cellBounds.width = prefSize.width; return cellBounds.contains(point); }
@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); }
private void ensureSelectedOnTop(int index) { // a hack to make selected iteam apear up in list TODO rewrite hack? Rectangle cellBounds = list.getCellBounds(index, list.getModel().getSize() - 1); list.scrollRectToVisible(cellBounds); }