<commit_before>package org.jetbrains.plugins.scala.components;

import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.actionSystem.impl.ActionButton;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.AbstractProjectComponent;
import com.intellij.openapi.fileEditor.*;
import com.intellij.openapi.keymap.KeymapManager;
import com.intellij.openapi.keymap.KeymapUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.plugins.scala.ScalaBundle;
import org.jetbrains.plugins.scala.ScalaFileType;
import org.jetbrains.plugins.scala.worksheet.actions.CleanWorksheetAction;
import org.jetbrains.plugins.scala.worksheet.actions.CopyWorksheetAction;
import org.jetbrains.plugins.scala.worksheet.actions.RunWorksheetAction;

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

/**
 * @author Ksenia.Sautina
 * @since 10/30/12
 */

public class WorksheetProjectComponent extends AbstractProjectComponent {
  private final FileEditorManager myFileEditorManager;

  public WorksheetProjectComponent(final Project project, FileEditorManager fileEditorManager) {
    super(project);
    myFileEditorManager = fileEditorManager;
    project.getMessageBus().connect(project).subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, new FileEditorManagerAdapter() {
      @Override
      public void fileOpened(FileEditorManager source, VirtualFile file) {
        updateNotifications(file);
      }
    });
  }

  public void updateNotifications(final VirtualFile file) {
    if (!ScalaFileType.WORKSHEET_EXTENSION.equals(file.getExtension())) return;
    ApplicationManager.getApplication().runReadAction(new Runnable() {
      public void run() {
        if (myProject.isDisposed()) return;
        FileEditor[] editors = myFileEditorManager.getAllEditors(file);
        for (FileEditor editor : editors) {
          JPanel panel = new JPanel();
          panel.setLayout(new FlowLayout(FlowLayout.LEFT));

          RunWorksheetAction executeAction = new RunWorksheetAction();
          Presentation executePresentation = executeAction.getTemplatePresentation();
          Shortcut[] shortcuts = KeymapManager.getInstance().getActiveKeymap().getShortcuts("Scala.RunWorksheet");
          if (shortcuts.length > 0) {
            String shortcutText = " (" + KeymapUtil.getShortcutText(shortcuts[0]) + ")";
            executePresentation.setText(ScalaBundle.message("worksheet.execute.button") + shortcutText);
          }
          ActionButton executeButton = new ActionButton(executeAction, executePresentation,
            ActionPlaces.EDITOR_TOOLBAR, ActionToolbar.DEFAULT_MINIMUM_BUTTON_SIZE);
          executeButton.getAction().getTemplatePresentation().setIcon(AllIcons.Actions.Execute);
          panel.add(executeButton);

          CleanWorksheetAction cleanAction = new CleanWorksheetAction();
          Presentation cleanPresentation = cleanAction.getTemplatePresentation();<commit_msg>#SCL-5287 Fixed +EA-42826<commit_after>package org.jetbrains.plugins.scala.components;

import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.ActionPlaces;
import com.intellij.openapi.actionSystem.ActionToolbar;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.actionSystem.Shortcut;
import com.intellij.openapi.actionSystem.impl.ActionButton;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.AbstractProjectComponent;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.FileEditorManagerAdapter;
import com.intellij.openapi.fileEditor.FileEditorManagerListener;
import com.intellij.openapi.keymap.KeymapManager;
import com.intellij.openapi.keymap.KeymapUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.project.ProjectManagerAdapter;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.plugins.scala.ScalaBundle;
import org.jetbrains.plugins.scala.ScalaFileType;
import org.jetbrains.plugins.scala.worksheet.actions.CleanWorksheetAction;
import org.jetbrains.plugins.scala.worksheet.actions.CopyWorksheetAction;
import org.jetbrains.plugins.scala.worksheet.actions.RunWorksheetAction;
import org.jetbrains.plugins.scala.worksheet.runconfiguration.WorksheetViewerInfo;

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

/**
 * @author Ksenia.Sautina
 * @since 10/30/12
 */

public class WorksheetProjectComponent extends AbstractProjectComponent {
  private final FileEditorManager myFileEditorManager;

  public WorksheetProjectComponent(final Project project, FileEditorManager fileEditorManager) {
    super(project);
    myFileEditorManager = fileEditorManager;
    project.getMessageBus().connect(project).subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, new FileEditorManagerAdapter() {
      @Override
      public void fileOpened(FileEditorManager source, VirtualFile file) {
        updateNotifications(file);
      }
    });
    project.getMessageBus().connect(project).subscribe(ProjectManager.TOPIC, new ProjectManagerAdapter() {
      @Override
      public void projectClosed(Project project) {
        Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
        Editor viewer = WorksheetViewerInfo.getViewer(editor);
        if (viewer != null) {
          EditorFactory.getInstance().releaseEditor(viewer);
        }
      }
    });
  }

  public void updateNotifications(final VirtualFile file) {
    if (!ScalaFileType.WORKSHEET_EXTENSION.equals(file.getExtension())) return;
    ApplicationManager.getApplication().runReadAction(new Runnable() {
      public void run() {
        if (myProject.isDisposed()) return;
        FileEditor[] editors = myFileEditorManager.getAllEditors(file);
        for (FileEditor editor : editors) {
          JPanel panel = new JPanel();
          panel.setLayout(new FlowLayout(FlowLayout.LEFT));

          RunWorksheetAction executeAction = new RunWorksheetAction();
          Presentation executePresentation = executeAction.getTemplatePresentation();
          Shortcut[] shortcuts = KeymapManager.getInstance().getActiveKeymap().getShortcuts("Scala.RunWorksheet");
          if (shortcuts.length > 0) {
            String shortcutText = " (" + KeymapUtil.getShortcutText(shortcuts[0]) + ")";
            executePresentation.setText(ScalaBundle.message("worksheet.execute.button") + shortcutText);
          }
          ActionButton executeButton = new ActionButton(executeAction, executePresentation,
            ActionPlaces.EDITOR_TOOLBAR, ActionToolbar.DEFAULT_MINIMUM_BUTTON_SIZE);
          executeButton.getAction().getTemplatePresentation().setIcon(AllIcons.Actions.Execute);
          panel.add(executeButton);

          CleanWorksheetAction cleanAction = new CleanWorksheetAction();
          Presentation cleanPresentation = cleanAction.getTemplatePresentation();
{'fraction_non_alphanumeric': 0.09624478442280945, 'fraction_numerical': 0.0034770514603616135, 'mean_word_length': 4.785197103781175, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6818287', 'n_tokens_mistral': 2044, 'n_tokens_neox': 1973, 'n_words': 305}