private Component cycle(Component currentComponent, int delta) {
   int index = -1;
   loop:
   for (int i = 0; i < m_Components.length; i++) {
     Component component = m_Components[i];
     for (Component c = currentComponent; c != null; c = c.getParent()) {
       if (component == c) {
         index = i;
         break loop;
       }
     }
   }
   // try to find enabled component in "delta" direction
   int initialIndex = index;
   while (true) {
     int newIndex = indexCycle(index, delta);
     if (newIndex == initialIndex) {
       break;
     }
     index = newIndex;
     //
     Component component = m_Components[newIndex];
     if (component.isEnabled() && component.isVisible() && component.isFocusable()) {
       return component;
     }
   }
   // not found
   return currentComponent;
 }
  private boolean accept(Component aComponent) {
    if (!(aComponent.isVisible()
        && aComponent.isDisplayable()
        && aComponent.isFocusable()
        && aComponent.isEnabled())) {
      return false;
    }

    // Verify that the Component is recursively enabled. Disabling a
    // heavyweight Container disables its children, whereas disabling
    // a lightweight Container does not.
    if (!(aComponent instanceof Window)) {
      for (Container enableTest = aComponent.getParent();
          enableTest != null;
          enableTest = enableTest.getParent()) {
        if (!(enableTest.isEnabled() || enableTest.isLightweight())) {
          return false;
        }
        if (enableTest instanceof Window) {
          break;
        }
      }
    }

    return true;
  }
 /**
  * This method navigates in the given direction giving focus to the next component in the given
  * direction.
  *
  * @param direction The direction to give focus to.
  */
 protected void navigateFocusedComp(int direction) {
   int count = toolBar.getComponentCount();
   switch (direction) {
     case EAST:
     case SOUTH:
       if (focusedCompIndex >= 0 && focusedCompIndex < count) {
         int i = focusedCompIndex + 1;
         boolean focusRequested = false;
         // Find component to focus and request focus on it.
         while (i != focusedCompIndex && !focusRequested) {
           if (i >= count) i = 0;
           Component comp = toolBar.getComponentAtIndex(i++);
           if (comp != null && comp.isFocusable() && comp.isEnabled()) {
             comp.requestFocus();
             focusRequested = true;
           }
         }
       }
       break;
     case WEST:
     case NORTH:
       if (focusedCompIndex >= 0 && focusedCompIndex < count) {
         int i = focusedCompIndex - 1;
         boolean focusRequested = false;
         // Find component to focus and request focus on it.
         while (i != focusedCompIndex && !focusRequested) {
           if (i < 0) i = count - 1;
           Component comp = toolBar.getComponentAtIndex(i--);
           if (comp != null && comp.isFocusable() && comp.isEnabled()) {
             comp.requestFocus();
             focusRequested = true;
           }
         }
       }
       break;
     default:
       break;
   }
 }
Esempio n. 4
0
    /** Returns either a new or recycled <code>Popup</code> containing the specified children. */
    static Popup getHeavyWeightPopup(Component owner, Component contents, int ownerX, int ownerY) {
      Window window = (owner != null) ? SwingUtilities.getWindowAncestor(owner) : null;
      HeavyWeightPopup popup = null;

      if (window != null) {
        popup = getRecycledHeavyWeightPopup(window);
      }

      boolean focusPopup = false;
      if (contents != null && contents.isFocusable()) {
        if (contents instanceof JPopupMenu) {
          JPopupMenu jpm = (JPopupMenu) contents;
          Component popComps[] = jpm.getComponents();
          for (Component popComp : popComps) {
            if (!(popComp instanceof MenuElement) && !(popComp instanceof JSeparator)) {
              focusPopup = true;
              break;
            }
          }
        }
      }

      if (popup == null
          || ((JWindow) popup.getComponent()).getFocusableWindowState() != focusPopup) {

        if (popup != null) {
          // The recycled popup can't serve us well
          // dispose it and create new one
          popup._dispose();
        }

        popup = new HeavyWeightPopup();
      }

      popup.reset(owner, contents, ownerX, ownerY);

      if (focusPopup) {
        JWindow wnd = (JWindow) popup.getComponent();
        wnd.setFocusableWindowState(true);
        // Set window name. We need this in BasicPopupMenuUI
        // to identify focusable popup window.
        wnd.setName("###focusableSwingPopup###");
      }

      return popup;
    }
  /**
   * @return <code>true</code> if and only if the passed event is already dispatched by the <code>
   *     IdeMouseEventDispatcher</code> and there is no need for any other processing of the event.
   *     If the method returns <code>false</code> then it means that the event should be delivered
   *     to normal event dispatching.
   */
  public boolean dispatchMouseEvent(MouseEvent e) {
    Component c = e.getComponent();

    // frame activation by mouse click
    if (e.getID() == MOUSE_PRESSED && c instanceof IdeFrame && !c.hasFocus()) {
      IdeFocusManager focusManager = IdeFocusManager.getGlobalInstance();
      if (focusManager instanceof FocusManagerImpl) {
        Component at = SwingUtilities.getDeepestComponentAt(c, e.getX(), e.getY());
        if (at != null && at.isFocusable()) {
          ((FocusManagerImpl) focusManager).setLastFocusedAtDeactivation((IdeFrame) c, at);
        }
      }
    }

    if (SystemInfo.isXWindow && e.isPopupTrigger() && e.getButton() != 3) {
      // we can do better than silly triggering popup on everything but left click
      resetPopupTrigger(e);
    }

    boolean ignore = false;
    if (!(e.getID() == MouseEvent.MOUSE_PRESSED
        || e.getID() == MouseEvent.MOUSE_RELEASED
        || e.getID() == MOUSE_CLICKED)) {
      ignore = true;
    }

    patchClickCount(e);

    if (e.isConsumed()
        || e.isPopupTrigger()
        || (e.getButton() > 3 ? e.getID() != MOUSE_PRESSED : e.getID() != MOUSE_RELEASED)
        || e.getClickCount() < 1
        || e.getButton() == MouseEvent.NOBUTTON) { // See #16995. It did happen
      ignore = true;
    }

    int modifiers = e.getModifiers();
    int modifiersEx = e.getModifiersEx();
    if (e.getID() == MOUSE_PRESSED) {
      myPressedModifiersStored = true;
      myModifiers = modifiers;
      myModifiersEx = modifiersEx;
    } else if (e.getID() == MOUSE_RELEASED) {
      if (myPressedModifiersStored) {
        myPressedModifiersStored = false;
        modifiers = myModifiers;
        modifiersEx = myModifiersEx;
      }
    }

    final JRootPane root = findRoot(e);
    if (root != null) {
      BlockState blockState = myRootPane2BlockedId.get(root);
      if (blockState != null) {
        if (SWING_EVENTS_PRIORITY.indexOf(blockState.currentEventId)
            < SWING_EVENTS_PRIORITY.indexOf(e.getID())) {
          blockState.currentEventId = e.getID();
          if (blockState.blockMode == IdeEventQueue.BlockMode.COMPLETE) {
            return true;
          } else {
            ignore = true;
          }
        } else {
          myRootPane2BlockedId.remove(root);
        }
      }
    }

    if (c == null) {
      throw new IllegalStateException("component cannot be null");
    }
    c = SwingUtilities.getDeepestComponentAt(c, e.getX(), e.getY());

    if (c instanceof IdeGlassPaneImpl) {
      c = ((IdeGlassPaneImpl) c).getTargetComponentFor(e);
    }

    if (c == null) { // do nothing if component doesn't contains specified point
      return false;
    }

    if (isHorizontalScrolling(c, e)) {
      boolean done = doHorizontalScrolling(c, (MouseWheelEvent) e);
      if (done) return true;
    }

    if (ignore) return false;

    // avoid "cyclic component initialization error" in case of dialogs shown because of component
    // initialization failure
    if (!KeymapManagerImpl.ourKeymapManagerInitialized) {
      return false;
    }

    final MouseShortcut shortcut = new MouseShortcut(e.getButton(), modifiersEx, e.getClickCount());
    fillActionsList(c, shortcut, IdeKeyEventDispatcher.isModalContext(c));
    ActionManagerEx actionManager = ActionManagerEx.getInstanceEx();
    if (actionManager != null) {
      AnAction[] actions = myActions.toArray(new AnAction[myActions.size()]);
      for (AnAction action : actions) {
        DataContext dataContext = DataManager.getInstance().getDataContext(c);
        Presentation presentation = myPresentationFactory.getPresentation(action);
        AnActionEvent actionEvent =
            new AnActionEvent(
                e,
                dataContext,
                ActionPlaces.MAIN_MENU,
                presentation,
                ActionManager.getInstance(),
                modifiers);
        action.beforeActionPerformedUpdate(actionEvent);

        if (presentation.isEnabled()) {
          actionManager.fireBeforeActionPerformed(action, dataContext, actionEvent);
          final Component context = PlatformDataKeys.CONTEXT_COMPONENT.getData(dataContext);

          if (context != null && !context.isShowing()) continue;

          action.actionPerformed(actionEvent);
          e.consume();
        }
      }
      if (actions.length > 0 && e.isConsumed()) return true;
    }
    return e.getButton() > 3;
  }
 public static boolean isFocusable(Component component) {
   return component.isFocusable()
       && component.isDisplayable()
       && component.isVisible()
       && component.isEnabled();
 }