Java Code Examples for com.intellij.openapi.wm.IdeFocusManager#findInstanceByComponent()

The following examples show how to use com.intellij.openapi.wm.IdeFocusManager#findInstanceByComponent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JBTable.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void permanentFocusOwnerChanged() {
  if (!isEditing()) {
    return;
  }

  final IdeFocusManager focusManager = IdeFocusManager.findInstanceByComponent(JBTable.this);
  focusManager.doWhenFocusSettlesDown(new ExpirableRunnable() {
    @Override
    public boolean isExpired() {
      return !isEditing();
    }

    @Override
    public void run() {
      Component c = focusManager.getFocusOwner();
      if (UIUtil.isMeaninglessFocusOwner(c)) {
        // this allows using popup menus and menu bar without stopping cell editing
        return;
      }
      while (c != null) {
        if (c instanceof JPopupMenu) {
          c = ((JPopupMenu)c).getInvoker();
        }
        if (c == JBTable.this) {
          // focus remains inside the table
          return;
        }
        else if (c instanceof Window) {
          if (c == SwingUtilities.getWindowAncestor(JBTable.this)) {
            removeCellEditor();
          }
          break;
        }
        c = c.getParent();
      }
    }
  });
}
 
Example 2
Source File: AbstractPopup.java    From consulo with Apache License 2.0 5 votes vote down vote up
private IdeFocusManager getFocusManager() {
  if (myProject != null) {
    return IdeFocusManager.getInstance(myProject);
  }
  if (myOwner != null) {
    return IdeFocusManager.findInstanceByComponent(myOwner);
  }
  return IdeFocusManager.findInstance();
}
 
Example 3
Source File: JBTable.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public boolean editCellAt(final int row, final int column, final EventObject e) {
  if (cellEditor != null && !cellEditor.stopCellEditing()) {
    return false;
  }

  if (row < 0 || row >= getRowCount() || column < 0 || column >= getColumnCount()) {
    return false;
  }

  if (!isCellEditable(row, column)) {
    return false;
  }

  if (e instanceof KeyEvent) {
    // do not start editing in autoStartsEdit mode on Ctrl-Z and other non-typed events
    if (!UIUtil.isReallyTypedEvent((KeyEvent)e) || ((KeyEvent)e).getKeyChar() == KeyEvent.CHAR_UNDEFINED) return false;

    SpeedSearchSupply supply = SpeedSearchSupply.getSupply(this);
    if (supply != null && supply.isPopupActive()) {
      return false;
    }
  }

  final TableCellEditor editor = getCellEditor(row, column);
  if (editor != null && editor.isCellEditable(e)) {
    editorComp = prepareEditor(editor, row, column);
    //((JComponent)editorComp).setBorder(null);
    if (editorComp == null) {
      removeEditor();
      return false;
    }
    editorComp.setBounds(getCellRect(row, column, false));
    add(editorComp);
    editorComp.validate();

    if (surrendersFocusOnKeyStroke()) {
      // this replaces focus request in JTable.processKeyBinding
      final IdeFocusManager focusManager = IdeFocusManager.findInstanceByComponent(this);
      focusManager.setTypeaheadEnabled(false);
      focusManager.requestFocus(editorComp, true).doWhenProcessed(() -> focusManager.setTypeaheadEnabled(true));
    }

    setCellEditor(editor);
    setEditingRow(row);
    setEditingColumn(column);
    editor.addCellEditorListener(this);

    return true;
  }
  return false;
}
 
Example 4
Source File: DesktopDataManagerImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
private Component getFocusedComponent() {
  Window activeWindow = TargetAWT.to(windowManager().getMostRecentFocusedWindow());
  if (activeWindow == null) {
    activeWindow = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
    if (activeWindow == null) {
      activeWindow = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow();
      if (activeWindow == null) return null;
    }
  }

  // In case we have an active floating toolwindow and some component in another window focused,
  // we want this other component to receive key events.
  // Walking up the window ownership hierarchy from the floating toolwindow would have led us to the main IdeFrame
  // whereas we want to be able to type in other frames as well.
  if (activeWindow instanceof ToolWindowFloatingDecorator) {
    IdeFocusManager ideFocusManager = IdeFocusManager.findInstanceByComponent(activeWindow);
    IdeFrame lastFocusedFrame = ideFocusManager.getLastFocusedFrame();
    JComponent frameComponent = lastFocusedFrame != null ? lastFocusedFrame.getComponent() : null;
    Window lastFocusedWindow = frameComponent != null ? SwingUtilities.getWindowAncestor(frameComponent) : null;
    boolean toolWindowIsNotFocused = windowManager().getFocusedComponent(activeWindow) == null;
    if (toolWindowIsNotFocused && lastFocusedWindow != null) {
      activeWindow = lastFocusedWindow;
    }
  }

  // try to find first parent window that has focus
  Window window = activeWindow;
  Component focusedComponent = null;
  while (window != null) {
    focusedComponent = windowManager().getFocusedComponent(window);
    if (focusedComponent != null) {
      break;
    }
    window = window.getOwner();
  }
  if (focusedComponent == null) {
    focusedComponent = activeWindow;
  }

  return focusedComponent;
}