package com.intellij.uiDesigner.propertyInspector.properties;

import com.intellij.uiDesigner.radComponents.RadContainer;
import com.intellij.uiDesigner.radComponents.RadRootContainer;
import com.intellij.uiDesigner.radComponents.RadButtonGroup;
import com.intellij.uiDesigner.radComponents.RadComponent;
import com.intellij.uiDesigner.propertyInspector.Property;
import com.intellij.uiDesigner.propertyInspector.PropertyEditor;
import com.intellij.uiDesigner.propertyInspector.PropertyRenderer;
import com.intellij.uiDesigner.propertyInspector.editors.ComboBoxPropertyEditor;
import com.intellij.uiDesigner.propertyInspector.renderers.LabelPropertyRenderer;
import com.intellij.uiDesigner.UIDesignerBundle;
import com.intellij.ui.ColoredListCellRenderer;
import com.intellij.ui.SimpleTextAttributes;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

/**
 * @author yole
 */
public class ButtonGroupProperty extends Property<RadComponent, RadButtonGroup> {
  private LabelPropertyRenderer<RadButtonGroup> myRenderer = new LabelPropertyRenderer<RadButtonGroup>();
  private ComboBoxPropertyEditor<RadButtonGroup> myEditor = new MyPropertyEditor();

  public ButtonGroupProperty() {
    super(null, "Button Group");
  }

  public RadButtonGroup getValue(RadComponent component) {
    final RadRootContainer rootContainer = getRootContainer(component);
    return rootContainer == null ? null : rootContainer.findGroupForComponent(component);
  }

  @Nullable private static RadRootContainer getRootContainer(final RadComponent component) {
    RadContainer container = component.getParent();
    while(container != null && !(container instanceof RadRootContainer)) {
      container = container.getParent();
    }
    return (RadRootContainer) container;
  }

  protected void setValueImpl(RadComponent component, RadButtonGroup value) throws Exception {
    final RadRootContainer radRootContainer = getRootContainer(component);
    assert radRootContainer != null;
    radRootContainer.setGroupForComponent(component, value);
  }

  @NotNull public PropertyRenderer<RadButtonGroup> getRenderer() {
    return myRenderer;
  }

  public PropertyEditor<RadButtonGroup> getEditor() {
    return myEditor;
  }

  @Override public boolean isModified(final RadComponent component) {
    return getValue(component) != null;
  }

  @Override public void resetValue(RadComponent component) throws Exception {
    setValueImpl(component, null);
  }

  private static class MyPropertyEditor extends ComboBoxPropertyEditor<RadButtonGroup> {
    private RadRootContainer myRootContainer;
    private RadComponent myComponent;

    public MyPropertyEditor() {
      myCbx.setRenderer(new ColoredListCellRenderer() {
        protected void customizeCellRenderer(JList list, Object value, int index, boolean selected, boolean hasFocus) {
          RadButtonGroup group = (RadButtonGroup) value;
          if (value == null) {
            append(UIDesignerBundle.message("button.group.none"), SimpleTextAttributes.REGULAR_ATTRIBUTES);
          }
          else if (group == RadButtonGroup.NEW_GROUP) {
            append(UIDesignerBundle.message("button.group.new"), SimpleTextAttributes.REGULAR_ATTRIBUTES);
          }
          else {
            append(group.getName(), SimpleTextAttributes.REGULAR_ATTRIBUTES);
          }
        }
      });

      myCbx.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
          if (e.getStateChange() == ItemEvent.SELECTED && e.getItem() == RadButtonGroup.NEW_GROUP) {
            String newGroupName = myRootContainer.suggestGroupName();
            newGroupName = (String)JOptionPane.showInputDialog(myCbx,
                                                               UIDesignerBundle.message("button.group.name.prompt"),
                                                               UIDesignerBundle.message("button.group.name.title"),
                                                               JOptionPane.QUESTION_MESSAGE, null, null, newGroupName);
            if (newGroupName != null) {
              RadButtonGroup group = myRootContainer.createGroup(newGroupName);
              myRootContainer.setGroupForComponent(myComponent, group);
              updateModel();
            }
          }
        }
      });
    }


    public JComponent getComponent(RadComponent component, RadButtonGroup value, boolean inplace) {
      myComponent = component;
      myRootContainer = getRootContainer(myComponent);
      updateModel();
      return myCbx;
    }

    private void updateModel() {
      RadButtonGroup[] groups = myRootContainer.getAllGroups();
      RadButtonGroup[] allGroups = new RadButtonGroup[groups.length+2];
      System.arraycopy(groups, 0, allGroups, 1, groups.length);
      allGroups [allGroups.length-1] = RadButtonGroup.NEW_GROUP;
      myCbx.setModel(new DefaultComboBoxModel(allGroups));
      myCbx.setSelectedItem(myRootContainer.findGroupForComponent(myComponent));
    }
  }
}

@@ -82,106 +82,77 @@
 j.ui
-Designer.radComponents.RadContainer;%0Aimport com.intellij.uiDesigner.radComponents.RadRootContainer
+.ColoredListCellRenderer;%0Aimport com.intellij.ui.SimpleTextAttributes
 ;%0Aim
@@ -184,36 +184,23 @@
 ner.
-radComponents.RadButtonGroup
+FormEditingUtil
 ;%0Aim
@@ -232,34 +232,24 @@
 ner.
-radComponents.RadComponent
+UIDesignerBundle
 ;%0Aim
@@ -635,159 +635,158 @@
 ner.
-UIDesignerBundle;%0Aimport com.intellij.ui.ColoredListCellRenderer;%0Aimport com.intellij.ui.SimpleTextAttributes;%0Aimport org.jetbrains.annotations.NotNull
+radComponents.RadButtonGroup;%0Aimport com.intellij.uiDesigner.radComponents.RadComponent;%0Aimport com.intellij.uiDesigner.radComponents.RadRootContainer
 ;%0Aim
@@ -817,23 +817,22 @@
 ations.N
+otN
 ull
-able
 ;%0A%0Aimpor
@@ -874,24 +874,21 @@
 ent.Item
-Listener
+Event
 ;%0Aimport
@@ -907,21 +907,24 @@
 ent.Item
-Event
+Listener
 ;%0A%0A/**%0A 
@@ -1128,17 +1128,152 @@
 Group%3E()
-;
+ %7B%0A    @Override protected void customize(final RadButtonGroup value) %7B%0A      setText(value == null ? %22%22 : value.getName());%0A    %7D%0A  %7D;%0A
 %0A  priva
@@ -1515,35 +1515,36 @@
 rootContainer = 
-get
+(Rad
 RootContainer(co
@@ -1532,32 +1532,57 @@
 RadRootContainer
+) FormEditingUtil.getRoot
 (component);%0A   
@@ -1677,321 +1677,8 @@
  %7D%0A%0A
-  @Nullable private static RadRootContainer getRootContainer(final RadComponent component) %7B%0A    RadContainer container = component.getParent();%0A    while(container != null && !(container instanceof RadRootContainer)) %7B%0A      container = container.getParent();%0A    %7D%0A    return (RadRootContainer) container;%0A  %7D%0A%0A
   pr
@@ -1806,35 +1806,36 @@
 RootContainer = 
-get
+(Rad
 RootContainer(co
@@ -1831,16 +1831,41 @@
 ontainer
+) FormEditingUtil.getRoot
 (compone
@@ -4398,19 +4398,20 @@
 ainer = 
-get
+(Rad
 RootCont
@@ -4415,16 +4415,41 @@
 ontainer
+) FormEditingUtil.getRoot
 (myCompo

fix ButtonGroup renderer; remove duplicated code