package org.jetbrains.plugins.gradle.ui;

import com.intellij.ide.DataManager;
import com.intellij.ide.ui.LafManager;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.actionSystem.ex.ActionButtonLook;
import com.intellij.openapi.actionSystem.impl.ActionButton;
import com.intellij.openapi.actionSystem.impl.PresentationFactory;
import com.intellij.openapi.util.AsyncResult;
import com.intellij.ui.ClickListener;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.gradle.util.GradleConstants;

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

/**
 * @author Denis Zhdanov
 * @since 1/16/12 5:20 PM
 */
public class RichTextActionProcessor implements RichTextControlBuilder.RichTextProcessor {
  
  @Override
  public JComponent process(@NotNull String s) {
    final ActionManager actionManager = ActionManager.getInstance();
    final AnAction action = actionManager.getAction(s);
    if (action == null) {
      return null;
    }
    final Presentation presentation = action.getTemplatePresentation();
    
    if (presentation.getIcon() != null) {
      return new ActionButton(action, presentation.clone(), GradleConstants.TOOL_WINDOW_TOOLBAR_PLACE, new Dimension(0, 0));
    }

    final String text = action.getTemplatePresentation().getText();
    JLabel result = new JLabel(text) {
      public void paint(Graphics g) {
        super.paint(g);
        final int y = g.getClipBounds().height - getFontMetrics(getFont()).getDescent() + 2;
        final int width = getFontMetrics(getFont()).stringWidth(getText());
        g.drawLine(0, y, width, y);
      }
    };
    Color color = UIUtil.isUnderDarcula() ? Color.ORANGE : Color.BLUE;
    result.setForeground(color);
    result.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

    new ClickListener() {
      @Override
      public boolean onClick(MouseEvent e, int clickCount) {
        final AsyncResult<DataContext> callback = DataManager.getInstance().getDataContextFromFocus();
        final DataContext context = callback.getResult();
        if (context == null) {
          return false;
        }
        final Presentation presentation = new PresentationFactory().getPresentation(action);
        action.actionPerformed(new AnActionEvent(
          e, context, GradleConstants.TOOL_WINDOW_TOOLBAR_PLACE, presentation, ActionManager.getInstance(), e.getModifiers()
        ));
        return true;
      }
    }.installOn(result);
    return result;
  }

  @NotNull
  @Override
  public String getKey() {
    return "action";
  }
}

@@ -35,16 +35,310 @@
 le.ui;%0A%0A
+import java.awt.Color;%0Aimport java.awt.Cursor;%0Aimport java.awt.Dimension;%0Aimport java.awt.Graphics;%0Aimport java.awt.event.MouseEvent;%0A%0Aimport javax.swing.JComponent;%0Aimport javax.swing.JLabel;%0A%0Aimport org.jetbrains.annotations.NotNull;%0Aimport org.jetbrains.plugins.gradle.util.GradleConstants;%0A
 import c
@@ -390,18 +390,35 @@
 lij.
-ide.ui.Laf
+openapi.actionSystem.Action
 Mana
@@ -463,17 +463,24 @@
 nSystem.
-*
+AnAction
 ;%0Aimport
@@ -518,27 +518,130 @@
 tem.
-ex.ActionButtonLook
+AnActionEvent;%0Aimport com.intellij.openapi.actionSystem.DataContext;%0Aimport com.intellij.openapi.actionSystem.Presentation
 ;%0Aim
@@ -888,184 +888,8 @@
 til;
-%0Aimport org.jetbrains.annotations.NotNull;%0Aimport org.jetbrains.plugins.gradle.util.GradleConstants;%0A%0Aimport javax.swing.*;%0Aimport java.awt.*;%0Aimport java.awt.event.MouseEvent;
 %0A%0A/*
@@ -1036,19 +1036,17 @@
 essor %7B%0A
-  
 %0A
+
   @Overr
@@ -1346,20 +1346,16 @@
 tion();%0A
-    
 %0A    if 

drop usage actionbuttonlock