/*
 * Copyright (c) 2000-2006 JetBrains s.r.o. All Rights Reserved.
 */

package com.intellij.uiDesigner.palette;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataConstants;
import com.intellij.openapi.fileTypes.StdFileTypes;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.WindowManager;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.uiDesigner.UIDesignerBundle;
import com.intellij.uiDesigner.compiler.Utils;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.lw.StringDescriptor;
import org.jetbrains.annotations.Nullable;

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

/**
 * @author yole
 */
public class AddComponentAction extends AnAction {
  public void actionPerformed(AnActionEvent e) {
    Project project = (Project)e.getDataContext().getData(DataConstants.PROJECT);
    if (project == null) return;
    GroupItem groupItem = (GroupItem)e.getDataContext().getData(GroupItem.class.getName());
    PsiFile psiFile = (PsiFile)e.getDataContext().getData(DataConstants.PSI_FILE);
    PsiElement elementToAdd = (psiFile != null) ? findElementToAdd(psiFile) : null;
    String className = "";
    if (elementToAdd instanceof PsiClass) {
      className = ((PsiClass) elementToAdd).getQualifiedName();
      assert className != null;
    }
    else if (elementToAdd instanceof PsiFile) {
      try {
        className = Utils.getBoundClassName(elementToAdd.getText());
      }
      catch (Exception e1) {
        className = "";
      }
    }

    // Show dialog
    final ComponentItem itemToBeAdded = new ComponentItem(
      project,
      className,
      null,
      null,
      new GridConstraints(),
      new HashMap<String, StringDescriptor>(),
      true/*all user defined components are removable*/,
      false,
      false
    );
    Window parentWindow = WindowManager.getInstance().suggestParentWindow(project);
    final ComponentItemDialog dialog = new ComponentItemDialog(project, parentWindow, itemToBeAdded, false);
    dialog.setTitle(UIDesignerBundle.message("title.add.component"));
    dialog.showGroupChooser(groupItem);
    dialog.show();
    if(!dialog.isOK()){
      return;
    }

    groupItem = dialog.getSelectedGroup();
    // If the itemToBeAdded is already in palette do nothing
    if(groupItem.containsItemClass(itemToBeAdded.getClassName())){
      return;
    }

    // add to the group

    final Palette palette = Palette.getInstance(project);
    palette.addItem(groupItem, itemToBeAdded);
    palette.fireGroupsChanged();
  }

  @Override public void update(AnActionEvent e) {
    Project project = (Project) e.getDataContext().getData(DataConstants.PROJECT);
    PsiFile psiFile = (PsiFile)e.getDataContext().getData(DataConstants.PSI_FILE);
    if (psiFile != null && project != null) {
      e.getPresentation().setVisible(findElementToAdd(psiFile) != null);
    }
    else {
      // invoked from palette
      e.getPresentation().setVisible(true);
      GroupItem groupItem = (GroupItem) e.getDataContext().getData(GroupItem.class.getName());
      e.getPresentation().setEnabled(project != null && (groupItem == null || !groupItem.isReadOnly()));
    }
  }

  @Nullable
  private static PsiElement findElementToAdd(final PsiFile psiFile) {
    if (psiFile.getFileType().equals(StdFileTypes.GUI_DESIGNER_FORM)) {
      return psiFile;
    }
    else if (psiFile.getFileType().equals(StdFileTypes.JAVA)) {
      final PsiClass psiClass = PsiTreeUtil.getChildOfType(psiFile, PsiClass.class);
      Project project = psiFile.getProject();
      final PsiClass componentClass = PsiManager.getInstance(project).findClass(JComponent.class.getName(), project.getAllScope());
      if (psiClass != null && componentClass != null && psiClass.isInheritor(componentClass, true) && psiClass.getQualifiedName() != null) {
        return psiClass;
      }
    }
    return null;
  }
}

@@ -2638,24 +2638,72 @@
 urn;%0A    %7D%0A%0A
+    assignDefaultIcon(project, itemToBeAdded);%0A%0A
     // add t
@@ -2858,16 +2858,793 @@
 );%0A  %7D%0A%0A
+  private static void assignDefaultIcon(final Project project, final ComponentItem itemToBeAdded) %7B%0A    Palette palette = Palette.getInstance(project);%0A    if (itemToBeAdded.getIconPath() == null %7C%7C itemToBeAdded.getIconPath().length() == 0) %7B%0A      PsiClass aClass = PsiManager.getInstance(project).findClass(itemToBeAdded.getClassName().replace('$', '.'), project.getAllScope());%0A      while(aClass != null) %7B%0A        final ComponentItem item = palette.getItem(aClass.getQualifiedName());%0A        if (item != null) %7B%0A          String iconPath = item.getIconPath();%0A          if (iconPath != null && iconPath.length() %3E 0) %7B%0A            itemToBeAdded.setIconPath(iconPath);%0A            return;%0A          %7D%0A        %7D%0A        aClass = aClass.getSuperClass();%0A      %7D%0A    %7D%0A  %7D%0A%0A
   @Overr

use base class icon in palette (IDEADEV-6781)