* return new JList() {
* public int getVisibleRowCount() {
* int size = getModel().getSize();
* return size < super.getVisibleRowCount() ? size : super.getVisibleRowCount();
* }
*
* public Dimension getPreferredScrollableViewportSize() {
* if (getModel().getSize() == 0) {
* return new Dimension(0, 0);
* }
* else {
* return super.getPreferredScrollableViewportSize();
* }
* }
* };
*
*
* @return the list.
*/
protected JList createList() {
return new JList() {
@Override
public int getVisibleRowCount() {
int size = getModel().getSize();
return size < super.getVisibleRowCount() ? size : super.getVisibleRowCount();
}
@Override
public Dimension getPreferredScrollableViewportSize() {
if (getModel().getSize() == 0) {
return new Dimension(0, 0);
}
else {
return super.getPreferredScrollableViewportSize();
}
}
};
}
/**
* Gets the list.
*
* @return the list.
*/
protected JList getList() {
return _list;
}
/**
* Sets the list data.
*
* @param objects
*/
protected void setListData(Object[] objects) {
if (getList() == null) {
return;
}
resetSelection();
getList().setListData(objects);
// update the view so that isViewSizeSet flag in JViewport is reset to false
if (_scroll != null) {
_scroll.setViewportView(getList());
}
}
/**
* Sets the list data.
*
* @param objects
*/
protected void setListData(Vector> objects) {
if (getList() == null) {
return;
}
resetSelection();
getList().setListData(objects);
// update the view so that isViewSizeSet flag in JViewport is reset to false
if (_scroll != null) {
_scroll.setViewportView(getList());
}
}
private void resetSelection() {
if (getList() == null) {
return;
}
getList().getSelectionModel().setAnchorSelectionIndex(-1); // has to call setAnchor first
getList().getSelectionModel().setLeadSelectionIndex(-1);
getList().getSelectionModel().clearSelection();
}
public Object getSelectedHint() {
return getList() == null ? null : getList().getSelectedValue();
}
@Override
public JComponent getDelegateComponent() {
return getList();
}
/**
* Gets the delegate keystrokes. Since we know the hints popup is a JList, we return eight keystrokes so that they
* can be delegate to the JList. Those keystrokes are DOWN, UP, PAGE_DOWN, PAGE_UP, HOME and END.
*
* @return the keystrokes that will be delegated to the JList when hints popup is visible.
*/
@Override
public KeyStroke[] getDelegateKeyStrokes() {
if (_keyStrokes == null) {
_keyStrokes = new KeyStroke[8];
_keyStrokes[0] = KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0);
_keyStrokes[1] = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0);
_keyStrokes[2] = KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN, 0);
_keyStrokes[3] = KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP, 0);
_keyStrokes[4] = KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN, KeyEvent.CTRL_DOWN_MASK);
_keyStrokes[5] = KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP, KeyEvent.CTRL_DOWN_MASK);
_keyStrokes[6] = KeyStroke.getKeyStroke(KeyEvent.VK_HOME, KeyEvent.CTRL_DOWN_MASK);
_keyStrokes[7] = KeyStroke.getKeyStroke(KeyEvent.VK_END, KeyEvent.CTRL_DOWN_MASK);
}
return _keyStrokes;
}
}