generify ComboBox

This commit is contained in:
peter
2016-02-26 14:39:42 +01:00
parent 5cc765c34f
commit 67a8ed4f48
2 changed files with 21 additions and 24 deletions
@@ -32,7 +32,6 @@ import javax.swing.plaf.basic.ComboPopup;
import javax.swing.table.TableCellEditor;
import java.awt.*;
import java.awt.event.*;
import java.util.List;
/**
* Due to many bugs and "features" in <code>JComboBox</code> implementation we provide
@@ -48,7 +47,7 @@ import java.util.List;
*
* @author Vladimir Kondratyev
*/
public class ComboBox extends ComboBoxWithWidePopup implements AWTEventListener {
public class ComboBox<E> extends ComboBoxWithWidePopup<E> implements AWTEventListener {
public static final String TABLE_CELL_EDITOR_PROPERTY = "tableCellEditor";
private int myMinimumAndPreferredWidth;
@@ -60,7 +59,7 @@ public class ComboBox extends ComboBoxWithWidePopup implements AWTEventListener
this(-1);
}
public ComboBox(final ComboBoxModel model) {
public ComboBox(final ComboBoxModel<E> model) {
this(model, -1);
}
@@ -68,11 +67,11 @@ public class ComboBox extends ComboBoxWithWidePopup implements AWTEventListener
* @param width preferred width of the combobox. Value <code>-1</code> means undefined.
*/
public ComboBox(final int width) {
this(new DefaultComboBoxModel(), width);
this(new DefaultComboBoxModel<E>(), width);
}
public ComboBox(final ComboBoxModel model, final int width) {
public ComboBox(final ComboBoxModel<E> model, final int width) {
super(model);
myMinimumAndPreferredWidth = width;
registerCancelOnEscape();
@@ -163,12 +162,9 @@ public class ComboBox extends ComboBoxWithWidePopup implements AWTEventListener
public void eventDispatched(AWTEvent event) {
if (event.getID() == WindowEvent.WINDOW_OPENED) {
final WindowEvent we = (WindowEvent)event;
final List<JBPopup> popups = JBPopupFactory.getInstance().getChildPopups(this);
if (popups != null) {
for (JBPopup each : popups) {
if (each.getContent() != null && SwingUtilities.isDescendingFrom(each.getContent(), we.getWindow())) {
super.setPopupVisible(false);
}
for (JBPopup each : JBPopupFactory.getInstance().getChildPopups(this)) {
if (each.getContent() != null && SwingUtilities.isDescendingFrom(each.getContent(), we.getWindow())) {
super.setPopupVisible(false);
}
}
}
@@ -202,13 +198,13 @@ public class ComboBox extends ComboBoxWithWidePopup implements AWTEventListener
return UIUtil.getComboBoxPopup(this);
}
public ComboBox(final Object[] items, final int preferredWidth) {
public ComboBox(final E[] items, final int preferredWidth) {
super(items);
myMinimumAndPreferredWidth = preferredWidth;
registerCancelOnEscape();
}
public ComboBox(@NotNull Object[] items) {
public ComboBox(@NotNull E[] items) {
this(items, -1);
}
@@ -23,24 +23,24 @@ import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class ComboBoxWithWidePopup extends JComboBox {
public class ComboBoxWithWidePopup<E> extends JComboBox<E> {
private boolean myLayingOut = false;
private int myMinLength = 20;
public ComboBoxWithWidePopup(final ComboBoxModel aModel) {
public ComboBoxWithWidePopup(final ComboBoxModel<E> aModel) {
super(aModel);
if (SystemInfo.isMac && UIUtil.isUnderAquaLookAndFeel()) setMaximumRowCount(25);
}
public ComboBoxWithWidePopup(final Object[] items) {
public ComboBoxWithWidePopup(final E[] items) {
super(items);
if (SystemInfo.isMac && UIUtil.isUnderAquaLookAndFeel()) setMaximumRowCount(25);
}
public ComboBoxWithWidePopup(final Vector<?> items) {
public ComboBoxWithWidePopup(@SuppressWarnings("UseOfObsoleteCollectionType") final Vector<E> items) {
super(items);
if (SystemInfo.isMac && UIUtil.isUnderAquaLookAndFeel()) setMaximumRowCount(25);
@@ -51,7 +51,7 @@ public class ComboBoxWithWidePopup extends JComboBox {
@SuppressWarnings("GtkPreferredJComboBoxRenderer")
@Override
public void setRenderer(ListCellRenderer renderer) {
public void setRenderer(ListCellRenderer<? super E> renderer) {
super.setRenderer(new AdjustingListCellRenderer(this, renderer));
}
@@ -92,18 +92,18 @@ public class ComboBoxWithWidePopup extends JComboBox {
return getPreferredSize();
}
private class AdjustingListCellRenderer implements ListCellRenderer {
private final ListCellRenderer myOldRenderer;
private class AdjustingListCellRenderer implements ListCellRenderer<E> {
private final ListCellRenderer<? super E> myOldRenderer;
private final ComboBoxWithWidePopup myComboBox;
public AdjustingListCellRenderer(ComboBoxWithWidePopup comboBox, ListCellRenderer oldRenderer) {
public AdjustingListCellRenderer(ComboBoxWithWidePopup<E> comboBox, ListCellRenderer<? super E> oldRenderer) {
myComboBox = comboBox;
myOldRenderer = oldRenderer;
}
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
Object _value = value;
public Component getListCellRendererComponent(JList<? extends E> list, E value, int index, boolean isSelected, boolean cellHasFocus) {
E _value = value;
if (index == -1 && _value instanceof String && !myComboBox.isValid()) {
int minLength = getMinLength();
@@ -112,7 +112,8 @@ public class ComboBoxWithWidePopup extends JComboBox {
if (size.width == 0) {
if (stringValue.length() > minLength) {
_value = stringValue.substring(0, minLength);
//noinspection unchecked
_value = (E)stringValue.substring(0, minLength);
}
}
}