/*
 * 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.application.options.colors;

import com.intellij.codeInsight.daemon.impl.SeverityRegistrar;
import com.intellij.codeInsight.daemon.impl.TrafficLightRenderer;
import com.intellij.openapi.application.ApplicationNamesInfo;
import com.intellij.openapi.editor.*;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.editor.ex.EditorMarkupModel;
import com.intellij.util.EventDispatcher;
import consulo.disposer.Disposable;
import consulo.disposer.Disposer;
import org.jetbrains.annotations.Nls;
import javax.annotation.Nonnull;

import java.awt.*;

public class FontEditorPreview implements PreviewPanel{
  private final EditorEx myEditor;

  private final ColorAndFontOptions myOptions;

  private final EventDispatcher<ColorAndFontSettingsListener> myDispatcher = EventDispatcher.create(ColorAndFontSettingsListener.class);

  FontEditorPreview(final ColorAndFontOptions options, boolean editable) {
    myOptions = options;

    @Nls String text = getIDEDemoText();

    myEditor = (EditorEx)createPreviewEditor(text, 10, 3, -1, myOptions, editable);

    installTrafficLights(myEditor);
  }

  public static String getIDEDemoText() {
    return
            ApplicationNamesInfo.getInstance().getFullProductName() +
            " is a full-featured IDE\n" +
            "with a high level of usability and outstanding\n" +
            "advanced code editing and refactoring support.\n" +
            "\n" +
            "abcdefghijklmnopqrstuvwxyz 0123456789 (){}[]\n" +
            "ABCDEFGHIJKLMNOPQRSTUVWXYZ +-*/= .,;:!? #&$%@|^\n" +
            // Create empty lines in order to make the gutter wide enough to display two-digits line numbers (other previews use long text
            // and we don't want different gutter widths on color pages switching).
            "\n" +
            "\n" +
            "\n";
  }

  static void installTrafficLights(@Nonnull EditorEx editor) {
    TrafficLightRenderer renderer = new TrafficLightRenderer(null, null) {
      @Nonnull
      @Override
      protected DaemonCodeAnalyzerStatus getDaemonCodeAnalyzerStatus(@Nonnull SeverityRegistrar severityRegistrar) {
        DaemonCodeAnalyzerStatus status = new DaemonCodeAnalyzerStatus();
        status.errorAnalyzingFinished = true;
        status.errorCount = new int[]{1, 2};
        return status;
      }
    };
    Disposer.register((Disposable)editor.getCaretModel(), renderer);
    ((EditorMarkupModel)editor.getMarkupModel()).setErrorStripeRenderer(renderer);
    ((EditorMarkupModel)editor.getMarkupModel()).setErrorStripeVisible(true);
  }

  static Editor createPreviewEditor(String text, int column, int line, int selectedLine, ColorAndFontOptions options, boolean editable) {
    EditorFactory editorFactory = EditorFactory.getInstance();
    Document editorDocument = editorFactory.createDocument(text);
    EditorEx editor = (EditorEx) (editable ? editorFactory.createEditor(editorDocument) : editorFactory.createViewer(editorDocument));
    editor.setColorsScheme(options.getSelectedScheme());
    EditorSettings settings = editor.getSettings();
    settings.setLineNumbersShown(true);
    settings.setWhitespacesShown(true);
    settings.setLineMarkerAreaShown(false);
    settings.setIndentGuidesShown(false);
    settings.setFoldingOutlineShown(false);
    settings.setAdditionalColumnsCount(0);
    settings.setAdditionalLinesCount(0);
    settings.setRightMarginShown(true);
    settings.setRightMargin(60);

    LogicalPosition pos = new LogicalPosition(line, column);
    editor.getCaretModel().moveToLogicalPosition(pos);
    if (selectedLine >= 0) {
      editor.getSelectionModel().setSelection(editorDocument.getLineStartOffset(selectedLine),
                                              editorDocument.getLineEndOffset(selectedLine));
    }

    return editor;
  }

  @Override
  public Component getPanel() {
    return myEditor.getComponent();
  }

  @Override
  public void updateView() {
    EditorColorsScheme scheme = updateOptionsScheme(myOptions.getSelectedScheme());

    myEditor.setColorsScheme(scheme);
    myEditor.reinitSettings();

  }

  protected EditorColorsScheme updateOptionsScheme(EditorColorsScheme selectedScheme) {
    return selectedScheme;
  }

  @Override
  public void blinkSelectedHighlightType(Object description) {
  }

  @Override
  public void addListener(@Nonnull final ColorAndFontSettingsListener listener) {
    myDispatcher.addListener(listener);
  }

  @Override
  public void disposeUIResources() {
    EditorFactory editorFactory = EditorFactory.getInstance();
    editorFactory.releaseEditor(myEditor);
  }
}

@@ -669,115 +669,22 @@
 lij.
+i
 co
-deInsight.daemon.impl.SeverityRegistrar;%0Aimport com.intellij.codeInsight.daemon.impl.TrafficLightRenderer
+ns.AllIcons
 ;%0Aim
@@ -972,96 +972,89 @@
 lij.
-util.EventDispatcher;%0Aimport consulo.disposer.Disposable;%0Aimport consulo.disposer.Dispos
+openapi.editor.markup.ErrorStripeRenderer;%0Aimport com.intellij.util.EventDispatch
 er;%0A
@@ -1091,16 +1091,17 @@
 ns.Nls;%0A
+%0A
 import j
@@ -1125,16 +1125,37 @@
 onnull;%0A
+import javax.swing.*;
 %0Aimport 
@@ -2512,44 +2512,84 @@
     
-TrafficLightRenderer r
+((EditorMarkupModel)editor.getMarkupModel()).setErrorStripeR
 enderer
- = 
+(
 new 
+Dumb
 Traf
@@ -2609,602 +2609,478 @@
 rer(
-null, null) %7B%0A      @Nonnull%0A      @Override%0A      protected DaemonCodeAnalyzerStatus getDaemonCodeAnalyzerStatus(@Nonnull SeverityRegistrar severityRegistrar) %7B%0A        DaemonCodeAnalyzerStatus status = new DaemonCodeAnalyzerStatus();%0A        status.errorAnalyzingFinished = true;%0A        status.errorCount = new int%5B%5D%7B1, 2%7D;%0A        return status
+));%0A    ((EditorMarkupModel)editor.getMarkupModel()).setErrorStripeVisible(true);%0A  %7D%0A%0A  private static class DumbTrafficLightRenderer implements ErrorStripeRenderer %7B%0A    @Override%0A    public void paint(@Nonnull Component c, Graphics g, @Nonnull Rectangle r) %7B%0A      Icon icon = AllIcons.General.InspectionsOK;%0A      icon.paintIcon(c, g, r.x, r.y)
 ;%0A    
-  
 %7D%0A
-    %7D;%0A    Disposer.register((Disposable)editor.getCaretModel(), renderer);%0A    ((EditorMarkupModel)editor.getMarkupModel()).setErrorStripeRenderer(renderer);%0A    ((EditorMarkupModel)editor.getMarkupModel()).setErrorStripeVisible(true);
+%0A    @Override%0A    public int getSquareSize() %7B%0A      return AllIcons.General.InspectionsOK.getIconHeight();%0A    %7D
 %0A  %7D

fix bug npe with opening font dialog