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   /**
   19    * @author Anton Avtamonov
   20    */
   21   
   22   package javax.swing.plaf.basic;
   23   
   24   import java.awt.event.ActionEvent;
   25   
   26   import javax.swing.AbstractAction;
   27   import javax.swing.Action;
   28   import javax.swing.JComboBox;
   29   import javax.swing.JComponent;
   30   import javax.swing.JList;
   31   
   32   import org.apache.harmony.x.swing.Utilities;
   33   
   34   
   35   class BasicComboBoxKeyboardActions {
   36       private abstract static class PassThroughAction extends AbstractAction {
   37           protected static void passThroughEvent(final ActionEvent event, final String correspondingCommand) {
   38               JComboBox comboBox = (JComboBox)event.getSource();
   39               JList popupList = ((BasicComboBoxUI)comboBox.getUI()).popup.getList();
   40               Action action = popupList.getActionMap().get(correspondingCommand);
   41               if (action != null) {
   42                   action.actionPerformed(new ActionEvent(popupList, ActionEvent.ACTION_PERFORMED, correspondingCommand, event.getWhen(), event.getModifiers()));
   43                   comboBox.setSelectedIndex(popupList.getSelectedIndex());
   44               }
   45           }
   46       }
   47       
   48       private abstract static class SelectNextPreviousAction extends AbstractAction {
   49           private String command;
   50           
   51           private SelectNextPreviousAction(final String command) {
   52               this.command = command;
   53           }
   54           
   55           abstract void selectValue(final BasicComboBoxUI ui);
   56           
   57           public void actionPerformed(ActionEvent e) {
   58               JComboBox comboBox = (JComboBox)e.getSource();
   59               if (!comboBox.isPopupVisible()) {
   60                   comboBox.showPopup();
   61               } else {
   62                   BasicComboBoxUI ui = ((BasicComboBoxUI)comboBox.getUI());
   63                   JList popupList = ui.popup.getList();
   64                   Action action = popupList.getActionMap().get(command);
   65                   if (action != null) {
   66                       action.actionPerformed(new ActionEvent(popupList, ActionEvent.ACTION_PERFORMED, command, 
   67                                              e.getWhen(), e.getModifiers()));
   68                       selectValue(ui);
   69                   }
   70               }
   71           }
   72       }
   73   
   74       private static AbstractAction selectPreviousAction = new SelectNextPreviousAction("selectPreviousRow") {
   75           void selectValue(final BasicComboBoxUI ui) {
   76               ui.selectPreviousPossibleValue();
   77           }
   78       };
   79       private static AbstractAction selectNextAction = new SelectNextPreviousAction("selectNextRow") {
   80           void selectValue(final BasicComboBoxUI ui) {
   81               ui.selectNextPossibleValue();
   82           }
   83       };
   84   
   85       private static AbstractAction togglePopupAction = new AbstractAction() {
   86           public void actionPerformed(final ActionEvent e) {
   87               JComboBox comboBox = (JComboBox)e.getSource();
   88               comboBox.setPopupVisible(!comboBox.isPopupVisible());
   89           }
   90       };
   91   
   92       private static AbstractAction pageUpPassThroughAction = new PassThroughAction() {
   93           public void actionPerformed(final ActionEvent e) {
   94               passThroughEvent(e, "scrollUp");
   95           }
   96       };
   97   
   98       private static AbstractAction pageDownPassThroughAction = new PassThroughAction() {
   99           public void actionPerformed(final ActionEvent e) {
  100               passThroughEvent(e, "scrollDown");
  101           }
  102       };
  103   
  104       private static AbstractAction homePassThroughAction = new PassThroughAction() {
  105           public void actionPerformed(final ActionEvent e) {
  106               passThroughEvent(e, "selectFirstRow");
  107           }
  108       };
  109   
  110       private static AbstractAction endPassThroughAction = new PassThroughAction() {
  111           public void actionPerformed(final ActionEvent e) {
  112               passThroughEvent(e, "selectLastRow");
  113           }
  114       };
  115   
  116   
  117       private static class HidePopupAction extends AbstractAction {
  118           protected final JComboBox comboBox;
  119   
  120           public HidePopupAction(final JComboBox comboBox) {
  121               this.comboBox = comboBox;
  122           }
  123   
  124           public void actionPerformed(final ActionEvent e) {
  125               comboBox.hidePopup();
  126               setValue();
  127           }
  128   
  129           public boolean isEnabled() {
  130               return comboBox.isPopupVisible();
  131           }
  132   
  133           protected void setValue() {
  134           }
  135       }
  136   
  137       private static class HidePopupAndSetValueAction extends HidePopupAction {
  138           public HidePopupAndSetValueAction(final JComboBox comboBox) {
  139               super(comboBox);
  140           }
  141   
  142           public boolean isEnabled() {
  143               return true;
  144           }
  145   
  146           protected void setValue() {
  147               BasicComboBoxUI ui = ((BasicComboBoxUI)comboBox.getUI());
  148               if (ui.isTableEditor) {
  149                   comboBox.setSelectedIndex(ui.popup.getList().getSelectedIndex());
  150               }
  151           }
  152       }
  153   
  154       public static void installKeyboardActions(final JComboBox comboBox) {
  155           Utilities.installKeyboardActions(comboBox, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, "ComboBox.ancestorInputMap", null);
  156   
  157           comboBox.getActionMap().put("hidePopup", new HidePopupAction(comboBox));
  158           comboBox.getActionMap().put("enterPressed", new HidePopupAndSetValueAction(comboBox));
  159           comboBox.getActionMap().put("selectNext", selectNextAction);
  160           comboBox.getActionMap().put("selectPrevious", selectPreviousAction);
  161           comboBox.getActionMap().put("togglePopup", togglePopupAction);
  162           comboBox.getActionMap().put("spacePopup", new AbstractAction() {
  163               public void actionPerformed(final ActionEvent e) {
  164                   comboBox.setPopupVisible(!comboBox.isPopupVisible());
  165               }
  166   
  167               public boolean isEnabled() {
  168                   return !comboBox.isEditable();
  169               }
  170           });
  171           comboBox.getActionMap().put("pageUpPassThrough", pageUpPassThroughAction);
  172           comboBox.getActionMap().put("pageDownPassThrough", pageDownPassThroughAction);
  173           comboBox.getActionMap().put("homePassThrough", homePassThroughAction);
  174           comboBox.getActionMap().put("endPassThrough", endPassThroughAction);
  175       }
  176   
  177       public static void uninstallKeyboardActions(final JComboBox comboBox) {
  178           Utilities.uninstallKeyboardActions(comboBox, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
  179       }
  180   }

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