/*
 * 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.intellij.openapi.roots.ui.configuration;

import com.intellij.openapi.Disposable;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ui.configuration.projectRoot.StructureConfigurableContext;
import com.intellij.openapi.util.Disposer;
import com.intellij.ui.ScrollPaneFactory;
import com.intellij.util.Alarm;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

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

/**
 * @author Konstantin Bulenkov
 */
public class ErrorPaneConfigurable extends JPanel implements Configurable, Disposable, ConfigurationErrors {
  private final Project myProject;
  private final StructureConfigurableContext myContext;
  private final Alarm myAlarm;
  private final ArrayList<ConfigurationError> myErrors = new ArrayList<ConfigurationError>();
  private final JTextPane myContent = new JTextPane();

  public ErrorPaneConfigurable(Project project, StructureConfigurableContext context) {
    super(new BorderLayout());
    myContent.setEditorKit(UIUtil.getHTMLEditorKit());
    myContent.setEditable(false);
    myContent.setBackground(UIUtil.getListBackground());
    final JScrollPane pane = ScrollPaneFactory.createScrollPane(myContent, true);
    pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    add(pane);
    myProject = project;
    myContext = context;
    myAlarm = new Alarm(this);
    project.getMessageBus().connect(this).subscribe(ConfigurationErrors.TOPIC, this);

    refresh();
  }

  public void refresh() {
    myAlarm.cancelAllRequests();
    myAlarm.addRequest(new Runnable() {
      @Override
      public void run() {
        String html = "<html>" +
                      "<header><style type='text/css'>" +
                      "body {" +
                      "  color: #333333;" +
                      "  font-family: '" + UIUtil.getLabelFont().getName() + "';" +
                      "  font-size: " + UIUtil.getLabelFont().getSize() + ";" +
                      "}" +
                      "a {" +
                      " text-decoration: none;" +
                      "}" +
                      "</style>" +
                      "</headear>" +
                      "<body>";
        int i = 0;
        html += "<ol>";
        for (ConfigurationError error : myErrors) {
          i++;
          String description = error.getDescription();
          if (description.startsWith("Module '")) {
            final int start = 8;
            final int end = description.indexOf("'", 9);
            final String moduleName = description.substring(start, end);
            description = "Module <a href='module://" + moduleName + "'>" + moduleName + "</a> " + description.substring(end + 1);
          }
          html+= "<li>" + description + "</li>";
        }
        html += "</ol></body></html>";
        myContent.setText(html);
      }
    }, 100);
  }

  @Nls
  @Override
  public String getDisplayName() {
    return "Problems";
  }

  @Nullable
  @Override
  public String getHelpTopic() {
    return null;
  }

  @Nullable
  @Override
  public JComponent createComponent() {
    return this;
  }

  @Override
  public boolean isModified() {
    return false;
  }

  @Override
  public void apply() throws ConfigurationException {

  }

  @Override
  public void reset() {

  }

  @Override
  public void disposeUIResources() {
    Disposer.dispose(this);
  }

  @Override
  public void dispose() {
  }

  @Override
  public void addError(@NotNull ConfigurationError error) {
    myErrors.add(error);
    refresh();
  }

  @Override
  public void removeError(@NotNull ConfigurationError error) {
    myErrors.remove(error);
    refresh();
  }
}

@@ -2833,32 +2833,196 @@
           %22%7D%22 +%0A
+                      %22li %7B%22 +%0A                      %22  margin-bottom: 3;%22 +%0A                      %22%7D%22 +%0A                      %22ol %7B%22 +%0A                      %22%7D%22 +%0A
                 
@@ -3031,24 +3031,24 @@
     %22a %7B%22 +%0A
-
             
@@ -3378,24 +3378,199 @@
 cription();%0A
+          if (description.startsWith(%22%3Chtml%3E%22) && description.endsWith(%22%3C/html%3E%22)) %7B%0A            description = description.substring(6, description.length() - 7);%0A          %7D%0A
           if
@@ -3772,16 +3772,16 @@
 , end);%0A
-
         
@@ -3907,32 +3907,145 @@
 1);%0A          %7D%0A
+          if (error.canBeFixed()) %7B%0A            description += %22 %3Ca href='fix://%22 + i + %22'%3EFix%3C/a%3E%22;%0A          %7D%0A
           html+=

add css styles to Error Pane