public static IssueLinker create(VCSHyperlinkProvider hp, Style issueHyperlinkStyle, File root, StyledDocument sd, String text) { int[] spans = hp.getSpans(text); if (spans == null) { return null; } if(spans.length % 2 != 0) { // XXX more info and log only _ONCE_ LOG.warning("Hyperlink provider " + hp.getClass().getName() + " returns wrong spans"); return null; } if(spans.length > 0) { IssueLinker l = new IssueLinker(hp, issueHyperlinkStyle, root, sd, text, spans); return l; } return null; }
@Override public void insertString(StyledDocument sd, Style style) throws BadLocationException { if(style == null) { style = authorStyle; } sd.insertString(sd.getLength(), author, style); String iconStyleName = AUTHOR_ICON_STYLE + author; Style iconStyle = sd.getStyle(iconStyleName); if(iconStyle == null) { iconStyle = sd.addStyle(iconStyleName, null); StyleConstants.setIcon(iconStyle, kenaiUser.getIcon()); } sd.insertString(sd.getLength(), " ", style); sd.insertString(sd.getLength(), " ", iconStyle); }
private void addDate (JTextPane pane, RevisionItem item, boolean selected, Collection<SearchHighlight> highlights) throws BadLocationException { LogEntry entry = item.getUserData(); StyledDocument sd = pane.getStyledDocument(); // clear document clearSD(pane, sd); Style selectedStyle = createSelectedStyle(pane); Style normalStyle = createNormalStyle(pane); Style style; if (selected) { style = selectedStyle; } else { style = normalStyle; } // add date sd.insertString(sd.getLength(), entry.getDate(), style); }
/** Write a horizontal separator into the log window. */ public void logDivider() { if (log == null) return; clearError(); StyledDocument doc = log.getStyledDocument(); Style dividerStyle = doc.addStyle("bar", styleRegular); JPanel jpanel = new JPanel(); jpanel.setBackground(Color.LIGHT_GRAY); jpanel.setPreferredSize(new Dimension(300, 1)); // 300 is arbitrary, // since it will // auto-stretch StyleConstants.setComponent(dividerStyle, jpanel); reallyLog(".", dividerStyle); // Any character would do; "." will be // replaced by the JPanel reallyLog("\n\n", styleRegular); log.setCaretPosition(doc.getLength()); lastSize = doc.getLength(); }
private void reallyLog(String text, Style style) { if (log == null || text.length() == 0) return; int i = text.lastIndexOf('\n'), j = text.lastIndexOf('\r'); if (i >= 0 && i < j) { i = j; } StyledDocument doc = log.getStyledDocument(); try { if (i < 0) { doc.insertString(doc.getLength(), text, style); } else { // Performs intelligent caret positioning doc.insertString(doc.getLength(), text.substring(0, i + 1), style); log.setCaretPosition(doc.getLength()); if (i < text.length() - 1) { doc.insertString(doc.getLength(), text.substring(i + 1), style); } } } catch (BadLocationException e) { // Harmless } if (style != styleRed) { lastSize = doc.getLength(); } }
/** Set the font name. */ public void setFontName(String fontName) { if (log == null) return; this.fontName = fontName; log.setFont(new Font(fontName, Font.PLAIN, fontSize)); StyleConstants.setFontFamily(styleRegular, fontName); StyleConstants.setFontFamily(styleBold, fontName); StyleConstants.setFontFamily(styleRed, fontName); StyleConstants.setFontSize(styleRegular, fontSize); StyleConstants.setFontSize(styleBold, fontSize); StyleConstants.setFontSize(styleRed, fontSize); // Changes all existing text StyledDocument doc = log.getStyledDocument(); Style temp = doc.addStyle("temp", null); StyleConstants.setFontFamily(temp, fontName); StyleConstants.setFontSize(temp, fontSize); doc.setCharacterAttributes(0, doc.getLength(), temp, false); // Changes all existing hyperlinks Font newFont = new Font(fontName, Font.BOLD, fontSize); for (JLabel link : links) { link.setFont(newFont); } }
/** * Try and recover from a BadLocationException thrown when inserting a string * into the log area. This method must only be called on the AWT event * handling thread. */ private void handleBadLocationException(BadLocationException e, String textToInsert, Style style) { originalErr.println("BadLocationException encountered when writing to " + "the log area: " + e); originalErr.println("trying to recover..."); Document newDocument = new DefaultStyledDocument(); try { StringBuilder sb = new StringBuilder(); sb.append("An error occurred when trying to write a message to the log area. The log\n"); sb.append("has been cleared to try and recover from this problem.\n\n"); sb.append(textToInsert); newDocument.insertString(0, sb.toString(), style); } catch(BadLocationException e2) { // oh dear, all bets are off now... e2.printStackTrace(originalErr); return; } // replace the log area's document with the new one setDocument(newDocument); }
private void reallyLog(String text, Style style) { if (log==null || text.length()==0) return; int i=text.lastIndexOf('\n'), j=text.lastIndexOf('\r'); if (i>=0 && i<j) { i=j; } StyledDocument doc=log.getStyledDocument(); try { if (i<0) { doc.insertString(doc.getLength(), text, style); } else { // Performs intelligent caret positioning doc.insertString(doc.getLength(), text.substring(0,i+1), style); log.setCaretPosition(doc.getLength()); if (i<text.length()-1) { doc.insertString(doc.getLength(), text.substring(i+1), style); } } } catch (BadLocationException e) { // Harmless } if (style!=styleRed) { lastSize=doc.getLength(); } }
/** Set the font name. */ public void setFontName(String fontName) { if (log==null) return; this.fontName = fontName; log.setFont(new Font(fontName, Font.PLAIN, fontSize)); StyleConstants.setFontFamily(styleRegular, fontName); StyleConstants.setFontFamily(styleBold, fontName); StyleConstants.setFontFamily(styleRed, fontName); StyleConstants.setFontSize(styleRegular, fontSize); StyleConstants.setFontSize(styleBold, fontSize); StyleConstants.setFontSize(styleRed, fontSize); // Changes all existing text StyledDocument doc=log.getStyledDocument(); Style temp=doc.addStyle("temp", null); StyleConstants.setFontFamily(temp, fontName); StyleConstants.setFontSize(temp, fontSize); doc.setCharacterAttributes(0, doc.getLength(), temp, false); // Changes all existing hyperlinks Font newFont = new Font(fontName, Font.BOLD, fontSize); for(JLabel link: links) { link.setFont(newFont); } }
static void addParagraph(Paragraph p) { try { Style s = null; for (int i = 0; i < p.data.length; i++) { AttributedContent run = p.data[i]; s = contentAttributes.get(run.attr); doc.insertString(doc.getLength(), run.content, s); } Style ls = styles.getStyle(p.logical); doc.setLogicalStyle(doc.getLength() - 1, ls); doc.insertString(doc.getLength(), "\n", null); } catch (BadLocationException e) { throw new RuntimeException(e); } }
private JTextPane getCodeArea(){ final StyleContext sc = new StyleContext(); final DefaultStyledDocument doc = new DefaultStyledDocument(sc); final JTextPane codeArea = new JTextPane(doc); codeArea.setBackground(new Color(0x25401C)); codeArea.setCaretColor(new Color(0xD1E8CE)); final Style bodyStyle = sc.addStyle("body", null); bodyStyle.addAttribute(StyleConstants.Foreground, new Color(0x789C6C)); bodyStyle.addAttribute(StyleConstants.FontSize, 13); bodyStyle.addAttribute(StyleConstants.FontFamily, "monospaced"); bodyStyle.addAttribute(StyleConstants.Bold, true); doc.setLogicalStyle(0, bodyStyle); return codeArea; }
private void appendToConsole(String message, Style style) { StyledDocument paneDocument = txtServerConsole.getStyledDocument(); Integer documentLength = paneDocument.getLength(); try { paneDocument.insertString(documentLength, message, style); } catch (BadLocationException ex) { // The system will attempt to update the log // if it fails to update the log, I sense an // infinite loop somewhere... System.out.println("[SERVER-WARNING] Failed to update log.\r\n\tError: " + ex.getMessage()); } }
private void makeStyles(StyledDocument doc) { Style s1 = doc.addStyle("regular", regStyle); StyleConstants.setFontFamily(s1, "SansSerif"); Style s2 = doc.addStyle("bold", s1); StyleConstants.setBold(s2, true); Style icon = doc.addStyle("input", s1); StyleConstants.setAlignment(icon, StyleConstants.ALIGN_CENTER); StyleConstants.setSpaceAbove(icon, 8); StyleConstants.setIcon(icon, Icons.in); Style icon2 = doc.addStyle("output", s1); StyleConstants.setAlignment(icon2, StyleConstants.ALIGN_CENTER); StyleConstants.setSpaceAbove(icon2, 8); StyleConstants.setIcon(icon2, Icons.out); }
/** * Returns a default-populated StyleContext for UHSTextAreas. * * <p>Changes to a style will immediately affect existing * components using its descendants only. The style itself * will not update for some reason.</p> * * <blockquote><pre> * @{code * Java's default * - base * - - regular * - - - visitable * - - - link * - - - hyper * - - - monospaced * } * </pre></blockquote> */ private static StyleContext getDefaultStyleContext() { StyleContext result = new StyleContext(); Style defaultStyle = StyleContext.getDefaultStyleContext().getStyle( StyleContext.DEFAULT_STYLE ); Style baseStyle = result.addStyle( "base", defaultStyle ); Style regularStyle = result.addStyle( STYLE_NAME_REGULAR, baseStyle ); Style visitableStyle = result.addStyle( STYLE_NAME_VISITABLE, regularStyle ); StyleConstants.setForeground( visitableStyle, VISITABLE_COLOR ); Style linkStyle = result.addStyle( STYLE_NAME_LINK, regularStyle ); StyleConstants.setForeground( linkStyle, LINK_COLOR ); Style hyperStyle = result.addStyle( STYLE_NAME_HYPERLINK, regularStyle ); StyleConstants.setForeground( hyperStyle, HYPER_COLOR ); StyleConstants.setUnderline( hyperStyle, true ); Style monospacedStyle = result.addStyle( STYLE_NAME_MONOSPACED, regularStyle ); StyleConstants.setFontFamily( monospacedStyle, "Monospaced" ); return result; }
/** * Write the styles used. */ protected void writeStyles() throws IOException { if(doc instanceof DefaultStyledDocument) { Enumeration<?> styles = ((DefaultStyledDocument)doc).getStyleNames(); while(styles.hasMoreElements()) writeStyle(doc.getStyle((String)styles.nextElement())); } else { // What else to do here? Style s = doc.getStyle("default"); if(s != null) writeStyle( s ); } }
/** * Sets up the thread. * * @param input the Reader to monitor * @param output the TextArea to send output to * @param color the color to use */ public ReaderToTextPane(Reader input, JTextPane output, Color color) { StyledDocument doc; Style style; setDaemon(true); m_Color = color; m_Input = new LineNumberReader(input); m_Output = output; doc = m_Output.getStyledDocument(); style = StyleContext.getDefaultStyleContext() .getStyle(StyleContext.DEFAULT_STYLE); style = doc.addStyle(getStyleName(), style); StyleConstants.setFontFamily(style, "monospaced"); StyleConstants.setForeground(style, m_Color); }
public void receivedMessage(ChatMessage message) { if (!isActive()) toFront(); insertHeader(message.getSender(), MessageOperation.RECEIVING_MESSAGE); int length = document.getLength(); StringBuffer strBfr = new StringBuffer(); strBfr.append(message.getText()); final Style cwStyle = sc.addStyle("ConstantWidth", null); StyleConstants.setForeground(cwStyle, ChatConstant.RECEIVED_MSG_COLOR); try { logWriter.log(strBfr.toString()); document.insertString(length, strBfr.toString(), cwStyle); scrollToLast(); } catch (BadLocationException e) { e.printStackTrace(); } }
public void groupJoinLeftMessage(User user, boolean isJoined) { int length = document.getLength(); StringBuffer strBfr = new StringBuffer(); if (isJoined) strBfr.append(user.getDisplayName() + " joined conversation." + NEW_LINE); else strBfr.append(user.getDisplayName() + " left conversation." + NEW_LINE); final Style cwStyle = sc.addStyle("ConstantWidth", null); StyleConstants.setFontFamily(cwStyle, "Sylfaen"); StyleConstants .setForeground(cwStyle, ChatConstant.GROUP_LEFT_MSG_COLOR); try { document.insertString(length, strBfr.toString(), cwStyle); scrollToLast(); } catch (BadLocationException e) { e.printStackTrace(); } }
public void setUsers(User[] users){ textPane.removeAll(); StringBuilder sb = new StringBuilder(); final Style cwStyle = new StyleContext().addStyle("ConstantWidth", null); StyleConstants.setBold(cwStyle, true); boolean firstUser = true; for(User user: users){ if(firstUser){ firstUser = false; } else{ sb.append(", "); } sb.append(user.getDisplayName()); } try { textPane.getDocument().insertString(textPane.getDocument().getLength(), "Participants: ", cwStyle); StyleConstants.setBold(cwStyle, false); textPane.getDocument().insertString(textPane.getDocument().getLength(), sb.toString(), cwStyle); } catch (BadLocationException e) { e.printStackTrace(); } }
protected void updateStyle(@NotNull JEditorPane editorPane, @Nullable JTree tree, Object value, boolean selected, boolean hasFocus) { final HTMLDocument htmlDocument = (HTMLDocument)editorPane.getDocument(); final Style style = htmlDocument.getStyleSheet().getStyle(MSG_STYLE); if (value instanceof LoadingNode) { StyleConstants.setForeground(style, JBColor.GRAY); } else { if (selected) { StyleConstants.setForeground(style, hasFocus ? UIUtil.getTreeSelectionForeground() : UIUtil.getTreeTextForeground()); } else { StyleConstants.setForeground(style, UIUtil.getTreeTextForeground()); } } if (UIUtil.isUnderGTKLookAndFeel() || UIUtil.isUnderNimbusLookAndFeel() && selected && hasFocus || tree != null && tree.getUI() instanceof WideSelectionTreeUI && ((WideSelectionTreeUI)tree.getUI()).isWideSelection()) { editorPane.setOpaque(false); } else { editorPane.setOpaque(selected && hasFocus); } htmlDocument.setCharacterAttributes(0, htmlDocument.getLength(), style, false); }
private void setupStyle() { Document document = myHTMLViewer.getDocument(); if (!(document instanceof StyledDocument)) { return; } StyledDocument styledDocument = (StyledDocument)document; EditorColorsManager colorsManager = EditorColorsManager.getInstance(); EditorColorsScheme scheme = colorsManager.getGlobalScheme(); Style style = styledDocument.addStyle("active", null); StyleConstants.setFontFamily(style, scheme.getEditorFontName()); StyleConstants.setFontSize(style, scheme.getEditorFontSize()); styledDocument.setCharacterAttributes(0, document.getLength(), style, false); }
/** * Update the font in the default style of the document. * * @param font the new font to use or null to remove the font attribute * from the document's style */ private void updateFont(Font font) { StyledDocument doc = (StyledDocument)getComponent().getDocument(); Style style = doc.getStyle(StyleContext.DEFAULT_STYLE); if (style == null) { return; } if (font == null) { style.removeAttribute(StyleConstants.FontFamily); style.removeAttribute(StyleConstants.FontSize); style.removeAttribute(StyleConstants.Bold); style.removeAttribute(StyleConstants.Italic); } else { StyleConstants.setFontFamily(style, font.getName()); StyleConstants.setFontSize(style, font.getSize()); StyleConstants.setBold(style, font.isBold()); StyleConstants.setItalic(style, font.isItalic()); } }
private static void renderArray( final TypeInstance instance, final StyledDocument document, final boolean renderData) { final Style arrayStyle = createDeclarationStyle(document); try { document.remove(0, document.getLength()); final BaseType baseType = instance.getBaseType(); appendString(document, baseType.getName(), arrayStyle); if (renderData) { appendString(document, renderInstanceData(baseType, instance.getAddress().getOffset(), instance.getSection()), createDataStyle(document)); } } catch (final BadLocationException exception) { CUtilityFunctions.logException(exception); } }
private static void renderAtomic( final TypeInstance instance, final StyledDocument document, final boolean renderData) { final Style atomicStyle = createDeclarationStyle(document); try { document.remove(0, document.getLength()); final BaseType baseType = instance.getBaseType(); appendString(document, baseType.getName(), atomicStyle); if (renderData) { appendString(document, renderInstanceData(baseType, instance.getAddress().getOffset(), instance.getSection()), createDataStyle(document)); } } catch (final BadLocationException exception) { CUtilityFunctions.logException(exception); } }
@Override public void messageAdded(MessageBuffer messageBuffer, Message message) { Style style; switch (message.getSeverity()) { case DEBUG: style = debugStyle; break; case ERROR: style = errorStyle; break; case INFO: style = infoStyle; break; case WARN: style = warnStyle; break; default: throw new UnsupportedOperationException("Severity not supported: " + message.getSeverity()); } append(debugStyle, String.format("%1$tH:%1$tM:%1$tS.%1$tL ", message.getTimestamp())); append(style, message.getCombinedMessage() + "\n"); }
public void startParsingLoop(StringBuilder sourcecode, int first, int current, int last, char currentChar) { try { if (debugDelegator.isVisible()) { StyledDocument doc= currentFrame.getDocument(); // highlightCurlys(sourcecode, lastCurrent, doc, current - lastCurrent); Style style= doc.getStyle(TextViewHelper.FETCH_STYLE); doc.setCharacterAttributes(current, last - current, style, false); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
/** * Add the Oracle text of all of this Card's faces to the given document, separated * by a separator on its own line. * * @param document document to add text to * @param printed whether to use printed or Oracle data for a card */ public void formatDocument(StyledDocument document, boolean printed) { Style textStyle = document.getStyle("text"); try { for (int f = 0; f < faces; f++) { formatDocument(document, printed, f); if (f < faces - 1) document.insertString(document.getLength(), "\n" + TEXT_SEPARATOR + "\n", textStyle); } } catch (Exception e) { e.printStackTrace(); } }
/** * Write the styles used. */ protected void writeStyles() throws IOException { if(doc instanceof DefaultStyledDocument) { Enumeration styles = ((DefaultStyledDocument)doc).getStyleNames(); while(styles.hasMoreElements()) writeStyle(doc.getStyle((String)styles.nextElement())); } else { // What else to do here? Style s = doc.getStyle("default"); if(s != null) writeStyle( s ); } }
public void append2(String s, Color color, Boolean bold){ StyledDocument doc = super.getStyledDocument(); SimpleAttributeSet sa = new SimpleAttributeSet(); StyleConstants.setAlignment(sa, StyleConstants.ALIGN_JUSTIFIED); StyleConstants.setLeftIndent(sa, 5); StyleConstants.setRightIndent(sa, 5); Style style = doc.addStyle("name", null); StyleConstants.setForeground(style, color); StyleConstants.setBold(style, bold); try { doc.insertString(doc.getLength(), s, style); } catch (BadLocationException e) { e.printStackTrace(); } doc.setParagraphAttributes(0, doc.getLength(), sa, false); }
/** Creates new form AlgorithmLogPanel */ public AlgorithmLogPanel() { initComponents(); StyledDocument doc = (StyledDocument) jTextPane1.getDocument(); // Create a style object and then set the style attributes Style defaultStyle = doc.addStyle("Default", null); StyleConstants.setItalic(defaultStyle, true); StyleConstants.setFontFamily(defaultStyle, "SansSerif"); Style infoStyle = doc.addStyle("Info", null); StyleConstants.setItalic(infoStyle, true); StyleConstants.setFontFamily(infoStyle, "SansSerif"); StyleConstants.setForeground(infoStyle, Color.LIGHT_GRAY); Style errorStyle = doc.addStyle("Error", null); StyleConstants.setFontFamily(errorStyle, "SansSerif"); StyleConstants.setForeground(errorStyle, Color.RED); dateFormat = new SimpleDateFormat("HH:mm:ss"); }
public static void appendIconAndText(JTextPane textPane, Image image, String message) { StyledDocument document = (StyledDocument)textPane.getDocument(); image = StatusMessageImageConverter.convertImage(image); try { JLabel jl = JLabelFactory.createJLabel("<html>" + message + "</html>", image); jl.setHorizontalAlignment(SwingConstants.LEFT); String styleName = "style"+message; Style textStyle = document.addStyle(styleName, null); StyleConstants.setComponent(textStyle, jl); document.insertString(document.getLength(), " ", document.getStyle(styleName)); } catch (BadLocationException e) { throw new IllegalStateException(e); } }
public static void appendIcon(JTextPane textPane, Image image) { StyledDocument document = (StyledDocument)textPane.getDocument(); image = StatusMessageImageConverter.convertImage(image); try { JLabel jl = JLabelFactory.createJLabel(image); jl.setHorizontalAlignment(SwingConstants.LEFT); String styleName = "style"+image.toString(); Style textStyle = document.addStyle(styleName, null); StyleConstants.setComponent(textStyle, jl); document.insertString(document.getLength(), " ", document.getStyle(styleName)); } catch (BadLocationException e) { throw new IllegalStateException(e); } }
public static void appendTextUsingLabel(JTextPane textPane, String message) { StyledDocument document = (StyledDocument)textPane.getDocument(); try { JLabel jl = JLabelFactory.createJLabel(message); jl.setHorizontalAlignment(SwingConstants.LEFT); String styleName = "style"+message; Style textStyle = document.addStyle(styleName, null); StyleConstants.setComponent(textStyle, jl); document.insertString(document.getLength(), " ", document.getStyle(styleName)); } catch (BadLocationException e) { throw new IllegalStateException(e); } }
protected void addStylesToDocument(JTextPane outputArea) { StyledDocument doc = outputArea.getStyledDocument(); Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE); Style regular = doc.addStyle("regular", def); StyleConstants.setFontFamily(def, "Monospaced"); promptStyle = doc.addStyle("prompt", regular); StyleConstants.setForeground(promptStyle, Color.BLUE); commandStyle = doc.addStyle("command", regular); StyleConstants.setForeground(commandStyle, Color.MAGENTA); outputStyle = doc.addStyle("output", regular); StyleConstants.setBold(outputStyle, true); }
public void append(String s, Color color, Boolean bold) { try { StyledDocument doc = (StyledDocument) text.getDocument(); Style style = doc.addStyle("name", null); StyleConstants.setForeground(style, color); StyleConstants.setBold(style, bold); doc.insertString(doc.getLength(), s, style); // Determine whether the scrollbar is currently at the very bottom // position. JScrollBar vbar = scrollpane.getVerticalScrollBar(); boolean autoScroll = ((vbar.getValue() + vbar.getVisibleAmount()) == vbar .getMaximum()); // append to the JTextArea (that's wrapped in a JScrollPane named // 'scrollPane' // now scroll if we were already at the bottom. if (autoScroll) { text.setCaretPosition(text.getDocument().getLength()); } } catch (BadLocationException exc) { exc.printStackTrace(); } }
/** * Add styles to the document. * * @param doc the StyledDocument to add styles to */ protected void addStylesToDocument(final StyledDocument doc) { //Initialize some styles. Style def = StyleContext.getDefaultStyleContext(). getStyle(StyleContext.DEFAULT_STYLE); Style regular = doc.addStyle("regular", def); StyleConstants.setFontFamily(def, "SansSerif"); StyleConstants.setFontSize(def, 12); StyleConstants.setLineSpacing(def, 2.0f); Style s = doc.addStyle("italic", regular); StyleConstants.setItalic(s, true); s = doc.addStyle("bold", regular); StyleConstants.setBold(s, true); s = doc.addStyle("small", regular); StyleConstants.setFontSize(s, 10); s = doc.addStyle("large", regular); StyleConstants.setFontSize(s, 14); }
public void append(String s, Color color, Boolean bold) { try { StyledDocument doc = super.getStyledDocument(); SimpleAttributeSet sa = new SimpleAttributeSet(); StyleConstants.setAlignment(sa, StyleConstants.ALIGN_JUSTIFIED); StyleConstants.setLeftIndent(sa, 10); StyleConstants.setRightIndent(sa, 10); StyleConstants.setSpaceAbove(sa, 5); StyleConstants.setSpaceBelow(sa, 5); Style style = doc.addStyle("name", null); StyleConstants.setForeground(style, color); StyleConstants.setBold(style, bold); doc.insertString(doc.getLength(), s, style); doc.setParagraphAttributes(0, doc.getLength(), sa, false); } catch (BadLocationException exc) { exc.printStackTrace(); } }