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

package com.intellij.uiDesigner.palette;

import com.intellij.ide.palette.PaletteGroup;
import com.intellij.ide.palette.PaletteItemProvider;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.fileTypes.StdFileTypes;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;

/**
 * @author yole
 */
public class UIDesignerPaletteProvider implements PaletteItemProvider {
  private Palette myPalette;
  private PropertyChangeSupport myPropertyChangeSupport = new PropertyChangeSupport(this);

  public UIDesignerPaletteProvider(final Palette palette) {
    myPalette = palette;
    myPalette.addListener(new Palette.Listener() {
      public void groupsChanged(Palette palette) {
        myPropertyChangeSupport.firePropertyChange("groups", null, null);
      }
    });
  }

  public PaletteGroup[] getActiveGroups(VirtualFile vFile) {
    if (vFile.getFileType().equals(StdFileTypes.GUI_DESIGNER_FORM)) {
      final ArrayList<GroupItem> groups = myPalette.getGroups();
      return groups.toArray(new PaletteGroup[groups.size()]);
    }
    return PaletteGroup.EMPTY_ARRAY;
  }

  public void addListener(PropertyChangeListener listener) {
    myPropertyChangeSupport.addPropertyChangeListener(listener);
  }

  public void removeListener(PropertyChangeListener listener) {
    myPropertyChangeSupport.removePropertyChangeListener(listener);
  }
}

@@ -239,23 +239,35 @@
 api.
-vfs.VirtualFile
+components.ProjectComponent
 ;%0Aim
@@ -315,16 +315,489 @@
 leTypes;
+%0Aimport com.intellij.openapi.project.Project;%0Aimport com.intellij.openapi.vfs.VirtualFile;%0Aimport com.intellij.psi.PsiClass;%0Aimport com.intellij.psi.PsiElement;%0Aimport com.intellij.refactoring.listeners.RefactoringElementListener;%0Aimport com.intellij.refactoring.listeners.RefactoringElementListenerProvider;%0Aimport com.intellij.refactoring.listeners.RefactoringListenerManager;%0Aimport com.intellij.refactoring.util.RefactoringUtil;%0Aimport org.jetbrains.annotations.NonNls;
 %0A%0Aimport
@@ -995,18 +995,65 @@
 Provider
- %7B
+, ProjectComponent %7B%0A  private Project myProject;
 %0A  priva
@@ -1164,16 +1164,191 @@
 t(this);
+%0A  private MyRefactoringListenerProvider myRefactoringListenerProvider = new MyRefactoringListenerProvider();%0A  @NonNls private static final String PROPERTY_GROUPS = %22groups%22;
 %0A%0A  publ
@@ -1376,16 +1376,33 @@
 rovider(
+Project project, 
 final Pa
@@ -1414,24 +1414,49 @@
  palette) %7B%0A
+    myProject = project;%0A
     myPalett
@@ -1574,16 +1574,100 @@
 ) %7B%0A    
+    fireGroupsChanged();%0A      %7D%0A    %7D);%0A%0A  %7D%0A%0A  private void fireGroupsChanged() %7B%0A
     myPr
@@ -1709,44 +1709,35 @@
 nge(
-%22groups%22, null, null);%0A      %7D%0A    %7D
+PROPERTY_GROUPS, null, null
 );%0A 
@@ -2312,11 +2312,1536 @@
 er);%0A  %7D
+%0A%0A  public void projectOpened() %7B%0A    RefactoringListenerManager.getInstance(myProject).addListenerProvider(myRefactoringListenerProvider);%0A  %7D%0A%0A  public void projectClosed() %7B%0A    RefactoringListenerManager.getInstance(myProject).removeListenerProvider(myRefactoringListenerProvider);%0A  %7D%0A%0A  @NonNls public String getComponentName() %7B%0A    return %22UIDesignerPaletteProvider%22;%0A  %7D%0A%0A  public void initComponent() %7B%0A  %7D%0A%0A  public void disposeComponent() %7B%0A  %7D%0A%0A  private class MyRefactoringListenerProvider implements RefactoringElementListenerProvider %7B%0A    public RefactoringElementListener getListener(PsiElement element) %7B%0A      if (element instanceof PsiClass) %7B%0A        PsiClass psiClass = (PsiClass) element;%0A        final ComponentItem item = myPalette.getItem(RefactoringUtil.getInnerClassNameForClassLoader(psiClass));%0A        if (item != null) %7B%0A          return new MyRefactoringElementListener(item);%0A        %7D%0A      %7D%0A      return null;%0A    %7D%0A%0A    private class MyRefactoringElementListener implements RefactoringElementListener %7B%0A      private ComponentItem myItem;%0A%0A      public MyRefactoringElementListener(final ComponentItem item) %7B%0A        myItem = item;%0A      %7D%0A%0A      public void elementMoved(PsiElement newElement) %7B%0A        elementRenamed(newElement);%0A      %7D%0A%0A      public void elementRenamed(PsiElement newElement) %7B%0A        PsiClass psiClass = (PsiClass) newElement;%0A        myItem.setClassName(RefactoringUtil.getInnerClassNameForClassLoader(psiClass));%0A        fireGroupsChanged();%0A      %7D%0A    %7D%0A  %7D
 %0A%7D%0A

update palette when class added to palette is moved/renamed (IDEADEV-4462)