package com.intellij.debugger.ui;

import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer;
import com.intellij.debugger.engine.evaluation.CodeFragmentKind;
import com.intellij.debugger.engine.evaluation.TextWithImports;
import com.intellij.debugger.engine.evaluation.TextWithImportsImpl;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.fileTypes.StdFileTypes;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.ui.EditorTextField;
import org.jetbrains.annotations.NonNls;

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

/**
 * Created by IntelliJ IDEA.
 * User: lex
 * Date: Apr 12, 2004
 * Time: 2:39:17 PM
 * To change this template use File | Settings | File Templates.
 */
public class DebuggerStatementEditor extends DebuggerEditorImpl {
  private static final Logger LOG = Logger.getInstance("#com.intellij.debugger.ui.DebuggerStatementEditor");

  private final EditorTextField myEditor;

  private int    myRecentIdx;

  public DebuggerStatementEditor(Project project, PsiElement context, @NonNls String recentsId) {
    super(project, context, recentsId);
    myRecentIdx = DebuggerRecents.getInstance(getProject()).getRecents(getRecentsId()).size();
    myEditor = new EditorTextField("", project, StdFileTypes.JAVA) {
      protected EditorEx createEditor() {
        EditorEx editor = super.createEditor();
        editor.setOneLineMode(false);
        editor.setVerticalScrollbarVisible(true);
        return editor;
      }
    };
    setLayout(new BorderLayout());
    add(myEditor, BorderLayout.CENTER);

    DefaultActionGroup actionGroup = new DefaultActionGroup(null, false);
    actionGroup.add(new ItemAction(IdeActions.ACTION_PREVIOUS_OCCURENCE, this){
      public void actionPerformed(AnActionEvent e) {
        LOG.assertTrue(myRecentIdx >= 0);
        // since recents are stored in a stack, previous item is at currentIndex + 1
        myRecentIdx += 1;
        updateTextFromRecents();
      }

      public void update(AnActionEvent e) {
        LinkedList<TextWithImports> recents = DebuggerRecents.getInstance(getProject()).getRecents(getRecentsId());
        e.getPresentation().setEnabled(myRecentIdx < recents.size());
      }
    });
    actionGroup.add(new ItemAction(IdeActions.ACTION_NEXT_OCCURENCE, this){
      public void actionPerformed(AnActionEvent e) {
        if(LOG.isDebugEnabled()) {
          LinkedList<TextWithImports> recents = DebuggerRecents.getInstance(getProject()).getRecents(getRecentsId());
          LOG.assertTrue(myRecentIdx < recents.size());
        }
        // since recents are stored in a stack, next item is at currentIndex - 1
        myRecentIdx -= 1;
        updateTextFromRecents();
      }

      public void update(AnActionEvent e) {
        e.getPresentation().setEnabled(myRecentIdx > 0);
      }
    });

    add(ActionManager.getInstance().createActionToolbar(ActionPlaces.COMBO_PAGER, actionGroup, false).getComponent(),
        BorderLayout.EAST);

    setText(new TextWithImportsImpl(CodeFragmentKind.EXPRESSION, ""));
  }

  private void updateTextFromRecents() {
    LinkedList<TextWithImports> recents = DebuggerRecents.getInstance(getProject()).getRecents(getRecentsId());
    LOG.assertTrue(myRecentIdx <= recents.size());
    setText(myRecentIdx < recents.size() ? recents.get(myRecentIdx) : new TextWithImportsImpl(CodeFragmentKind.EXPRESSION, ""));
  }

  public JComponent getPreferredFocusedComponent() {
    return myEditor.getEditor().getContentComponent();
  }

  public TextWithImports getText() {
    return createItem(myEditor.getDocument(), getProject());
  }

  public void setText(TextWithImports text) {
    myEditor.setDocument(createDocument(text));
    final Editor editor = myEditor.getEditor();
    if (editor != null) {
      DaemonCodeAnalyzer.getInstance(getProject()).updateVisibleHighlighters(editor);
    }
  }

  public TextWithImports createText(String text, String importsString) {
    return new TextWithImportsImpl(CodeFragmentKind.CODE_BLOCK, text, importsString);
  }

  private static abstract class ItemAction extends AnAction {
    public ItemAction(String sourceActionName, JComponent component) {
      copyFrom(ActionManager.getInstance().getAction(sourceActionName));
      registerCustomShortcutSet(getShortcutSet(), component);
    }
  }

}

@@ -739,26 +739,85 @@
 va.util.
-LinkedList
+ArrayList;%0Aimport java.util.LinkedList;%0Aimport java.util.ListIterator
 ;%0A%0A/**%0A 
@@ -1378,81 +1378,27 @@
 x = 
-Debug
 ge
-r
+t
 Recent
-s.getInstance(getProject()).getRecents(getRecentsId()).size
+ItemsCount
 ();%0A
@@ -2208,124 +2208,8 @@
 ) %7B%0A
-        LinkedList%3CTextWithImports%3E recents = DebuggerRecents.getInstance(getProject()).getRecents(getRecentsId());%0A
     
@@ -2253,36 +2253,43 @@
 RecentIdx %3C 
-r
+getR
 ecent
-s.size
+ItemsCount
 ());%0A      %7D
@@ -2465,126 +2465,8 @@
 ) %7B%0A
-          LinkedList%3CTextWithImports%3E recents = DebuggerRecents.getInstance(getProject()).getRecents(getRecentsId());%0A
     
@@ -2500,28 +2500,35 @@
 ntIdx %3C 
-r
+getR
 ecent
-s.size
+ItemsCount
 ());%0A   
@@ -3069,22 +3069,26 @@
 ) %7B%0A    
-Linked
+java.util.
 List%3CTex
@@ -3115,75 +3115,19 @@
 s = 
-DebuggerRecents.getInstance(getProject()).getRecents(getRecentsId()
+getRecents(
 );%0A 
@@ -3302,32 +3302,610 @@
 ION, %22%22));%0A  %7D%0A%0A
+  private java.util.List%3CTextWithImports%3E getRecents() %7B%0A    final LinkedList%3CTextWithImports%3E recents = DebuggerRecents.getInstance(getProject()).getRecents(getRecentsId());%0A    final ArrayList%3CTextWithImports%3E reversed = new ArrayList%3CTextWithImports%3E(recents.size());%0A    for (final ListIterator%3CTextWithImports%3E it = recents.listIterator(recents.size()); it.hasPrevious();) %7B%0A      reversed.add(it.previous());%0A    %7D%0A    return reversed;%0A  %7D%0A%0A  private int getRecentItemsCount() %7B%0A    return DebuggerRecents.getInstance(getProject()).getRecents(getRecentsId()).size();%0A  %7D%0A%0A
   public JCompon

fix Code Fragment Evaluation dialog: list of recent items reversed (IDEADEV-17504)