下面列出了javax.swing.JList#locationToIndex ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Show popup menu in response to a mouse event.
*
* @param e Event.
*/
@Override
protected void showPopup(MouseEvent e) {
// Retrieve information
if (!(e.getComponent() instanceof JList)) {
return;
}
JList tmpList = (JList) e.getComponent();
int position = tmpList.locationToIndex(e.getPoint());
if (position < 0) {
return;
}
Object object = tmpList.getModel().getElementAt(position);
if (!(object instanceof Page)) {
return;
}
Page link = (Page) object;
showPopup(tmpList, link, e.getX(), e.getY());
}
public static boolean clicksInCheckBox(Point e, JList list) {
int index = list.locationToIndex(e);
int hotspot = new JCheckBox().getPreferredSize().width;
Rectangle bounds = list.getCellBounds(index, index);
if (bounds != null) {
if (list.getComponentOrientation().isLeftToRight()) {
return e.getX() < bounds.x + hotspot;
} else {
return e.getX() > bounds.x + bounds.width - hotspot;
}
} else {
return false;
}
}
private void showJList (JScrollPane view, Point pt) {
JList list = (JList) view.getViewport().getView();
Point p = SwingUtilities.convertPoint(view, pt.x, pt.y, list);
int row = list.locationToIndex(p);
if (row == -1) {
hide();
return;
}
Rectangle bds = list.getCellBounds(row,
row);
//GetCellBounds returns a width that is the
//full component width; we want only what
//the renderer really needs.
ListCellRenderer ren = list.getCellRenderer();
Dimension rendererSize =
ren.getListCellRendererComponent(list,
list.getModel().getElementAt(row),
row, false, false).getPreferredSize();
bds.width = rendererSize.width;
if (bds == null || !bds.contains(p)) {
hide();
return;
}
if (setCompAndRow (list, row)) {
Rectangle visible = getShowingRect (view);
Rectangle[] rects = getRects (bds, visible);
if (rects.length > 0) {
ensureOldPopupsHidden();
painter.configure(
list.getModel().getElementAt(row),
view, list, row);
showPopups (rects, bds, visible, list, view);
} else {
hide();
}
}
}
private String getSelectedText(JList list, MouseEvent e) {
int index = list.locationToIndex(e.getPoint());
if (index >= 0) {
int x = e.getX();
if (x < checkBoxWidth) {
return (String) list.getModel().getElementAt(index);
}
}
return null;
}
@Override
public void mouseClicked(MouseEvent e) {
if (e.isPopupTrigger()) {
return;
}
if (!(e.getSource() instanceof JList)) {
return;
}
JList list = (JList) e.getSource();
int index = list.locationToIndex(e.getPoint());
if (index < 0) {
return;
}
toggle(list.getModel().getElementAt(index));
}
/**
* A variation of locationToIndex() which only returns an index if the
* Point is within the actual bounds of a list item (not just in the cell)
* and if the JList has the "List.isFileList" client property set.
* Otherwise, this method returns -1.
* This is used to make Windows {@literal L&F} JFileChooser act
* like native dialogs.
*/
public static int loc2IndexFileList(JList<?> list, Point point) {
int index = list.locationToIndex(point);
if (index != -1) {
Object bySize = list.getClientProperty("List.isFileList");
if (bySize instanceof Boolean && ((Boolean)bySize).booleanValue() &&
!pointIsInActualBounds(list, index, point)) {
index = -1;
}
}
return index;
}
@Override
public void mouseDragged(MouseEvent e) {
Object src = e.getSource();
if (mouseDragging && (src instanceof JList)) {
JList<?> srcList = (JList<?>) src;
DefaultListModel<?> srcModel = (DefaultListModel<?>) srcList.getModel();
int currentIndex = srcList.locationToIndex(e.getPoint());
if (currentIndex != dragSourceIndex) {
int dragTargetIndex = srcList.getSelectedIndex();
moveElement(srcModel, dragSourceIndex, dragTargetIndex);
dragSourceIndex = currentIndex;
}
}
}
@Override
public void mouseDragged(MouseEvent e) {
removeListeners();
Object src = e.getSource();
// Check to see if we are in a state we care about
if (!mouseDragging || !(src instanceof JList)) {
return;
}
JList<?> srcList = (JList<?>) src;
WeaponListModel srcModel = (WeaponListModel) srcList.getModel();
int currentIndex = srcList.locationToIndex(e.getPoint());
if (currentIndex != dragSourceIndex) {
int dragTargetIndex = srcList.getSelectedIndex();
Mounted weap1 = srcModel.getWeaponAt(dragSourceIndex);
srcModel.swapIdx(dragSourceIndex, dragTargetIndex);
dragSourceIndex = currentIndex;
Entity ent = weap1.getEntity();
// Is the sort order custom?
int customId = Entity.WeaponSortOrder.CUSTOM.ordinal();
// Update weap sort order drop down
if (weapSortOrder.getSelectedIndex() != customId) {
// Set the order to custom
ent.setWeaponSortOrder(Entity.WeaponSortOrder.CUSTOM);
weapSortOrder.setSelectedIndex(customId);
}
// Update custom order
for (int i = 0; i < srcModel.getSize(); i++) {
Mounted m = srcModel.getWeaponAt(i);
ent.setCustomWeaponOrder(m, i);
}
if (unitDisplay.getClientGUI() != null) {
unitDisplay.getClientGUI().getMenuBar()
.updateSaveWeaponOrderMenuItem();
}
}
addListeners();
}
private void manItemInCombo() {
if (comboBox.getItemCount() > 0) {
final Object comp = comboBox.getUI().getAccessibleChild(comboBox, 0);
if ((comp instanceof JPopupMenu)) {
final JList list = new JList(comboBox.getModel());
final JPopupMenu popup = (JPopupMenu) comp;
final JScrollPane scrollPane = (JScrollPane) popup.getComponent(0);
final JViewport viewport = scrollPane.getViewport();
final Rectangle rect = popup.getVisibleRect();
final Point pt = viewport.getViewPosition();
row = list.locationToIndex(pt);
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
if (e.getButton() != MouseEvent.BUTTON1) {
return;
}
if (e.getClickCount() != 2) {
return;
}
if (!(e.getComponent() instanceof JList)) {
return;
}
JList list = (JList) e.getComponent();
int position = list.locationToIndex(e.getPoint());
if (position < 0) {
return;
}
Object object = list.getModel().getElementAt(position);
if (!(object instanceof Page)) {
return;
}
Page page = (Page) object;
ArrayList<Page> knownPages = null;
if ((pageWindow != null) && (pageWindow.getPage() != null)) {
Page basePage = pageWindow.getPage();
knownPages = new ArrayList<Page>(1);
knownPages.add(basePage);
for (Page backLink : basePage.getAllLinksToPage()) {
PageRedirect redirects = backLink.getRedirects();
if ((redirects != null) &&
(redirects.isRedirect()) &&
(Page.areSameTitle(basePage.getTitle(), redirects.getTitle()))) {
knownPages.add(backLink);
}
}
}
OnePageAnalysisWindow.createAnalysisWindow(page.getTitle(), knownPages, wikipedia);
}
public void mouseClicked(MouseEvent event) {
JList list = (JList) event.getSource();
int index = list.locationToIndex(event.getPoint());
CheckListItem item = (CheckListItem) list.getModel().getElementAt(index);
item.setSelected(!item.isSelected());
list.repaint(list.getCellBounds(index, index));
}
public static int getIndexAt(JList list, Point point) {
if (point == null)
return list.getSelectedIndex();
return list.locationToIndex(point);
}