📄 basictableui.java
字号:
/* * @(#)BasicTableUI.java 1.159 06/04/14 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package javax.swing.plaf.basic;import java.awt.*;import java.awt.datatransfer.*;import java.awt.dnd.*;import java.awt.event.*;import java.util.Enumeration;import java.util.EventObject;import java.util.Hashtable;import java.util.TooManyListenersException;import javax.swing.*;import javax.swing.event.*;import javax.swing.plaf.*;import javax.swing.text.*;import javax.swing.table.*;import javax.swing.plaf.basic.DragRecognitionSupport.BeforeDrag;import sun.swing.SwingUtilities2;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import sun.swing.DefaultLookup;import sun.swing.UIAction;/** * BasicTableUI implementation * * @version 1.159 04/14/06 * @author Philip Milne * @author Shannon Hickey (drag and drop) */public class BasicTableUI extends TableUI{ private static final StringBuilder BASELINE_COMPONENT_KEY = new StringBuilder("Table.baselineComponent");//// Instance Variables// // The JTable that is delegating the painting to this UI. protected JTable table; protected CellRendererPane rendererPane; // Listeners that are attached to the JTable protected KeyListener keyListener; protected FocusListener focusListener; protected MouseInputListener mouseInputListener; private Handler handler; /** * Local cache of Table's client property "Table.isFileList" */ private boolean isFileList = false; //// Helper class for keyboard actions// private static class Actions extends UIAction { private static final String CANCEL_EDITING = "cancel"; private static final String SELECT_ALL = "selectAll"; private static final String CLEAR_SELECTION = "clearSelection"; private static final String START_EDITING = "startEditing"; private static final String NEXT_ROW = "selectNextRow"; private static final String NEXT_ROW_CELL = "selectNextRowCell"; private static final String NEXT_ROW_EXTEND_SELECTION = "selectNextRowExtendSelection"; private static final String NEXT_ROW_CHANGE_LEAD = "selectNextRowChangeLead"; private static final String PREVIOUS_ROW = "selectPreviousRow"; private static final String PREVIOUS_ROW_CELL = "selectPreviousRowCell"; private static final String PREVIOUS_ROW_EXTEND_SELECTION = "selectPreviousRowExtendSelection"; private static final String PREVIOUS_ROW_CHANGE_LEAD = "selectPreviousRowChangeLead"; private static final String NEXT_COLUMN = "selectNextColumn"; private static final String NEXT_COLUMN_CELL = "selectNextColumnCell"; private static final String NEXT_COLUMN_EXTEND_SELECTION = "selectNextColumnExtendSelection"; private static final String NEXT_COLUMN_CHANGE_LEAD = "selectNextColumnChangeLead"; private static final String PREVIOUS_COLUMN = "selectPreviousColumn"; private static final String PREVIOUS_COLUMN_CELL = "selectPreviousColumnCell"; private static final String PREVIOUS_COLUMN_EXTEND_SELECTION = "selectPreviousColumnExtendSelection"; private static final String PREVIOUS_COLUMN_CHANGE_LEAD = "selectPreviousColumnChangeLead"; private static final String SCROLL_LEFT_CHANGE_SELECTION = "scrollLeftChangeSelection"; private static final String SCROLL_LEFT_EXTEND_SELECTION = "scrollLeftExtendSelection"; private static final String SCROLL_RIGHT_CHANGE_SELECTION = "scrollRightChangeSelection"; private static final String SCROLL_RIGHT_EXTEND_SELECTION = "scrollRightExtendSelection"; private static final String SCROLL_UP_CHANGE_SELECTION = "scrollUpChangeSelection"; private static final String SCROLL_UP_EXTEND_SELECTION = "scrollUpExtendSelection"; private static final String SCROLL_DOWN_CHANGE_SELECTION = "scrollDownChangeSelection"; private static final String SCROLL_DOWN_EXTEND_SELECTION = "scrollDownExtendSelection"; private static final String FIRST_COLUMN = "selectFirstColumn"; private static final String FIRST_COLUMN_EXTEND_SELECTION = "selectFirstColumnExtendSelection"; private static final String LAST_COLUMN = "selectLastColumn"; private static final String LAST_COLUMN_EXTEND_SELECTION = "selectLastColumnExtendSelection"; private static final String FIRST_ROW = "selectFirstRow"; private static final String FIRST_ROW_EXTEND_SELECTION = "selectFirstRowExtendSelection"; private static final String LAST_ROW = "selectLastRow"; private static final String LAST_ROW_EXTEND_SELECTION = "selectLastRowExtendSelection"; // add the lead item to the selection without changing lead or anchor private static final String ADD_TO_SELECTION = "addToSelection"; // toggle the selected state of the lead item and move the anchor to it private static final String TOGGLE_AND_ANCHOR = "toggleAndAnchor"; // extend the selection to the lead item private static final String EXTEND_TO = "extendTo"; // move the anchor to the lead and ensure only that item is selected private static final String MOVE_SELECTION_TO = "moveSelectionTo"; // give focus to the JTableHeader, if one exists private static final String FOCUS_HEADER = "focusHeader"; protected int dx; protected int dy; protected boolean extend; protected boolean inSelection; protected boolean forwards; protected boolean vertically; protected boolean toLimit; protected int leadRow; protected int leadColumn; Actions(String name) { super(name); } Actions(String name, int dx, int dy, boolean extend, boolean inSelection) { super(name); // Actions spcifying true for "inSelection" are // fairly sensitive to bad parameter values. They require // that one of dx and dy be 0 and the other be -1 or 1. // Bogus parameter values could cause an infinite loop. // To prevent any problems we massage the params here // and complain if we get something we can't deal with. if (inSelection) { this.inSelection = true; // look at the sign of dx and dy only dx = sign(dx); dy = sign(dy); // make sure one is zero, but not both assert (dx == 0 || dy == 0) && !(dx == 0 && dy == 0); } this.dx = dx; this.dy = dy; this.extend = extend; } Actions(String name, boolean extend, boolean forwards, boolean vertically, boolean toLimit) { this(name, 0, 0, extend, false); this.forwards = forwards; this.vertically = vertically; this.toLimit = toLimit; } private static int clipToRange(int i, int a, int b) { return Math.min(Math.max(i, a), b-1); } private void moveWithinTableRange(JTable table, int dx, int dy) { leadRow = clipToRange(leadRow+dy, 0, table.getRowCount()); leadColumn = clipToRange(leadColumn+dx, 0, table.getColumnCount()); } private static int sign(int num) { return (num < 0) ? -1 : ((num == 0) ? 0 : 1); } /** * Called to move within the selected range of the given JTable. * This method uses the table's notion of selection, which is * important to allow the user to navigate between items visually * selected on screen. This notion may or may not be the same as * what could be determined by directly querying the selection models. * It depends on certain table properties (such as whether or not * row or column selection is allowed). When performing modifications, * it is recommended that caution be taken in order to preserve * the intent of this method, especially when deciding whether to * query the selection models or interact with JTable directly. */ private boolean moveWithinSelectedRange(JTable table, int dx, int dy, ListSelectionModel rsm, ListSelectionModel csm) { // Note: The Actions constructor ensures that only one of // dx and dy is 0, and the other is either -1 or 1 // find out how many items the table is showing as selected // and the range of items to navigate through int totalCount; int minX, maxX, minY, maxY; boolean rs = table.getRowSelectionAllowed(); boolean cs = table.getColumnSelectionAllowed(); // both column and row selection if (rs && cs) { totalCount = table.getSelectedRowCount() * table.getSelectedColumnCount(); minX = csm.getMinSelectionIndex(); maxX = csm.getMaxSelectionIndex(); minY = rsm.getMinSelectionIndex(); maxY = rsm.getMaxSelectionIndex(); // row selection only } else if (rs) { totalCount = table.getSelectedRowCount(); minX = 0; maxX = table.getColumnCount() - 1; minY = rsm.getMinSelectionIndex(); maxY = rsm.getMaxSelectionIndex(); // column selection only } else if (cs) { totalCount = table.getSelectedColumnCount(); minX = csm.getMinSelectionIndex(); maxX = csm.getMaxSelectionIndex(); minY = 0; maxY = table.getRowCount() - 1; // no selection allowed } else { totalCount = 0; // A bogus assignment to stop javac from complaining // about unitialized values. In this case, these // won't even be used. minX = maxX = minY = maxY = 0; } // For some cases, there is no point in trying to stay within the // selected area. Instead, move outside the selection, wrapping at // the table boundaries. The cases are: boolean stayInSelection; // - nothing selected if (totalCount == 0 || // - one item selected, and the lead is already selected (totalCount == 1 && table.isCellSelected(leadRow, leadColumn))) { stayInSelection = false; maxX = table.getColumnCount() - 1; maxY = table.getRowCount() - 1; // the mins are calculated like this in case the max is -1 minX = Math.min(0, maxX); minY = Math.min(0, maxY); } else { stayInSelection = true; } // the algorithm below isn't prepared to deal with -1 lead/anchor // so massage appropriately here first if (dy == 1 && leadColumn == -1) { leadColumn = minX; leadRow = -1; } else if (dx == 1 && leadRow == -1) { leadRow = minY; leadColumn = -1; } else if (dy == -1 && leadColumn == -1) { leadColumn = maxX; leadRow = maxY + 1; } else if (dx == -1 && leadRow == -1) { leadRow = maxY; leadColumn = maxX + 1; } // In cases where the lead is not within the search range, // we need to bring it within one cell for the the search // to work properly. Check these here. leadRow = Math.min(Math.max(leadRow, minY - 1), maxY + 1); leadColumn = Math.min(Math.max(leadColumn, minX - 1), maxX + 1); // find the next position, possibly looping until it is selected do { calcNextPos(dx, minX, maxX, dy, minY, maxY); } while (stayInSelection && !table.isCellSelected(leadRow, leadColumn)); return stayInSelection; } /** * Find the next lead row and column based on the given * dx/dy and max/min values. */ private void calcNextPos(int dx, int minX, int maxX, int dy, int minY, int maxY) { if (dx != 0) { leadColumn += dx; if (leadColumn > maxX) { leadColumn = minX; leadRow++; if (leadRow > maxY) { leadRow = minY; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -