synthtableui.java

来自「java jdk 1.4的源码」· Java 代码 · 共 1,411 行 · 第 1/4 页

JAVA
1,411
字号
	    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 anchorRow = table.getSelectionModel().getAnchorSelectionIndex();            int anchorColumn =		table.getColumnModel().getSelectionModel().getAnchorSelectionIndex();            if (anchorRow != -1 && anchorColumn != -1 && !table.isEditing()) {                if (!table.editCellAt(anchorRow, anchorColumn)) {                    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();			}		    }                }            }        }    }////  The Table's focus listener//    /**     * This inner class is marked &quot;public&quot; due to a compiler bug.     * This class should be treated as a &quot;protected&quot; inner class.     * Instantiate it only within subclasses of BasicTableUI.     */    class FocusHandler implements FocusListener {        private void repaintAnchorCell( ) {	    int rc = table.getRowCount();	    int cc = table.getColumnCount();            int ar = table.getSelectionModel().getAnchorSelectionIndex();            int ac = table.getColumnModel().getSelectionModel().getAnchorSelectionIndex();	    if (ar < 0 || ar >= rc || ac < 0 || ac >= cc) {		return;	    }            Rectangle dirtyRect = table.getCellRect(ar, ac, false);            table.repaint(dirtyRect);        }        public void focusGained(FocusEvent e) {            repaintAnchorCell();        }        public void focusLost(FocusEvent e) {            repaintAnchorCell();        }    }////  The Table's mouse and mouse motion listeners//    /**     * This inner class is marked &quot;public&quot; due to a compiler bug.     * This class should be treated as a &quot;protected&quot; inner class.     * Instantiate it only within subclasses of BasicTableUI.     */    class MouseInputHandler implements MouseInputListener {        // Component receiving mouse events during editing.        // May not be editorComponent.        private Component dispatchComponent;	private boolean selectedOnPress;//  The Table's mouse listener methods.        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);        }        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);        }	private boolean shouldIgnore(MouseEvent e) {	    return e.isConsumed() || (!(SwingUtilities.isLeftMouseButton(e) && table.isEnabled()));	}        public void mousePressed(MouseEvent e) {	    if (e.isConsumed()) {		selectedOnPress = false;		return;	    }	    selectedOnPress = true;	    adjustFocusAndSelection(e);	}	void adjustFocusAndSelection(MouseEvent e) {	    if (shouldIgnore(e)) {	        return;	    }            Point p = e.getPoint();            int row = table.rowAtPoint(p);            int column = table.columnAtPoint(p);	    // The autoscroller can generate drag events outside the Table's range.            if ((column == -1) || (row == -1)) {                return;            }            if (table.editCellAt(row, column, e)) {                setDispatchComponent(e);                repostEvent(e);            }	    else if (table.isRequestFocusEnabled()) {                table.requestFocus();	    }            CellEditor editor = table.getCellEditor();            if (editor == null || editor.shouldSelectCell(e)) {		boolean adjusting = (e.getID() == MouseEvent.MOUSE_PRESSED) ? true : false;                setValueIsAdjusting(adjusting);                table.changeSelection(row, column, e.isControlDown(), e.isShiftDown());	    }        }        public void mouseReleased(MouseEvent e) {	    if (selectedOnPress) {		if (shouldIgnore(e)) {		    return;		}		repostEvent(e);		dispatchComponent = null;		setValueIsAdjusting(false);	    } else {		adjustFocusAndSelection(e);	    }        }        public void mouseEntered(MouseEvent e) {}        public void mouseExited(MouseEvent e) {}//  The Table's mouse motion listener methods.        public void mouseMoved(MouseEvent e) {}        public void mouseDragged(MouseEvent e) {	    if (shouldIgnore(e)) {	        return;	    }            repostEvent(e);            CellEditor editor = table.getCellEditor();            if (editor == null || editor.shouldSelectCell(e)) {                Point p = e.getPoint();                int row = table.rowAtPoint(p);                int column = table.columnAtPoint(p);	        // The autoscroller can generate drag events outside the Table's range.                if ((column == -1) || (row == -1)) {                    return;                }	        table.changeSelection(row, column, false, true);            }        }    }////  Factory methods for the Listeners//    /**     * Creates the key listener for handling keyboard navigation in the JTable.     */    protected KeyListener createKeyListener() {	return null;    }    /**     * Creates the focus listener for handling keyboard navigation in the JTable.     */    protected FocusListener createFocusListener() {        return new FocusHandler();    }    /**     * Creates the mouse listener for the JTable.     */    protected MouseInputListener createMouseInputListener() {        return new MouseInputHandler();    }    /**     * Creates the property change listener for the JTable.     */    private PropertyChangeListener createPropertyChangeListener() {        return new PropertyChangeHandler();    }    public static void loadActionMap(ActionMap map) {        // NOTE: this needs to remain static. If you have a need to        // have Actions that reference the UI in the ActionMap,        // then you'll also need to change the registeration of the        // ActionMap.	map.put("selectNextColumn", new NavigationalAction		(1, 0, false, false, false));	map.put("selectPreviousColumn", new NavigationalAction		(-1, 0, false, false, false));	map.put("selectNextRow", new NavigationalAction		(0, 1, false, false, false));	map.put("selectPreviousRow", new NavigationalAction		(0, -1, false, false, false));	map.put("selectNextColumnExtendSelection", new NavigationalAction		(1, 0, false, true, false));	map.put("selectPreviousColumnExtendSelection", new NavigationalAction		(-1, 0, false, true, false));	map.put("selectNextRowExtendSelection", new NavigationalAction		(0, 1, false, true, false));	map.put("selectPreviousRowExtendSelection", new NavigationalAction		(0, -1, false, true, false));	map.put("scrollUpChangeSelection",		new PagingAction(false, false, true, false));	map.put("scrollDownChangeSelection",		new PagingAction(false, true, true, false));	map.put("selectFirstColumn",		new PagingAction(false, false, false, true));	map.put("selectLastColumn",		new PagingAction(false, true, false, true));	map.put("scrollUpExtendSelection",		new PagingAction(true, false, true, false));	map.put("scrollDownExtendSelection",		new PagingAction(true, true, true, false));	map.put("selectFirstColumnExtendSelection",		new PagingAction(true, false, false, true));	map.put("selectLastColumnExtendSelection",		new PagingAction(true, true, false, true));	map.put("selectFirstRow",		new PagingAction(false, false, true, true));	map.put("selectLastRow",		new PagingAction(false, true, true, true));	map.put("selectFirstRowExtendSelection",		new PagingAction(true, false, true, true));	map.put("selectLastRowExtendSelection",		new PagingAction(true, true, true, true));	map.put("selectNextColumnCell",		new NavigationalAction(1, 0, true, false, true));	map.put("selectPreviousColumnCell",		new NavigationalAction(-1, 0, true, false, true));	map.put("selectNextRowCell",		new NavigationalAction(0, 1, true, false, true));	map.put("selectPreviousRowCell",		new NavigationalAction(0, -1, true, false, true));	map.put("selectAll", new SelectAllAction());	map.put("cancel", new CancelEditingAction());	map.put("startEditing", new StartEditingAction());        map.put(TransferHandler.getCutAction().getValue(Action.NAME),                TransferHandler.getCutAction());        map.put(TransferHandler.getCopyAction().getValue(Action.NAME),                TransferHandler.getCopyAction());        map.put(TransferHandler.getPasteAction().getValue(Action.NAME),                TransferHandler.getPasteAction());        // PENDING: this is currently broke, the actions should do        // the necessary mapping and not require us to reregister the actions.        // In order to reregister actions we would have to make this        // a per component action map.        map.put("scrollLeftChangeSelection",		new PagingAction(false, false, false, false));        map.put("scrollRightChangeSelection",		new PagingAction(false, true, false, false));        map.put("scrollLeftExtendSelection",		new PagingAction(true, false, false, false));        map.put("scrollRightExtendSelection",		new PagingAction(true, true, false, false));    }////  The installation/uninstall procedures and support//    public static ComponentUI createUI(JComponent c) {        return new SynthTableUI();    }//  Installation    public void installUI(JComponent c) {        table = (JTable)c;        rendererPane = new CellRendererPane();        table.add(rendererPane);        installDefaults();        installListeners();        installKeyboardActions();

⌨️ 快捷键说明

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