Java Code Examples for javax.swing.text.StyleContext#addStyle()

The following examples show how to use javax.swing.text.StyleContext#addStyle() . 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: StackTraceColorizer.java    From otroslogviewer with Apache License 2.0 6 votes vote down vote up
protected void initStyles() {
  styleContext = new StyleContext();
  Style defaultStyle = styleContext.getStyle(StyleContext.DEFAULT_STYLE);
  StyleConstants.setFontFamily(defaultStyle, "courier");
  styleStackTrace = styleContext.addStyle("stackTrace", defaultStyle);

  StyleConstants.setBackground(styleStackTrace, theme.getColor(ThemeKey.LOG_DETAILS_STACKTRACE_BACKGROUND));
  StyleConstants.setForeground(styleStackTrace, theme.getColor(ThemeKey.LOG_DETAILS_STACKTRACE_FOREGROUND));
  stylePackage = styleContext.addStyle("stylePackage", styleStackTrace);
  styleClass = styleContext.addStyle("styleClass", stylePackage);
  StyleConstants.setForeground(styleClass, theme.getColor(ThemeKey.LOG_DETAILS_STACKTRACE_CLASS));
  StyleConstants.setBold(styleClass, true);
  styleMethod = styleContext.addStyle("styleMethod", styleStackTrace);
  StyleConstants.setForeground(styleMethod, theme.getColor(ThemeKey.LOG_DETAILS_STACKTRACE_METHOD));
  StyleConstants.setItalic(styleMethod, true);
  StyleConstants.setBold(styleMethod, true);
  styleFile = styleContext.addStyle("styleFile", styleStackTrace);
  StyleConstants.setForeground(styleFile, theme.getColor(ThemeKey.LOG_DETAILS_STACKTRACE_FLE));
  StyleConstants.setUnderline(styleFile, true);

  styleCodeComment = styleContext.addStyle("styleCodeComment", defaultStyle);
  StyleConstants.setForeground(styleCodeComment, theme.getColor(ThemeKey.LOG_DETAILS_STACKTRACE_COMMENT));
  StyleConstants.setItalic(styleCodeComment, true);
}
 
Example 2
Source File: SearchResultColorizer.java    From otroslogviewer with Apache License 2.0 6 votes vote down vote up
@Override
public Collection<MessageFragmentStyle> colorize(String textToColorize) {
  Collection<MessageFragmentStyle> list = new ArrayList<>();
  if (StringUtils.isEmpty(searchString)) {
    return list;
  }
  StyleContext sc = new StyleContext();
  Style searchStyle = sc.addStyle("searchResult", sc.getStyle(StyleContext.DEFAULT_STYLE));
  final Color color = theme.getColor(ThemeKey.SEARCH_RESULT);
  StyleConstants.setBackground(searchStyle, color);
  if (searchMode.equals(SearchMode.STRING_CONTAINS)) {
    list.addAll(colorizeString(textToColorize, searchStyle, searchString));
  } else if (searchMode.equals(SearchMode.REGEX)) {
    list.addAll(MessageColorizerUtils.colorizeRegex(searchStyle, textToColorize, Pattern.compile(searchString, Pattern.CASE_INSENSITIVE), 0));
  }
  for (MessageFragmentStyle style : list) {
    style.setSearchResult(true);
  }
  return list;
}
 
Example 3
Source File: Utility.java    From freecol with GNU General Public License v2.0 5 votes vote down vote up
public static void initStyleContext(Font font) {
    Style defaultStyle = StyleContext.getDefaultStyleContext()
        .getStyle(StyleContext.DEFAULT_STYLE);

    STYLE_CONTEXT = new StyleContext();
    Style regular = STYLE_CONTEXT.addStyle("regular", defaultStyle);
    StyleConstants.setFontFamily(regular, font.getFamily());
    StyleConstants.setFontSize(regular, font.getSize());

    Style buttonStyle = STYLE_CONTEXT.addStyle("button", regular);
    StyleConstants.setForeground(buttonStyle, LINK_COLOR);

    Style right = STYLE_CONTEXT.addStyle("right", regular);
    StyleConstants.setAlignment(right, StyleConstants.ALIGN_RIGHT);
}
 
Example 4
Source File: JViewPortBackingStoreImageTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static void createStyles() {
    styles = new StyleContext();
    doc = new DefaultStyledDocument(styles);
    contentAttributes = new HashMap<>();

    // no attributes defined
    Style s = styles.addStyle(null, null);
    contentAttributes.put("none", s);

    Style def = styles.getStyle(StyleContext.DEFAULT_STYLE);

    Style heading = styles.addStyle("heading", def);
    StyleConstants.setFontFamily(heading, "SansSerif");
    StyleConstants.setBold(heading, true);
    StyleConstants.setAlignment(heading, StyleConstants.ALIGN_CENTER);
    StyleConstants.setSpaceAbove(heading, 10);
    StyleConstants.setSpaceBelow(heading, 10);
    StyleConstants.setFontSize(heading, 18);

    // Title
    Style sty = styles.addStyle("title", heading);
    StyleConstants.setFontSize(sty, 32);

    // author
    sty = styles.addStyle("author", heading);
    StyleConstants.setItalic(sty, true);
    StyleConstants.setSpaceBelow(sty, 25);
}
 
Example 5
Source File: JViewPortBackingStoreImageTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
static void createStyles() {
    styles = new StyleContext();
    doc = new DefaultStyledDocument(styles);
    contentAttributes = new HashMap<>();

    // no attributes defined
    Style s = styles.addStyle(null, null);
    contentAttributes.put("none", s);

    Style def = styles.getStyle(StyleContext.DEFAULT_STYLE);

    Style heading = styles.addStyle("heading", def);
    StyleConstants.setFontFamily(heading, "SansSerif");
    StyleConstants.setBold(heading, true);
    StyleConstants.setAlignment(heading, StyleConstants.ALIGN_CENTER);
    StyleConstants.setSpaceAbove(heading, 10);
    StyleConstants.setSpaceBelow(heading, 10);
    StyleConstants.setFontSize(heading, 18);

    // Title
    Style sty = styles.addStyle("title", heading);
    StyleConstants.setFontSize(sty, 32);

    // author
    sty = styles.addStyle("author", heading);
    StyleConstants.setItalic(sty, true);
    StyleConstants.setSpaceBelow(sty, 25);
}
 
Example 6
Source File: MagicTextPane.java    From MtgDesktopCompanion with GNU General Public License v3.0 5 votes vote down vote up
public void updateTextWithIcons() {

		textPane.setText(textPane.getText().replaceAll("(?m)^[ \t]*\r?\n", ""));
		Pattern p = Pattern.compile(CardsPatterns.MANA_PATTERN.getPattern());
		Matcher m = p.matcher(textPane.getText());

		String text = textPane.getText();
		StyleContext context = new StyleContext();
		StyledDocument document = new DefaultStyledDocument(context);

		Style labelStyle = context.getStyle(StyleContext.DEFAULT_STYLE);

		Style italic = context.addStyle("italicStyle", labelStyle);
		StyleConstants.setItalic(italic, true);

		int cumule = 0;
		try {
			document.insertString(0, text, null);
			while (m.find()) {
				Image ic = manaPanel.getManaSymbol(m.group());

				int width = 15;
				if (m.group().equals("{100}"))
					width = 30;

				JLabel label = new JLabel(new ImageIcon(ic.getScaledInstance(width, 15, Image.SCALE_DEFAULT)));
				label.setAlignmentY(SwingConstants.TOP);

				StyleConstants.setComponent(labelStyle, label);

				document.remove(m.start() + cumule, (m.end() - m.start()));
				document.insertString(m.start() + cumule, m.group(), labelStyle);
			}

			textPane.setDocument(document);
		} catch (BadLocationException e) {
			textPane.setText(text);
		}
	}
 
Example 7
Source File: SoapMessageColorizer.java    From otroslogviewer with Apache License 2.0 5 votes vote down vote up
private void initStyles() {
  StyleContext sc = new StyleContext();
  Style parent = sc.getStyle(StyleContext.DEFAULT_STYLE);

  StyleConstants.setFontFamily(parent, "courier");
  StyleConstants.setFontSize(parent, 13);

  styleElementName = sc.addStyle("elementName", parent);
  StyleConstants.setForeground(styleElementName, theme.getColor(ThemeKey.LOG_DETAILS_SOAP_ELEMENT_NAME));

  styleAttributeName = sc.addStyle("attributeName", parent);
  StyleConstants.setForeground(styleAttributeName, theme.getColor(ThemeKey.LOG_DETAILS_SOAP_ATTRIBUTE_NAME));

  styleAttributeValue = sc.addStyle("attributeValue", parent);
  StyleConstants.setForeground(styleAttributeValue, theme.getColor(ThemeKey.LOG_DETAILS_SOAP_ATTRIBUTE_VALUE));

  styleContent = sc.addStyle("content", parent);
  StyleConstants.setBackground(styleContent, theme.getColor(ThemeKey.LOG_DETAILS_SOAP_CONTENT_BACKGROUND));
  StyleConstants.setForeground(styleContent, theme.getColor(ThemeKey.LOG_DETAILS_SOAP_CONTENT_FOREGROUND));

  styleOperator = sc.addStyle("operator", parent);
  StyleConstants.setForeground(styleOperator, theme.getColor(ThemeKey.LOG_DETAILS_SOAP_OPERATOR));
  StyleConstants.setBold(styleOperator, true);

  styleComments = sc.addStyle("comments", parent);
  StyleConstants.setForeground(styleComments, theme.getColor(ThemeKey.LOG_DETAILS_SOAP_COMMENTS));

  styleCData = sc.addStyle("cdata", parent);
  StyleConstants.setForeground(styleCData, theme.getColor(ThemeKey.LOG_DETAILS_SOAP_CDATA_FOREGROUND));
  StyleConstants.setBackground(styleCData, theme.getColor(ThemeKey.LOG_DETAILS_SOAP_CDATA_BACKGROUND));

  styleDOCTYPE = sc.addStyle("doctype", sc.addStyle("doctype", parent));
}
 
Example 8
Source File: StyledConsoleLogger.java    From importer-exporter with Apache License 2.0 5 votes vote down vote up
public StyledConsoleLogger(JTextPane textPane, Charset encoding) {
    this.textPane = textPane;
    this.encoding = encoding;

    doc = textPane.getStyledDocument();
    context = new StyleContext();

    // add default styles for each log level
    for (LogLevel level : LogLevel.values())
        context.addStyle(level.value(), null);

    out = getStyledPrintStream(context.getStyle(LogLevel.INFO.value()));
    err = getStyledPrintStream(context.getStyle(LogLevel.ERROR.value()));
}
 
Example 9
Source File: GroovyFilter.java    From groovy with Apache License 2.0 4 votes vote down vote up
private void init() {
    StyleContext styleContext = StyleContext.getDefaultStyleContext();
    Style defaultStyle = styleContext.getStyle(StyleContext.DEFAULT_STYLE);

    Style comment = styleContext.addStyle(COMMENT, defaultStyle);
    StyleConstants.setForeground(comment, COMMENT_COLOR);
    StyleConstants.setItalic(comment, true);

    Style quotes = styleContext.addStyle(QUOTES, defaultStyle);
    StyleConstants.setForeground(quotes, Color.MAGENTA.darker().darker());

    Style charQuotes = styleContext.addStyle(SINGLE_QUOTES, defaultStyle);
    StyleConstants.setForeground(charQuotes, Color.GREEN.darker().darker());

    Style slashyQuotes = styleContext.addStyle(SLASHY_QUOTES, defaultStyle);
    StyleConstants.setForeground(slashyQuotes, Color.ORANGE.darker());

    Style digit = styleContext.addStyle(DIGIT, defaultStyle);
    StyleConstants.setForeground(digit, Color.RED.darker());

    Style operation = styleContext.addStyle(OPERATION, defaultStyle);
    StyleConstants.setBold(operation, true);

    Style ident = styleContext.addStyle(IDENT, defaultStyle);

    Style reservedWords = styleContext.addStyle(RESERVED_WORD, defaultStyle);
    StyleConstants.setBold(reservedWords, true);
    StyleConstants.setForeground(reservedWords, Color.BLUE.darker().darker());

    Style leftParens = styleContext.addStyle(IDENT, defaultStyle);

    getRootNode().putStyle(SLASH_STAR_COMMENT, comment);
    getRootNode().putStyle(SLASH_SLASH_COMMENT, comment);
    getRootNode().putStyle(QUOTES, quotes);
    getRootNode().putStyle(SINGLE_QUOTES, charQuotes);
    getRootNode().putStyle(SLASHY_QUOTES, slashyQuotes);

    getRootNode().putStyle(new String[] {
        HEX_INTEGER_LITERAL,
        OCTAL_INTEGER_LITERAL,
        BINARY_INTEGER_LITERAL,
        DECIMAL_FLOATING_POINT_LITERAL,
        HEXADECIMAL_FLOATING_POINT_LITERAL,
        DECIMAL_INTEGER_LITERAL,
    }, digit);

    getRootNode().putStyle(OPERATION, operation);
    StructuredSyntaxDocumentFilter.LexerNode node = createLexerNode();
    node.putStyle(RESERVED_WORDS, reservedWords);
    node.putStyle(LEFT_PARENS, leftParens);
    getRootNode().putChild(OPERATION, node);

    getRootNode().putStyle(IDENT, ident);
    node = createLexerNode();
    node.putStyle(RESERVED_WORDS, reservedWords);
    getRootNode().putChild(IDENT, node);
}
 
Example 10
Source File: StyleProperties.java    From otroslogviewer with Apache License 2.0 4 votes vote down vote up
public static Style getStyle(Theme.Type themeType,StyleContext styleContext, DataConfiguration styleConfig, String styleName, int group) {
  Style style = styleContext.addStyle(styleName, styleContext.getStyle(StyleContext.DEFAULT_STYLE));

  final String groupSuffix;
  if (group <= 0) {
    groupSuffix = "";
  } else {
    groupSuffix = "." + group;
  }

  String fontFamily = styleConfig.getString(PROP_FONT_FAMILY + groupSuffix, "");
  if (fontFamily.trim().length() > 0) {
    StyleConstants.setFontFamily(style, styleConfig.getString(PROP_FONT_FAMILY + groupSuffix));
  }

  if (styleConfig.getString(PROP_FONT_SIZE + groupSuffix, "").trim().length() > 0) {
    StyleConstants.setFontSize(style, styleConfig.getInt(PROP_FONT_SIZE + groupSuffix));
  }

  if (styleConfig.getString(PROP_FONT_BOLD + groupSuffix, "").trim().length() > 0) {
    StyleConstants.setBold(style, styleConfig.getBoolean(PROP_FONT_BOLD + groupSuffix));
  }

  if (styleConfig.getString(PROP_FONT_ITALIC + groupSuffix, "").trim().length() > 0) {
    StyleConstants.setItalic(style, styleConfig.getBoolean(PROP_FONT_ITALIC + groupSuffix));
  }

  if (styleConfig.getString(PROP_FONT_UNDERLINE + groupSuffix, "").trim().length() > 0) {
    StyleConstants.setUnderline(style, styleConfig.getBoolean(PROP_FONT_UNDERLINE + groupSuffix));
  }

  String theme;
  if (themeType.equals(Theme.Type.Light)) {
    theme = "light";
  } else {
    theme = "dark";
  }

  if (styleConfig.getString(PROP_BACKGROUND + groupSuffix + "." + theme,"").trim().length() > 0) {
    StyleConstants.setBackground(style, styleConfig.getColor(PROP_BACKGROUND + groupSuffix + "." + theme, null));
  } else if (styleConfig.getString(PROP_BACKGROUND + groupSuffix, "").trim().length() > 0) {
    StyleConstants.setBackground(style, styleConfig.getColor(PROP_BACKGROUND + groupSuffix));
  }

  if (styleConfig.getString(PROP_FOREGROUND + groupSuffix + "." + theme,"").trim().length() > 0) {
    StyleConstants.setForeground(style, styleConfig.getColor(PROP_FOREGROUND + groupSuffix + "." + theme, null));
  } else if (styleConfig.getString(PROP_FOREGROUND + groupSuffix, "").trim().length() > 0) {
    StyleConstants.setForeground(style, styleConfig.getColor(PROP_FOREGROUND + groupSuffix));
  }

  return style;
}
 
Example 11
Source File: LogDataFormatter.java    From otroslogviewer with Apache License 2.0 4 votes vote down vote up
public LogDataFormatter(OtrosApplication otrosApplication,
                        LogData logData, //
                        DateFormat dateFormat,//
                        MessageUpdateUtils messageUtils,//
                        PluginableElementsContainer<MessageColorizer> colorizersContainer,//
                        PluginableElementsContainer<MessageFormatter> formattersContainer,//
                        CancelStatus cancelStatus, int maximumMessageSize) {
  this.otrosApplication = otrosApplication;
  this.ld = logData;
  this.dateFormat = dateFormat;
  this.messageUtils = messageUtils;
  this.colorizersContainer = colorizersContainer;
  this.formattersContainer = formattersContainer;
  this.cancelStatus = cancelStatus;
  this.maximumMessageSize = maximumMessageSize;
  final Theme theme = otrosApplication.getTheme();

  sc = new StyleContext();
  Style defaultStyle = sc.getStyle(StyleContext.DEFAULT_STYLE);
  mainStyle = sc.addStyle("MainStyle", defaultStyle);
  StyleConstants.setForeground(mainStyle, theme.getColor(ThemeKey.LOG_DETAILS_DEFAULT));

  messageStyle = sc.addStyle("message", defaultStyle);
  StyleConstants.setForeground(messageStyle, theme.getColor(ThemeKey.LOG_DETAILS_MESSAGE));

  messageMonospacedStyle = sc.addStyle("messageMonospaced", messageStyle);
  StyleConstants.setFontFamily(messageMonospacedStyle, "monospaced");
  StyleConstants.setForeground(messageMonospacedStyle, theme.getColor(ThemeKey.LOG_DETAILS_MESSAGE));

  valueStyle = sc.addStyle("valueStyle", null);
  StyleConstants.setFontFamily(valueStyle, "monospaced");
  StyleConstants.setForeground(valueStyle, theme.getColor(ThemeKey.LOG_DETAILS_VALUE));

  propertyStyle = sc.addStyle("property", null);
  StyleConstants.setFontFamily(propertyStyle, "monospaced");
  StyleConstants.setForeground(propertyStyle, theme.getColor(ThemeKey.LOG_DETAILS_PROPERTY));

  propertyNameStyle = sc.addStyle("propertyName", valueStyle);
  final Color propValueColor = theme.getColor(ThemeKey.LOG_DETAILS_PROPERTY_KEY);
  StyleConstants.setForeground(propertyNameStyle, propValueColor);

  propertyValueStyle = sc.addStyle("propertyValue", valueStyle);
  final Color color = theme.getColor(ThemeKey.LOG_DETAILS_PROPERTY_VALUE);
  StyleConstants.setForeground(propertyValueStyle, color);


}