package jetbrains.mps.lang.editor.diagram.runtime.jetpad.palette.ui;

/*Generated by MPS */

import com.intellij.openapi.actionSystem.impl.ActionToolbarImpl;
import org.jetbrains.annotations.NotNull;
import com.intellij.openapi.actionSystem.ActionGroup;
import com.intellij.openapi.actionSystem.impl.ActionButton;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.ex.ActionButtonLook;
import com.intellij.openapi.actionSystem.Presentation;
import java.awt.Dimension;
import com.intellij.openapi.actionSystem.DataContext;
import java.awt.event.MouseEvent;

/*package*/ class DiagramPaletteActionToolbar extends ActionToolbarImpl {

  public DiagramPaletteActionToolbar(String place, @NotNull ActionGroup actionGroup, boolean horizontal) {
    super(place, actionGroup, horizontal);
  }

  @Override
  @NotNull
  protected ActionButton createToolbarButton(AnAction action, ActionButtonLook look, String place, Presentation presentation, Dimension minimumSize) {
    if (action.displayTextInToolbar()) {
      return super.createToolbarButton(action, look, place, presentation, minimumSize);
    }
    ActionButton button = new ActionButton(action, presentation, place, minimumSize) {
      @Override
      protected DataContext getDataContext() {
        return getToolbarDataContext();
      }

      @Override
      protected void processMouseEvent(MouseEvent event) {
        if (event.getID() == MouseEvent.MOUSE_RELEASED) {
          updateChildren();
        }
        super.processMouseEvent(event);
        if (event.getID() == MouseEvent.MOUSE_RELEASED) {
          removeChildren();
        }
      }

      @Override
      public void click() {
        updateChildren();
        super.click();
        removeChildren();
      }
      private void updateChildren() {
        if (myAction instanceof PopupPaletteActionGroupAdapter) {
          ((PopupPaletteActionGroupAdapter) myAction).updateChildren();
        }
      }
      private void removeChildren() {
        if (myAction instanceof PopupPaletteActionGroupAdapter) {
          ((PopupPaletteActionGroupAdapter) myAction).removeChildren();
        }
      }
    };
    button.setLook(look);
    return button;
  }

}
