Exemple #1
0
  /*
   * Fix for BugTraq ID 4041703.
   * Set the focus to a reasonable default for an Applet.
   */
  private void setDefaultFocus() {
    Component toFocus = null;
    Container parent = getParent();

    if (parent != null) {
      if (parent instanceof Window) {
        toFocus = getMostRecentFocusOwnerForWindow((Window) parent);
        if (toFocus == parent || toFocus == null) {
          toFocus = parent.getFocusTraversalPolicy().getInitialComponent((Window) parent);
        }
      } else if (parent.isFocusCycleRoot()) {
        toFocus = parent.getFocusTraversalPolicy().getDefaultComponent(parent);
      }
    }

    if (toFocus != null) {
      if (parent instanceof EmbeddedFrame) {
        ((EmbeddedFrame) parent).synthesizeWindowActivation(true);
      }
      // EmbeddedFrame might have focus before the applet was added.
      // Thus after its activation the most recent focus owner will be
      // restored. We need the applet's initial focusabled component to
      // be focused here.
      toFocus.requestFocusInWindow();
    }
  }
    public void actionPerformed(ActionEvent e) {
      int selIndexBefore = getSelectedIndex();
      myDefaultAction.actionPerformed(e);
      int selIndexCurrent = getSelectedIndex();
      if (selIndexBefore != selIndexCurrent) {
        return;
      }
      if (myFocusNext && selIndexCurrent == 0) {
        return;
      }

      KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
      Container container = kfm.getCurrentFocusCycleRoot();
      FocusTraversalPolicy policy = container.getFocusTraversalPolicy();
      if (policy == null) {
        policy = kfm.getDefaultFocusTraversalPolicy();
      }
      Component next =
          myFocusNext
              ? policy.getComponentAfter(container, PaletteItemsComponent.this)
              : policy.getComponentBefore(container, PaletteItemsComponent.this);
      if (next instanceof PaletteGroupComponent) {
        clearSelection();
        next.requestFocus();
        ((PaletteGroupComponent) next).scrollRectToVisible(next.getBounds());
      }
    }
 public void actionPerformed(ActionEvent e) {
   KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
   Container container = kfm.getCurrentFocusCycleRoot();
   FocusTraversalPolicy policy = container.getFocusTraversalPolicy();
   if (null == policy) policy = kfm.getDefaultFocusTraversalPolicy();
   Component next =
       moveDown
           ? policy.getComponentAfter(container, PaletteGroupHeader.this)
           : policy.getComponentBefore(container, PaletteGroupHeader.this);
   if (null != next && next instanceof PaletteComponentList) {
     final PaletteComponentList list = (PaletteComponentList) next;
     if (list.getModel().getSize() != 0) {
       list.takeFocusFrom(PaletteGroupHeader.this, list == myComponentList ? 0 : -1);
       return;
     } else {
       next =
           moveDown
               ? policy.getComponentAfter(container, next)
               : policy.getComponentBefore(container, next);
     }
   }
   if (null != next && next instanceof PaletteGroupHeader) {
     next.requestFocus();
   }
 }
  // By default the GridBagLayout stuffs up tab ordering for the
  // borrow book and return book panes, so I need this to ensure it's all
  // the right way round.
  private void initFocusTraversalPolicy() {
    Container nearestRoot = (isFocusCycleRoot()) ? this : getFocusCycleRootAncestor();
    final FocusTraversalPolicy defaultPolicy = nearestRoot.getFocusTraversalPolicy();

    MapFocusTraversalPolicy mine = new MapFocusTraversalPolicy(defaultPolicy, tabbedPane);
    mine.putAfter(retISBN, retCustID);
    mine.putAfter(retCustID, returnButton);
    mine.putAfter(returnButton, tabbedPane);
    mine.putAfter(borISBN, borCustID);
    mine.putAfter(borCustID, borDay);
    mine.putAfter(borDay, borMonth);
    mine.putAfter(borMonth, borYear);
    mine.putAfter(borYear, borrowButton);
    mine.putAfter(borrowButton, tabbedPane);

    mine.putBefore(retCustID, retISBN);
    mine.putBefore(returnButton, retCustID);
    mine.putBefore(borCustID, borISBN);
    mine.putBefore(borDay, borCustID);
    mine.putBefore(borMonth, borDay);
    mine.putBefore(borYear, borMonth);
    mine.putBefore(borrowButton, borYear);

    mine.putTabBefore("Borrow Book", borrowButton);
    mine.putTabBefore("Return Book", returnButton);

    nearestRoot.setFocusTraversalPolicy(mine);
  }
Exemple #5
0
  public Component findNextFocus() {
    // Find focus owner
    Component c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
    Container root = c == null ? null : c.getFocusCycleRootAncestor();

    if (root != null) {
      FocusTraversalPolicy policy = root.getFocusTraversalPolicy();
      Component nextFocus = policy.getComponentAfter(root, c);
      if (nextFocus == null) {
        nextFocus = policy.getDefaultComponent(root);
      }
      return nextFocus;
    }
    return null;
  }
Exemple #6
0
 /**
  * Traverse to the next forward or backward component using the container's FocusTraversalPolicy.
  *
  * @param comp the assumed current focuse component
  * @param forward if true, returns the next focus component, otherwise the previous one.
  * @return
  */
 public static Component getNextFocus(Component comp, boolean forward) {
   Container focusContainer = comp.getFocusCycleRootAncestor();
   while (focusContainer != null
       && (!focusContainer.isShowing()
           || !focusContainer.isFocusable()
           || !focusContainer.isEnabled())) {
     comp = focusContainer;
     focusContainer = comp.getFocusCycleRootAncestor();
   }
   Component next = null;
   if (focusContainer != null) {
     final FocusTraversalPolicy policy = focusContainer.getFocusTraversalPolicy();
     next =
         forward
             ? policy.getComponentAfter(focusContainer, comp)
             : policy.getComponentBefore(focusContainer, comp);
     if (next == null) {
       next = policy.getDefaultComponent(focusContainer);
     }
   }
   return next;
 }
    public void actionPerformed(ActionEvent e) {
      JDesktopPane dp = (JDesktopPane) e.getSource();
      String key = getName();

      if (CLOSE == key || MAXIMIZE == key || MINIMIZE == key || RESTORE == key) {
        setState(dp, key);
      } else if (ESCAPE == key) {
        if (sourceFrame == dp.getSelectedFrame() && focusOwner != null) {
          focusOwner.requestFocus();
        }
        moving = false;
        resizing = false;
        sourceFrame = null;
        focusOwner = null;
      } else if (MOVE == key || RESIZE == key) {
        sourceFrame = dp.getSelectedFrame();
        if (sourceFrame == null) {
          return;
        }
        moving = (key == MOVE) ? true : false;
        resizing = (key == RESIZE) ? true : false;

        focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
        if (!SwingUtilities.isDescendingFrom(focusOwner, sourceFrame)) {
          focusOwner = null;
        }
        sourceFrame.requestFocus();
      } else if (LEFT == key
          || RIGHT == key
          || UP == key
          || DOWN == key
          || SHRINK_RIGHT == key
          || SHRINK_LEFT == key
          || SHRINK_UP == key
          || SHRINK_DOWN == key) {
        JInternalFrame c = dp.getSelectedFrame();
        if (sourceFrame == null
            || c != sourceFrame
            || KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()
                != sourceFrame) {
          return;
        }
        Insets minOnScreenInsets = UIManager.getInsets("Desktop.minOnScreenInsets");
        Dimension size = c.getSize();
        Dimension minSize = c.getMinimumSize();
        int dpWidth = dp.getWidth();
        int dpHeight = dp.getHeight();
        int delta;
        Point loc = c.getLocation();
        if (LEFT == key) {
          if (moving) {
            c.setLocation(
                loc.x + size.width - MOVE_RESIZE_INCREMENT < minOnScreenInsets.right
                    ? -size.width + minOnScreenInsets.right
                    : loc.x - MOVE_RESIZE_INCREMENT,
                loc.y);
          } else if (resizing) {
            c.setLocation(loc.x - MOVE_RESIZE_INCREMENT, loc.y);
            c.setSize(size.width + MOVE_RESIZE_INCREMENT, size.height);
          }
        } else if (RIGHT == key) {
          if (moving) {
            c.setLocation(
                loc.x + MOVE_RESIZE_INCREMENT > dpWidth - minOnScreenInsets.left
                    ? dpWidth - minOnScreenInsets.left
                    : loc.x + MOVE_RESIZE_INCREMENT,
                loc.y);
          } else if (resizing) {
            c.setSize(size.width + MOVE_RESIZE_INCREMENT, size.height);
          }
        } else if (UP == key) {
          if (moving) {
            c.setLocation(
                loc.x,
                loc.y + size.height - MOVE_RESIZE_INCREMENT < minOnScreenInsets.bottom
                    ? -size.height + minOnScreenInsets.bottom
                    : loc.y - MOVE_RESIZE_INCREMENT);
          } else if (resizing) {
            c.setLocation(loc.x, loc.y - MOVE_RESIZE_INCREMENT);
            c.setSize(size.width, size.height + MOVE_RESIZE_INCREMENT);
          }
        } else if (DOWN == key) {
          if (moving) {
            c.setLocation(
                loc.x,
                loc.y + MOVE_RESIZE_INCREMENT > dpHeight - minOnScreenInsets.top
                    ? dpHeight - minOnScreenInsets.top
                    : loc.y + MOVE_RESIZE_INCREMENT);
          } else if (resizing) {
            c.setSize(size.width, size.height + MOVE_RESIZE_INCREMENT);
          }
        } else if (SHRINK_LEFT == key && resizing) {
          // Make sure we don't resize less than minimum size.
          if (minSize.width < (size.width - MOVE_RESIZE_INCREMENT)) {
            delta = MOVE_RESIZE_INCREMENT;
          } else {
            delta = size.width - minSize.width;
          }

          // Ensure that we keep the internal frame on the desktop.
          if (loc.x + size.width - delta < minOnScreenInsets.left) {
            delta = loc.x + size.width - minOnScreenInsets.left;
          }
          c.setSize(size.width - delta, size.height);
        } else if (SHRINK_RIGHT == key && resizing) {
          // Make sure we don't resize less than minimum size.
          if (minSize.width < (size.width - MOVE_RESIZE_INCREMENT)) {
            delta = MOVE_RESIZE_INCREMENT;
          } else {
            delta = size.width - minSize.width;
          }

          // Ensure that we keep the internal frame on the desktop.
          if (loc.x + delta > dpWidth - minOnScreenInsets.right) {
            delta = (dpWidth - minOnScreenInsets.right) - loc.x;
          }

          c.setLocation(loc.x + delta, loc.y);
          c.setSize(size.width - delta, size.height);
        } else if (SHRINK_UP == key && resizing) {
          // Make sure we don't resize less than minimum size.
          if (minSize.height < (size.height - MOVE_RESIZE_INCREMENT)) {
            delta = MOVE_RESIZE_INCREMENT;
          } else {
            delta = size.height - minSize.height;
          }

          // Ensure that we keep the internal frame on the desktop.
          if (loc.y + size.height - delta < minOnScreenInsets.bottom) {
            delta = loc.y + size.height - minOnScreenInsets.bottom;
          }

          c.setSize(size.width, size.height - delta);
        } else if (SHRINK_DOWN == key && resizing) {
          // Make sure we don't resize less than minimum size.
          if (minSize.height < (size.height - MOVE_RESIZE_INCREMENT)) {
            delta = MOVE_RESIZE_INCREMENT;
          } else {
            delta = size.height - minSize.height;
          }

          // Ensure that we keep the internal frame on the desktop.
          if (loc.y + delta > dpHeight - minOnScreenInsets.top) {
            delta = (dpHeight - minOnScreenInsets.top) - loc.y;
          }

          c.setLocation(loc.x, loc.y + delta);
          c.setSize(size.width, size.height - delta);
        }
      } else if (NEXT_FRAME == key || PREVIOUS_FRAME == key) {
        dp.selectFrame((key == NEXT_FRAME) ? true : false);
      } else if (NAVIGATE_NEXT == key || NAVIGATE_PREVIOUS == key) {
        boolean moveForward = true;
        if (NAVIGATE_PREVIOUS == key) {
          moveForward = false;
        }
        Container cycleRoot = dp.getFocusCycleRootAncestor();

        if (cycleRoot != null) {
          FocusTraversalPolicy policy = cycleRoot.getFocusTraversalPolicy();
          if (policy != null && policy instanceof SortingFocusTraversalPolicy) {
            SortingFocusTraversalPolicy sPolicy = (SortingFocusTraversalPolicy) policy;
            boolean idc = sPolicy.getImplicitDownCycleTraversal();
            try {
              sPolicy.setImplicitDownCycleTraversal(false);
              if (moveForward) {
                KeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent(dp);
              } else {
                KeyboardFocusManager.getCurrentKeyboardFocusManager().focusPreviousComponent(dp);
              }
            } finally {
              sPolicy.setImplicitDownCycleTraversal(idc);
            }
          }
        }
      }
    }