basictableheaderui.java

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

JAVA
569
字号
/* BasicTableHeaderUI.java --   Copyright (C) 2004 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. */package javax.swing.plaf.basic;import java.awt.Component;import java.awt.Cursor;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Rectangle;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import javax.swing.CellRendererPane;import javax.swing.JComponent;import javax.swing.LookAndFeel;import javax.swing.Timer;import javax.swing.UIManager;import javax.swing.border.Border;import javax.swing.event.MouseInputListener;import javax.swing.plaf.ComponentUI;import javax.swing.plaf.TableHeaderUI;import javax.swing.table.JTableHeader;import javax.swing.table.TableCellRenderer;import javax.swing.table.TableColumn;import javax.swing.table.TableColumnModel;/** * Basic pluggable look and feel interface for JTableHeader. */public class BasicTableHeaderUI extends TableHeaderUI{  /**   * The width of the space (in both direction) around the column boundary,   * where mouse cursor changes shape into "resize"   */  static int COLUMN_BOUNDARY_TOLERANCE = 3;    public static ComponentUI createUI(JComponent h)  {    return new BasicTableHeaderUI();  }    /**   * The table header that is using this interface.   */  protected JTableHeader header;    /**   * The mouse input listener, responsible for mouse manipulations with   * the table header.   */  protected MouseInputListener mouseInputListener;    /**   * Paint the header cell.   */  protected CellRendererPane rendererPane;    /**   * The header cell border.   */  private Border cellBorder;    /**   * If not null, one of the columns is currently being dragged.   */  Rectangle draggingHeaderRect;    /**   * Handles column movement and rearrangement by mouse. The same instance works   * both as mouse listener and the mouse motion listner.   */  public class MouseInputHandler      implements MouseInputListener  {    /**     * If true, the cursor is being already shown in the alternative "resize"     * shape.      */    boolean showingResizeCursor;    /**     * The position, from where the cursor is dragged during resizing. Double     * purpose field (absolute value during resizing and relative offset during     * column dragging).     */    int draggingFrom = - 1;        /**     * The number of the column being dragged.     */    int draggingColumnNumber;        /**     * The previous preferred width of the column.     */    int prevPrefWidth = - 1;    /**     * The timer to coalesce column resizing events.     */    Timer timer;    /**     * Returns without action, part of the MouseInputListener interface.     */    public void mouseClicked(MouseEvent e)    {      // Nothing to do.    }    /**     * If being in the resizing mode, handle resizing.     */    public void mouseDragged(MouseEvent e)    {      TableColumn resizeIt = header.getResizingColumn();      if (resizeIt != null && header.getResizingAllowed())        {          // The timer is intialised on demand.          if (timer == null)            {              // The purpose of timer is to coalesce events. If the queue              // is free, the repaint event is fired immediately.              timer = new Timer(1, new ActionListener()              {                public void actionPerformed(ActionEvent e)                {                  header.getTable().doLayout();                }              });              timer.setRepeats(false);              timer.setCoalesce(true);            }          resizeIt.setPreferredWidth(prevPrefWidth + e.getX() - draggingFrom);          timer.restart();        }      else if (draggingHeaderRect != null && header.getReorderingAllowed())        {          draggingHeaderRect.x = e.getX() + draggingFrom;          header.repaint();        }    }    /**     * Returns without action, part of the MouseInputListener interface.     */    public void mouseEntered(MouseEvent e)    {      // Nothing to do.    }    /**     * Reset drag information of the column resizing.     */    public void mouseExited(MouseEvent e)    {      if (header.getResizingColumn() != null && header.getResizingAllowed())        endResizing();      if (header.getDraggedColumn() != null && header.getReorderingAllowed())        endDragging(null);    }    /**     * Change the mouse cursor if the mouse if above the column boundary.     */    public void mouseMoved(MouseEvent e)    {      // When dragging, the functionality is handled by the mouseDragged.      if (e.getButton() == 0 && header.getResizingAllowed())        {          TableColumnModel model = header.getColumnModel();          int n = model.getColumnCount();          if (n < 2)            // It must be at least two columns to have at least one boundary.            // Otherwise, nothing to do.            return;          boolean onBoundary = false;          int x = e.getX();          int a = x - COLUMN_BOUNDARY_TOLERANCE;          int b = x + COLUMN_BOUNDARY_TOLERANCE;          int p = 0;          Scan: for (int i = 0; i < n - 1; i++)            {              p += model.getColumn(i).getWidth();              if (p >= a && p <= b)                {                  TableColumn column = model.getColumn(i);                  onBoundary = true;                  draggingFrom = x;                  prevPrefWidth = column.getWidth();                  header.setResizingColumn(column);                  break Scan;                }            }          if (onBoundary != showingResizeCursor)            {              // Change the cursor shape, if needed.              if (onBoundary)                {                  if (p < x)                    header.setCursor(Cursor.getPredefinedCursor                                     (Cursor.W_RESIZE_CURSOR));                  else                    header.setCursor(Cursor.getPredefinedCursor                                     (Cursor.E_RESIZE_CURSOR));                }              else                {                  header.setCursor(Cursor.getDefaultCursor());                  header.setResizingColumn(null);                }              showingResizeCursor = onBoundary;            }        }    }    /**     * Starts the dragging/resizing procedure.     */    public void mousePressed(MouseEvent e)    {      if (header.getResizingAllowed())        {          TableColumn resizingColumn = header.getResizingColumn();          if (resizingColumn != null)            {              resizingColumn.setPreferredWidth(resizingColumn.getWidth());              return;            }        }      if (header.getReorderingAllowed())        {          TableColumnModel model = header.getColumnModel();          int n = model.getColumnCount();          if (n < 2)            // It must be at least two columns to change the column location.            return;

⌨️ 快捷键说明

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