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.InputMap;
   29   import javax.swing.JComponent;
   30   import javax.swing.JScrollBar;
   31   import javax.swing.JScrollPane;
   32   import javax.swing.KeyStroke;
   33   
   34   import org.apache.harmony.x.swing.Utilities;
   35   
   36   
   37   class BasicScrollPaneKeyboardActions {
   38       private abstract static class PassThroughAction extends AbstractAction {
   39           protected static void passThroughEvent(final ActionEvent event, final JComponent destinationComponent, final String correspondingCommand) {
   40               if (!destinationComponent.isVisible()) {
   41                   return;
   42               }
   43               Action action = destinationComponent.getActionMap().get(correspondingCommand);
   44               if (action != null) {
   45                   action.actionPerformed(new ActionEvent(destinationComponent, ActionEvent.ACTION_PERFORMED, correspondingCommand, event.getWhen(), event.getModifiers()));
   46               }
   47           }
   48       }
   49   
   50       private abstract static class PassThroughToVerticalScrollbarAction extends PassThroughAction {
   51           protected static void passThroughEvent(final ActionEvent event, final String correspondingCommand) {
   52               JScrollPane scrollPane = (JScrollPane)event.getSource();
   53               JScrollBar vsb = scrollPane.getVerticalScrollBar();
   54               passThroughEvent(event, vsb, correspondingCommand);
   55           }
   56       }
   57   
   58       private abstract static class PassThroughToHorizontalScrollbarAction extends PassThroughAction {
   59           protected static void passThroughEvent(final ActionEvent event, final String correspondingCommand) {
   60               JScrollPane scrollPane = (JScrollPane)event.getSource();
   61               JScrollBar hsb = scrollPane.getHorizontalScrollBar();
   62               passThroughEvent(event, hsb, correspondingCommand);
   63           }
   64       }
   65   
   66   
   67       private static PassThroughAction unitScrollRightAction = new PassThroughToHorizontalScrollbarAction() {
   68           public void actionPerformed(final ActionEvent e) {
   69               passThroughEvent(e, "positiveUnitIncrement");
   70           }
   71       };
   72   
   73       private static PassThroughAction unitScrollLeftAction = new PassThroughToHorizontalScrollbarAction() {
   74           public void actionPerformed(final ActionEvent e) {
   75               passThroughEvent(e, "negativeUnitIncrement");
   76           }
   77       };
   78   
   79       private static PassThroughAction unitScrollUpAction = new PassThroughToVerticalScrollbarAction() {
   80           public void actionPerformed(final ActionEvent e) {
   81               passThroughEvent(e, "negativeUnitIncrement");
   82           }
   83       };
   84   
   85       private static PassThroughAction unitScrollDownAction = new PassThroughToVerticalScrollbarAction() {
   86           public void actionPerformed(final ActionEvent e) {
   87               passThroughEvent(e, "positiveUnitIncrement");
   88           }
   89       };
   90   
   91       private static PassThroughAction scrollDownAction = new PassThroughToVerticalScrollbarAction() {
   92           public void actionPerformed(final ActionEvent e) {
   93               passThroughEvent(e, "positiveBlockIncrement");
   94           }
   95       };
   96   
   97       private static PassThroughAction scrollUpAction = new PassThroughToVerticalScrollbarAction() {
   98           public void actionPerformed(final ActionEvent e) {
   99               passThroughEvent(e, "negativeBlockIncrement");
  100           }
  101       };
  102   
  103       private static PassThroughAction scrollLeftAction = new PassThroughToHorizontalScrollbarAction() {
  104           public void actionPerformed(final ActionEvent e) {
  105               passThroughEvent(e, "negativeBlockIncrement");
  106           }
  107       };
  108   
  109       private static PassThroughAction scrollRightAction = new PassThroughToHorizontalScrollbarAction() {
  110           public void actionPerformed(final ActionEvent e) {
  111               passThroughEvent(e, "positiveBlockIncrement");
  112           }
  113       };
  114   
  115       private static PassThroughAction scrollHomeAction = new PassThroughAction() {
  116           public void actionPerformed(final ActionEvent e) {
  117               JScrollPane scrollPane = (JScrollPane)e.getSource();
  118   
  119               JScrollBar vsb = scrollPane.getVerticalScrollBar();
  120               passThroughEvent(e, vsb, "minScroll");
  121               JScrollBar hsb = scrollPane.getHorizontalScrollBar();
  122               passThroughEvent(e, hsb, "minScroll");
  123           }
  124       };
  125   
  126       private static PassThroughAction scrollEndAction = new PassThroughAction() {
  127           public void actionPerformed(final ActionEvent e) {
  128               JScrollPane scrollPane = (JScrollPane)e.getSource();
  129   
  130               JScrollBar vsb = scrollPane.getVerticalScrollBar();
  131               passThroughEvent(e, vsb, "maxScroll");
  132               JScrollBar hsb = scrollPane.getHorizontalScrollBar();
  133               passThroughEvent(e, hsb, "maxScroll");
  134           }
  135       };
  136   
  137   
  138   
  139       public static void installKeyboardActions(final JScrollPane scrollPane) {
  140           Utilities.installKeyboardActions(scrollPane, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, "ScrollPane.ancestorInputMap", "ScrollPane.ancestorInputMap.RightToLeft");
  141   
  142           scrollPane.getActionMap().put("unitScrollRight", unitScrollRightAction);
  143           scrollPane.getActionMap().put("unitScrollDown", unitScrollDownAction);
  144           scrollPane.getActionMap().put("unitScrollLeft", unitScrollLeftAction);
  145           scrollPane.getActionMap().put("unitScrollUp", unitScrollUpAction);
  146           scrollPane.getActionMap().put("scrollUp", scrollUpAction);
  147           scrollPane.getActionMap().put("scrollDown", scrollDownAction);
  148           scrollPane.getActionMap().put("scrollLeft", scrollLeftAction);
  149           scrollPane.getActionMap().put("scrollRight", scrollRightAction);
  150           scrollPane.getActionMap().put("scrollHome", scrollHomeAction);
  151           scrollPane.getActionMap().put("scrollEnd", scrollEndAction);
  152       }
  153   
  154       public static void uninstallKeyboardActions(final JScrollPane scrollPane) {
  155           Utilities.uninstallKeyboardActions(scrollPane, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
  156       }
  157   
  158   
  159       private static void appendInputMap(final InputMap source, final InputMap additional) {
  160           KeyStroke[] keyStrokes = additional.keys();
  161           for (int i = 0; i < keyStrokes.length; i++) {
  162               source.put(keyStrokes[i], additional.get(keyStrokes[i]));
  163           }
  164       }
  165   }

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