/*
 * Copyright 2000-2014 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.jetbrains.python.documentation;

import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.ui.components.JBList;

import javax.swing.*;
import javax.swing.text.BadLocationException;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author yole
 */
public class PythonDocumentationEntryEditor extends DialogWrapper {
  private JPanel myPanel;
  private JTextField myNameField;
  private JTextField myURLPatternTextField;
  private JButton myInsertButton;
  private JBList myMacroList;

  public PythonDocumentationEntryEditor(Component parent) {
    super(parent, true);
    init();
    setTitle("Edit Documentation URL");
    myInsertButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        Object text = myMacroList.getSelectedValue();
        if (text != null) {
          String macroName = text.toString();
          int pos = macroName.indexOf(" - ");
          if (pos >= 0) {
            macroName = macroName.substring(0, pos);
          }
          try {
            myURLPatternTextField.getDocument().insertString(myURLPatternTextField.getCaretPosition(), macroName, null);
          }
          catch (BadLocationException ignored) {
          }
        }
      }
    });
  }

  @Override
  protected JComponent createCenterPanel() {
    return myPanel;
  }

  public PythonDocumentationMap.Entry getEntry() {
    return new PythonDocumentationMap.Entry(myNameField.getText(), myURLPatternTextField.getText());
  }

  public void setEntry(PythonDocumentationMap.Entry entry) {
    myNameField.setText(entry.getPrefix());
    myURLPatternTextField.setText(entry.getUrlPattern());
  }

  @Override
  public JComponent getPreferredFocusedComponent() {
    return myNameField;
  }

  @Override
  protected String getDimensionServiceKey() {
    return getClass().getName();
  }
}

@@ -746,24 +746,117 @@
 ax.swing.*;%0A
+import javax.swing.event.ListSelectionEvent;%0Aimport javax.swing.event.ListSelectionListener;%0A
 import javax
@@ -2008,16 +2008,347 @@
     %7D);%0A
+    myMacroList.addListSelectionListener(new ListSelectionListener() %7B%0A      @Override%0A      public void valueChanged(ListSelectionEvent e) %7B%0A        updateButtonEnabled();%0A      %7D%0A    %7D);%0A    updateButtonEnabled();%0A  %7D%0A%0A  private void updateButtonEnabled() %7B%0A    myInsertButton.setEnabled(myMacroList.getSelectedValue() != null);%0A
   %7D%0A%0A  @

Disable button when no selection (PY-7369)