Java Code Examples for javax.swing.text.Style#addAttribute()

The following examples show how to use javax.swing.text.Style#addAttribute() . 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: MarkdownDocument.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private Style[] createHeaderStyles(Font font) {
    String  family = font.getFamily();
    int     size   = font.getSize();
    int[]   sizes  = {size * 2, size * 3 / 2, size * 5 / 4, size, size * 7 / 8};
    int     count  = sizes.length;
    Style[] styles = new Style[count];
    for (int i = 0; i < count; i++) {
        Style h = addStyle("h" + (i + 1), null);
        h.addAttribute(StyleConstants.FontFamily, family);
        h.addAttribute(StyleConstants.FontSize, Integer.valueOf(sizes[i]));
        h.addAttribute(StyleConstants.Bold, Boolean.TRUE);
        h.addAttribute(StyleConstants.SpaceBelow, Float.valueOf(sizes[i] / 2.0f));
        styles[i] = h;
    }
    return styles;
}
 
Example 2
Source File: StackTraceColorizer.java    From otroslogviewer with Apache License 2.0 6 votes vote down vote up
protected Collection<MessageFragmentStyle> colorizeStackTraceRegex(final Style style, String text, Pattern regex, int group) {
  ArrayList<MessageFragmentStyle> list = new ArrayList<>();
  Matcher matcher = regex.matcher(text);
  Style styleToUse = style;
  while (matcher.find()) {
    LocationInfo locationInfo = LocationInfo.parse(matcher.group(0));
    if (locationInfo != null) {
      String name = styleToUse.getName();
      Style newStyle = styleContext.addStyle(name + "-" + locationInfo.toString(), styleToUse);
      newStyle.addAttribute(STYLE_ATTRIBUTE_LOCATION_INFO, locationInfo);
      StyleConstants.setForeground(newStyle, StyleConstants.getForeground(styleToUse));
      StyleConstants.setBold(newStyle, StyleConstants.isBold(styleToUse));
      StyleConstants.setItalic(newStyle, StyleConstants.isItalic(styleToUse));
      styleToUse = newStyle;
    }
    int start = matcher.start(group);
    int end = matcher.end(group);
    if (end - start > 0) {
      MessageFragmentStyle messageFragmentStyle = new MessageFragmentStyle(start, end - start, styleToUse, false);
      list.add(messageFragmentStyle);
    }
  }
  return list;
}
 
Example 3
Source File: StackTraceSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void underlineStacktraces(StyledDocument doc, JTextPane textPane, List<StackTracePosition> stacktraces, String comment) {
    Style defStyle = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
    Style hlStyle = doc.addStyle("regularBlue-stacktrace", defStyle); // NOI18N
    hlStyle.addAttribute(HyperlinkSupport.STACKTRACE_ATTRIBUTE, new StackTraceAction());
    StyleConstants.setForeground(hlStyle, UIUtils.getLinkColor());
    StyleConstants.setUnderline(hlStyle, true);

    int last = 0;
    textPane.setText(""); // NOI18N
    for (StackTraceSupport.StackTracePosition stp : stacktraces) {
        int start = stp.getStartOffset();
        int end = stp.getEndOffset();

        if (last < start) {
            insertString(doc, comment, last, start, defStyle);
        }
        last = start;

        // for each line skip leading whitespaces (look bad underlined)
        boolean inStackTrace = (comment.charAt(start) > ' ');
        for (int i = start; i < end; i++) {
            char ch = comment.charAt(i);
            if ((inStackTrace && ch == '\n') || (!inStackTrace && ch > ' ')) {
                insertString(doc, comment, last, i, inStackTrace ? hlStyle : defStyle);
                inStackTrace = !inStackTrace;
                last = i;
            }
        }

        if (last < end) {
            insertString(doc, comment, last, end, inStackTrace ? hlStyle : defStyle);
        }
        last = end;
    }
    try {
        doc.insertString(doc.getLength(), comment.substring(last), defStyle);
    } catch (BadLocationException ex) {
        Support.LOG.log(Level.SEVERE, null, ex);
    }
}
 
Example 4
Source File: MarkdownDocument.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private Style createBulletStyle(Font font) {
    Style style = addStyle("bullet", null);
    style.addAttribute(StyleConstants.FontFamily, font.getFamily());
    style.addAttribute(StyleConstants.FontSize, Integer.valueOf(font.getSize()));
    int indent = TextDrawing.getSimpleWidth(font, "• ");
    style.addAttribute(StyleConstants.FirstLineIndent, Float.valueOf(-indent));
    style.addAttribute(StyleConstants.LeftIndent, Float.valueOf(indent));
    return style;
}
 
Example 5
Source File: StackTraceColorizer.java    From otroslogviewer with Apache License 2.0 5 votes vote down vote up
protected Collection<MessageFragmentStyle> findExceptionNameAndMessage(final Style style, String subTextFragment) {
  final ArrayList<MessageFragmentStyle> result = new ArrayList<>();
  Matcher matcherMessage = exceptionNameAndMessage.matcher(subTextFragment);
  while (matcherMessage.find()) {
    final int beginIndex = matcherMessage.start(1);
    final int endIndex = matcherMessage.end(1);
    final String msg = subTextFragment.substring(beginIndex, endIndex).replaceFirst("Caused by: ", "");
    final Style style1 = styleContext.addStyle("exceptionMessage-" + msg, style);
    style1.addAttribute(STYLE_ATTRIBUTE_EXCEPTION_MSG, msg);
    result.add(new MessageFragmentStyle(beginIndex, endIndex - beginIndex, style1, false));
    System.out.println("Setting style with exceptionMessage " + style1);
  }
  return result;
}
 
Example 6
Source File: LoggingPanel.java    From importer-exporter with Apache License 2.0 5 votes vote down vote up
Color apply(Object key, JButton button) {
    Style style = mainView.getStyledConsoleLogger().getStyle(level);
    Color tmp = (Color) style.getAttribute(key);

    Color color = JColorChooser.showDialog(mainView, Language.I18N.getString("pref.general.logging.title.select"), tmp);
    if (color != null) {
        style.addAttribute(key, color);
        button.setBackground(color);
        updatePreview();

        return color;
    } else
        return tmp;
}
 
Example 7
Source File: LoggingPanel.java    From importer-exporter with Apache License 2.0 5 votes vote down vote up
void select(Color color, Color defaultColor, Object key, JCheckBox checkBox, JButton button) {
    Style style = mainView.getStyledConsoleLogger().getStyle(level);
    button.setEnabled(checkBox.isSelected());

    if (checkBox.isSelected()) {
        style.addAttribute(key, color != null ? color : defaultColor);
        button.setBackground(color != null ? color : defaultColor);
    } else {
        style.removeAttribute(key);
        button.setBackground(null);
    }

    updatePreview();
}
 
Example 8
Source File: AddModulePanel.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void showDescription() {
    StyledDocument doc = descValue.getStyledDocument();
    final Boolean matchCase = matchCaseValue.isSelected();
    try {
        doc.remove(0, doc.getLength());
        ModuleDependency[] deps = getSelectedDependencies();
        if (deps.length != 1) {
            return;
        }
        String longDesc = deps[0].getModuleEntry().getLongDescription();
        if (longDesc != null) {
            doc.insertString(0, longDesc, null);
        }
        String filterText = filterValue.getText().trim();
        if (filterText.length() != 0 && !FILTER_DESCRIPTION.equals(filterText)) {
            doc.insertString(doc.getLength(), "\n\n", null); // NOI18N
            Style bold = doc.addStyle(null, null);
            bold.addAttribute(StyleConstants.Bold, Boolean.TRUE);
            doc.insertString(doc.getLength(), getMessage("TEXT_matching_filter_contents"), bold);
            doc.insertString(doc.getLength(), "\n", null); // NOI18N
            if (filterText.length() > 0) {
                String filterTextLC = matchCase?filterText:filterText.toLowerCase(Locale.US);
                Style match = doc.addStyle(null, null);
                match.addAttribute(StyleConstants.Background, UIManager.get("selection.highlight")!=null?
                        UIManager.get("selection.highlight"):new Color(246, 248, 139));
                boolean isEven = false;
                Style even = doc.addStyle(null, null);
                even.addAttribute(StyleConstants.Background, UIManager.get("Panel.background"));
                if (filterer == null) {
                    return; // #101776
                }
                for (String hit : filterer.getMatchesFor(filterText, deps[0])) {
                    int loc = doc.getLength();
                    doc.insertString(loc, hit, (isEven ? even : null));
                    int start = (matchCase?hit:hit.toLowerCase(Locale.US)).indexOf(filterTextLC);
                    while (start != -1) {
                        doc.setCharacterAttributes(loc + start, filterTextLC.length(), match, true);
                        start = hit.toLowerCase(Locale.US).indexOf(filterTextLC, start + 1);
                    }
                    doc.insertString(doc.getLength(), "\n", (isEven ? even : null)); // NOI18N
                    isEven ^= true;
                }
            } else {
                Style italics = doc.addStyle(null, null);
                italics.addAttribute(StyleConstants.Italic, Boolean.TRUE);
                doc.insertString(doc.getLength(), getMessage("TEXT_no_filter_specified"), italics);
            }
        }
        descValue.setCaretPosition(0);
    } catch (BadLocationException e) {
        Util.err.notify(ErrorManager.INFORMATIONAL, e);
    }
}
 
Example 9
Source File: MarkdownDocument.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
private Style createBodyStyle(Font font) {
    Style style = addStyle("body", null);
    style.addAttribute(StyleConstants.FontFamily, font.getFamily());
    style.addAttribute(StyleConstants.FontSize, Integer.valueOf(font.getSize()));
    return style;
}