basiccomboboxui.java

来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 1,343 行 · 第 1/3 页

JAVA
1,343
字号
    Component comp = DEFAULT_RENDERER.getListCellRendererComponent(listBox,                                                                   " ", -1,                                                                   false,                                                                   false);    currentValuePane.add(comp);    comp.setFont(comboBox.getFont());    Dimension d = comp.getPreferredSize();    currentValuePane.remove(comp);    return d;  }  /**   * Returns the size of the display area for the combo box. This size will be    * the size of the combo box, not including the arrowButton.   *   * @return The size of the display area for the combo box.   */  protected Dimension getDisplaySize()  {    Dimension dim = new Dimension();    ListCellRenderer renderer = comboBox.getRenderer();    if (renderer == null)      {        renderer = DEFAULT_RENDERER;      }        Object prototype = comboBox.getPrototypeDisplayValue();    if (prototype != null)      {        Component comp = renderer.getListCellRendererComponent          (listBox, prototype, -1, false, false);        currentValuePane.add(comp);        comp.setFont(comboBox.getFont());        Dimension renderSize = comp.getPreferredSize();        currentValuePane.remove(comp);        dim.height = renderSize.height;        dim.width = renderSize.width;      }    else      {        ComboBoxModel model = comboBox.getModel();        int size = model.getSize();        if (size > 0)          {            for (int i = 0; i < size; ++i)              {                Component comp = renderer.getListCellRendererComponent                (listBox, model.getElementAt(i), -1, false, false);                currentValuePane.add(comp);                comp.setFont(comboBox.getFont());                Dimension renderSize = comp.getPreferredSize();                currentValuePane.remove(comp);                dim.width = Math.max(dim.width, renderSize.width);                dim.height = Math.max(dim.height, renderSize.height);              }          }        else          {            dim = getDefaultSize();            if (comboBox.isEditable())              dim.width = 100;          }      }    if (comboBox.isEditable())      {        Dimension editSize = editor.getPreferredSize();        dim.width = Math.max(dim.width, editSize.width);        dim.height = Math.max(dim.height, editSize.height);      }    displaySize.setSize(dim.width, dim.height);    return dim;  }  /**   * Installs the keyboard actions for the {@link JComboBox} as specified   * by the look and feel.   */  protected void installKeyboardActions()  {    // FIXME: Need to implement.  }  /**   * Uninstalls the keyboard actions for the {@link JComboBox} there were   * installed by in {@link #installListeners}.   */  protected void uninstallKeyboardActions()  {    // FIXME: Need to implement.  }  /**   * A {@link LayoutManager} used to position the sub-components of the   * {@link JComboBox}.   *    * @see BasicComboBoxUI#createLayoutManager()   */  public class ComboBoxLayoutManager implements LayoutManager  {    /**     * Creates a new ComboBoxLayoutManager object.     */    public ComboBoxLayoutManager()    {      // Nothing to do here.    }    /**     * Adds a component to the layout.  This method does nothing, since the     * layout manager doesn't need to track the components.     *      * @param name  the name to associate the component with (ignored).     * @param comp  the component (ignored).     */    public void addLayoutComponent(String name, Component comp)    {      // Do nothing    }    /**     * Removes a component from the layout.  This method does nothing, since     * the layout manager doesn't need to track the components.     *      * @param comp  the component.     */    public void removeLayoutComponent(Component comp)    {      // Do nothing    }    /**     * Returns preferred layout size of the JComboBox.     *     * @param parent  the Container for which the preferred size should be      *                calculated.     *     * @return The preferred size for the given container     */    public Dimension preferredLayoutSize(Container parent)    {      return parent.getPreferredSize();    }    /**     * Returns the minimum layout size.     *      * @param parent  the container.     *      * @return The minimum size.     */    public Dimension minimumLayoutSize(Container parent)    {      return parent.getMinimumSize();    }    /**     * Arranges the components in the container.  It puts arrow     * button right end part of the comboBox. If the comboBox is editable     * then editor is placed to the left of arrow  button, starting from the     * beginning.     *     * @param parent Container that should be layed out.     */    public void layoutContainer(Container parent)    {      // Position editor component to the left of arrow button if combo box is       // editable      Insets i = getInsets();      int arrowSize = comboBox.getHeight() - (i.top + i.bottom);      int editorWidth = comboBox.getBounds().width - arrowSize;      if (arrowButton != null)        arrowButton.setBounds(comboBox.getWidth() - (i.right + arrowSize),                              i.top, arrowSize, arrowSize);      if (editor != null)        editor.setBounds(rectangleForCurrentValue());    }  }  /**   * Handles focus changes occuring in the combo box. This class is   * responsible for repainting combo box whenever focus is gained or lost   * and also for hiding popup list of items whenever combo box loses its   * focus.   */  public class FocusHandler extends Object implements FocusListener  {    /**     * Creates a new FocusHandler object.     */    public FocusHandler()    {      // Nothing to do here.    }    /**     * Invoked when combo box gains focus. It repaints main     * part of combo box accordingly.     *     * @param e the FocusEvent     */    public void focusGained(FocusEvent e)    {      hasFocus = true;      comboBox.repaint();    }    /**     * Invoked when the combo box loses focus.  It repaints the main part     * of the combo box accordingly and hides the popup list of items.     *     * @param e the FocusEvent     */    public void focusLost(FocusEvent e)    {      hasFocus = false;      if (! e.isTemporary() && comboBox.isLightWeightPopupEnabled())        setPopupVisible(comboBox, false);      comboBox.repaint();    }  }  /**   * Handles {@link ItemEvent}s fired by the {@link JComboBox} when its    * selected item changes.   */  public class ItemHandler extends Object implements ItemListener  {    /**     * Creates a new ItemHandler object.     */    public ItemHandler()    {      // Nothing to do here.    }    /**     * Invoked when selected item becomes deselected or when     * new item becomes selected.     *     * @param e the ItemEvent representing item's state change.     */    public void itemStateChanged(ItemEvent e)    {      ComboBoxModel model = comboBox.getModel();      Object v = model.getSelectedItem();      if (editor != null)        {          comboBox.configureEditor(comboBox.getEditor(), v);        }      comboBox.repaint();    }  }  /**   * KeyHandler handles key events occuring while JComboBox has focus.   */  public class KeyHandler extends KeyAdapter  {    public KeyHandler()    {      // Nothing to do here.    }    /**     * Invoked whenever key is pressed while JComboBox is in focus.     */    public void keyPressed(KeyEvent e)    {      // FIXME: This method calls JComboBox.selectWithKeyChar if the key that was       // pressed is not a navigation key.     }  }  /**   * Handles the changes occurring in the JComboBox's data model.   */  public class ListDataHandler extends Object implements ListDataListener  {    /**     * Creates a new ListDataHandler object.     */    public ListDataHandler()    {      // Nothing to do here.    }    /**     * Invoked if the content's of JComboBox's data model are changed.     *     * @param e ListDataEvent describing the change.     */    public void contentsChanged(ListDataEvent e)    {      if (e.getIndex0() != -1 || e.getIndex1() != -1)        {          isMinimumSizeDirty = true;          comboBox.revalidate();        }      if (editor != null)        {          comboBox.configureEditor(comboBox.getEditor(),                                   comboBox.getSelectedItem());        }      comboBox.repaint();    }    /**     * Invoked when items are added to the JComboBox's data model.     *     * @param e ListDataEvent describing the change.     */    public void intervalAdded(ListDataEvent e)    {      int start = e.getIndex0();      int end = e.getIndex1();      if (start == 0 && comboBox.getItemCount() - (end - start + 1) == 0)        {          contentsChanged(e);        }      else if (start != -1  || end != -1)        {          ListCellRenderer renderer = comboBox.getRenderer();          ComboBoxModel model = comboBox.getModel();          int w = displaySize.width;          int h = displaySize.height;          // TODO: Optimize using prototype here.          for (int i = start; i <= end; ++i)            {              Component comp =                renderer.getListCellRendererComponent(listBox,                                                      model.getElementAt(i),                                                      -1, false, false);              currentValuePane.add(comp);              comp.setFont(comboBox.getFont());              Dimension dim = comp.getPreferredSize();              w = Math.max(w, dim.width);              h = Math.max(h, dim.height);              currentValuePane.remove(comp);            }          if (displaySize.width < w || displaySize.height < h)            {              if (displaySize.width < w)                {                  displaySize.width = w;                }              if (displaySize.height < h)                {                  displaySize.height = h;                }              comboBox.revalidate();              if (editor != null)                {                  comboBox.configureEditor(comboBox.getEditor(),                                           comboBox.getSelectedItem());                }            }        }          }    /**     * Invoked when items are removed from the JComboBox's     * data model.     *     * @param e ListDataEvent describing the change.     */    public void intervalRemoved(ListDataEvent e)    {      contentsChanged(e);    }  }  /**   * Handles {@link PropertyChangeEvent}s fired by the {@link JComboBox}.   */  public class PropertyChangeHandler extends Object    implements PropertyChangeListener  {    /**     * Creates a new instance.     */    public PropertyChangeHandler()    {      // Nothing to do here.    }    /**     * Invoked whenever bound property of JComboBox changes.     *      * @param e  the event.     */    public void propertyChange(PropertyChangeEvent e)    {      // Lets assume every change invalidates the minimumsize.      isMinimumSizeDirty = true;      if (e.getPropertyName().equals("enabled"))        {	  arrowButton.setEnabled(comboBox.isEnabled());	  if (comboBox.isEditable())	    comboBox.getEditor().getEditorComponent().setEnabled(comboBox	                                                         .isEnabled());        }      else if (e.getPropertyName().equals("editable"))        {	  if (comboBox.isEditable())	    {	      configureEditor();	      addEditor();	    }	  else	    {	      unconfigureEditor();	      removeEditor();	    }	  comboBox.revalidate();	  comboBox.repaint();        }      else if (e.getPropertyName().equals("dataModel"))        {	  // remove ListDataListener from old model and add it to new model	  ComboBoxModel oldModel = (ComboBoxModel) e.getOldValue();	  if (oldModel != null)	    oldModel.removeListDataListener(listDataListener);	  if ((ComboBoxModel) e.getNewValue() != null)	    comboBox.getModel().addListDataListener(listDataListener);        }      else if (e.getPropertyName().equals("font"))        {          Font font = (Font) e.getNewValue();          editor.setFont(font);          listBox.setFont(font);          arrowButton.setFont(font);          comboBox.revalidate();          comboBox.repaint();        }      // FIXME: Need to handle changes in other bound properties.	    }  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?