📄 basicspinnerui.java
字号:
private void installKeyboardActions() { InputMap iMap = getInputMap(JComponent. WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); SwingUtilities.replaceUIInputMap(spinner, JComponent. WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, iMap); SwingUtilities.replaceUIActionMap(spinner, getActionMap()); } /** * Returns the InputMap to install for <code>condition</code>. */ private InputMap getInputMap(int condition) { if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) { return (InputMap)UIManager.get("Spinner.ancestorInputMap"); } return null; } private ActionMap getActionMap() { ActionMap map = (ActionMap)UIManager.get("Spinner.actionMap"); if (map == null) { map = createActionMap(); if (map != null) { UIManager.getLookAndFeelDefaults().put("Spinner.actionMap", map); } } return map; } private ActionMap createActionMap() { ActionMap map = new ActionMapUIResource(); map.put("increment", nextButtonHandler); map.put("decrement", previousButtonHandler); return map; } /** * A handler for spinner arrow button mouse and action events. When * a left mouse pressed event occurs we look up the (enabled) spinner * that's the source of the event and start the autorepeat timer. The * timer fires action events until any button is released at which * point the timer is stopped and the reference to the spinner cleared. * The timer doesn't start until after a 300ms delay, so often the * source of the initial (and final) action event is just the button * logic for mouse released - which means that we're relying on the fact * that our mouse listener runs after the buttons mouse listener. * <p> * Note that one instance of this handler is shared by all slider previous * arrow buttons and likewise for all of the next buttons, * so it doesn't have any state that persists beyond the limits * of a single button pressed/released gesture. */ private static class ArrowButtonHandler extends AbstractAction implements MouseListener, UIResource { final javax.swing.Timer autoRepeatTimer; final boolean isNext; JSpinner spinner = null; ArrowButtonHandler(String name, boolean isNext) { super(name); this.isNext = isNext; autoRepeatTimer = new javax.swing.Timer(60, this); autoRepeatTimer.setInitialDelay(300); } private JSpinner eventToSpinner(AWTEvent e) { Object src = e.getSource(); while ((src instanceof Component) && !(src instanceof JSpinner)) { src = ((Component)src).getParent(); } return (src instanceof JSpinner) ? (JSpinner)src : null; } public void actionPerformed(ActionEvent e) { JSpinner spinner = this.spinner; if (!(e.getSource() instanceof javax.swing.Timer)) { // Most likely resulting from being in ActionMap. spinner = eventToSpinner(e); } if (spinner != null) { try { int calendarField = getCalendarField(spinner); spinner.commitEdit(); if (calendarField != -1) { ((SpinnerDateModel)spinner.getModel()). setCalendarField(calendarField); } Object value = (isNext) ? spinner.getNextValue() : spinner.getPreviousValue(); if (value != null) { spinner.setValue(value); select(spinner); } } catch (IllegalArgumentException iae) { UIManager.getLookAndFeel().provideErrorFeedback(spinner); } catch (ParseException pe) { UIManager.getLookAndFeel().provideErrorFeedback(spinner); } } } /** * If the spinner's editor is a DateEditor, this selects the field * associated with the value that is being incremented. */ private void select(JSpinner spinner) { JComponent editor = spinner.getEditor(); if (editor instanceof JSpinner.DateEditor) { JSpinner.DateEditor dateEditor = (JSpinner.DateEditor)editor; JFormattedTextField ftf = dateEditor.getTextField(); Format format = dateEditor.getFormat(); Object value; if (format != null && (value = spinner.getValue()) != null) { SpinnerDateModel model = dateEditor.getModel(); DateFormat.Field field = DateFormat.Field.ofCalendarField( model.getCalendarField()); if (field != null) { try { AttributedCharacterIterator iterator = format. formatToCharacterIterator(value); if (!select(ftf, iterator, field) && field == DateFormat.Field.HOUR0) { select(ftf, iterator, DateFormat.Field.HOUR1); } } catch (IllegalArgumentException iae) {} } } } } /** * Selects the passed in field, returning true if it is found, * false otherwise. */ private boolean select(JFormattedTextField ftf, AttributedCharacterIterator iterator, DateFormat.Field field) { int max = ftf.getDocument().getLength(); iterator.first(); do { Map attrs = iterator.getAttributes(); if (attrs != null && attrs.containsKey(field)){ int start = iterator.getRunStart(field); int end = iterator.getRunLimit(field); if (start != -1 && end != -1 && start <= max && end <= max) { ftf.select(start, end); } return true; } } while (iterator.next() != CharacterIterator.DONE); return false; } /** * Returns the calendarField under the start of the selection, or * -1 if there is no valid calendar field under the selection (or * the spinner isn't editing dates. */ private int getCalendarField(JSpinner spinner) { JComponent editor = spinner.getEditor(); if (editor instanceof JSpinner.DateEditor) { JSpinner.DateEditor dateEditor = (JSpinner.DateEditor)editor; JFormattedTextField ftf = dateEditor.getTextField(); int start = ftf.getSelectionStart(); JFormattedTextField.AbstractFormatter formatter = ftf.getFormatter(); if (formatter instanceof InternationalFormatter) { Format.Field[] fields = ((InternationalFormatter) formatter).getFields(start); for (int counter = 0; counter < fields.length; counter++) { if (fields[counter] instanceof DateFormat.Field) { int calendarField; if (fields[counter] == DateFormat.Field.HOUR1) { calendarField = Calendar.HOUR; } else { calendarField = ((DateFormat.Field) fields[counter]).getCalendarField(); } if (calendarField != -1) { return calendarField; } } } } } return -1; } public void mousePressed(MouseEvent e) { if (SwingUtilities.isLeftMouseButton(e) && e.getComponent().isEnabled()) { spinner = eventToSpinner(e); autoRepeatTimer.start(); focusSpinnerIfNecessary(); } } public void mouseReleased(MouseEvent e) { autoRepeatTimer.stop(); spinner = null; } public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } /** * Requests focus on a child of the spinner if the spinner doesn't * have focus. */ private void focusSpinnerIfNecessary() { Component fo = KeyboardFocusManager. getCurrentKeyboardFocusManager().getFocusOwner(); if (spinner.isRequestFocusEnabled() && ( fo == null || !SwingUtilities.isDescendingFrom(fo, spinner))) { Container root = spinner; if (!root.isFocusCycleRoot()) { root = root.getFocusCycleRootAncestor(); } if (root != null) { FocusTraversalPolicy ftp = root.getFocusTraversalPolicy(); Component child = ftp.getComponentAfter(root, spinner); if (child != null && SwingUtilities.isDescendingFrom( child, spinner)) { child.requestFocus(); } } } } } /** * A simple layout manager for the editor and the next/previous buttons. * See the BasicSpinnerUI javadoc for more information about exactly * how the components are arranged. */ private static class SpinnerLayout implements LayoutManager { private Component nextButton = null; private Component previousButton = null; private Component editor = null; public void addLayoutComponent(String name, Component c) { if ("Next".equals(name)) { nextButton = c; } else if ("Previous".equals(name)) { previousButton = c; } else if ("Editor".equals(name)) { editor = c; } } public void removeLayoutComponent(Component c) { if (c == nextButton) { c = null; } else if (c == previousButton) { previousButton = null; } else if (c == editor) { editor = null; } } private Dimension preferredSize(Component c) { return (c == null) ? zeroSize : c.getPreferredSize(); } public Dimension preferredLayoutSize(Container parent) { Dimension nextD = preferredSize(nextButton); Dimension previousD = preferredSize(previousButton); Dimension editorD = preferredSize(editor); /* Force the editors height to be a multiple of 2 */ editorD.height = ((editorD.height + 1) / 2) * 2; Dimension size = new Dimension(editorD.width, editorD.height); size.width += Math.max(nextD.width, previousD.width); Insets insets = parent.getInsets(); size.width += insets.left + insets.right; size.height += insets.top + insets.bottom; return size; } public Dimension minimumLayoutSize(Container parent) { return preferredLayoutSize(parent); } private void setBounds(Component c, int x, int y, int width, int height) { if (c != null) { c.setBounds(x, y, width, height); } } public void layoutContainer(Container parent) { int width = parent.getWidth(); int height = parent.getHeight(); Insets insets = parent.getInsets(); Dimension nextD = preferredSize(nextButton); Dimension previousD = preferredSize(previousButton); int buttonsWidth = Math.max(nextD.width, previousD.width); int editorHeight = height - (insets.top + insets.bottom); // The arrowButtonInsets value is used instead of the JSpinner's // insets if not null. Defining this to be (0, 0, 0, 0) causes the // buttons to be aligned with the outer edge of the spinner's // border, and leaving it as "null" places the buttons completely // inside the spinner's border. Insets buttonInsets = UIManager.getInsets("Spinner.arrowButtonInsets"); if (buttonInsets == null) { buttonInsets = insets; } /* Deal with the spinner's componentOrientation property. */ int editorX, editorWidth, buttonsX; if (parent.getComponentOrientation().isLeftToRight()) { editorX = insets.left; editorWidth = width - insets.left - buttonsWidth - buttonInsets.right; buttonsX = width - buttonsWidth - buttonInsets.right; } else { buttonsX = buttonInsets.left; editorX = buttonsX + buttonsWidth; editorWidth = width - buttonInsets.left - buttonsWidth - insets.right; } int nextY = buttonInsets.top; int nextHeight = (height / 2) + (height % 2) - nextY; int previousY = buttonInsets.top + nextHeight; int previousHeight = height - previousY - buttonInsets.bottom; setBounds(editor, editorX, insets.top, editorWidth, editorHeight); setBounds(nextButton, buttonsX, nextY, buttonsWidth, nextHeight); setBounds(previousButton, buttonsX, previousY, buttonsWidth, previousHeight); } } /** * Detect JSpinner property changes we're interested in and delegate. Subclasses * shouldn't need to replace the default propertyChangeListener (although they * can by overriding createPropertyChangeListener) since all of the interesting * property changes are delegated to protected methods. */ private static class PropertyChangeHandler implements PropertyChangeListener { public void propertyChange(PropertyChangeEvent e) { String propertyName = e.getPropertyName(); if (e.getSource() instanceof JSpinner) { JSpinner spinner = (JSpinner)(e.getSource()); SpinnerUI spinnerUI = spinner.getUI(); if (spinnerUI instanceof BasicSpinnerUI) { BasicSpinnerUI ui = (BasicSpinnerUI)spinnerUI; if ("editor".equals(propertyName)) { JComponent oldEditor = (JComponent)e.getOldValue(); JComponent newEditor = (JComponent)e.getNewValue(); ui.replaceEditor(oldEditor, newEditor); ui.updateEnabledState(); } else if ("enabled".equals(propertyName)) { ui.updateEnabledState(); } } } else if (e.getSource() instanceof JComponent) { JComponent c = (JComponent)e.getSource(); if ((c.getParent() instanceof JPanel) && (c.getParent().getParent() instanceof JSpinner) && "border".equals(propertyName)) { JSpinner spinner = (JSpinner)c.getParent().getParent(); SpinnerUI spinnerUI = spinner.getUI(); if (spinnerUI instanceof BasicSpinnerUI) { BasicSpinnerUI ui = (BasicSpinnerUI)spinnerUI; ui.maybeRemoveEditorBorder(c); } } } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -