javax.swing.text.StyledDocument#addStyle ( )源码实例Demo

下面列出了javax.swing.text.StyledDocument#addStyle ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: Spade   文件: AboutDialog.java
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);
}
 
源代码2 项目: netbeans   文件: VCSHyperlinkSupport.java
@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);
}
 
源代码3 项目: org.alloytools.alloy   文件: SwingLogPanel.java
/** 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();
}
 
源代码4 项目: org.alloytools.alloy   文件: SwingLogPanel.java
/** 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);
    }
}
 
源代码5 项目: WorldGrower   文件: JTextPaneUtils.java
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);
	}
}
 
源代码6 项目: WorldGrower   文件: JTextPaneUtils.java
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);
	}
}
 
源代码7 项目: consulo   文件: Browser.java
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);
}
 
源代码8 项目: groovy   文件: ConsoleSupport.java
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);
}
 
源代码9 项目: java-swing-tips   文件: MainPanel.java
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));
}
 
源代码10 项目: FancyBing   文件: GuiUtil.java
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);
}
 
源代码11 项目: netbeans   文件: StackTraceSupport.java
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);
    }
}
 
源代码12 项目: binnavi   文件: BaseTypeTableCellRenderer.java
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;
}
 
源代码13 项目: binnavi   文件: BaseTypeTableCellRenderer.java
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;
}
 
/**
 * 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");
}
 
源代码15 项目: dsworkbench   文件: AttackCalculationPanel.java
/**
 * 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");
}
 
源代码16 项目: dsworkbench   文件: RetimerCalculationPanel.java
/**
 * 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<>();
}
 
/**
 * 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");
}
 
源代码18 项目: aion-germany   文件: ViewPane.java
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);
}
 
源代码19 项目: netbeans   文件: ModelItem.java
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);
}
 
源代码20 项目: netbeans   文件: AddModulePanel.java
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);
    }
}