Home » openjdk-7 » javax » swing » plaf » basic » [javadoc | source]

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   /**
   18    * @author Dennis Ushakov
   19    */
   20   
   21   package javax.swing.plaf.basic;
   22   
   23   import java.awt.event.ActionEvent;
   24   
   25   import javax.swing.AbstractAction;
   26   import javax.swing.JComponent;
   27   import javax.swing.JFormattedTextField;
   28   import javax.swing.JSpinner;
   29   import javax.swing.JSpinner.DateEditor;
   30   
   31   import org.apache.harmony.awt.text.TextUtils;
   32   import org.apache.harmony.x.swing.Utilities;
   33   
   34   
   35   final class BasicSpinnerKeyboardActions {
   36       public static AbstractAction incrementAction = new AbstractAction() {
   37           public void actionPerformed(final ActionEvent e) {
   38               JSpinner spinner = (JSpinner)e.getSource();
   39               if (spinner.getEditor() instanceof DateEditor) {
   40                   DateEditor dateEditor = (DateEditor)spinner.getEditor();
   41                   JFormattedTextField textField = dateEditor.getTextField();
   42                   int calendarField = getCalendarField(textField);
   43                   dateEditor.getModel().setCalendarField(calendarField);
   44   
   45                   nextValue(spinner);
   46   
   47                   selectCalendarField(textField, calendarField);
   48               } else {
   49                   nextValue(spinner);
   50               }
   51   
   52           }
   53   
   54           private void nextValue(final JSpinner spinner) {
   55               Object nextValue = spinner.getNextValue();
   56               if (nextValue != null) {
   57                   spinner.setValue(nextValue);
   58               }
   59           }
   60       };
   61   
   62       public static AbstractAction decrementAction = new AbstractAction() {
   63           public void actionPerformed(final ActionEvent e) {
   64               JSpinner spinner = (JSpinner)e.getSource();
   65               if (spinner.getEditor() instanceof DateEditor) {
   66                   DateEditor dateEditor = (DateEditor)spinner.getEditor();
   67                   JFormattedTextField textField = dateEditor.getTextField();
   68                   int calendarField = getCalendarField(textField);
   69                   dateEditor.getModel().setCalendarField(calendarField);
   70   
   71                   previousValue(spinner);
   72   
   73                   selectCalendarField(textField, calendarField);
   74               } else {
   75                   previousValue(spinner);
   76               }
   77           }
   78   
   79           private void previousValue(final JSpinner spinner) {
   80               Object previousValue = spinner.getPreviousValue();
   81               if (previousValue != null) {
   82                   spinner.setValue(previousValue);
   83               }
   84           }
   85       };
   86   
   87       public static void installKeyboardActions(final JSpinner spinner) {
   88           Utilities.installKeyboardActions(spinner, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, "Spinner.ancestorInputMap", null);
   89   
   90           spinner.getActionMap().put("increment", incrementAction);
   91           spinner.getActionMap().put("decrement", decrementAction);
   92       }
   93   
   94       public static void uninstallKeyboardActions(final JSpinner spinner) {
   95           Utilities.uninstallKeyboardActions(spinner, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
   96       }
   97   
   98   
   99       private static int getCalendarField(final JFormattedTextField textField) {
  100           return TextUtils.getCalendarField(textField);
  101       }
  102   
  103       private static void selectCalendarField(final JFormattedTextField textField,
  104                                               final int calendarField) {
  105           TextUtils.selectCalendarField(textField, calendarField);
  106       }
  107   }

Home » openjdk-7 » javax » swing » plaf » basic » [javadoc | source]