<commit_before>package org.hanuna.gitalk.ui.impl;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.progress.BackgroundTaskQueue;
import com.intellij.openapi.progress.EmptyProgressIndicator;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
import com.intellij.util.Consumer;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.ui.UIUtil;
import git4idea.repo.GitRepository;
import gitlog.GitActionHandlerImpl;
import gitlog.GitLogComponent;
import org.hanuna.gitalk.commit.Hash;
import org.hanuna.gitalk.common.CacheGet;
import org.hanuna.gitalk.common.Executor;
import org.hanuna.gitalk.common.Function;
import org.hanuna.gitalk.common.MyTimer;
import org.hanuna.gitalk.common.compressedlist.UpdateRequest;
import org.hanuna.gitalk.data.CommitDataGetter;
import org.hanuna.gitalk.data.DataPack;
import org.hanuna.gitalk.data.DataPackUtils;
import org.hanuna.gitalk.data.impl.CacheCommitDataGetter;
import org.hanuna.gitalk.data.impl.DataLoaderImpl;
import org.hanuna.gitalk.data.impl.FakeCommitsInfo;
import org.hanuna.gitalk.data.rebase.GitActionHandler;
import org.hanuna.gitalk.data.rebase.InteractiveRebaseBuilder;
import org.hanuna.gitalk.git.reader.CommitDataReader;
import org.hanuna.gitalk.git.reader.util.GitException;
import org.hanuna.gitalk.graph.elements.GraphElement;
import org.hanuna.gitalk.graph.elements.Node;
import org.hanuna.gitalk.graphmodel.FragmentManager;
import org.hanuna.gitalk.graphmodel.GraphFragment;
import org.hanuna.gitalk.log.commit.CommitData;
import org.hanuna.gitalk.log.commit.parents.FakeCommitParents;
import org.hanuna.gitalk.log.commit.parents.RebaseCommand;
import org.hanuna.gitalk.printmodel.SelectController;
import org.hanuna.gitalk.refs.Ref;
import org.hanuna.gitalk.ui.ControllerListener;
import org.hanuna.gitalk.ui.DragDropListener;
import org.hanuna.gitalk.ui.UI_Controller;
import org.hanuna.gitalk.ui.tables.GraphTableModel;
import org.hanuna.gitalk.ui.tables.refs.refs.RefTreeModel;
import org.hanuna.gitalk.ui.tables.refs.refs.RefTreeModelImpl;
import org.hanuna.gitalk.ui.tables.refs.refs.RefTreeTableModel;
import org.jdesktop.swingx.treetable.TreeTableModel;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.table.TableModel;
import java.io.IOException;
import java.util.*;

/**
 * @author erokhins
 */
public class UI_ControllerImpl implements UI_Controller {

  private volatile DataLoaderImpl dataLoader;
  private final EventsController events = new EventsController();
  private final Project myProject;
  private final BackgroundTaskQueue myDataLoaderQueue;

  private DataPack dataPack;
  private DataPackUtils dataPackUtils;
  private RefTreeTableModel refTableModel;
  private RefTreeModel refTreeModel;

  private GraphTableModel graphTableModel;

  private GraphElement prevGraphElement = null;
  private Set<Hash> prevSelectionBranches;
  private List<Ref> myRefs;

  private DragDropListener dragDropListener = DragDropListener.EMPTY;
  private GitActionHandler myGitActionHandler;
  private final GitActionHandler.Callback myCallback = new Callback();

  private final MyInteractiveRebaseBuilder rebaseDelegate = new MyInteractiveRebaseBuilder();
  private InteractiveRebaseBuilder myInteractiveRebaseBuilder = new InteractiveRebaseBuilder() {

    @Override
    public void startRebase(Ref subjectRef, Node onto) {
      rebaseDelegate.startRebase(subjectRef, onto);
      refresh(true);
    }

    @Override
    public void startRebaseOnto(Ref subjectRef, Node base, List<Node> nodesToRebase) {
      rebaseDelegate.startRebaseOnto(subjectRef, base, nodesToRebase);
      refresh(true);
    }

    @Override
    public void moveCommits(Ref subjectRef, Node base, InsertPosition position, List<Node> nodesToInsert) {
      rebaseDelegate.moveCommits(subjectRef, base, position, nodesToInsert);
      refresh(true);
    }

    @Override
    public void fixUp(Ref subjectRef, Node target, List<Node> nodesToFixUp) {
      rebaseDelegate.fixUp(subjectRef, target, nodesToFixUp);
      refresh(true);
    }

    @Override
    public List<RebaseCommand> getRebaseCommands() {
      return super.getRebaseCommands();
    }
  };
  private boolean myCommitDetailsPreloaded;

  public UI_ControllerImpl(Project project) {
    myProject = project;
    myGitActionHandler = new GitActionHandlerImpl(myProject, this);
    myDataLoaderQueue = new BackgroundTaskQueue(myProject, "Loading...");
  }

  private void dataInit() {
    dataPack = dataLoader.getDataPack();
    refTreeModel = new RefTreeModelImpl(dataPack.getRefsModel());
    refTableModel = new RefTreeTableModel(refTreeModel);
    myRefs = dataPack.getRefsModel().getAllRefs();
    graphTableModel = new GraphTableModel(dataPack);
    dataPackUtils = new DataPackUtils(dataPack);

    prevSelectionBranches = new HashSet<Hash>(refTreeModel.getCheckedCommits());

    ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
      public void run() {
        preloadCommitDetails();
      }
    });

  }

  private final CacheGet<Hash, CommitData> commitDataCache = new CacheGet<Hash, CommitData>(new Function<Hash, CommitData>() {
    @NotNull
    @Override
    public CommitData get(@NotNull Hash key) {
      return CommitDataReader.readCommitData(myProject, key.toStrHash());
      }
  }, 5000);


  public void init(final boolean readAllLog, boolean inBackground, final boolean reusePreviousGitOutput) {
    final Consumer<ProgressIndicator> doInit = new Consumer<ProgressIndicator>() {
      @Override
      public void consume(final ProgressIndicator indicator) {
        events.setState(ControllerListener.State.PROGRESS);
        dataLoader = new DataLoaderImpl(myProject, reusePreviousGitOutput, commitDataCache);
        Executor<String> statusUpdater = new Executor<String>() {
          @Override
          public void execute(String key) {
            events.setUpdateProgressMessage(key);
            indicator.setText(key);<commit_msg>style<commit_after>package org.hanuna.gitalk.ui.impl;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.progress.BackgroundTaskQueue;
import com.intellij.openapi.progress.EmptyProgressIndicator;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
import com.intellij.util.Consumer;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.ui.UIUtil;
import git4idea.repo.GitRepository;
import gitlog.GitActionHandlerImpl;
import gitlog.GitLogComponent;
import org.hanuna.gitalk.commit.Hash;
import org.hanuna.gitalk.common.CacheGet;
import org.hanuna.gitalk.common.Executor;
import org.hanuna.gitalk.common.Function;
import org.hanuna.gitalk.common.MyTimer;
import org.hanuna.gitalk.common.compressedlist.UpdateRequest;
import org.hanuna.gitalk.data.CommitDataGetter;
import org.hanuna.gitalk.data.DataPack;
import org.hanuna.gitalk.data.DataPackUtils;
import org.hanuna.gitalk.data.impl.CacheCommitDataGetter;
import org.hanuna.gitalk.data.impl.DataLoaderImpl;
import org.hanuna.gitalk.data.impl.FakeCommitsInfo;
import org.hanuna.gitalk.data.rebase.GitActionHandler;
import org.hanuna.gitalk.data.rebase.InteractiveRebaseBuilder;
import org.hanuna.gitalk.git.reader.CommitDataReader;
import org.hanuna.gitalk.git.reader.util.GitException;
import org.hanuna.gitalk.graph.elements.GraphElement;
import org.hanuna.gitalk.graph.elements.Node;
import org.hanuna.gitalk.graphmodel.FragmentManager;
import org.hanuna.gitalk.graphmodel.GraphFragment;
import org.hanuna.gitalk.log.commit.CommitData;
import org.hanuna.gitalk.log.commit.parents.FakeCommitParents;
import org.hanuna.gitalk.log.commit.parents.RebaseCommand;
import org.hanuna.gitalk.printmodel.SelectController;
import org.hanuna.gitalk.refs.Ref;
import org.hanuna.gitalk.ui.ControllerListener;
import org.hanuna.gitalk.ui.DragDropListener;
import org.hanuna.gitalk.ui.UI_Controller;
import org.hanuna.gitalk.ui.tables.GraphTableModel;
import org.hanuna.gitalk.ui.tables.refs.refs.RefTreeModel;
import org.hanuna.gitalk.ui.tables.refs.refs.RefTreeModelImpl;
import org.hanuna.gitalk.ui.tables.refs.refs.RefTreeTableModel;
import org.jdesktop.swingx.treetable.TreeTableModel;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.table.TableModel;
import java.io.IOException;
import java.util.*;

/**
 * @author erokhins
 */
public class UI_ControllerImpl implements UI_Controller {

  private volatile DataLoaderImpl dataLoader;
  private final EventsController events = new EventsController();
  private final Project myProject;
  private final BackgroundTaskQueue myDataLoaderQueue;

  private DataPack dataPack;
  private DataPackUtils dataPackUtils;
  private RefTreeTableModel refTableModel;
  private RefTreeModel refTreeModel;

  private GraphTableModel graphTableModel;

  private GraphElement prevGraphElement = null;
  private Set<Hash> prevSelectionBranches;
  private List<Ref> myRefs;

  private DragDropListener dragDropListener = DragDropListener.EMPTY;
  private GitActionHandler myGitActionHandler;
  private final GitActionHandler.Callback myCallback = new Callback();

  private final MyInteractiveRebaseBuilder rebaseDelegate = new MyInteractiveRebaseBuilder();
  private InteractiveRebaseBuilder myInteractiveRebaseBuilder = new InteractiveRebaseBuilder() {

    @Override
    public void startRebase(Ref subjectRef, Node onto) {
      rebaseDelegate.startRebase(subjectRef, onto);
      refresh(true);
    }

    @Override
    public void startRebaseOnto(Ref subjectRef, Node base, List<Node> nodesToRebase) {
      rebaseDelegate.startRebaseOnto(subjectRef, base, nodesToRebase);
      refresh(true);
    }

    @Override
    public void moveCommits(Ref subjectRef, Node base, InsertPosition position, List<Node> nodesToInsert) {
      rebaseDelegate.moveCommits(subjectRef, base, position, nodesToInsert);
      refresh(true);
    }

    @Override
    public void fixUp(Ref subjectRef, Node target, List<Node> nodesToFixUp) {
      rebaseDelegate.fixUp(subjectRef, target, nodesToFixUp);
      refresh(true);
    }

    @Override
    public List<RebaseCommand> getRebaseCommands() {
      return super.getRebaseCommands();
    }
  };

  private boolean myCommitDetailsPreloaded;
  private final CacheGet<Hash, CommitData> commitDataCache = new CacheGet<Hash, CommitData>(new Function<Hash, CommitData>() {
    @NotNull
    @Override
    public CommitData get(@NotNull Hash key) {
      return CommitDataReader.readCommitData(myProject, key.toStrHash());
      }
  }, 5000);

  public UI_ControllerImpl(Project project) {
    myProject = project;
    myGitActionHandler = new GitActionHandlerImpl(myProject, this);
    myDataLoaderQueue = new BackgroundTaskQueue(myProject, "Loading...");
  }

  private void dataInit() {
    dataPack = dataLoader.getDataPack();
    refTreeModel = new RefTreeModelImpl(dataPack.getRefsModel());
    refTableModel = new RefTreeTableModel(refTreeModel);
    myRefs = dataPack.getRefsModel().getAllRefs();
    graphTableModel = new GraphTableModel(dataPack);
    dataPackUtils = new DataPackUtils(dataPack);

    prevSelectionBranches = new HashSet<Hash>(refTreeModel.getCheckedCommits());

    ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
      public void run() {
        preloadCommitDetails();
      }
    });

  }

  public void init(final boolean readAllLog, boolean inBackground, final boolean reusePreviousGitOutput) {
    final Consumer<ProgressIndicator> doInit = new Consumer<ProgressIndicator>() {
      @Override
      public void consume(final ProgressIndicator indicator) {
        events.setState(ControllerListener.State.PROGRESS);
        dataLoader = new DataLoaderImpl(myProject, reusePreviousGitOutput, commitDataCache);
        Executor<String> statusUpdater = new Executor<String>() {
          @Override
          public void execute(String key) {
            events.setUpdateProgressMessage(key);
            indicator.setText(key);
{'fraction_non_alphanumeric': 0.09239262807501827, 'fraction_numerical': 0.0008118860112040269, 'mean_word_length': 5.152847152847153, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, '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': '424022', 'n_tokens_mistral': 3678, 'n_tokens_neox': 3620, 'n_words': 677}