/* * Fires AccessibleActiveDescendant PropertyChangeEvent to notify listeners * on the popup menu invoker that a popup list item has been selected */ private void fireActiveDescendant() { if (JPopupMenu.this instanceof BasicComboPopup) { // get the popup list JList popupList = ((BasicComboPopup) JPopupMenu.this).getList(); if (popupList == null) { return; } // get the first selected item AccessibleContext ac = popupList.getAccessibleContext(); AccessibleSelection selection = ac.getAccessibleSelection(); if (selection == null) { return; } Accessible a = selection.getAccessibleSelection(0); if (a == null) { return; } AccessibleContext selectedItem = a.getAccessibleContext(); // fire the event with the popup invoker as the source. if (selectedItem != null && invoker != null) { AccessibleContext invokerContext = invoker.getAccessibleContext(); if (invokerContext != null) { // Check invokerContext because Component.getAccessibleContext // returns null. Classes that extend Component are responsible // for returning a non-null AccessibleContext. invokerContext.firePropertyChange( ACCESSIBLE_ACTIVE_DESCENDANT_PROPERTY, null, selectedItem); } } } }
@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)); } } }
@SuppressWarnings("unchecked") private static JButton getButton(JList list, Point pt, int index) { Container c = (Container) 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; } }
@Override public void actionPerformed(ActionEvent e) { MutableComboBoxModel m = (MutableComboBoxModel) list.getModel(); if (m.getSize() > 1) { m.removeElementAt(index); comboBox.showPopup(); } }
@Override public Component getListCellRendererComponent( JList<? extends E> list, E value, int index, boolean isSelected, boolean cellHasFocus) { label.setText(Objects.toString(value, "")); this.list = list; this.index = index; if (isSelected) { setBackground(list.getSelectionBackground()); label.setForeground(list.getSelectionForeground()); } else { setBackground(index % 2 == 0 ? EVEN_COLOR : list.getBackground()); label.setForeground(list.getForeground()); } MutableComboBoxModel m = (MutableComboBoxModel) list.getModel(); if (index < 0 || m.getSize() - 1 <= 0) { setOpaque(false); deleteButton.setVisible(false); label.setForeground(list.getForeground()); } else { boolean f = index == rolloverIndex; setOpaque(true); deleteButton.setVisible(true); deleteButton.getModel().setRollover(f); deleteButton.setForeground(f ? Color.WHITE : list.getForeground()); } return this; }
@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; }
@Override public void updateUI() { if (Objects.nonNull(cbml)) { JList<?> list = getList(); if (Objects.nonNull(list)) { list.removeMouseListener(cbml); list.removeMouseMotionListener(cbml); } } super.updateUI(); setRenderer(new ButtonsRenderer<E>(this)); JList<?> list = getList(); if (Objects.nonNull(list)) { cbml = new CellButtonsMouseListener(); list.addMouseListener(cbml); list.addMouseMotionListener(cbml); } }
@Override public void mouseExited(MouseEvent e) { JList list = (JList) e.getComponent(); ButtonsRenderer renderer = (ButtonsRenderer) list.getCellRenderer(); renderer.rolloverIndex = -1; }
private static void listRepaint(JList list, Rectangle rect) { if (Objects.nonNull(rect)) { list.repaint(rect); } }