better fix for IDEA-120011 quick documentation looses styling

This commit is contained in:
Gregory.Shrago
2014-04-02 21:53:15 +04:00
parent b45b456d03
commit fd0f629e48
3 changed files with 50 additions and 44 deletions
@@ -24,6 +24,7 @@ import com.intellij.openapi.util.registry.Registry;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.ui.ColorUtil;
import com.intellij.util.containers.hash.HashMap;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;
import sun.awt.AppContext;
@@ -38,9 +39,11 @@ import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;
import java.awt.*;
import java.io.*;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.List;
import java.util.Properties;
@@ -102,7 +105,7 @@ public class DarculaLaf extends BasicLookAndFeel {
LafManagerImpl.initInputMapDefaults(defaults);
initIdeaDefaults(defaults);
patchStyledEditorKit();
patchStyledEditorKit(defaults);
patchComboBox(metalDefaults, defaults);
defaults.remove("Spinner.arrowButtonBorder");
defaults.put("Spinner.arrowButtonSize", new Dimension(16, 5));
@@ -140,20 +143,16 @@ public class DarculaLaf extends BasicLookAndFeel {
}
@SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
private void patchStyledEditorKit() {
private void patchStyledEditorKit(UIDefaults defaults) {
URL url = getClass().getResource(getPrefix() + ".css");
StyleSheet styleSheet = UIUtil.loadStyleSheet(url);
defaults.put("StyledEditorKit.JBDefaultStyle", styleSheet);
try {
InputStream is = getClass().getResourceAsStream(getPrefix() + ".css");
if (is != null) {
StyleSheet defaultStyles = new StyleSheet();
Reader r = new BufferedReader(new InputStreamReader(is, "UTF-8"));
defaultStyles.loadRules(r, null);
r.close();
final Field keyField = HTMLEditorKit.class.getDeclaredField("DEFAULT_STYLES_KEY");
keyField.setAccessible(true);
final Object key = keyField.get(null);
AppContext.getAppContext().put(key, defaultStyles);
}
} catch (Exception e) {
Field keyField = HTMLEditorKit.class.getDeclaredField("DEFAULT_STYLES_KEY");
keyField.setAccessible(true);
AppContext.getAppContext().put(keyField.get(null), UIUtil.loadStyleSheet(url));
}
catch (Exception e) {
log(e);
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2013 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.
@@ -44,7 +44,6 @@ import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.URL;
@@ -205,8 +204,6 @@ public class TipUIUtil {
public static JEditorPane createTipBrowser() {
JEditorPane browser = new JEditorPane();
browser.setEditable(false);
HTMLEditorKit editorKit = new HTMLEditorKit();
browser.setEditorKit(editorKit);
browser.setBackground(UIUtil.getTextFieldBackground());
browser.addHyperlinkListener(
new HyperlinkListener() {
@@ -217,16 +214,15 @@ public class TipUIUtil {
}
}
);
try {
// set default CSS for plugin tips
URL resource = ResourceUtil.getResource(TipUIUtil.class, "/tips/css/", UIUtil.isUnderDarcula() ? "tips_darcula.css" : "tips.css");
StyleSheet sheet = new StyleSheet();
sheet.loadRules(new InputStreamReader(resource.openStream()), resource);
editorKit.setStyleSheet(sheet);
}
catch (IOException ignored) {
}
URL resource = ResourceUtil.getResource(TipUIUtil.class, "/tips/css/", UIUtil.isUnderDarcula() ? "tips_darcula.css" : "tips.css");
final StyleSheet styleSheet = UIUtil.loadStyleSheet(resource);
HTMLEditorKit kit = new HTMLEditorKit() {
@Override
public StyleSheet getStyleSheet() {
return styleSheet != null ? styleSheet : super.getStyleSheet();
}
};
browser.setEditorKit(kit);
return browser;
}
}
@@ -22,6 +22,7 @@ import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.*;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.CharsetToolkit;
import com.intellij.ui.*;
import com.intellij.util.*;
import com.intellij.util.containers.ContainerUtil;
@@ -58,7 +59,9 @@ import java.awt.image.ImageObserver;
import java.awt.image.PixelGrabber;
import java.beans.PropertyChangeListener;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
@@ -1919,6 +1922,20 @@ public class UIUtil {
return getBorderColor();
}
@Nullable
public static StyleSheet loadStyleSheet(@Nullable URL url) {
if (url == null) return null;
try {
StyleSheet styleSheet = new StyleSheet();
styleSheet.loadRules(new InputStreamReader(url.openStream(), CharsetToolkit.UTF8), url);
return styleSheet;
}
catch (IOException e) {
LOG.warn(url + " loading failed", e);
return null;
}
}
public static HTMLEditorKit getHTMLEditorKit() {
Font font = getLabelFont();
@NonNls String family = font != null ? font.getFamily() : "Tahoma";
@@ -1926,22 +1943,16 @@ public class UIUtil {
final String customCss = String.format("body, div, p { font-family: %s; font-size: %s; } p { margin-top: 0; }", family, size);
if (isUnderDarcula()) {
final HTMLEditorKit kit = new HTMLEditorKit();
kit.getStyleSheet().addRule(customCss);
return kit;
} else {
final StyleSheet style = new StyleSheet();
style.addStyleSheet(DEFAULT_HTML_KIT_CSS);
style.addRule(customCss);
final StyleSheet style = new StyleSheet();
style.addStyleSheet(isUnderDarcula() ? (StyleSheet)UIManager.getDefaults().get("StyledEditorKit.JBDefaultStyle") : DEFAULT_HTML_KIT_CSS);
style.addRule(customCss);
return new HTMLEditorKit() {
@Override
public StyleSheet getStyleSheet() {
return style;
}
};
}
return new HTMLEditorKit() {
@Override
public StyleSheet getStyleSheet() {
return style;
}
};
}
public static void removeScrollBorder(final Component c) {