basiccombopopup.java

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

JAVA
1,120
字号
        if (index == -1)          {            if (point.y < 0)              index = 0;            else              index = comboBox.getModel().getSize() - 1;          }        if (list.getSelectedIndex() != index)          {            list.setSelectedIndex(index);            if (shouldScroll)              list.ensureIndexIsVisible(index);          }      }  }  /**   * InvocationMouseHandler is a listener that listens to mouse events   * occuring in the combo box. Note that this listener doesn't listen to   * mouse events occuring in the popup portion of the combo box, it only   * listens to main combo box part(area that displays selected item).  This   * listener is responsible for showing and hiding popup portion  of the   * combo box.   */  protected class InvocationMouseHandler extends MouseAdapter  {    /**     * Creates a new InvocationMouseHandler object.     */    protected InvocationMouseHandler()    {      // Nothing to do here.    }    /**     * This method is invoked whenever mouse is being pressed over the main     * part of the combo box. This method will show popup if  the popup is     * not shown on the screen right now, and it will hide popup otherwise.     *     * @param e MouseEvent that should be handled     */    public void mousePressed(MouseEvent e)    {      if (SwingUtilities.isLeftMouseButton(e) && comboBox.isEnabled())        {          delegateFocus(e);          togglePopup();        }    }    /**     * This method is invoked whenever mouse event was originated in the combo     * box and released either in the combBox list of items or in the combo     * box itself.     *     * @param e MouseEvent that should be handled     */    public void mouseReleased(MouseEvent e)    {      Component component = (Component) e.getSource();      Dimension size = component.getSize();      Rectangle bounds = new Rectangle(0, 0, size.width - 1, size.height - 1);      // If mouse was released inside the bounds of combo box then do nothing,      // Otherwise if mouse was released inside the list of combo box items      // then change selection and close popup      if (! bounds.contains(e.getPoint()))        {          MouseEvent convEvent = convertMouseEvent(e);          Point point = convEvent.getPoint();          Rectangle visRect = new Rectangle();          list.computeVisibleRect(visRect);          if (visRect.contains(point))            {              updateListBoxSelectionForEvent(convEvent, false);              comboBox.setSelectedIndex(list.getSelectedIndex());            }          hide();        }      hasEntered = false;      stopAutoScrolling();    }  }  /**   * InvocationMouseMotionListener is a mouse listener that listens to mouse   * dragging events occuring in the combo box.   */  protected class InvocationMouseMotionHandler extends MouseMotionAdapter  {    /**     * Creates a new InvocationMouseMotionHandler object.     */    protected InvocationMouseMotionHandler()    {      // Nothing to do here.    }    /**     * This method is responsible for highlighting item in the drop down list     * over which the mouse is currently being dragged.     */    public void mouseDragged(MouseEvent e)    {      if (isVisible())        {          MouseEvent convEvent = convertMouseEvent(e);          Rectangle visRect = new Rectangle();          list.computeVisibleRect(visRect);          if (convEvent.getPoint().y >= visRect.y              && (convEvent.getPoint().y <= visRect.y + visRect.height - 1))            {              hasEntered = true;              if (isAutoScrolling)                stopAutoScrolling();              Point point = convEvent.getPoint();              if (visRect.contains(point))                {                  valueIsAdjusting = true;                  updateListBoxSelectionForEvent(convEvent, false);                  valueIsAdjusting = false;                }            }          else if (hasEntered)            {              int dir = convEvent.getPoint().y < visRect.y ? SCROLL_UP                                                           : SCROLL_DOWN;              if (isAutoScrolling && scrollDirection != dir)                {                  stopAutoScrolling();                  startAutoScrolling(dir);                }              else if (!isAutoScrolling)                startAutoScrolling(dir);            }          else if (e.getPoint().y < 0)            {              hasEntered = true;              startAutoScrolling(SCROLL_UP);            }        }    }  }  /**   * ItemHandler is an item listener that listens to selection events occuring   * in the combo box. FIXME: should specify here what it does when item is   * selected or deselected in the combo box list.   */  protected class ItemHandler extends Object implements ItemListener  {    /**     * Creates a new ItemHandler object.     */    protected ItemHandler()    {      // Nothing to do here.    }    /**     * This method responds to the selection events occuring in the combo box.     *     * @param e ItemEvent specifying the combo box's selection     */    public void itemStateChanged(ItemEvent e)    {      if (e.getStateChange() == ItemEvent.SELECTED && ! valueIsAdjusting)        {          valueIsAdjusting = true;          syncListSelection();          valueIsAdjusting = false;          list.ensureIndexIsVisible(comboBox.getSelectedIndex());        }    }  }  /**   * ListMouseHandler is a listener that listens to mouse events occuring in   * the combo box's list of items. This class is responsible for hiding   * popup portion of the combo box if the mouse is released inside the combo   * box's list.   */  protected class ListMouseHandler extends MouseAdapter  {    protected ListMouseHandler()    {      // Nothing to do here.    }    public void mousePressed(MouseEvent e)    {      // Nothing to do here.    }    public void mouseReleased(MouseEvent anEvent)    {      comboBox.setSelectedIndex(list.getSelectedIndex());      hide();    }  }  /**   * ListMouseMotionHandler listens to mouse motion events occuring in the   * combo box's list. This class is responsible for highlighting items in   * the list when mouse is moved over them   */  protected class ListMouseMotionHandler extends MouseMotionAdapter  {    protected ListMouseMotionHandler()    {      // Nothing to do here.    }    public void mouseMoved(MouseEvent anEvent)    {      Point point = anEvent.getPoint();      Rectangle visRect = new Rectangle();      list.computeVisibleRect(visRect);      if (visRect.contains(point))        {          valueIsAdjusting = true;          updateListBoxSelectionForEvent(anEvent, false);          valueIsAdjusting = false;        }    }  }  /**   * This class listens to changes occuring in the bound properties of the   * combo box   */  protected class PropertyChangeHandler extends Object    implements PropertyChangeListener  {    protected PropertyChangeHandler()    {      // Nothing to do here.    }    public void propertyChange(PropertyChangeEvent e)    {      if (e.getPropertyName().equals("renderer"))        {	  list.setCellRenderer(comboBox.getRenderer());	  if (isVisible())	    hide();        }      if (e.getPropertyName().equals("model"))        {          ComboBoxModel oldModel = (ComboBoxModel) e.getOldValue();          uninstallComboBoxModelListeners(oldModel);          ComboBoxModel newModel = (ComboBoxModel) e.getNewValue();          list.setModel(newModel);          installComboBoxModelListeners(newModel);          if (comboBox.getItemCount() > 0)            comboBox.setSelectedIndex(0);          if (isVisible())            hide();        }    }  }  // ------ private helper methods --------------------  /**   * This method uninstalls listeners installed by the UI   */  private void uninstallListeners()  {    uninstallComboBoxListeners();    uninstallComboBoxModelListeners(comboBox.getModel());  }  /**   * This method uninstalls Listeners registered with combo boxes list of   * items   */  private void uninstallListListeners()  {    list.removeMouseListener(listMouseListener);    listMouseListener = null;    list.removeMouseMotionListener(listMouseMotionListener);    listMouseMotionListener = null;  }  /**   * This method uninstalls listeners listening to combo box  associated with   * this popup menu   */  private void uninstallComboBoxListeners()  {    comboBox.removeItemListener(itemListener);    itemListener = null;    comboBox.removePropertyChangeListener(propertyChangeListener);    propertyChangeListener = null;  }  void syncListSelection()  {    int index = comboBox.getSelectedIndex();    if (index == -1)      list.clearSelection();    else      list.setSelectedIndex(index);  }  // --------------------------------------------------------------------  //  The following classes are here only for backwards API compatibility  //  They aren't used.  // --------------------------------------------------------------------  /**   * This class is not used any more.   */  public class ListDataHandler extends Object implements ListDataListener  {    public ListDataHandler()    {      // Nothing to do here.    }    public void contentsChanged(ListDataEvent e)    {      // Nothing to do here.    }    public void intervalAdded(ListDataEvent e)    {      // Nothing to do here.    }    public void intervalRemoved(ListDataEvent e)    {      // Nothing to do here.    }  }  /**   * This class is not used anymore   */  protected class ListSelectionHandler extends Object    implements ListSelectionListener  {    protected ListSelectionHandler()    {      // Nothing to do here.    }    public void valueChanged(ListSelectionEvent e)    {      // Nothing to do here.    }  }  /**   * This class is not used anymore   */  public class InvocationKeyHandler extends KeyAdapter  {    public InvocationKeyHandler()    {      // Nothing to do here.    }    public void keyReleased(KeyEvent e)    {      // Nothing to do here.    }  }}

⌨️ 快捷键说明

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