001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Dennis Ushakov
019: * @version $Revision$
020: */package javax.swing.plaf.basic;
021:
022: import java.awt.event.ActionEvent;
023:
024: import javax.swing.AbstractAction;
025: import javax.swing.JComponent;
026: import javax.swing.JFormattedTextField;
027: import javax.swing.JSpinner;
028: import javax.swing.JSpinner.DateEditor;
029:
030: import org.apache.harmony.awt.text.TextUtils;
031: import org.apache.harmony.x.swing.Utilities;
032:
033: final class BasicSpinnerKeyboardActions {
034: public static AbstractAction incrementAction = new AbstractAction() {
035: public void actionPerformed(final ActionEvent e) {
036: JSpinner spinner = (JSpinner) e.getSource();
037: if (spinner.getEditor() instanceof DateEditor) {
038: DateEditor dateEditor = (DateEditor) spinner
039: .getEditor();
040: JFormattedTextField textField = dateEditor
041: .getTextField();
042: int calendarField = getCalendarField(textField);
043: dateEditor.getModel().setCalendarField(calendarField);
044:
045: nextValue(spinner);
046:
047: selectCalendarField(textField, calendarField);
048: } else {
049: nextValue(spinner);
050: }
051:
052: }
053:
054: private void nextValue(final JSpinner spinner) {
055: Object nextValue = spinner.getNextValue();
056: if (nextValue != null) {
057: spinner.setValue(nextValue);
058: }
059: }
060: };
061:
062: public static AbstractAction decrementAction = new AbstractAction() {
063: public void actionPerformed(final ActionEvent e) {
064: JSpinner spinner = (JSpinner) e.getSource();
065: if (spinner.getEditor() instanceof DateEditor) {
066: DateEditor dateEditor = (DateEditor) spinner
067: .getEditor();
068: JFormattedTextField textField = dateEditor
069: .getTextField();
070: int calendarField = getCalendarField(textField);
071: dateEditor.getModel().setCalendarField(calendarField);
072:
073: previousValue(spinner);
074:
075: selectCalendarField(textField, calendarField);
076: } else {
077: previousValue(spinner);
078: }
079: }
080:
081: private void previousValue(final JSpinner spinner) {
082: Object previousValue = spinner.getPreviousValue();
083: if (previousValue != null) {
084: spinner.setValue(previousValue);
085: }
086: }
087: };
088:
089: public static void installKeyboardActions(final JSpinner spinner) {
090: Utilities.installKeyboardActions(spinner,
091: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
092: "Spinner.ancestorInputMap", null);
093:
094: spinner.getActionMap().put("increment", incrementAction);
095: spinner.getActionMap().put("decrement", decrementAction);
096: }
097:
098: public static void uninstallKeyboardActions(final JSpinner spinner) {
099: Utilities.uninstallKeyboardActions(spinner,
100: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
101: }
102:
103: private static int getCalendarField(
104: final JFormattedTextField textField) {
105: return TextUtils.getCalendarField(textField);
106: }
107:
108: private static void selectCalendarField(
109: final JFormattedTextField textField, final int calendarField) {
110: TextUtils.selectCalendarField(textField, calendarField);
111: }
112: }
|