javax.swing.text.html.StyleSheet Java Examples

The following examples show how to use javax.swing.text.html.StyleSheet. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HelpForm.java    From WePush with MIT License 6 votes vote down vote up
/**
 * 初始化使用帮助tab
 */
public static void init() {
    helpForm = getInstance();

    try {
        helpForm.getHelpTextPane().setEditable(false);
        HTMLEditorKit kit = new HTMLEditorKit();
        helpForm.getHelpTextPane().setEditorKit(kit);
        StyleSheet styleSheet = kit.getStyleSheet();
        styleSheet.addRule("h2{color:#FBC87A;}");
        helpForm.getHelpTextPane().setContentType("text/html; charset=utf-8");
        helpForm.getHelpTextPane().setPage(MainWindow.class.getResource("/page/help.html"));
    } catch (IOException e) {
        e.printStackTrace();
    }

}
 
Example #2
Source File: BrokenPlatformCustomizer.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void postInitComponents () {
    this.jLabel2.setVisible(false);
    this.platformHome.setVisible(false);
    final Collection installFolders = platform.getInstallFolderURLs();
    if (platform.getInstallFolders().isEmpty() && installFolders.size() > 0) {
        this.jLabel2.setVisible(true);
        this.platformHome.setVisible(true);
        this.platformHome.setForeground(new Color (164,0,0));
        this.platformHome.setText (Utilities.toFile(URI.create(((URL)installFolders.iterator().next()).toExternalForm())).getAbsolutePath());
    }
    HTMLEditorKit htmlkit = new HTMLEditorKit();                
    StyleSheet css = htmlkit.getStyleSheet();
    if (css.getStyleSheets() == null) {
        StyleSheet css2 = new StyleSheet();
        Font f = jLabel2.getFont();
        css2.addRule(new StringBuffer("body { font-size: ").append(f.getSize()) // NOI18N
            .append("; font-family: ").append(f.getName()).append("; }").toString()); // NOI18N
        css2.addStyleSheet(css);
        htmlkit.setStyleSheet(css2);
    }
    jTextPane1.setEditorKit(htmlkit);        
    jTextPane1.setText(NbBundle.getMessage(BrokenPlatformCustomizer.class,"MSG_BrokenProject"));
}
 
Example #3
Source File: OperatorDocumentationBrowser.java    From rapidminer-studio with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * This method creates and returns a stylesheet that makes the documentation look as it's
 * supposed to look.
 *
 * @return the stylesheet
 */
private StyleSheet createStyleSheet(StyleSheet css) {
	css.addRule("body {font-family: Open Sans; font-size: 10px;}");
	css.addRule("p {font-size:10px; font-family: Open Sans; margin-top: 0px; padding-top: 0px;}");
	css.addRule("ul li {padding-bottom:1ex; font-family: Open Sans; font-size:10px; list-style-type: circle;}");
	css.addRule("h2 {font-size:14px; font-family: Open Sans; margin-bottom: 0px; margin-top: 0px;}");
	css.addRule("h4 {color: #000000; font-size:10px; font-family: Open Sans; font-weight: bold; margin-bottom: 5px;}");
	css.addRule("h5 {color: #3399FF; font-size:11px; font-family: Open Sans;}");
	css.addRule("h5 img {margin-right:8px; font-family: Open Sans;}");
	css.addRule(
			".parametersHeading {color: #000000; font-size:10px; font-family: Open Sans; font-weight: bold; margin-bottom: 0px;}");
	css.addRule(".parametersTable {cellspacing: 0px; border: 0;}");
	css.addRule(".typeIcon {height: 10px; width: 10px;}");
	css.addRule("td {vertical-align: top; font-family: Open Sans;}");
	css.addRule(".lilIcon {padding: 2px 4px 2px 0px;}");
	css.addRule("td {font-size: 10px; font-family: Open Sans;}");
	css.addRule(".packageName {color: #777777; font-size:10px; font-family: Open Sans; font-weight: normal;}");
	css.addRule(".parameterDetails {color: #777777; font-size:9px; font-family: Open Sans;}");
	css.addRule(".parameterDetailsCell{margin-bottom: 4px; padding-bottom: 4px;}");
	css.addRule(".tutorialProcessLink {margin-top: 6px; margin-bottom: 5px;}");
	css.addRule("hr {border: 0;height: 1px;}");
	css.addRule("a {color:" + SwingTools.getColorHexValue(Colors.LINKBUTTON_LOCAL) + "}");
	css.addRule("table {align:left;}");
	css.addRule(".tags {font-size: 9px; color: #777777;}");
	return css;
}
 
Example #4
Source File: HtmlRichTextConverter.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
private boolean isInvisible( final javax.swing.text.Element textElement ) {
  final HTMLDocument htmlDocument = (HTMLDocument) textElement.getDocument();
  final StyleSheet sheet = htmlDocument.getStyleSheet();
  final AttributeSet attr = computeStyle( textElement, sheet );
  final Object o = attr.getAttribute( CSS.Attribute.DISPLAY );
  if ( "none".equals( String.valueOf( o ) ) ) {
    return true;
  }
  final Object tag = findTag( textElement.getAttributes() );
  if ( tag == HTML.Tag.COMMENT ) {
    return true;
  }
  if ( tag == HTML.Tag.SCRIPT ) {
    return true;
  }
  if ( tag == HTML.Tag.HEAD ) {
    return true;
  }
  return false;
}
 
Example #5
Source File: DarculaLaf.java    From Darcula with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
private static void patchStyledEditorKit() {
  try {
    StyleSheet defaultStyles = new StyleSheet();
    InputStream is = DarculaLaf.class.getResourceAsStream("darcula.css");
    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 (Throwable e) {
    log(e);
  }
}
 
Example #6
Source File: SingleResultOverview.java    From rapidminer-studio with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Creates the main text representation of this result.
 *
 * @param text
 * @return
 */
private Component makeMainLabel(String text) {
	JEditorPane label = new ExtendedHTMLJEditorPane("text/html", text);
	StyleSheet css = ((HTMLEditorKit) label.getEditorKit()).getStyleSheet();
	css.addRule("body {font-family:Sans;font-size:11pt}");
	css.addRule("h3 {margin:0; padding:0}");
	css.addRule("h4 {margin-bottom:0; margin-top:1ex; padding:0}");
	css.addRule("p  {margin-top:0; margin-bottom:1ex; padding:0}");
	css.addRule("ul {margin-top:0; margin-bottom:1ex; list-style-image: url("
			+ Tools.getResource("icons/help/circle.png") + ")}");
	css.addRule("ul li {padding-bottom: 2px}");
	css.addRule("li.outPorts {padding-bottom: 0px}");
	css.addRule("ul li ul {margin-top:0; margin-bottom:1ex; list-style-image: url("
			+ Tools.getResource("icons/help/line.png") + ")");
	css.addRule("li ul li {padding-bottom:0}");

	label.setEditable(false);
	label.setBackground(Colors.WHITE);

	JScrollPane pane = new JScrollPane(label);
	pane.setBackground(Colors.WHITE);
	pane.setBorder(null);
	return pane;
}
 
Example #7
Source File: ExtendedHTMLEditorKit.java    From rapidminer-studio with GNU Affero General Public License v3.0 6 votes vote down vote up
public ExtendedHTMLEditorKit() {
	styleSheet = new StyleSheet();
	try {
		InputStream is = HTMLEditorKit.class.getResourceAsStream(DEFAULT_CSS);
		Reader r = new BufferedReader(new InputStreamReader(is, "ISO-8859-1"));
		styleSheet.loadRules(r, null);
		r.close();
	} catch (Exception e) {
		// LogService.getRoot().log(Level.WARNING, "Cannot install stylesheet: "+e, e);
		LogService.getRoot().log(
				Level.WARNING,
				I18N.getMessage(LogService.getRoot().getResourceBundle(),
						"com.rapidminer.gui.tools.ExtendedHTMLEditorKit.installing_stylesheet_error", e), e);
		// on error we simply have no styles... the html
		// will look mighty wrong but still function.
	}
}
 
Example #8
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
private MainPanel() {
  super(new BorderLayout());
  Font font = makeFont(getClass().getResource("Burnstown Dam.ttf")).orElseGet(this::getFont);
  GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(font);

  JPanel p = new JPanel(new GridLayout(0, 1));
  JLabel label = new JLabel();
  label.setFont(font.deriveFont(Font.PLAIN, 24));
  label.setText("1: setFont(font.deriveFont(Font.PLAIN, 24))");
  p.add(label);
  JLabel label2 = new JLabel();
  label2.setText("<html><font size='+3' face='Burnstown Dam'>2: html, font, size,+3</font></html>");
  p.add(label2);

  StyleSheet styleSheet = new StyleSheet();
  styleSheet.addRule("body {font-size: 24pt; font-family: Burnstown Dam;}");
  // styleSheet.addRule(".highlight {color: red; background: green; font-family: Burnstown Dam.ttf; }");
  HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
  htmlEditorKit.setStyleSheet(styleSheet);
  JEditorPane editor = new JEditorPane();
  editor.setEditorKit(htmlEditorKit);
  editor.setText(makeTestHtml());
  JEditorPane editor2 = new JEditorPane();
  editor2.setFont(font);
  editor2.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
  editor2.setText("4: putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE)");
  JPanel p2 = new JPanel(new GridLayout(0, 1));
  p2.add(new JScrollPane(editor));
  p2.add(new JScrollPane(editor2));

  add(p, BorderLayout.NORTH);
  add(p2);
  setPreferredSize(new Dimension(320, 240));
}
 
Example #9
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
private MainPanel() {
  super(new GridLayout(2, 1));
  String html = "<html><h2>H2</h2>text<ul><li>list: %s</li></ul></html>";

  JEditorPane editor0 = makeEditorPane();
  editor0.setText(String.format(html, "Default"));

  String url = getClass().getResource("bullet.png").toString();
  JEditorPane editor1 = makeEditorPane();
  HTMLEditorKit htmlEditorKit = (HTMLEditorKit) editor1.getEditorKit();
  StyleSheet styleSheet = htmlEditorKit.getStyleSheet();
  styleSheet.addRule(String.format("ul{list-style-image:url(%s);margin:0px 20px;}", url));
  editor1.setText(String.format(html, "bullet.png"));

  // styleSheet.addRule("ul{list-style-type:circle;margin:0px 20px;}");
  // styleSheet.addRule("ul{list-style-type:disc;margin:0px 20px;}");
  // styleSheet.addRule("ul{list-style-type:square;margin:0px 20px;}");
  // styleSheet.addRule("ul{list-style-type:decimal;margin:0px 20px;}");

  // Pseudo element is not supported in javax.swing.text.html.CSS
  // styleSheet.addRule("ul{list-style-type:none;margin:0px 20px;}");
  // styleSheet.addRule("ul li:before{content: "\u00BB";}");

  add(new JScrollPane(editor0));
  add(new JScrollPane(editor1));
  setPreferredSize(new Dimension(320, 240));
}
 
Example #10
Source File: InfoBuilder.java    From raccoon4 with Apache License 2.0 5 votes vote down vote up
@Override
protected JPanel assemble() {
	JPanel ret = new JPanel();
	ret.setLayout(new BoxLayout(ret, BoxLayout.Y_AXIS));

	content = new HyperTextPane("-").withTransparency();
	HTMLEditorKit kit = new HTMLEditorKit();
	content.setEditorKit(kit);
	StyleSheet styleSheet = kit.getStyleSheet();
	styleSheet.addRule("body {color:#292929; font-family:Helvetica, Arial, sans-serif; margin: 4px;}");
	
	if (listener != null) {
		content.addHyperlinkListener(listener);
	}
	JPanel wrapper = new JPanel(false);
	wrapper.setLayout(new GridLayout(1, 0, 0, 0));
	JLabel lbl = new JLabel(title, new ImageIcon(getClass().getResource(
			"/icons/famfam/icons/bell.png")), SwingConstants.LEADING);
	if (color != null) {
		lbl.setForeground(color);
	}
	wrapper.add(lbl);
	wrapper.setBackground(wrapper.getBackground().darker());
	wrapper.setBorder(BorderFactory.createEtchedBorder());

	ret.add(wrapper);
	ret.add(content);
	return ret;
}
 
Example #11
Source File: KHtmlEdit.java    From stendhal with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void initStylesForTextPane(final JTextPane textPane, int mainTextSize) {
	textPane.setContentType("text/html");

	final HTMLDocument doc = (HTMLDocument) textPane.getDocument();
	final StyleSheet css = doc.getStyleSheet();

	/*
	 * Configure standard styles
	 */
	css.addRule("body { font-family: Dialog; font-size: " + (mainTextSize + 1)
			+ "pt }");
	css.addRule("a { color: blue; font-style: italic }");

	css.addRule("._timestamp { color: " + colorToRGB(HEADER_COLOR)
			+ "; font-size: " + (mainTextSize - 1)
			+ "pt; font-style: italic }");
	css.addRule("._header { color: " + colorToRGB(HEADER_COLOR) + " }");

	/*
	 * Configure notification types
	 */
	for (final NotificationType type : NotificationType.values()) {
		final Color color = type.getColor();

		if (color != null) {
			css.addRule("." + type.getMnemonic() + " { color: "
					+ colorToRGB(color) + "; font-weight: bold; }");
		}
	}
}
 
Example #12
Source File: LongMessageDialog.java    From rapidminer-studio with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Creates a dialog to display a long message.
 *
 * @param owner
 *            the owner window in which the dialog is displayed
 * @param i18nKey
 *            the i18n key
 * @param message
 *            the actual message
 * @param i18nArgs
 *            additional i18n arguments
 * @since 6.5.0
 */
public LongMessageDialog(Window owner, String i18nKey, String message, Object... i18nArgs) {
	super(owner, i18nKey, ModalityType.APPLICATION_MODAL, i18nArgs);
	JEditorPane textComponent = new ExtendedHTMLJEditorPane("text/html", message);
	// so it does not steel ENTER from the default button
	textComponent.setFocusable(false);
	StyleSheet css = ((HTMLEditorKit) textComponent.getEditorKit()).getStyleSheet();
	css.addRule("body { margin : 0; font-family : sans-serif; font-size : 9px; font-style : normal; }");
	css.addRule(".error { font-weight:bold; color:red; font-style:plain; }");
	css.addRule("code { font-weight:bold; color:#000088; }");
	textComponent.setEditable(false);
	JScrollPane scrollPane = new ExtendedJScrollPane(textComponent);
	scrollPane.setBorder(createBorder());
	layoutDefault(scrollPane, NORMAL, makeCloseButton());
}
 
Example #13
Source File: ExtendedHTMLJEditorPane.java    From rapidminer-studio with GNU Affero General Public License v3.0 5 votes vote down vote up
public static void installDefaultStylesheet(StyleSheet css) {
	css.addRule("body {font-family:Sans;font-size:12pt;}");
	css.addRule("h3 {margin:0; padding:0;margin-top:8px;margin-bottom:3px; }");
	// String hcolor =
	// Integer.toHexString(SwingTools.DARKEST_BLUE.darker().darker().darker().getRGB());
	// hcolor = hcolor.substring(2, 8);
	String hcolor = "446699";
	css.addRule("h4 {margin-bottom:1px; margin-top:2ex; padding:0; color:#" + hcolor + "; font-size:12px}");
	// css.addRule("h2, h3, h4 { border-width:3px; border-style:solid;
	// border-color:#"+Integer.toHexString(SwingTools.RAPID_I_ORANGE.getRGB())+"; }");
	css.addRule("p  {margin-top:0; margin-bottom:2ex; padding:0;}");
	// css.addRule("ul {margin-top:0; margin-bottom:1ex; list-style-image:url(" +
	// Tools.getResource("icons/help/circle.png") + "); }");
	css.addRule("ul.ports {margin-top:0; margin-bottom:1ex; list-style-image:url("
			+ Tools.getResource("icons/help/circle.png") + "); }");
	css.addRule("ul li {padding-bottom:1ex}");
	// css.addRule("li.outPorts {padding-bottom:0px}");
	css.addRule("ul.param_dep {margin-top:0; margin-bottom:1ex; list-style-type:none; list-style-image:none; }");
	// css.addRule("ul li ul {margin-top:0; margin-bottom:1ex; list-style-type:none;
	// list-style-image:none; }");
	// css.addRule("ul li small ul {margin-top:0; list-style-type:none; list-style-image:none;
	// }");
	css.addRule("li ul li {padding-bottom:0}");
	// css.addRule("a {text-decoration:none}");
	// css.addRule("a:hover {text-decoration:underline}");
	css.addRule("dt  {font-weight:bold;}");
	// css.addRule("a {text-decoration:underline; font-weight:bold;color:blue;}");
	css.addRule("hr  {color:red; background-color:red}");
	css.addRule("a {color:" + SwingTools.getColorHexValue(Colors.LINKBUTTON_REMOTE) + "}");
}
 
Example #14
Source File: MainFrameComponentFactory.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void setStyleSheets() {
    StyleSheet styleSheet = new StyleSheet();
    styleSheet.addRule("body {font-size: " + Driver.getFontSize() + "pt}");
    styleSheet.addRule("H1 {color: red;  font-size: 120%; font-weight: bold;}");
    styleSheet.addRule("code {font-family: courier; font-size: " + Driver.getFontSize() + "pt}");
    styleSheet.addRule(" a:link { color: #0000FF; } ");
    styleSheet.addRule(" a:visited { color: #800080; } ");
    styleSheet.addRule(" a:active { color: #FF0000; text-decoration: underline; } ");
    HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
    htmlEditorKit.setStyleSheet(styleSheet);
    mainFrame.summaryHtmlArea.setEditorKit(htmlEditorKit);
}
 
Example #15
Source File: ThreadDumpView.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Document createDefaultDocument() {
    StyleSheet styles = getStyleSheet();
    StyleSheet ss = new StyleSheet();

    ss.addStyleSheet(styles);

    HTMLDocument doc = new CustomHTMLDocument(ss);
    doc.setParser(getParser());
    doc.setAsynchronousLoadPriority(4);
    doc.setTokenThreshold(100);
    return doc;
}
 
Example #16
Source File: ThreadDumpWindow.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Document createDefaultDocument() {
    StyleSheet styles = getStyleSheet();
    StyleSheet ss = new StyleSheet();

    ss.addStyleSheet(styles);

    HTMLDocument doc = new CustomHTMLDocument(ss);
    doc.setParser(getParser());
    doc.setAsynchronousLoadPriority(4);
    doc.setTokenThreshold(100);
    return doc;
}
 
Example #17
Source File: RestUtils.java    From SmartIM with Apache License 2.0 5 votes vote down vote up
public static void loadStyleAsync(final StyleSheet styleSheet) {
    // new Thread() {
    // @Override
    // public void run() {
    // loadStyleSync(styleSheet);
    // }
    // }.start();
}
 
Example #18
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
private static JEditorPane makeEditorPane(HTMLEditorKit kit, String text) {
  JEditorPane editor = new JEditorPane();
  editor.setEditable(false);
  editor.setContentType("text/html");
  editor.setEditorKit(kit);
  editor.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
  editor.setText(text);
  StyleSheet style = kit.getStyleSheet();
  style.addRule("span {color: orange;}");
  style.addRule("img {align: middle; valign: middle; vertical-align: middle;}");
  return editor;
}
 
Example #19
Source File: CommonTipsDialog.java    From MooTool with MIT License 5 votes vote down vote up
public void setHtmlText(String htmlText) {
    textPane1.setContentType("text/html; charset=utf-8");
    HTMLEditorKit kit = new HTMLEditorKit();
    textPane1.setEditorKit(kit);
    StyleSheet styleSheet = kit.getStyleSheet();
    styleSheet.addRule("h2{color:#FBC87A;}");
    styleSheet.addRule("body{font-family:" + buttonOK.getFont().getName() + ";font-size:" + buttonOK.getFont().getSize() + ";}");
    textPane1.setText(htmlText);
}
 
Example #20
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
private MainPanel() {
  super(new GridLayout(3, 1));
  add(makeUrlPanel("Default", HREF));

  // Customize detault html link color in java swing - Stack Overflow
  // https://stackoverflow.com/questions/26749495/customize-detault-html-link-color-in-java-swing
  HTMLEditorKit kit = new HTMLEditorKit();
  StyleSheet styleSheet = kit.getStyleSheet();
  styleSheet.addRule("a{color:#FF0000;}");
  add(makeUrlPanel("styleSheet.addRule(\"a{color:#FF0000;}\")", HREF));

  add(makeUrlPanel("<a style='color:#00FF00'...", String.format("<html><a style='color:#00FF00' href='%s'>%s</a>", MYSITE, MYSITE)));

  setPreferredSize(new Dimension(320, 240));
}
 
Example #21
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
private MainPanel() {
  super(new BorderLayout());
  StyleSheet styleSheet = new StyleSheet();
  styleSheet.addRule("body {font-size: 12pt;}");
  styleSheet.addRule(".highlight {color: red; background: green}");
  HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
  htmlEditorKit.setStyleSheet(styleSheet);
  // HTMLDocument htmlDocument = (HTMLDocument) htmlEditorKit.createDefaultDocument();
  JEditorPane editor = new JEditorPane();
  editor.setEditorKit(htmlEditorKit);
  // editor.setDocument(htmlDocument);
  editor.setText(makeTestHtml());
  add(new JScrollPane(editor));
  setPreferredSize(new Dimension(320, 240));
}
 
Example #22
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
private MainPanel() {
  super(new BorderLayout());

  StyleSheet styleSheet = new StyleSheet();
  styleSheet.addRule(".str {color:#008800}");
  styleSheet.addRule(".kwd {color:#000088}");
  styleSheet.addRule(".com {color:#880000}");
  styleSheet.addRule(".typ {color:#660066}");
  styleSheet.addRule(".lit {color:#006666}");
  styleSheet.addRule(".pun {color:#666600}");
  styleSheet.addRule(".pln {color:#000000}");
  styleSheet.addRule(".tag {color:#000088}");
  styleSheet.addRule(".atn {color:#660066}");
  styleSheet.addRule(".atv {color:#008800}");
  styleSheet.addRule(".dec {color:#660066}");

  HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
  htmlEditorKit.setStyleSheet(styleSheet);
  editor.setEditorKit(htmlEditorKit);
  editor.setEditable(false);

  JButton button = new JButton("open");
  button.addActionListener(e -> {
    JFileChooser fileChooser = new JFileChooser();
    int ret = fileChooser.showOpenDialog(getRootPane());
    if (ret == JFileChooser.APPROVE_OPTION) {
      loadFile(fileChooser.getSelectedFile().getAbsolutePath());
    }
  });

  Box box = Box.createHorizontalBox();
  box.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
  box.add(Box.createHorizontalGlue());
  box.add(button);

  add(new JScrollPane(editor));
  add(box, BorderLayout.SOUTH);
  setPreferredSize(new Dimension(320, 240));
}
 
Example #23
Source File: HtmlPane.java    From Girinoscope with Apache License 2.0 5 votes vote down vote up
public HtmlPane(String text) {
    HTMLEditorKit kit = new HTMLEditorKit() {
        @Override
        public Document createDefaultDocument() {
            HTMLDocument document = (HTMLDocument) super.createDefaultDocument();
            document.setBase(Icon.class.getResource("/org/hihan/girinoscope/ui/"));
            return document;
        }
    };

    super.setEditorKit(kit);
    setContentType("text/html");
    super.setText(text);
    super.setEditable(false);

    StyleSheet styleSheet = ((HTMLDocument) super.getDocument()).getStyleSheet();
    styleSheet.addRule("body {bgcolor: white; font-family: Sans-Serif;}");

    super.addHyperlinkListener(new HyperlinkListener() {
        @Override
        public void hyperlinkUpdate(final HyperlinkEvent event) {
            if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                final String href = getHref(event);
                try {
                    Desktop.getDesktop().browse(new URI(href));
                } catch (URISyntaxException | IOException e) {
                    LOGGER.log(Level.WARNING, "Can’t open link " + href, e);
                }
            }
        }
    });
}
 
Example #24
Source File: BrowserSwing.java    From Rel with Apache License 2.0 5 votes vote down vote up
private void setEnhancedOutputStyle(JTextPane pane) {
	pane.setContentType("text/html");
	pane.setEditable(false);
	pane.setEnabled(true);
	HTMLEditorKit editorKit = new HTMLEditorKit();
	HTMLDocument defaultDocument = (HTMLDocument) editorKit.createDefaultDocument();
	pane.setEditorKit(editorKit);
	pane.setDocument(defaultDocument);
	StyleSheet css = editorKit.getStyleSheet();
	for (String entry : style.getFormattedStyle())
		css.addRule(entry);
}
 
Example #25
Source File: JBLabel.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void updateStyle(@Nonnull JEditorPane pane) {
  myEditorPane.setFont(getFont());
  myEditorPane.setForeground(getForeground());
  EditorKit kit = pane.getEditorKit();
  if (kit instanceof HTMLEditorKit) {
    StyleSheet css = ((HTMLEditorKit)kit).getStyleSheet();
    css.addRule("body, p {" +
                "color:#" + ColorUtil.toHex(getForeground()) + ";" +
                "font-family:" + getFont().getFamily() + ";" +
                "font-size:" + getFont().getSize() + "pt;" +
                "white-space:" + (myAllowAutoWrapping ? "normal" : "nowrap") + ";}");
  }
}
 
Example #26
Source File: CommitPanel.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void customizeLinksStyle() {
  Document document = getDocument();
  if (document instanceof HTMLDocument) {
    StyleSheet styleSheet = ((HTMLDocument)document).getStyleSheet();
    String linkColor = "#" + ColorUtil.toHex(JBUI.CurrentTheme.Link.linkColor());
    styleSheet.addRule("a { color: " + linkColor + "; text-decoration: none;}");
  }
}
 
Example #27
Source File: NotificationMessageElement.java    From consulo with Apache License 2.0 5 votes vote down vote up
protected JEditorPane installJep(@Nonnull JEditorPane myEditorPane) {
  String message = StringUtil.join(this.getText(), "<br>");
  myEditorPane.setEditable(false);
  myEditorPane.setOpaque(false);
  myEditorPane.setEditorKit(UIUtil.getHTMLEditorKit());
  myEditorPane.setHighlighter(null);

  final StyleSheet styleSheet = ((HTMLDocument)myEditorPane.getDocument()).getStyleSheet();
  final Style style = styleSheet.addStyle(MSG_STYLE, null);
  styleSheet.addStyle(LINK_STYLE, style);
  myEditorPane.setText(message);

  return myEditorPane;
}
 
Example #28
Source File: UIUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@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) {
    getLogger().warn(url + " loading failed", e);
    return null;
  }
}
 
Example #29
Source File: ModernDarkLaf.java    From consulo with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
private void patchStyledEditorKit(UIDefaults defaults) {
  URL url = getClass().getResource(getPrefix() + (JBUI.isHiDPI() ? "@2x.css" : ".css"));
  StyleSheet styleSheet = UIUtil.loadStyleSheet(url);
  defaults.put("StyledEditorKit.JBDefaultStyle", styleSheet);
  try {
    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);
  }
}
 
Example #30
Source File: DarculaLaf.java    From consulo with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
private void patchStyledEditorKit(UIDefaults defaults) {
  URL url = getClass().getResource(getPrefix() + (JBUI.isHiDPI() ? "@2x.css" : ".css"));
  StyleSheet styleSheet = UIUtil.loadStyleSheet(url);
  defaults.put("StyledEditorKit.JBDefaultStyle", styleSheet);
  try {
    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);
  }
}