basictableui.java

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

JAVA
1,330
字号
/* BasicTableUI.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.Color;import java.awt.Component;import java.awt.ComponentOrientation;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Point;import java.awt.Rectangle;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.FocusEvent;import java.awt.event.FocusListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.awt.event.MouseEvent;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import javax.swing.AbstractAction;import javax.swing.ActionMap;import javax.swing.CellRendererPane;import javax.swing.DefaultCellEditor;import javax.swing.DefaultListSelectionModel;import javax.swing.InputMap;import javax.swing.JComponent;import javax.swing.JTable;import javax.swing.KeyStroke;import javax.swing.ListSelectionModel;import javax.swing.LookAndFeel;import javax.swing.UIManager;import javax.swing.border.Border;import javax.swing.event.ChangeEvent;import javax.swing.event.MouseInputListener;import javax.swing.plaf.ActionMapUIResource;import javax.swing.plaf.ComponentUI;import javax.swing.plaf.InputMapUIResource;import javax.swing.plaf.TableUI;import javax.swing.table.TableCellEditor;import javax.swing.table.TableCellRenderer;import javax.swing.table.TableColumnModel;import javax.swing.table.TableModel;public class BasicTableUI extends TableUI{  public static ComponentUI createUI(JComponent comp)   {    return new BasicTableUI();  }  protected FocusListener focusListener;    protected KeyListener keyListener;     protected MouseInputListener  mouseInputListener;     protected CellRendererPane rendererPane;     protected JTable table;  /** The normal cell border. */  Border cellBorder;  /** The action bound to KeyStrokes. */  TableAction action;  /**   * Listens for changes to the tables properties.   */  private PropertyChangeListener propertyChangeListener;  /**   * Handles key events for the JTable. Key events should be handled through   * the InputMap/ActionMap mechanism since JDK1.3. This class is only there   * for backwards compatibility.   *    * @author Roman Kennke (kennke@aicas.com)   */  public class KeyHandler implements KeyListener  {    /**     * Receives notification that a key has been pressed and released.     * Activates the editing session for the focused cell by pressing the     * character keys.     *     * @param event the key event     */    public void keyTyped(KeyEvent event)    {      // Key events should be handled through the InputMap/ActionMap mechanism      // since JDK1.3. This class is only there for backwards compatibility.            // Editor activation is a specific kind of response to ''any''      // character key. Hence it is handled here.      if (!table.isEditing() && table.isEnabled())        {          int r = table.getSelectedRow();          int c = table.getSelectedColumn();          if (table.isCellEditable(r, c))            table.editCellAt(r, c);        }    }    /**     * Receives notification that a key has been pressed.     *     * @param event the key event     */    public void keyPressed(KeyEvent event)    {      // Key events should be handled through the InputMap/ActionMap mechanism      // since JDK1.3. This class is only there for backwards compatibility.    }    /**     * Receives notification that a key has been released.     *     * @param event the key event     */    public void keyReleased(KeyEvent event)    {      // Key events should be handled through the InputMap/ActionMap mechanism      // since JDK1.3. This class is only there for backwards compatibility.    }  }  public class FocusHandler implements FocusListener  {    public void focusGained(FocusEvent e)     {      // TODO: Implement this properly.    }    public void focusLost(FocusEvent e)     {      // TODO: Implement this properly.    }  }  public class MouseInputHandler implements MouseInputListener  {    Point begin, curr;    private void updateSelection(boolean controlPressed)    {      // Update the rows      int lo_row = table.rowAtPoint(begin);      int hi_row  = table.rowAtPoint(curr);      ListSelectionModel rowModel = table.getSelectionModel();      if (lo_row != -1 && hi_row != -1)        {          if (controlPressed && rowModel.getSelectionMode()               != ListSelectionModel.SINGLE_SELECTION)            rowModel.addSelectionInterval(lo_row, hi_row);          else            rowModel.setSelectionInterval(lo_row, hi_row);        }            // Update the columns      int lo_col = table.columnAtPoint(begin);      int hi_col = table.columnAtPoint(curr);      ListSelectionModel colModel = table.getColumnModel().        getSelectionModel();      if (lo_col != -1 && hi_col != -1)        {          if (controlPressed && colModel.getSelectionMode() !=               ListSelectionModel.SINGLE_SELECTION)            colModel.addSelectionInterval(lo_col, hi_col);          else            colModel.setSelectionInterval(lo_col, hi_col);        }    }        /**     * For the double click, start the cell editor.     */    public void mouseClicked(MouseEvent e)    {      Point p = e.getPoint();      int row = table.rowAtPoint(p);      int col = table.columnAtPoint(p);      if (table.isCellEditable(row, col))        {          // If the cell editor is the default editor, we request the          // number of the required clicks from it. Otherwise,          // require two clicks (double click).          TableCellEditor editor = table.getCellEditor(row, col);          if (editor instanceof DefaultCellEditor)            {              DefaultCellEditor ce = (DefaultCellEditor) editor;              if (e.getClickCount() < ce.getClickCountToStart())                return;            }          else if (e.getClickCount() < 2)            return;          table.editCellAt(row, col);        }    }    public void mouseDragged(MouseEvent e)     {      if (table.isEnabled())        {          curr = new Point(e.getX(), e.getY());          updateSelection(e.isControlDown());        }    }    public void mouseEntered(MouseEvent e)     {      // TODO: What should be done here, if anything?    }    public void mouseExited(MouseEvent e)     {      // TODO: What should be done here, if anything?    }    public void mouseMoved(MouseEvent e)     {      // TODO: What should be done here, if anything?    }    public void mousePressed(MouseEvent e)     {      if (table.isEnabled())        {          ListSelectionModel rowModel = table.getSelectionModel();          ListSelectionModel colModel = table.getColumnModel().getSelectionModel();          int rowLead = rowModel.getLeadSelectionIndex();          int colLead = colModel.getLeadSelectionIndex();          begin = new Point(e.getX(), e.getY());          curr = new Point(e.getX(), e.getY());          //if control is pressed and the cell is already selected, deselect it          if (e.isControlDown() && table.              isCellSelected(table.rowAtPoint(begin),table.columnAtPoint(begin)))            {                                                     table.getSelectionModel().              removeSelectionInterval(table.rowAtPoint(begin),                                       table.rowAtPoint(begin));              table.getColumnModel().getSelectionModel().              removeSelectionInterval(table.columnAtPoint(begin),                                       table.columnAtPoint(begin));            }          else            updateSelection(e.isControlDown());          // If we were editing, but the moved to another cell, stop editing          if (rowLead != rowModel.getLeadSelectionIndex() ||              colLead != colModel.getLeadSelectionIndex())            if (table.isEditing())              table.editingStopped(new ChangeEvent(e));        }    }    public void mouseReleased(MouseEvent e)     {      if (table.isEnabled())        {          begin = null;          curr = null;        }    }  }  /**   * Listens for changes to the model property of the JTable and adjusts some   * settings.   *   * @author Roman Kennke (kennke@aicas.com)   */  private class PropertyChangeHandler implements PropertyChangeListener  {    /**     * Receives notification if one of the JTable's properties changes.     *     * @param ev the property change event     */    public void propertyChange(PropertyChangeEvent ev)    {      String propName = ev.getPropertyName();      if (propName.equals("model"))        {          ListSelectionModel rowSel = table.getSelectionModel();          rowSel.clearSelection();          ListSelectionModel colSel = table.getColumnModel().getSelectionModel();          colSel.clearSelection();          TableModel model = table.getModel();          // Adjust lead and anchor selection indices of the row and column          // selection models.          if (model.getRowCount() > 0)            {              rowSel.setAnchorSelectionIndex(0);              rowSel.setLeadSelectionIndex(0);            }          else

⌨️ 快捷键说明

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