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