下面列出了javax.swing.text.StyleContext#addStyle ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
@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;
}
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);
}
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);
}
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);
}
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);
}
}
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));
}
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()));
}
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);
}
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;
}
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);
}