📄 basictableui.java
字号:
// DefaultListSelectionModel return sender != null && ((JTable)sender).getColumnModel().getSelectionModel() instanceof DefaultListSelectionModel; } else if (key == ADD_TO_SELECTION && sender instanceof JTable) { // This action is typically bound to SPACE. // If the table is already in an editing mode, SPACE should // simply enter a space character into the table, and not // select a cell. Likewise, if the lead cell is already selected // then hitting SPACE should just enter a space character // into the cell and begin editing. In both of these cases // this action will be disabled. JTable table = (JTable)sender; int leadRow = getAdjustedLead(table, true); int leadCol = getAdjustedLead(table, false); return !(table.isEditing() || table.isCellSelected(leadRow, leadCol)); } else if (key == FOCUS_HEADER && sender instanceof JTable) { JTable table = (JTable)sender; return table.getTableHeader() != null; } return true; } }//// The Table's Key listener// /** * This inner class is marked "public" due to a compiler bug. * This class should be treated as a "protected" inner class. * Instantiate it only within subclasses of BasicTableUI. * <p>As of Java 2 platform v1.3 this class is no longer used. * Instead <code>JTable</code> * overrides <code>processKeyBinding</code> to dispatch the event to * the current <code>TableCellEditor</code>. */ public class KeyHandler implements KeyListener { // NOTE: This class exists only for backward compatability. All // its functionality has been moved into Handler. If you need to add // new functionality add it to the Handler, but make sure this // class calls into the Handler. public void keyPressed(KeyEvent e) { getHandler().keyPressed(e); } public void keyReleased(KeyEvent e) { getHandler().keyReleased(e); } public void keyTyped(KeyEvent e) { getHandler().keyTyped(e); } }//// The Table's focus listener// /** * This inner class is marked "public" due to a compiler bug. * This class should be treated as a "protected" inner class. * Instantiate it only within subclasses of BasicTableUI. */ public class FocusHandler implements FocusListener { // NOTE: This class exists only for backward compatability. All // its functionality has been moved into Handler. If you need to add // new functionality add it to the Handler, but make sure this // class calls into the Handler. public void focusGained(FocusEvent e) { getHandler().focusGained(e); } public void focusLost(FocusEvent e) { getHandler().focusLost(e); } }//// The Table's mouse and mouse motion listeners// /** * This inner class is marked "public" due to a compiler bug. * This class should be treated as a "protected" inner class. * Instantiate it only within subclasses of BasicTableUI. */ public class MouseInputHandler implements MouseInputListener { // NOTE: This class exists only for backward compatability. All // its functionality has been moved into Handler. If you need to add // new functionality add it to the Handler, but make sure this // class calls into the Handler. public void mouseClicked(MouseEvent e) { getHandler().mouseClicked(e); } public void mousePressed(MouseEvent e) { getHandler().mousePressed(e); } public void mouseReleased(MouseEvent e) { getHandler().mouseReleased(e); } public void mouseEntered(MouseEvent e) { getHandler().mouseEntered(e); } public void mouseExited(MouseEvent e) { getHandler().mouseExited(e); } public void mouseMoved(MouseEvent e) { getHandler().mouseMoved(e); } public void mouseDragged(MouseEvent e) { getHandler().mouseDragged(e); } } private class Handler implements FocusListener, MouseInputListener, PropertyChangeListener, ListSelectionListener, ActionListener, BeforeDrag { // FocusListener private void repaintLeadCell( ) { int lr = getAdjustedLead(table, true); int lc = getAdjustedLead(table, false); if (lr < 0 || lc < 0) { return; } Rectangle dirtyRect = table.getCellRect(lr, lc, false); table.repaint(dirtyRect); } public void focusGained(FocusEvent e) { repaintLeadCell(); } public void focusLost(FocusEvent e) { repaintLeadCell(); } // KeyListener public void keyPressed(KeyEvent e) { } public void keyReleased(KeyEvent e) { } public void keyTyped(KeyEvent e) { KeyStroke keyStroke = KeyStroke.getKeyStroke(e.getKeyChar(), e.getModifiers()); // We register all actions using ANCESTOR_OF_FOCUSED_COMPONENT // which means that we might perform the appropriate action // in the table and then forward it to the editor if the editor // had focus. Make sure this doesn't happen by checking our // InputMaps. InputMap map = table.getInputMap(JComponent.WHEN_FOCUSED); if (map != null && map.get(keyStroke) != null) { return; } map = table.getInputMap(JComponent. WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); if (map != null && map.get(keyStroke) != null) { return; } keyStroke = KeyStroke.getKeyStrokeForEvent(e); // The AWT seems to generate an unconsumed \r event when // ENTER (\n) is pressed. if (e.getKeyChar() == '\r') { return; } int leadRow = getAdjustedLead(table, true); int leadColumn = getAdjustedLead(table, false); if (leadRow != -1 && leadColumn != -1 && !table.isEditing()) { if (!table.editCellAt(leadRow, leadColumn)) { return; } } // Forwarding events this way seems to put the component // in a state where it believes it has focus. In reality // the table retains focus - though it is difficult for // a user to tell, since the caret is visible and flashing. // Calling table.requestFocus() here, to get the focus back to // the table, seems to have no effect. Component editorComp = table.getEditorComponent(); if (table.isEditing() && editorComp != null) { if (editorComp instanceof JComponent) { JComponent component = (JComponent)editorComp; map = component.getInputMap(JComponent.WHEN_FOCUSED); Object binding = (map != null) ? map.get(keyStroke) : null; if (binding == null) { map = component.getInputMap(JComponent. WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); binding = (map != null) ? map.get(keyStroke) : null; } if (binding != null) { ActionMap am = component.getActionMap(); Action action = (am != null) ? am.get(binding) : null; if (action != null && SwingUtilities. notifyAction(action, keyStroke, e, component, e.getModifiers())) { e.consume(); } } } } } // MouseInputListener // Component receiving mouse events during editing. // May not be editorComponent. private Component dispatchComponent; public void mouseClicked(MouseEvent e) {} private void setDispatchComponent(MouseEvent e) { Component editorComponent = table.getEditorComponent(); Point p = e.getPoint(); Point p2 = SwingUtilities.convertPoint(table, p, editorComponent); dispatchComponent = SwingUtilities.getDeepestComponentAt(editorComponent, p2.x, p2.y); SwingUtilities2.setSkipClickCount(dispatchComponent, e.getClickCount() - 1); } private boolean repostEvent(MouseEvent e) { // Check for isEditing() in case another event has // caused the editor to be removed. See bug #4306499. if (dispatchComponent == null || !table.isEditing()) { return false; } MouseEvent e2 = SwingUtilities.convertMouseEvent(table, e, dispatchComponent); dispatchComponent.dispatchEvent(e2); return true; } private void setValueIsAdjusting(boolean flag) { table.getSelectionModel().setValueIsAdjusting(flag); table.getColumnModel().getSelectionModel(). setValueIsAdjusting(flag); } // The row and column where the press occurred and the // press event itself private int pressedRow; private int pressedCol; private MouseEvent pressedEvent; // Whether or not the mouse press (which is being considered as part // of a drag sequence) also caused the selection change to be fully // processed. private boolean dragPressDidSelection; // Set to true when a drag gesture has been fully recognized and DnD // begins. Use this to ignore further mouse events which could be // delivered if DnD is cancelled (via ESCAPE for example) private boolean dragStarted; // Whether or not we should start the editing timer on release private boolean shouldStartTimer; // To cache the return value of pointOutsidePrefSize since we use // it multiple times. private boolean outsidePrefSize; // Used to delay the start of editing. private Timer timer = null; private boolean canStartDrag() { if (pressedRow == -1 || pressedCol == -1) { return false; } if (isFileList) { return !outsidePrefSize; } // if this is a single selection table if ((table.getSelectionModel().getSelectionMode() == ListSelectionModel.SINGLE_SELECTION) && (table.getColumnModel().getSelectionModel().getSelectionMode() == ListSelectionModel.SINGLE_SELECTION)) { return true; } return table.isCellSelected(pressedRow, pressedCol); } public void mousePressed(MouseEvent e) { if (SwingUtilities2.shouldIgnore(e, table)) { return; } if (table.isEditing() && !table.getCellEditor().stopCellEditing()) { Component editorComponent = table.getEditorComponent(); if (editorComponent != null && !editorComponent.hasFocus()) { SwingUtilities2.compositeRequestFocus(editorComponent); } return; } Point p = e.getPoint(); pressedRow = table.rowAtPoint(p); pressedCol = table.columnAtPoint(p); outsidePrefSize = pointOutsidePrefSize(pressedRow, pressedCol, p); if (isFileList) { shouldStartTimer = table.isCellSelected(pressedRow, pressedCol) && !e.isShiftDown() && !e.isControlDown() && !outsidePrefSize; } if (table.getDragEnabled()) { mousePressedDND(e); } else { SwingUtilities2.adjustFocus(table); if (!isFileList) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -