/*
 * 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.xdebugger.impl.actions;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.LogicalPosition;
import com.intellij.openapi.editor.event.EditorMouseEventArea;
import com.intellij.openapi.project.Project;
import com.intellij.xdebugger.impl.DebuggerSupport;
import com.intellij.xdebugger.impl.evaluate.quick.common.QuickEvaluateHandler;
import com.intellij.xdebugger.impl.evaluate.quick.common.ValueHintType;
import com.intellij.xdebugger.impl.evaluate.quick.common.ValueLookupManager;
import org.jetbrains.annotations.NotNull;

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

/**
 * @author nik
 */
public class QuickEvaluateAction extends XDebuggerActionBase {
  public QuickEvaluateAction() {
    super(true);
  }

  @NotNull
  protected DebuggerActionHandler getHandler(@NotNull final DebuggerSupport debuggerSupport) {
    return new QuickEvaluateHandlerWrapper(debuggerSupport.getQuickEvaluateHandler());
  }

  private static class QuickEvaluateHandlerWrapper extends DebuggerActionHandler {
    private final QuickEvaluateHandler myHandler;

    public QuickEvaluateHandlerWrapper(final QuickEvaluateHandler handler) {
      myHandler = handler;
    }

    public void perform(@NotNull final Project project, final AnActionEvent event) {
      Editor editor = event.getData(PlatformDataKeys.EDITOR);

      if(editor != null) {
        LogicalPosition logicalPosition = editor.getCaretModel().getLogicalPosition();
        ValueLookupManager.getInstance(project).showHint(myHandler, editor, editor.logicalPositionToXY(logicalPosition), ValueHintType.MOUSE_CLICK_HINT);
      }
    }

    public boolean isEnabled(@NotNull final Project project, final AnActionEvent event) {
      Editor editor = event.getData(PlatformDataKeys.EDITOR);
      MouseEvent mouseEvent = event.getInputEvent() instanceof MouseEvent ? (MouseEvent)event.getInputEvent(): null;
      if (editor == null || mouseEvent == null || !(mouseEvent.isAltDown()))
        return false;
      Component deepestComponentAt = SwingUtilities.getDeepestComponentAt(mouseEvent.getComponent(), mouseEvent.getX(), mouseEvent.getY());
      if (!SwingUtilities.isDescendingFrom(deepestComponentAt, editor.getComponent()))
        return false;
      EditorMouseEventArea area = editor.getMouseEventArea(SwingUtilities.convertMouseEvent(mouseEvent.getComponent(), mouseEvent, deepestComponentAt));
      return myHandler.isEnabled(project) && area == EditorMouseEventArea.EDITING_AREA;
    }
  }
}

@@ -20,10 +20,10 @@
 0-20
-09
+12
  Jet
@@ -1318,24 +1318,58 @@
 java.awt.*;%0A
+import java.awt.event.InputEvent;%0A
 import java.
@@ -2122,25 +2122,24 @@
 DITOR);%0A
-%0A
       if
 (editor 
@@ -2130,16 +2130,17 @@
       if
+ 
 (editor 
@@ -2285,16 +2285,27 @@
 roject).
+%0A          
 showHint
@@ -2499,32 +2499,88 @@
 nEvent event) %7B%0A
+      if (!myHandler.isEnabled(project)) return false;%0A%0A
       Editor edi
@@ -2635,41 +2635,49 @@
     
-MouseEvent mouseEvent = event.get
+if (editor == null) return false;%0A%0A      
 Inpu
@@ -2686,47 +2686,22 @@
 vent
-()
  in
-stanceof MouseEvent ? (MouseEvent)
+putEvent = 
 even
@@ -2721,14 +2721,8 @@
 nt()
-: null
 ;%0A  
@@ -2733,55 +2733,49 @@
 if (
-editor == null %7C%7C mouseEvent == null %7C%7C !(mouse
+inputEvent instanceof MouseEvent && input
 Even
@@ -2788,17 +2788,18 @@
 tDown())
-)
+ %7B
 %0A       
@@ -2795,38 +2795,74 @@
 ) %7B%0A        
-return false;%0A
+MouseEvent mouseEvent = (MouseEvent)inputEvent;%0A  
       Compon
@@ -2865,32 +2865,25 @@
 mponent 
-deepestC
+c
 omponent
 At = Swi
@@ -2874,18 +2874,16 @@
 omponent
-At
  = Swing
@@ -2986,21 +2986,22 @@
 ;%0A      
+  
 if (
-!
 SwingUti
@@ -3024,32 +3024,25 @@
 ingFrom(
-deepestC
+c
 omponent
 At, edit
@@ -3033,18 +3033,16 @@
 omponent
-At
 , editor
@@ -3058,16 +3058,18 @@
 nent()))
+ %7B
 %0A       
@@ -3073,226 +3073,222 @@
     
-return false;%0A      EditorMouseEventArea area = editor.getMouseEventArea(SwingUtilities.convertMouseEvent(mouseEvent.getComponent(), mouseEvent, deepestComponentAt));%0A      return myHandler.isEnabled(project) && 
+  MouseEvent convertedEvent = SwingUtilities.convertMouseEvent(mouseEvent.getComponent(), mouseEvent, component);%0A          EditorMouseEventArea area = editor.getMouseEventArea(convertedEvent);%0A          if (
 area 
-=
+!
 = Ed
@@ -3318,16 +3318,94 @@
 ING_AREA
+) %7B%0A            return false;%0A          %7D%0A        %7D%0A      %7D%0A%0A      return true
 ;%0A    %7D%0A

Fix Quick Evaluate Expression action