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

The following examples show how to use javax.swing.text.StyledDocument#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: ConsoleSupport.java    From groovy with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 2
Source File: Browser.java    From consulo with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 3
Source File: AboutDialog.java    From Spade with GNU General Public License v3.0 6 votes vote down vote up
public static void addStylesToDocument(StyledDocument doc)
{
	//Initialize some styles.
	Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
	
	Style regular = doc.addStyle("regular", def);
	StyleConstants.setFontFamily(def, "SansSerif");
	
	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, 16);
	StyleConstants.setBold(s, true);
}
 
Example 4
Source File: JTextPaneUtils.java    From WorldGrower with GNU General Public License v3.0 6 votes vote down vote up
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);
	}
}
 
Example 5
Source File: JTextPaneUtils.java    From WorldGrower with GNU General Public License v3.0 6 votes vote down vote up
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);
	}
}
 
Example 6
Source File: VCSHyperlinkSupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 7
Source File: SwingLogPanel.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/** 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();
}
 
Example 8
Source File: SwingLogPanel.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/** 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);
    }
}
 
Example 9
Source File: BaseTypeTableCellRenderer.java    From binnavi with Apache License 2.0 5 votes vote down vote up
private static Style createDataStyle(final StyledDocument document) {
  final Style declStyle = document.addStyle("DATA_STYLE", null);
  StyleConstants.setBackground(declStyle, Color.WHITE);
  StyleConstants.setForeground(declStyle, Color.BLUE);
  StyleConstants.setFontFamily(declStyle, GuiHelper.getMonospaceFont());
  StyleConstants.setFontSize(declStyle, 11);
  return declStyle;
}
 
Example 10
Source File: DefenseCalculationSettingsPanel.java    From dsworkbench with Apache License 2.0 5 votes vote down vote up
/**
 * Creates new form AttackSourcePanel
 */
DefenseCalculationSettingsPanel() {
    initComponents();
    jXCollapsiblePane1.setLayout(new BorderLayout());
    jXCollapsiblePane1.add(jInfoScrollPane, BorderLayout.CENTER);
    jInfoTextPane.setText(GENERAL_INFO);
    StyledDocument doc = (StyledDocument) jTextPane1.getDocument();
    Style defaultStyle = doc.addStyle("Default", null);
    StyleConstants.setItalic(defaultStyle, true);
    StyleConstants.setFontFamily(defaultStyle, "SansSerif");
    dateFormat = new SimpleDateFormat("HH:mm:ss");
}
 
Example 11
Source File: RetimerCalculationPanel.java    From dsworkbench with Apache License 2.0 5 votes vote down vote up
/**
 * Creates new form AttackSourcePanel
 */
RetimerCalculationPanel() {
    initComponents();
    jXCollapsiblePane1.setLayout(new BorderLayout());
    jXCollapsiblePane1.add(jInfoScrollPane, BorderLayout.CENTER);
    jInfoTextPane.setText(GENERAL_INFO);
    StyledDocument doc = (StyledDocument) jTextPane1.getDocument();
    Style defaultStyle = doc.addStyle("Default", null);
    StyleConstants.setItalic(defaultStyle, true);
    StyleConstants.setFontFamily(defaultStyle, "SansSerif");
    dateFormat = new SimpleDateFormat("HH:mm:ss");
    retimes = new LinkedList<>();
}
 
Example 12
Source File: AttackCalculationPanel.java    From dsworkbench with Apache License 2.0 5 votes vote down vote up
/**
 * Creates new form AttackSourcePanel
 */
AttackCalculationPanel() {
    initComponents();
    jXCollapsiblePane1.setLayout(new BorderLayout());
    jXCollapsiblePane1.add(jInfoScrollPane, BorderLayout.CENTER);
    jInfoTextPane.setText(GENERAL_INFO);
    StyledDocument doc = (StyledDocument) jTextPane1.getDocument();
    Style defaultStyle = doc.addStyle("Default", null);
    StyleConstants.setItalic(defaultStyle, true);
    StyleConstants.setFontFamily(defaultStyle, "SansSerif");
    dateFormat = new SimpleDateFormat("HH:mm:ss");
}
 
Example 13
Source File: SupportRefillCalculationPanel.java    From dsworkbench with Apache License 2.0 5 votes vote down vote up
/**
 * Creates new form AttackSourcePanel
 */
SupportRefillCalculationPanel() {
    initComponents();
    jXCollapsiblePane1.setLayout(new BorderLayout());
    jXCollapsiblePane1.add(jInfoScrollPane, BorderLayout.CENTER);
    jInfoTextPane.setText(GENERAL_INFO);
    StyledDocument doc = (StyledDocument) jTextPane1.getDocument();
    Style defaultStyle = doc.addStyle("Default", null);
    StyleConstants.setItalic(defaultStyle, true);
    StyleConstants.setFontFamily(defaultStyle, "SansSerif");
    dateFormat = new SimpleDateFormat("HH:mm:ss");
}
 
Example 14
Source File: BaseTypeTableCellRenderer.java    From binnavi with Apache License 2.0 5 votes vote down vote up
private static Style createDeclarationStyle(final StyledDocument document) {
  final Style declStyle = document.addStyle("DECL_STYLE", null);
  StyleConstants.setBackground(declStyle, Color.WHITE);
  StyleConstants.setForeground(declStyle, Color.BLACK);
  StyleConstants.setFontFamily(declStyle, GuiHelper.getMonospaceFont());
  StyleConstants.setFontSize(declStyle, 11);
  return declStyle;
}
 
Example 15
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
private MainPanel() {
  super(new BorderLayout(5, 5));
  JButton ok = new JButton("Test");
  ok.addActionListener(e -> append("Test test test test", true));

  JButton err = new JButton("Error");
  err.addActionListener(e -> append("Error error error error", false));

  JButton clr = new JButton("Clear");
  clr.addActionListener(e -> jtp.setText(""));

  Box box = Box.createHorizontalBox();
  box.add(Box.createHorizontalGlue());
  box.add(ok);
  box.add(err);
  box.add(Box.createHorizontalStrut(5));
  box.add(clr);

  jtp.setEditable(false);
  StyledDocument doc = jtp.getStyledDocument();
  // Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
  Style def = doc.getStyle(StyleContext.DEFAULT_STYLE);

  // Style regular = doc.addStyle("regular", def);
  // StyleConstants.setForeground(def, Color.BLACK);

  Style error = doc.addStyle("error", def);
  StyleConstants.setForeground(error, Color.RED);

  JScrollPane scroll = new JScrollPane(jtp);
  scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  scroll.getVerticalScrollBar().setUnitIncrement(25);

  add(scroll);
  add(box, BorderLayout.SOUTH);
  setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
  setPreferredSize(new Dimension(320, 240));
}
 
Example 16
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 17
Source File: GuiUtil.java    From FancyBing with GNU General Public License v3.0 5 votes vote down vote up
public static void addStyle(JTextPane textPane, String name,
                            Color foreground, Color background,
                            boolean bold)
{
    StyledDocument doc = textPane.getStyledDocument();
    StyleContext context = StyleContext.getDefaultStyleContext();
    Style def = context.getStyle(StyleContext.DEFAULT_STYLE);
    Style style = doc.addStyle(name, def);
    if (foreground != null)
        StyleConstants.setForeground(style, foreground);
    if (background != null)
        StyleConstants.setBackground(style, background);
    StyleConstants.setBold(style, bold);
}
 
Example 18
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 19
Source File: ModelItem.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void updateFramesImpl(JEditorPane pane, boolean rawData) throws BadLocationException {
    Style defaultStyle = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
    StyledDocument doc = (StyledDocument)pane.getDocument();
    Style timingStyle = doc.addStyle("timing", defaultStyle);
    StyleConstants.setForeground(timingStyle, Color.lightGray);
    Style infoStyle = doc.addStyle("comment", defaultStyle);
    StyleConstants.setForeground(infoStyle, Color.darkGray);
    StyleConstants.setBold(infoStyle, true);
    SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss:SSS");
    pane.setText("");
    StringBuilder sb = new StringBuilder();
    int lastFrameType = -1;
    for (Network.WebSocketFrame f : wsRequest.getFrames()) {
        int opcode = f.getOpcode();
        if (opcode == 0) { // "continuation frame"
            opcode = lastFrameType;
        } else {
            lastFrameType = opcode;
        }
        if (opcode == 1) { // "text frame"
            if (!rawData) {
                doc.insertString(doc.getLength(), formatter.format(f.getTimestamp()), timingStyle);
                doc.insertString(doc.getLength(), f.getDirection() == Network.Direction.SEND ? " SENT " : " RECV ", timingStyle);
            }
            doc.insertString(doc.getLength(), f.getPayload()+"\n", defaultStyle);
        } else if (opcode == 2) { // "binary frame"
            if (!rawData) {
                doc.insertString(doc.getLength(), formatter.format(f.getTimestamp()), timingStyle);
                doc.insertString(doc.getLength(), f.getDirection() == Network.Direction.SEND ? " SENT " : " RECV ", timingStyle);
            }
            // XXX: binary data???
            doc.insertString(doc.getLength(), f.getPayload()+"\n", defaultStyle);
        } else if (opcode == 8) { // "close frame"
            if (!rawData) {
                doc.insertString(doc.getLength(), formatter.format(f.getTimestamp()), timingStyle);
                doc.insertString(doc.getLength(), f.getDirection() == Network.Direction.SEND ? " SENT " : " RECV ", timingStyle);
            }
            doc.insertString(doc.getLength(), "Frame closed\n", infoStyle);
        }
    }
    data = sb.toString();
    pane.setCaretPosition(0);
}
 
Example 20
Source File: ViewPane.java    From aion-germany with GNU General Public License v3.0 4 votes vote down vote up
private void addStylesToHexDump(StyledDocument doc) {
	Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);

	Style base = doc.addStyle("base", def);
	StyleConstants.setFontFamily(base, "Monospaced");

	Style regular = doc.addStyle("regular", base);
	//StyleConstants.setUnderline(regular , true);

	Style s = doc.addStyle("d", regular);
	StyleConstants.setBackground(s, new Color(72, 164, 255));

	s = doc.addStyle("dbs", regular);
	StyleConstants.setBackground(s, new Color(72, 164, 255));

	s = doc.addStyle("Q", regular);
	StyleConstants.setBackground(s, new Color(255, 255, 128));

	s = doc.addStyle("h", regular);
	StyleConstants.setBackground(s, Color.ORANGE);

	s = doc.addStyle("hbs", regular);
	StyleConstants.setBackground(s, Color.ORANGE);

	s = doc.addStyle("s", regular);
	StyleConstants.setBackground(s, new Color(156, 220, 156));

	s = doc.addStyle("S", regular);
	StyleConstants.setBackground(s, new Color(156, 220, 156));

	s = doc.addStyle("c", regular);
	StyleConstants.setBackground(s, Color.PINK);

	s = doc.addStyle("cbs", regular);
	StyleConstants.setBackground(s, Color.PINK);

	s = doc.addStyle("f", regular);
	StyleConstants.setBackground(s, Color.LIGHT_GRAY);

	Color bxColor = new Color(255, 234, 213);
	s = doc.addStyle("b", regular);
	StyleConstants.setBackground(s, bxColor);
	s = doc.addStyle("x", regular);
	StyleConstants.setBackground(s, bxColor);

	s = doc.addStyle("op", regular);
	StyleConstants.setBackground(s, Color.YELLOW);

	s = doc.addStyle("selected", regular);
	StyleConstants.setBackground(s, Color.BLUE);

	s = doc.addStyle("chk", regular);
	StyleConstants.setBackground(s, Color.GREEN);
}