/*
 * Copyright 2000-2017 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.openapi.updateSettings.impl;

import com.intellij.ide.plugins.IdeaPluginDescriptor;
import com.intellij.ide.plugins.PluginHeaderPanel;
import com.intellij.ide.plugins.PluginManager;
import com.intellij.ide.plugins.PluginManagerMain;
import com.intellij.openapi.extensions.PluginId;
import com.intellij.openapi.ui.Splitter;
import com.intellij.openapi.util.Comparing;
import com.intellij.ui.ColoredTableCellRenderer;
import com.intellij.ui.OrderPanel;
import com.intellij.ui.ScrollPaneFactory;
import com.intellij.ui.SimpleTextAttributes;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.ui.UIUtil;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.util.List;
import java.util.Set;

/**
 * @author anna
 * Date: 04-Dec-2007
 */
public class DetectedPluginsPanel extends OrderPanel<PluginDownloader> {
  private final List<Listener> myListeners = ContainerUtil.createLockFreeCopyOnWriteList();

  private JEditorPane myDescriptionPanel = new JEditorPane();
  private PluginHeaderPanel myHeader;

  public DetectedPluginsPanel() {
    super(PluginDownloader.class);
    JTable entryTable = getEntryTable();
    myHeader = new PluginHeaderPanel(null);
    entryTable.setTableHeader(null);
    entryTable.setDefaultRenderer(PluginDownloader.class, new ColoredTableCellRenderer() {
      protected void customizeCellRenderer(JTable table,
                                           Object value,
                                           boolean selected,
                                           boolean hasFocus,
                                           int row,
                                           int column) {
        setBorder(null);
        PluginDownloader downloader = (PluginDownloader)value;
        if (downloader != null) {
          String pluginName = downloader.getPluginName();
          append(pluginName, SimpleTextAttributes.REGULAR_ATTRIBUTES);
          IdeaPluginDescriptor ideaPluginDescriptor = PluginManager.getPlugin(PluginId.getId(downloader.getPluginId()));
          if (ideaPluginDescriptor != null) {
            String oldPluginName = ideaPluginDescriptor.getName();
            if (!Comparing.strEqual(pluginName, oldPluginName)) {
              append(" - " + oldPluginName, SimpleTextAttributes.REGULAR_ATTRIBUTES);
            }
          }
          String loadedVersion = downloader.getPluginVersion();
          if (loadedVersion != null || (ideaPluginDescriptor != null && ideaPluginDescriptor.getVersion() != null)) {
            String installedVersion = ideaPluginDescriptor != null && ideaPluginDescriptor.getVersion() != null
                                            ? ideaPluginDescriptor.getVersion() + (loadedVersion != null ? " " + UIUtil.rightArrow() + " " : "")
                                            : "";
            String availableVersion = loadedVersion != null ? loadedVersion : "";
            append(" " + installedVersion + availableVersion, SimpleTextAttributes.GRAYED_SMALL_ATTRIBUTES);
          }
        }
      }
    });
    entryTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
      public void valueChanged(ListSelectionEvent e) {
        int selectedRow = entryTable.getSelectedRow();
        if (selectedRow != -1) {
          PluginDownloader selection = getValueAt(selectedRow);
          IdeaPluginDescriptor descriptor = selection.getDescriptor();
          if (descriptor != null) {
            PluginManagerMain.pluginInfoUpdate(descriptor, null, myDescriptionPanel, myHeader);
          }
        }
      }
    });
    setCheckboxColumnName("");
    myDescriptionPanel.setPreferredSize(new Dimension(400, -1));
    myDescriptionPanel.setEditable(false);
    myDescriptionPanel.setContentType(UIUtil.HTML_MIME);
    myDescriptionPanel.addHyperlinkListener(new PluginManagerMain.MyHyperlinkListener());
    removeAll();

    Splitter splitter = new Splitter(false);
    splitter.setFirstComponent(ScrollPaneFactory.createScrollPane(entryTable));
    splitter.setSecondComponent(ScrollPaneFactory.createScrollPane(myDescriptionPanel));
    add(splitter, BorderLayout.CENTER);
  }

  public String getCheckboxColumnName() {
    return "";
  }

  public boolean isCheckable(PluginDownloader downloader) {
    return true;
  }

  public boolean isChecked(PluginDownloader downloader) {
    return !getSkippedPlugins().contains(downloader.getPluginId());
  }

  public void setChecked(PluginDownloader downloader, boolean checked) {
    if (checked) {
      getSkippedPlugins().remove(downloader.getPluginId());
    }
    else {
      getSkippedPlugins().add(downloader.getPluginId());
    }
    for (Listener listener : myListeners) {
      listener.stateChanged();
    }
  }

  protected Set<String> getSkippedPlugins() {
    return UpdateChecker.getDisabledToUpdatePlugins();
  }

  public void addStateListener(Listener l) {
    myListeners.add(l);
  }

  public interface Listener {
    void stateChanged();
  }
}
@@ -897,16 +897,64 @@
 uginId;%0A
+import com.intellij.openapi.ui.OnePixelDivider;%0A
 import c
@@ -1057,109 +1057,60 @@
 .ui.
-ColoredTableCellRenderer;%0Aimport com.intellij.ui.OrderPanel;%0Aimport com.intellij.ui.ScrollPaneFactory
+*;%0Aimport com.intellij.util.containers.ContainerUtil
 ;%0Aim
@@ -1132,30 +1132,19 @@
 ij.u
-i.SimpleTextAttributes
+til.ui.JBUI
 ;%0Aim
@@ -1170,67 +1170,58 @@
 til.
-containers.ContainerUtil;%0Aimport com.intellij.util.ui.UIUti
+ui.UIUtil;%0Aimport org.jetbrains.annotations.NotNul
 l;%0A%0A
@@ -4590,16 +4590,24 @@
 r = new 
+OnePixel
 Splitter
@@ -4650,38 +4650,16 @@
 ent(
-ScrollPaneFactory.createScroll
+wrapWith
 Pane
@@ -4669,16 +4669,22 @@
 tryTable
+, 1, 0
 ));%0A    
@@ -4715,38 +4715,16 @@
 ent(
-ScrollPaneFactory.createScroll
+wrapWith
 Pane
@@ -4742,16 +4742,22 @@
 ionPanel
+, 0, 1
 ));%0A    
@@ -4789,32 +4789,308 @@
 t.CENTER);%0A  %7D%0A%0A
+  @NotNull%0A  private static JScrollPane wrapWithPane(@NotNull JComponent c, int left, int right) %7B%0A    JScrollPane pane = ScrollPaneFactory.createScrollPane(c);%0A    pane.setBorder(JBUI.Borders.customLine(OnePixelDivider.BACKGROUND, 1, left, 1, right));%0A    return pane;%0A  %7D %0A%0A
   public String 

use one pixel divider