⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basiclistui.java

📁 gcc的组建
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
  }  /**   * Creates a new BasicListUI object.   */  public BasicListUI()  {    updateLayoutStateNeeded = 1;    rendererPane = new CellRendererPane();  }  /**   * Installs various default settings (mostly colors) from the {@link   * UIDefaults} into the {@link JList}   *   * @see #uninstallDefaults   */  protected void installDefaults()  {    LookAndFeel.installColorsAndFont(list, "List.background",                                     "List.foreground", "List.font");    list.setSelectionForeground(UIManager.getColor("List.selectionForeground"));    list.setSelectionBackground(UIManager.getColor("List.selectionBackground"));    list.setOpaque(true);  }  /**   * Resets to <code>null</code> those defaults which were installed in    * {@link #installDefaults}   */  protected void uninstallDefaults()  {    list.setForeground(null);    list.setBackground(null);    list.setSelectionForeground(null);    list.setSelectionBackground(null);  }  /**   * Attaches all the listeners we have in the UI class to the {@link   * JList}, its model and its selection model.   *   * @see #uninstallListeners   */  protected void installListeners()  {    if (focusListener == null)      focusListener = createFocusListener();    list.addFocusListener(focusListener);    if (listDataListener == null)      listDataListener = createListDataListener();    list.getModel().addListDataListener(listDataListener);    if (listSelectionListener == null)      listSelectionListener = createListSelectionListener();    list.addListSelectionListener(listSelectionListener);    if (mouseInputListener == null)      mouseInputListener = createMouseInputListener();    list.addMouseListener(mouseInputListener);    list.addMouseMotionListener(mouseInputListener);    if (propertyChangeListener == null)      propertyChangeListener = createPropertyChangeListener();    list.addPropertyChangeListener(propertyChangeListener);    // FIXME: Are these two really needed? At least they are not documented.    //keyListener = new KeyHandler();    componentListener = new ComponentHandler();    list.addComponentListener(componentListener);    //list.addKeyListener(keyListener);  }  /**   * Detaches all the listeners we attached in {@link #installListeners}.   */  protected void uninstallListeners()  {    list.removeFocusListener(focusListener);    list.getModel().removeListDataListener(listDataListener);    list.removeListSelectionListener(listSelectionListener);    list.removeMouseListener(mouseInputListener);    //list.removeKeyListener(keyListener);    list.removeMouseMotionListener(mouseInputListener);    list.removePropertyChangeListener(propertyChangeListener);  }    /**   * Installs keyboard actions for this UI in the {@link JList}.   */  protected void installKeyboardActions()  {    InputMap focusInputMap = (InputMap) UIManager.get("List.focusInputMap");    InputMapUIResource parentInputMap = new InputMapUIResource();    // FIXME: The JDK uses a LazyActionMap for parentActionMap    ActionMap parentActionMap = new ActionMapUIResource();    action = new ListAction();    Object keys[] = focusInputMap.allKeys();    // Register key bindings in the UI InputMap-ActionMap pair    for (int i = 0; i < keys.length; i++)      {        KeyStroke stroke = (KeyStroke)keys[i];        String actionString = (String) focusInputMap.get(stroke);        parentInputMap.put(KeyStroke.getKeyStroke(stroke.getKeyCode(),                                                  stroke.getModifiers()),                           actionString);        parentActionMap.put (actionString,                              new ActionListenerProxy(action, actionString));      }    // Register the new InputMap-ActionMap as the parents of the list's    // InputMap and ActionMap    parentInputMap.setParent(list.getInputMap().getParent());    parentActionMap.setParent(list.getActionMap().getParent());    list.getInputMap().setParent(parentInputMap);    list.getActionMap().setParent(parentActionMap);  }  /**   * Uninstalls keyboard actions for this UI in the {@link JList}.   */  protected void uninstallKeyboardActions()  {    // TODO: Implement this properly.  }  /**   * Installs the various aspects of the UI in the {@link JList}. In   * particular, calls {@link #installDefaults}, {@link #installListeners}   * and {@link #installKeyboardActions}. Also saves a reference to the   * provided component, cast to a {@link JList}.   *   * @param c The {@link JList} to install the UI into   */  public void installUI(final JComponent c)  {    super.installUI(c);    list = (JList) c;    installDefaults();    installListeners();    installKeyboardActions();    maybeUpdateLayoutState();  }  /**   * Uninstalls all the aspects of the UI which were installed in {@link   * #installUI}. When finished uninstalling, drops the saved reference to   * the {@link JList}.   *   * @param c Ignored; the UI is uninstalled from the {@link JList}   * reference saved during the call to {@link #installUI}   */  public void uninstallUI(final JComponent c)  {    uninstallKeyboardActions();    uninstallListeners();    uninstallDefaults();    list = null;  }  /**   * Gets the size this list would prefer to assume. This is calculated by   * calling {@link #getCellBounds} over the entire list.   *   * @param c Ignored; uses the saved {@link JList} reference    *   * @return DOCUMENT ME!   */  public Dimension getPreferredSize(JComponent c)  {    int size = list.getModel().getSize();    if (size == 0)      return new Dimension(0, 0);    int visibleRows = list.getVisibleRowCount();    int layoutOrientation = list.getLayoutOrientation();    Rectangle bounds = getCellBounds(list, 0, list.getModel().getSize() - 1);    Dimension retVal = bounds.getSize();    Component parent = list.getParent();    if ((visibleRows == -1) && (parent instanceof JViewport))      {        JViewport viewport = (JViewport) parent;        if (layoutOrientation == JList.HORIZONTAL_WRAP)          {            int h = viewport.getSize().height;            int cellsPerCol = h / cellHeight;            int w = size / cellsPerCol * cellWidth;            retVal = new Dimension(w, h);          }        else if (layoutOrientation == JList.VERTICAL_WRAP)          {            int w = viewport.getSize().width;            int cellsPerRow = Math.max(w / cellWidth, 1);            int h = size / cellsPerRow * cellHeight;            retVal = new Dimension(w, h);          }      }    return retVal;  }  /**   * Paints a single cell in the list.   *   * @param g The graphics context to paint in   * @param row The row number to paint   * @param bounds The bounds of the cell to paint, assuming a coordinate   * system beginning at <code>(0,0)</code> in the upper left corner of the   * list   * @param rend A cell renderer to paint with   * @param data The data to provide to the cell renderer   * @param sel A selection model to provide to the cell renderer   * @param lead The lead selection index of the list   */  protected void paintCell(Graphics g, int row, Rectangle bounds,                 ListCellRenderer rend, ListModel data,                 ListSelectionModel sel, int lead)  {    boolean isSel = list.isSelectedIndex(row);    boolean hasFocus = (list.getLeadSelectionIndex() == row) && BasicListUI.this.list.hasFocus();    Component comp = rend.getListCellRendererComponent(list,                                                       data.getElementAt(row),                                                       0, isSel, hasFocus);    rendererPane.paintComponent(g, comp, list, bounds);  }  /**   * Paints the list by repeatedly calling {@link #paintCell} for each visible   * cell in the list.   *   * @param g The graphics context to paint with   * @param c Ignored; uses the saved {@link JList} reference    */  public void paint(Graphics g, JComponent c)  {    int nrows = list.getModel().getSize();    if (nrows == 0)      return;    maybeUpdateLayoutState();    ListCellRenderer render = list.getCellRenderer();    ListModel model = list.getModel();    ListSelectionModel sel = list.getSelectionModel();    int lead = sel.getLeadSelectionIndex();    Rectangle clip = g.getClipBounds();    int startIndex = list.locationToIndex(new Point(clip.x, clip.y));    int endIndex = list.locationToIndex(new Point(clip.x + clip.width,                                                  clip.y + clip.height));        for (int row = startIndex; row <= endIndex; ++row)      {        Rectangle bounds = getCellBounds(list, row, row);        if (bounds.intersects(clip))          paintCell(g, row, bounds, render, model, sel, lead);      }  }  /**   * Computes the index of a list cell given a point within the list. If the   * location lies outside the bounds of the list, the greatest index in the   * list model is returned.   *   * @param list the list which on which the computation is based on   * @param location the coordinates   *   * @return the index of the list item that is located at the given   *         coordinates or <code>-1</code> if the list model is empty   */  public int locationToIndex(JList list, Point location)  {    int layoutOrientation = list.getLayoutOrientation();    int index = -1;    switch (layoutOrientation)      {      case JList.VERTICAL:        index = convertYToRow(location.y);        break;      case JList.HORIZONTAL_WRAP:        // determine visible rows and cells per row        int visibleRows = list.getVisibleRowCount();        int cellsPerRow = -1;        int numberOfItems = list.getModel().getSize();        Dimension listDim = list.getSize();        if (visibleRows <= 0)          {            try              {                cellsPerRow = listDim.width / cellWidth;              }            catch (ArithmeticException ex)              {                cellsPerRow = 1;              }          }        else          {            cellsPerRow = numberOfItems / visibleRows + 1;          }        // determine index for the given location        int cellsPerColumn = numberOfItems / cellsPerRow + 1;        int gridX = Math.min(location.x / cellWidth, cellsPerRow - 1);        int gridY = Math.min(location.y / cellHeight, cellsPerColumn);        index = gridX + gridY * cellsPerRow;        break;      case JList.VERTICAL_WRAP:        // determine visible rows and cells per column        int visibleRows2 = list.getVisibleRowCount();        if (visibleRows2 <= 0)          {            Dimension listDim2 = list.getSize();            visibleRows2 = listDim2.height / cellHeight;          }        int numberOfItems2 = list.getModel().getSize();        int cellsPerRow2 = numberOfItems2 / visibleRows2 + 1;        int gridX2 = Math.min(location.x / cellWidth, cellsPerRow2 - 1);        int gridY2 = Math.min(location.y / cellHeight, visibleRows2);        index = gridY2 + gridX2 * visibleRows2;        break;      }    return index;  }  public Point indexToLocation(JList list, int index)  {    int layoutOrientation = list.getLayoutOrientation();    Point loc = null;    switch (layoutOrientation)      {      case JList.VERTICAL:        loc = new Point(0, convertRowToY(index));        break;      case JList.HORIZONTAL_WRAP:        // determine visible rows and cells per row        int visibleRows = list.getVisibleRowCount();        int numberOfCellsPerRow = -1;        if (visibleRows <= 0)          {            Dimension listDim = list.getSize();            numberOfCellsPerRow = Math.max(listDim.width / cellWidth, 1);          }        else          {            int numberOfItems = list.getModel().getSize();            numberOfCellsPerRow = numberOfItems / visibleRows + 1;          }        // compute coordinates inside the grid        int gridX = index % numberOfCellsPerRow;        int gridY = index / numberOfCellsPerRow;        int locX = gridX * cellWidth;        int locY = gridY * cellHeight;        loc = new Point(locX, locY);        break;      case JList.VERTICAL_WRAP:        // determine visible rows and cells per column        int visibleRows2 = list.getVisibleRowCount();        if (visibleRows2 <= 0)          {            Dimension listDim2 = list.getSize();            visibleRows2 = listDim2.height / cellHeight;          }        // compute coordinates inside the grid        if (visibleRows2 > 0)          {            int gridY2 = index % visibleRows2;            int gridX2 = index / visibleRows2;            int locX2 = gridX2 * cellWidth;            int locY2 = gridY2 * cellHeight;            loc = new Point(locX2, locY2);          }        else          loc = new Point(0, convertRowToY(index));        break;      }    return loc;  }  /**   * Creates and returns the focus listener for this UI.   *   * @return the focus listener for this UI   */  protected FocusListener createFocusListener()  {    return new FocusHandler();  }  /**   * Creates and returns the list data listener for this UI.   *   * @return the list data listener for this UI   */  protected ListDataListener createListDataListener()  {    return new ListDataHandler();  }  /**   * Creates and returns the list selection listener for this UI.   *   * @return the list selection listener for this UI   */  protected ListSelectionListener createListSelectionListener()  {    return new ListSelectionHandler();  }  /**   * Creates and returns the mouse input listener for this UI.   *   * @return the mouse input listener for this UI   */  protected MouseInputListener createMouseInputListener()  {    return new MouseInputHandler();  }  /**   * Creates and returns the property change listener for this UI.   *   * @return the property change listener for this UI   */  protected PropertyChangeListener createPropertyChangeListener()  {    return new PropertyChangeHandler();  }  /**   * Selects the next list item and force it to be visible.   */  protected void selectNextIndex()  {    int index = list.getSelectionModel().getLeadSelectionIndex();    if (index < list.getModel().getSize() - 1)      {        index++;        list.setSelectedIndex(index);      }    list.ensureIndexIsVisible(index);  }  /**   * Selects the previous list item and force it to be visible.   */  protected void selectPreviousIndex()  {    int index = list.getSelectionModel().getLeadSelectionIndex();    if (index > 0)      {        index--;        list.setSelectedIndex(index);      }    list.ensureIndexIsVisible(index);  }}

⌨️ 快捷键说明

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