/*
 * Copyright 2000-2011 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.refactoring.turnRefsToSuper;

import com.intellij.openapi.help.HelpManager;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiClass;
import com.intellij.refactoring.HelpID;
import com.intellij.refactoring.JavaRefactoringSettings;
import com.intellij.refactoring.RefactoringBundle;
import com.intellij.refactoring.ui.ClassCellRenderer;
import com.intellij.refactoring.ui.RefactoringDialog;
import com.intellij.refactoring.util.RefactoringHierarchyUtil;
import com.intellij.ui.IdeBorderFactory;
import com.intellij.ui.ScrollPaneFactory;
import com.intellij.ui.components.JBList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

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

/**
 * @author dsl
 * Date: 06.06.2002
 */
public class TurnRefsToSuperDialog extends RefactoringDialog {
  @NotNull private final PsiClass mySubClass;
  private final List mySuperClasses;

  private JList mySuperClassesList = null;
  private final JCheckBox myCbReplaceInstanceOf = new JCheckBox();

  TurnRefsToSuperDialog(Project project, @NotNull PsiClass subClass, List superClasses) {
    super(project, true);

    mySubClass = subClass;
    mySuperClasses = superClasses;

    setTitle(TurnRefsToSuperHandler.REFACTORING_NAME);
    init();
  }

  @Nullable
  public PsiClass getSuperClass() {
    if(mySuperClassesList != null) {
      return (PsiClass) mySuperClassesList.getSelectedValue();
    }
    else {
      return null;
    }
  }

  public boolean isUseInInstanceOf() {
    return myCbReplaceInstanceOf.isSelected();
  }

  protected void doHelpAction() {
    HelpManager.getInstance().invokeHelp(HelpID.TURN_REFS_TO_SUPER);
  }

  public JComponent getPreferredFocusedComponent() {
    return mySuperClassesList;
  }


  protected JComponent createNorthPanel() {
    JPanel panel = new JPanel();
    panel.setLayout(new GridBagLayout());
    GridBagConstraints gbConstraints = new GridBagConstraints();

    gbConstraints.insets = new Insets(4, 8, 4, 8);
    gbConstraints.weighty = 1;
    gbConstraints.weightx = 1;
    gbConstraints.gridy = 0;
    gbConstraints.gridwidth = GridBagConstraints.REMAINDER;
    gbConstraints.fill = GridBagConstraints.BOTH;
    gbConstraints.anchor = GridBagConstraints.WEST;
    final JLabel classListLabel = new JLabel();
    panel.add(classListLabel, gbConstraints);

    mySuperClassesList = new JBList(mySuperClasses.toArray());
    mySuperClassesList.setCellRenderer(new ClassCellRenderer(mySuperClassesList.getCellRenderer()));
    mySuperClassesList.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    classListLabel.setText(RefactoringBundle.message("turnRefsToSuper.change.usages.to", mySubClass.getQualifiedName()));

    PsiClass nearestBase = RefactoringHierarchyUtil.getNearestBaseClass(mySubClass, true);
    int indexToSelect = 0;
    if(nearestBase != null) {
      indexToSelect = mySuperClasses.indexOf(nearestBase);
    }
    mySuperClassesList.setSelectedIndex(indexToSelect);
    gbConstraints.gridy++;
    panel.add(ScrollPaneFactory.createScrollPane(mySuperClassesList), gbConstraints);

    gbConstraints.gridy++;
    myCbReplaceInstanceOf.setText(RefactoringBundle.message("turnRefsToSuper.use.superclass.in.instanceof"));
    myCbReplaceInstanceOf.setSelected(false);
    myCbReplaceInstanceOf.setFocusable(false);
    panel.add(myCbReplaceInstanceOf, gbConstraints);

    return panel;
  }

  protected String getDimensionServiceKey() {
    return "#com.intellij.refactoring.turnRefsToSuper.TurnRefsToSuperDialog";
  }

  protected void doAction() {
    JavaRefactoringSettings.getInstance().TURN_REFS_TO_SUPER_PREVIEW_USAGES = isPreviewUsages();
    final PsiClass superClass = getSuperClass();
    if (superClass != null) {
      invokeRefactoring(new TurnRefsToSuperProcessor(getProject(), mySubClass, superClass, isUseInInstanceOf()));
    }
  }

  protected JComponent createCenterPanel() {
    return null;
  }
}

@@ -1093,49 +1093,8 @@
 il;%0A
-import com.intellij.ui.IdeBorderFactory;%0A
 impo
@@ -1173,16 +1173,52 @@
 JBList;%0A
+import com.intellij.util.ui.UIUtil;%0A
 import o
@@ -2430,13 +2430,14 @@
 eate
-North
+Center
 Pane
@@ -2503,395 +2503,65 @@
 new 
-GridBagLayout());%0A    GridBagConstraints gbConstraints = new GridBagConstraints();%0A%0A    gbConstraints.insets = new Insets(4, 8, 4, 8);%0A    gbConstraints.weighty = 1;%0A    gbConstraints.weightx = 1;%0A    gbConstraints.gridy = 0;%0A    gbConstraints.gridwidth = GridBagConstraints.REMAINDER;%0A    gbConstraints.fill = GridBagConstraints.BOTH;%0A    gbConstraints.anchor = GridBagConstraints.WEST;
+BorderLayout(UIUtil.DEFAULT_HGAP, UIUtil.DEFAULT_VGAP));%0A
 %0A   
@@ -2635,29 +2635,34 @@
 tLabel, 
-gbConstraints
+BorderLayout.NORTH
 );%0A%0A    
@@ -3315,35 +3315,8 @@
 t);%0A
-    gbConstraints.gridy++;%0A
     
@@ -3385,51 +3385,30 @@
 t), 
-gbConstraints);%0A%0A    gbConstraints.gridy++;
+BorderLayout.CENTER);%0A
 %0A   
@@ -3648,21 +3648,26 @@
 Of, 
-gbConstraints
+BorderLayout.SOUTH
 );%0A%0A
@@ -4170,38 +4170,37 @@
 Component create
-Center
+North
 Panel() %7B%0A    re

Use Interface Where Possible Dialog. Correct state with minimum size.