/*
 * Copyright 2000-2009 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 org.jetbrains.idea.maven.indices;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import gnu.trove.THashMap;
import org.jetbrains.idea.maven.model.MavenId;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.Map;

public class MavenArtifactSearchDialog extends DialogWrapper {
  private MavenId myResult;

  private JTabbedPane myTabbedPane;
  private MavenArtifactSearchPanel myArtifactsPanel;
  private MavenArtifactSearchPanel myClassesPanel;

  private final Map<MavenArtifactSearchPanel, Boolean> myOkButtonStates = new THashMap<MavenArtifactSearchPanel, Boolean>();

  public static MavenId searchForClass(Project project, String className) {
    MavenArtifactSearchDialog d = new MavenArtifactSearchDialog(project, className, true);
    d.show();
    if (!d.isOK()) return null;

    return d.getResult();
  }

  public static MavenId searchForArtifact(Project project) {
    MavenArtifactSearchDialog d = new MavenArtifactSearchDialog(project, "", false);
    d.show();
    if (!d.isOK()) return null;

    return d.getResult();
  }

  private MavenArtifactSearchDialog(Project project, String initialText, boolean classMode) {
    super(project, true);

    initComponents(project, initialText, classMode);

    setTitle("Maven Artifact Search");
    updateOkButtonState();
    init();

    myArtifactsPanel.scheduleSearch();
    myClassesPanel.scheduleSearch();
  }

  private void initComponents(Project project, String initialText, boolean classMode) {
    myTabbedPane = new JTabbedPane(JTabbedPane.TOP);
    myTabbedPane.setFocusable(false);

    MavenArtifactSearchPanel.Listener listener = new MavenArtifactSearchPanel.Listener() {
      public void itemSelected() {
        clickDefaultButton();
      }

      public void canSelectStateChanged(MavenArtifactSearchPanel from, boolean canSelect) {
        myOkButtonStates.put(from, canSelect);
        updateOkButtonState();
      }
    };

    myArtifactsPanel = new MavenArtifactSearchPanel(project, !classMode ? initialText : "", false, listener, this);
    myClassesPanel = new MavenArtifactSearchPanel(project, classMode ? initialText : "", true, listener, this);

    myTabbedPane.addTab("Search for artifact", myArtifactsPanel);
    myTabbedPane.addTab("Search for class", myClassesPanel);
    myTabbedPane.setSelectedIndex(classMode ? 1 : 0);

    myTabbedPane.setMnemonicAt(0, KeyEvent.VK_A);
    myTabbedPane.setDisplayedMnemonicIndexAt(0, myTabbedPane.getTitleAt(0).indexOf("artifact"));
    myTabbedPane.setMnemonicAt(1, KeyEvent.VK_C);
    myTabbedPane.setDisplayedMnemonicIndexAt(1, myTabbedPane.getTitleAt(1).indexOf("class"));

    myTabbedPane.setPreferredSize(new Dimension(900, 600));

    myTabbedPane.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent e) {
        updateOkButtonState();
      }
    });

    updateOkButtonState();
  }

  private void updateOkButtonState() {
    Boolean canSelect = myOkButtonStates.get(myTabbedPane.getSelectedComponent());
    if (canSelect == null) canSelect = false;
    setOKActionEnabled(canSelect);
  }

  @Override
  protected Action getOKAction() {
    Action result = super.getOKAction();
    result.putValue(Action.NAME, "Add");
    return result;
  }

  protected JComponent createCenterPanel() {
    return myTabbedPane;
  }

  @Override
  public JComponent getPreferredFocusedComponent() {
    return myTabbedPane.getSelectedIndex() == 0
           ? myArtifactsPanel.getSearchField()
           : myClassesPanel.getSearchField();
  }

  @Override
  protected String getDimensionServiceKey() {
    return "Maven.ArtifactSearchDialog";
  }

  public MavenId getResult() {
    return myResult;
  }

  @Override
  protected void doOKAction() {
    MavenArtifactSearchPanel panel = myTabbedPane.getSelectedIndex() == 0 ? myArtifactsPanel : myClassesPanel;
    myResult = panel.getResult();
    super.doOKAction();
  }
}

@@ -724,24 +724,66 @@
 logWrapper;%0A
+import com.intellij.ui.TabbedPaneWrapper;%0A
 import gnu.t
@@ -797,16 +797,16 @@
 ashMap;%0A
-
 import o
@@ -969,40 +969,8 @@
 .*;%0A
-import java.awt.event.KeyEvent;%0A
 impo
@@ -1090,17 +1090,16 @@
 private 
-J
 TabbedPa
@@ -1100,16 +1100,23 @@
 bbedPane
+Wrapper
  myTabbe
@@ -2268,17 +2268,16 @@
 e = new 
-J
 TabbedPa
@@ -2282,62 +2282,23 @@
 Pane
-(JTabbedPane.TOP);%0A    myTabbedPane.setFocusable(false
+Wrapper(project
 );%0A%0A
@@ -3080,299 +3080,22 @@
 ane.
-setMnemonicAt(0, KeyEvent.VK_A);%0A    myTabbedPane.setDisplayedMnemonicIndexAt(0, myTabbedPane.getTitleAt(0).indexOf(%22artifact%22));%0A    myTabbedPane.setMnemonicAt(1, KeyEvent.VK_C);%0A    myTabbedPane.setDisplayedMnemonicIndexAt(1, myTabbedPane.getTitleAt(1).indexOf(%22class%22));%0A%0A    myTabbedPane
+getComponent()
 .set
@@ -3723,24 +3723,24 @@
 erPanel() %7B%0A
-
     return m
@@ -3746,24 +3746,39 @@
 myTabbedPane
+.getComponent()
 ;%0A  %7D%0A%0A  @Ov

add dependency dialog uses TabbedPaneWrapper instead of JTabbedPane