<commit_before>/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.openapi.wm;

import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.ActionCallback;
import com.intellij.openapi.util.Expirable;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;

public abstract class IdeFocusManager {

  /**
   * Requests focus on a component
   * @param c
   * @param forced
   * @return action callback that either notifies when the focus was obtained or focus request was droppped
   */
  @NotNull
  public abstract ActionCallback requestFocus(@NotNull Component c, boolean forced);

  /**
   * Runs a request focus command, actual focus request is defined by the user in the command itself
   * @param command
   * @param forced
   * @return action callback that either notifies when the focus was obtained or focus request was droppped
   */
  @NotNull
  public abstract ActionCallback requestFocus(@NotNull FocusCommand command, boolean forced);

  /**
   * Finds most suitable component to request focus to. For instance you may pass a JPanel instance,
   * this method will traverse into it's children to find focusable component
   * @param comp
   * @return suitable component to focus
   */
  @Nullable
  public abstract JComponent getFocusTargetFor(@NotNull final JComponent comp);


  public abstract void doWhenFocusSettlesDown(@NotNull Runnable runnable);

  @Nullable
  public abstract Component getFocusedDescendantFor(final Component comp);

  public abstract boolean dispatch(KeyEvent e);

  public abstract void suspendKeyProcessingUntil(@NotNull ActionCallback done);

  public abstract boolean isFocusBeingTransferred();

  public abstract ActionCallback requestDefaultFocus(boolean forced);

  public static IdeFocusManager getInstance(@Nullable Project project) {
    if (project == null) return getGlobalInstance();

    if (project.isDisposed() || !project.isInitialized()) return getGlobalInstance();
    return project.getComponent(IdeFocusManager.class);
  }

  @NotNull
  public static IdeFocusManager findInstanceByContext(@Nullable DataContext context) {
    IdeFocusManager instance = null;
    if (context != null) {
      instance = getInstanceSafe(PlatformDataKeys.PROJECT.getData(context));
    }

    if (instance == null) {
      instance = findByComponent(KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow());
    }

    if (instance == null) {
      instance = getGlobalInstance();
    }

    return instance;
  }

  @NotNull
  public static IdeFocusManager findInstanceByComponent(@NotNull Component c) {
    final IdeFocusManager instance = findByComponent(c);
    return instance != null ? instance : findInstanceByContext(null);
  }


  @Nullable
  private static IdeFocusManager findByComponent(Component c) {
    final Component parent = UIUtil.findUltimateParent(c);
    if (parent instanceof IdeFrame) {
      return getInstanceSafe(((IdeFrame)parent).getProject());
    }
    return null;
  }


  @Nullable
  private static IdeFocusManager getInstanceSafe(@Nullable Project project) {
    if (project != null && !project.isDisposed() && project.isInitialized()) {
      return IdeFocusManager.getInstance(project);
    } else {
      return null;
    }
  }

  @NotNull
  public static IdeFocusManager findInstance() {
    final Component owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
    return owner != null ? findInstanceByComponent(owner) : findInstanceByContext(null);
  }

  public abstract Expirable getTimestamp(boolean trackOnlyForcedCommands);

  public static IdeFocusManager getGlobalInstance() {
    return ApplicationManager.getApplication().getComponent(IdeFocusManager.class);
  }

}
<commit_msg>IdeFocusManager -- fix for appliction == null<commit_after>/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.openapi.wm;

import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.ActionCallback;
import com.intellij.openapi.util.Expirable;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;

public abstract class IdeFocusManager {

  /**
   * Requests focus on a component
   * @param c
   * @param forced
   * @return action callback that either notifies when the focus was obtained or focus request was droppped
   */
  @NotNull
  public abstract ActionCallback requestFocus(@NotNull Component c, boolean forced);

  /**
   * Runs a request focus command, actual focus request is defined by the user in the command itself
   * @param command
   * @param forced
   * @return action callback that either notifies when the focus was obtained or focus request was droppped
   */
  @NotNull
  public abstract ActionCallback requestFocus(@NotNull FocusCommand command, boolean forced);

  /**
   * Finds most suitable component to request focus to. For instance you may pass a JPanel instance,
   * this method will traverse into it's children to find focusable component
   * @param comp
   * @return suitable component to focus
   */
  @Nullable
  public abstract JComponent getFocusTargetFor(@NotNull final JComponent comp);


  public abstract void doWhenFocusSettlesDown(@NotNull Runnable runnable);

  @Nullable
  public abstract Component getFocusedDescendantFor(final Component comp);

  public abstract boolean dispatch(KeyEvent e);

  public abstract void suspendKeyProcessingUntil(@NotNull ActionCallback done);

  public abstract boolean isFocusBeingTransferred();

  public abstract ActionCallback requestDefaultFocus(boolean forced);

  public static IdeFocusManager getInstance(@Nullable Project project) {
    if (project == null) return getGlobalInstance();

    if (project.isDisposed() || !project.isInitialized()) return getGlobalInstance();
    return project.getComponent(IdeFocusManager.class);
  }

  @NotNull
  public static IdeFocusManager findInstanceByContext(@Nullable DataContext context) {
    IdeFocusManager instance = null;
    if (context != null) {
      instance = getInstanceSafe(PlatformDataKeys.PROJECT.getData(context));
    }

    if (instance == null) {
      instance = findByComponent(KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow());
    }

    if (instance == null) {
      instance = getGlobalInstance();
    }

    return instance;
  }

  @NotNull
  public static IdeFocusManager findInstanceByComponent(@NotNull Component c) {
    final IdeFocusManager instance = findByComponent(c);
    return instance != null ? instance : findInstanceByContext(null);
  }


  @Nullable
  private static IdeFocusManager findByComponent(Component c) {
    final Component parent = UIUtil.findUltimateParent(c);
    if (parent instanceof IdeFrame) {
      return getInstanceSafe(((IdeFrame)parent).getProject());
    }
    return null;
  }


  @Nullable
  private static IdeFocusManager getInstanceSafe(@Nullable Project project) {
    if (project != null && !project.isDisposed() && project.isInitialized()) {
      return IdeFocusManager.getInstance(project);
    } else {
      return null;
    }
  }

  @NotNull
  public static IdeFocusManager findInstance() {
    final Component owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
    return owner != null ? findInstanceByComponent(owner) : findInstanceByContext(null);
  }

  public abstract Expirable getTimestamp(boolean trackOnlyForcedCommands);

  public static IdeFocusManager getGlobalInstance() {
    Application app = ApplicationManager.getApplication();
    return app != null ? app.getComponent(IdeFocusManager.class) : PassThroughtIdeFocusManager.getInstance(); 
  }

}

{'fraction_non_alphanumeric': 0.08178042806942817, 'fraction_numerical': 0.0025556383771696304, 'mean_word_length': 4.463641652123328, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '140185', 'n_tokens_mistral': 2453, 'n_tokens_neox': 2388, 'n_words': 790}