jdk/src/share/classes/javax/swing/plaf/basic/BasicComboBoxUI.java
changeset 25565 ce603b34c98d
parent 21278 ef8a3a2a72f2
child 25763 51d1f910f68d
equal deleted inserted replaced
25564:871d3490f14d 25565:ce603b34c98d
     1 /*
     1 /*
     2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    59  * @author Arnaud Weber
    59  * @author Arnaud Weber
    60  * @author Tom Santos
    60  * @author Tom Santos
    61  * @author Mark Davidson
    61  * @author Mark Davidson
    62  */
    62  */
    63 public class BasicComboBoxUI extends ComboBoxUI {
    63 public class BasicComboBoxUI extends ComboBoxUI {
    64     protected JComboBox comboBox;
    64     protected JComboBox<Object> comboBox;
    65     /**
    65     /**
    66      * This protected field is implementation specific. Do not access directly
    66      * This protected field is implementation specific. Do not access directly
    67      * or override.
    67      * or override.
    68      */
    68      */
    69     protected boolean   hasFocus = false;
    69     protected boolean   hasFocus = false;
    72     // in the JTable DefaultCellEditor.
    72     // in the JTable DefaultCellEditor.
    73     private boolean isTableCellEditor = false;
    73     private boolean isTableCellEditor = false;
    74     private static final String IS_TABLE_CELL_EDITOR = "JComboBox.isTableCellEditor";
    74     private static final String IS_TABLE_CELL_EDITOR = "JComboBox.isTableCellEditor";
    75 
    75 
    76     // This list is for drawing the current item in the combo box.
    76     // This list is for drawing the current item in the combo box.
    77     protected JList   listBox;
    77     protected JList<Object>   listBox;
    78 
    78 
    79     // Used to render the currently selected item in the combo box.
    79     // Used to render the currently selected item in the combo box.
    80     // It doesn't have anything to do with the popup's rendering.
    80     // It doesn't have anything to do with the popup's rendering.
    81     protected CellRendererPane currentValuePane = new CellRendererPane();
    81     protected CellRendererPane currentValuePane = new CellRendererPane();
    82 
    82 
   201      * @since 1.7
   201      * @since 1.7
   202      */
   202      */
   203     protected Insets padding;
   203     protected Insets padding;
   204 
   204 
   205     // Used for calculating the default size.
   205     // Used for calculating the default size.
   206     private static ListCellRenderer getDefaultListCellRenderer() {
   206     private static ListCellRenderer<Object> getDefaultListCellRenderer() {
   207         ListCellRenderer renderer = (ListCellRenderer)AppContext.
   207         @SuppressWarnings("unchecked")
       
   208         ListCellRenderer<Object> renderer = (ListCellRenderer)AppContext.
   208                          getAppContext().get(COMBO_UI_LIST_CELL_RENDERER_KEY);
   209                          getAppContext().get(COMBO_UI_LIST_CELL_RENDERER_KEY);
   209 
   210 
   210         if (renderer == null) {
   211         if (renderer == null) {
   211             renderer = new DefaultListCellRenderer();
   212             renderer = new DefaultListCellRenderer();
   212             AppContext.getAppContext().put(COMBO_UI_LIST_CELL_RENDERER_KEY,
   213             AppContext.getAppContext().put(COMBO_UI_LIST_CELL_RENDERER_KEY,
   243 
   244 
   244     @Override
   245     @Override
   245     public void installUI( JComponent c ) {
   246     public void installUI( JComponent c ) {
   246         isMinimumSizeDirty = true;
   247         isMinimumSizeDirty = true;
   247 
   248 
   248         comboBox = (JComboBox)c;
   249         @SuppressWarnings("unchecked")
       
   250         JComboBox<Object> tmp = (JComboBox)c;
       
   251         comboBox = tmp;
   249         installDefaults();
   252         installDefaults();
   250         popup = createPopup();
   253         popup = createPopup();
   251         listBox = popup.getList();
   254         listBox = popup.getList();
   252 
   255 
   253         // Is this combo box a cell editor?
   256         // Is this combo box a cell editor?
   506      * explicitly set with <code>setRenderer</code>.
   509      * explicitly set with <code>setRenderer</code>.
   507      *
   510      *
   508      * @return a <code>ListCellRender</code> used for the combo box
   511      * @return a <code>ListCellRender</code> used for the combo box
   509      * @see javax.swing.JComboBox#setRenderer
   512      * @see javax.swing.JComboBox#setRenderer
   510      */
   513      */
   511     protected ListCellRenderer createRenderer() {
   514     protected ListCellRenderer<Object> createRenderer() {
   512         return new BasicComboBoxRenderer.UIResource();
   515         return new BasicComboBoxRenderer.UIResource();
   513     }
   516     }
   514 
   517 
   515     /**
   518     /**
   516      * Creates the default editor that will be used in editable combo boxes.
   519      * Creates the default editor that will be used in editable combo boxes.
   863     //
   866     //
   864 
   867 
   865     /**
   868     /**
   866      * Tells if the popup is visible or not.
   869      * Tells if the popup is visible or not.
   867      */
   870      */
   868     public boolean isPopupVisible( JComboBox c ) {
   871     public boolean isPopupVisible( JComboBox<?> c ) {
   869         return popup.isVisible();
   872         return popup.isVisible();
   870     }
   873     }
   871 
   874 
   872     /**
   875     /**
   873      * Hides the popup.
   876      * Hides the popup.
   874      */
   877      */
   875     public void setPopupVisible( JComboBox c, boolean v ) {
   878     public void setPopupVisible( JComboBox<?> c, boolean v ) {
   876         if ( v ) {
   879         if ( v ) {
   877             popup.show();
   880             popup.show();
   878         } else {
   881         } else {
   879             popup.hide();
   882             popup.hide();
   880         }
   883         }
   882 
   885 
   883     /**
   886     /**
   884      * Determines if the JComboBox is focus traversable.  If the JComboBox is editable
   887      * Determines if the JComboBox is focus traversable.  If the JComboBox is editable
   885      * this returns false, otherwise it returns true.
   888      * this returns false, otherwise it returns true.
   886      */
   889      */
   887     public boolean isFocusTraversable( JComboBox c ) {
   890     public boolean isFocusTraversable( JComboBox<?> c ) {
   888         return !comboBox.isEditable();
   891         return !comboBox.isEditable();
   889     }
   892     }
   890 
   893 
   891     //
   894     //
   892     // end ComboBoxUI Implementation
   895     // end ComboBoxUI Implementation
   954         getDisplaySize();
   957         getDisplaySize();
   955         if (sameBaseline) {
   958         if (sameBaseline) {
   956             Insets insets = c.getInsets();
   959             Insets insets = c.getInsets();
   957             height = height - insets.top - insets.bottom;
   960             height = height - insets.top - insets.bottom;
   958             if (!comboBox.isEditable()) {
   961             if (!comboBox.isEditable()) {
   959                 ListCellRenderer renderer = comboBox.getRenderer();
   962                 ListCellRenderer<Object> renderer = comboBox.getRenderer();
   960                 if (renderer == null)  {
   963                 if (renderer == null)  {
   961                     renderer = new DefaultListCellRenderer();
   964                     renderer = new DefaultListCellRenderer();
   962                 }
   965                 }
   963                 Object value = null;
   966                 Object value = null;
   964                 Object prototypeValue = comboBox.getPrototypeDisplayValue();
   967                 Object prototypeValue = comboBox.getPrototypeDisplayValue();
  1011         getDisplaySize();
  1014         getDisplaySize();
  1012         if (comboBox.isEditable()) {
  1015         if (comboBox.isEditable()) {
  1013             return editor.getBaselineResizeBehavior();
  1016             return editor.getBaselineResizeBehavior();
  1014         }
  1017         }
  1015         else if (sameBaseline) {
  1018         else if (sameBaseline) {
  1016             ListCellRenderer renderer = comboBox.getRenderer();
  1019             ListCellRenderer<Object> renderer = comboBox.getRenderer();
  1017             if (renderer == null)  {
  1020             if (renderer == null)  {
  1018                 renderer = new DefaultListCellRenderer();
  1021                 renderer = new DefaultListCellRenderer();
  1019             }
  1022             }
  1020             Object value = null;
  1023             Object value = null;
  1021             Object prototypeValue = comboBox.getPrototypeDisplayValue();
  1024             Object prototypeValue = comboBox.getPrototypeDisplayValue();
  1203 
  1206 
  1204     /**
  1207     /**
  1205      * Paints the currently selected item.
  1208      * Paints the currently selected item.
  1206      */
  1209      */
  1207     public void paintCurrentValue(Graphics g,Rectangle bounds,boolean hasFocus) {
  1210     public void paintCurrentValue(Graphics g,Rectangle bounds,boolean hasFocus) {
  1208         ListCellRenderer renderer = comboBox.getRenderer();
  1211         ListCellRenderer<Object> renderer = comboBox.getRenderer();
  1209         Component c;
  1212         Component c;
  1210 
  1213 
  1211         if ( hasFocus && !isPopupVisible(comboBox) ) {
  1214         if ( hasFocus && !isPopupVisible(comboBox) ) {
  1212             c = renderer.getListCellRendererComponent( listBox,
  1215             c = renderer.getListCellRendererComponent( listBox,
  1213                                                        comboBox.getSelectedItem(),
  1216                                                        comboBox.getSelectedItem(),
  1320         if (!isDisplaySizeDirty)  {
  1323         if (!isDisplaySizeDirty)  {
  1321             return new Dimension(cachedDisplaySize);
  1324             return new Dimension(cachedDisplaySize);
  1322         }
  1325         }
  1323         Dimension result = new Dimension();
  1326         Dimension result = new Dimension();
  1324 
  1327 
  1325         ListCellRenderer renderer = comboBox.getRenderer();
  1328         ListCellRenderer<Object> renderer = comboBox.getRenderer();
  1326         if (renderer == null)  {
  1329         if (renderer == null)  {
  1327             renderer = new DefaultListCellRenderer();
  1330             renderer = new DefaultListCellRenderer();
  1328         }
  1331         }
  1329 
  1332 
  1330         sameBaseline = true;
  1333         sameBaseline = true;
  1336                                                                                prototypeValue,
  1339                                                                                prototypeValue,
  1337                                                                                -1, false, false));
  1340                                                                                -1, false, false));
  1338         } else {
  1341         } else {
  1339             // Calculate the dimension by iterating over all the elements in the combo
  1342             // Calculate the dimension by iterating over all the elements in the combo
  1340             // box list.
  1343             // box list.
  1341             ComboBoxModel model = comboBox.getModel();
  1344             ComboBoxModel<Object> model = comboBox.getModel();
  1342             int modelSize = model.getSize();
  1345             int modelSize = model.getSize();
  1343             int baseline = -1;
  1346             int baseline = -1;
  1344             Dimension d;
  1347             Dimension d;
  1345 
  1348 
  1346             Component cpn;
  1349             Component cpn;
  1482             super(name);
  1485             super(name);
  1483         }
  1486         }
  1484 
  1487 
  1485         public void actionPerformed( ActionEvent e ) {
  1488         public void actionPerformed( ActionEvent e ) {
  1486             String key = getName();
  1489             String key = getName();
  1487             JComboBox comboBox = (JComboBox)e.getSource();
  1490             @SuppressWarnings("unchecked")
       
  1491             JComboBox<Object> comboBox = (JComboBox)e.getSource();
  1488             BasicComboBoxUI ui = (BasicComboBoxUI)BasicLookAndFeel.getUIOfType(
  1492             BasicComboBoxUI ui = (BasicComboBoxUI)BasicLookAndFeel.getUIOfType(
  1489                                   comboBox.getUI(), BasicComboBoxUI.class);
  1493                                   comboBox.getUI(), BasicComboBoxUI.class);
  1490             if (key == HIDE) {
  1494             if (key == HIDE) {
  1491                 comboBox.firePopupMenuCanceled();
  1495                 comboBox.firePopupMenuCanceled();
  1492                 comboBox.setPopupVisible(false);
  1496                 comboBox.setPopupVisible(false);
  1623                     }
  1627                     }
  1624                 }
  1628                 }
  1625             }
  1629             }
  1626         }
  1630         }
  1627 
  1631 
  1628         private int getNextIndex(JComboBox comboBox, String key) {
  1632         private int getNextIndex(JComboBox<?> comboBox, String key) {
  1629             int listHeight = comboBox.getMaximumRowCount();
  1633             int listHeight = comboBox.getMaximumRowCount();
  1630 
  1634 
  1631             int selectedIndex = comboBox.getSelectedIndex();
  1635             int selectedIndex = comboBox.getSelectedIndex();
  1632             if (UIManager.getBoolean("ComboBox.noActionOnKeyNavigation")
  1636             if (UIManager.getBoolean("ComboBox.noActionOnKeyNavigation")
  1633                     && (comboBox.getUI() instanceof BasicComboBoxUI)) {
  1637                     && (comboBox.getUI() instanceof BasicComboBoxUI)) {
  1683                     isMinimumSizeDirty = true;
  1687                     isMinimumSizeDirty = true;
  1684                     isDisplaySizeDirty = true;
  1688                     isDisplaySizeDirty = true;
  1685                     comboBox.revalidate();
  1689                     comboBox.revalidate();
  1686                 }
  1690                 }
  1687             } else {
  1691             } else {
  1688                 JComboBox comboBox = (JComboBox)e.getSource();
  1692                 @SuppressWarnings("unchecked")
       
  1693                 JComboBox<?> comboBox = (JComboBox)e.getSource();
  1689                 if ( propertyName == "model" ) {
  1694                 if ( propertyName == "model" ) {
  1690                     ComboBoxModel newModel = (ComboBoxModel)e.getNewValue();
  1695                     @SuppressWarnings("unchecked")
  1691                     ComboBoxModel oldModel = (ComboBoxModel)e.getOldValue();
  1696                     ComboBoxModel<?> newModel = (ComboBoxModel)e.getNewValue();
       
  1697                     @SuppressWarnings("unchecked")
       
  1698                     ComboBoxModel<?> oldModel = (ComboBoxModel)e.getOldValue();
  1692 
  1699 
  1693                     if ( oldModel != null && listDataListener != null ) {
  1700                     if ( oldModel != null && listDataListener != null ) {
  1694                         oldModel.removeListDataListener( listDataListener );
  1701                         oldModel.removeListDataListener( listDataListener );
  1695                     }
  1702                     }
  1696 
  1703 
  1895         public Dimension minimumLayoutSize(Container parent) {
  1902         public Dimension minimumLayoutSize(Container parent) {
  1896             return parent.getMinimumSize();
  1903             return parent.getMinimumSize();
  1897         }
  1904         }
  1898 
  1905 
  1899         public void layoutContainer(Container parent) {
  1906         public void layoutContainer(Container parent) {
  1900             JComboBox cb = (JComboBox)parent;
  1907             @SuppressWarnings("unchecked")
       
  1908             JComboBox<?> cb = (JComboBox)parent;
  1901             int width = cb.getWidth();
  1909             int width = cb.getWidth();
  1902             int height = cb.getHeight();
  1910             int height = cb.getHeight();
  1903 
  1911 
  1904             Insets insets = getInsets();
  1912             Insets insets = getInsets();
  1905             int buttonHeight = height - (insets.top + insets.bottom);
  1913             int buttonHeight = height - (insets.top + insets.bottom);
  1957 
  1965 
  1958     class DefaultKeySelectionManager implements JComboBox.KeySelectionManager, UIResource {
  1966     class DefaultKeySelectionManager implements JComboBox.KeySelectionManager, UIResource {
  1959         private String prefix = "";
  1967         private String prefix = "";
  1960         private String typedString = "";
  1968         private String typedString = "";
  1961 
  1969 
  1962         public int selectionForKey(char aKey,ComboBoxModel aModel) {
  1970         public int selectionForKey(char aKey,ComboBoxModel<?> aModel) {
  1963             if (lastTime == 0L) {
  1971             if (lastTime == 0L) {
  1964                 prefix = "";
  1972                 prefix = "";
  1965                 typedString = "";
  1973                 typedString = "";
  1966             }
  1974             }
  1967             boolean startingFromSelection = true;
  1975             boolean startingFromSelection = true;