Example usage for javax.swing.text StyleConstants setFontFamily

List of usage examples for javax.swing.text StyleConstants setFontFamily

Introduction

In this page you can find the example usage for javax.swing.text StyleConstants setFontFamily.

Prototype

public static void setFontFamily(MutableAttributeSet a, String fam) 

Source Link

Document

Sets the font attribute.

Usage

From source file:gda.gui.BatonPanel.java

private JTextPane getLogPanel() {
    if (logPanel == null) {
        logPanel = new JTextPane();
        logPanel.setEditable(false);/*from w ww . j a v  a2 s  .c om*/
        StyledDocument doc = logPanel.getStyledDocument();

        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);
    }
    return logPanel;
}

From source file:dylemator.DylematorUI.java

private void displayCenteredText(String text) {
    textArea.setText("");
    text = "\n\n\n" + text;
    SimpleAttributeSet attribs = new SimpleAttributeSet();
    if (sd == null)
        sd = new SettingsDialog(this, true);
    float fnt = sd.getFontSize();
    StyledDocument doc = (StyledDocument) textArea.getDocument();
    Style style = doc.getStyle(StyleContext.DEFAULT_STYLE);//doc.addStyle("MyStyle",null);
    StyleConstants.setFontSize(style, (int) fnt);
    StyleConstants.setLineSpacing(attribs, 0.5f);
    StyleConstants.setFontFamily(style, "Segoe UI");
    StyleConstants.setAlignment(attribs, StyleConstants.ALIGN_CENTER);
    textArea.setParagraphAttributes(attribs, true);

    try {/*from w ww .j  a v a 2s  . c o  m*/
        doc.insertString(doc.getLength(), text, style);
        doc.setParagraphAttributes(0, doc.getLength() - 1, attribs, false);
    } catch (BadLocationException ex) {
        Logger.getLogger(DylematorUI.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:ome.formats.importer.gui.GuiImporter.java

/**
 * This method appends data to the output window
 * //from  w w w  . j a va 2  s .com
 * @param s - text to append
 */
public void appendToOutput(String s) {
    try {
        StyledDocument doc = (StyledDocument) outputTextPane.getDocument();
        Style style = doc.addStyle("StyleName", null);
        StyleConstants.setForeground(style, Color.black);
        StyleConstants.setFontFamily(style, "SansSerif");
        StyleConstants.setFontSize(style, 12);
        StyleConstants.setBold(style, false);

        doc.insertString(doc.getLength(), s, style);

        //trim the document size so it doesn't grow to big
        int maxChars = 200000;
        if (doc.getLength() > maxChars)
            doc.remove(0, doc.getLength() - maxChars);

        //outputTextPane.setDocument(doc);
    } catch (BadLocationException e) {
    }
}

From source file:ome.formats.importer.gui.GuiImporter.java

/**
 * This method appends data to the output window.
 * //from   w  ww  .j  a va 2  s . c o  m
 * @param s - string to append
 */
public void appendToDebug(String s) {
    log.debug(s);
    try {
        StyledDocument doc = (StyledDocument) debugTextPane.getDocument();

        Style style = doc.addStyle("StyleName", null);
        StyleConstants.setForeground(style, Color.black);
        StyleConstants.setFontFamily(style, "SansSerif");
        StyleConstants.setFontSize(style, 12);
        StyleConstants.setBold(style, false);

        doc.insertString(doc.getLength(), s, style);

        //trim the document size so it doesn't grow to big
        int maxChars = 200000;
        if (doc.getLength() > maxChars)
            doc.remove(0, doc.getLength() - maxChars);

        //debugTextPane.setDocument(doc);
    } catch (BadLocationException e) {
    }
}

From source file:org.astrojournal.logging.JTextPaneAppender.java

/**
 * Add the graphical JTextPane to capture the logging information.
 * /*from   ww  w  .  j  av a2 s  .  co  m*/
 * @param textPane
 */
public static void addJTextPane(final JTextPane textPane) {
    // The application graphical output
    jTextPane = textPane;

    // Define JTextPane styles
    StyledDocument doc = jTextPane.getStyledDocument();
    styleRegular = doc.addStyle("regular", null);
    StyleConstants.setForeground(styleRegular, Color.BLACK);
    StyleConstants.setFontSize(styleRegular, 12);
    StyleConstants.setFontFamily(styleRegular, "Arial");

    styleBold = doc.addStyle("bold", styleRegular);
    StyleConstants.setBold(styleBold, true);

    styleItalic = doc.addStyle("italic", styleRegular);
    StyleConstants.setItalic(styleItalic, true);

    styleSmall = doc.addStyle("small", styleRegular);
    StyleConstants.setFontSize(styleSmall, 11);

    styleSmallItalic = doc.addStyle("smallItalic", styleSmall);
    StyleConstants.setItalic(styleSmallItalic, true);

    styleRed = doc.addStyle("red", styleBold);
    StyleConstants.setForeground(styleRed, Color.RED);

    styleBlue = doc.addStyle("blue", styleSmall);
    StyleConstants.setForeground(styleBlue, Color.BLUE);

    // Remove the Appender Console as the GUI is being initialised and
    // therefore having this information twice is not desirable.
    Logger logger = LogManager.getLogger("Console");
    org.apache.logging.log4j.core.Logger coreLogger = (org.apache.logging.log4j.core.Logger) logger;
    LoggerContext context = coreLogger.getContext();
    coreLogger.removeAppender(context.getConfiguration().getAppender("Console"));
}

From source file:org.docx4all.ui.main.WordMLEditor.java

private JEditorPane createSourceView(WordMLTextPane editorView) {
    //Create the Source View
    JEditorPane sourceView = new JEditorPane();

    MutableAttributeSet attrs = new SimpleAttributeSet();
    StyleConstants.setFontFamily(attrs, FontManager.getInstance().getSourceViewFontFamilyName());
    StyleConstants.setFontSize(attrs, FontManager.getInstance().getSourceViewFontSize());

    // TODO - only do this if the font is available.
    Font font = new Font("Arial Unicode MS", Font.PLAIN, 12);

    System.out.println(font.getFamily());
    System.out.println(font.getFontName());
    System.out.println(font.getPSName());

    sourceView.setFont(font);/*w w w . j a va  2s.  co m*/
    //sourceView.setFont(FontManager.getInstance().getFontInAction(attrs));

    sourceView.setContentType("text/xml; charset=UTF-16");

    // Instantiate a XMLEditorKit with wrapping enabled.
    XMLEditorKit kit = new XMLEditorKit(true);
    // Set the wrapping style.
    kit.setWrapStyleWord(true);

    sourceView.setEditorKit(kit);

    WordMLDocument editorViewDoc = (WordMLDocument) editorView.getDocument();

    try {
        editorViewDoc.readLock();

        editorView.getWordMLEditorKit().saveCaretText();

        DocumentElement elem = (DocumentElement) editorViewDoc.getDefaultRootElement();
        WordprocessingMLPackage wmlPackage = ((DocumentML) elem.getElementML()).getWordprocessingMLPackage();
        String filePath = (String) editorView.getDocument().getProperty(WordMLDocument.FILE_PATH_PROPERTY);

        //Do not include the last paragraph which is an extra paragraph.
        elem = (DocumentElement) elem.getElement(elem.getElementCount() - 1);
        ElementML paraML = elem.getElementML();
        ElementML bodyML = paraML.getParent();
        paraML.delete();

        Document doc = DocUtil.read(sourceView, wmlPackage);
        doc.putProperty(WordMLDocument.FILE_PATH_PROPERTY, filePath);
        doc.putProperty(WordMLDocument.WML_PACKAGE_PROPERTY, wmlPackage);
        doc.addDocumentListener(getToolbarStates());

        //Below are the properties used by bounce.jar library
        //See http://www.edankert.com/bounce/xmleditorkit.html
        doc.putProperty(PlainDocument.tabSizeAttribute, new Integer(4));
        doc.putProperty(XMLDocument.AUTO_INDENTATION_ATTRIBUTE, Boolean.TRUE);
        doc.putProperty(XMLDocument.TAG_COMPLETION_ATTRIBUTE, Boolean.TRUE);

        //Remember to put 'paraML' as last paragraph
        bodyML.addChild(paraML);

    } finally {
        editorViewDoc.readUnlock();
    }

    kit.setStyle(XMLStyleConstants.ATTRIBUTE_NAME, new Color(255, 0, 0), Font.PLAIN);

    sourceView.addFocusListener(getToolbarStates());
    //sourceView.setDocument(doc);
    sourceView.putClientProperty(Constants.LOCAL_VIEWS_SYNCHRONIZED_FLAG, Boolean.TRUE);

    return sourceView;
}

From source file:org.openmicroscopy.shoola.agents.fsimporter.view.ImporterUI.java

/**
 * Creates the component hosting the debug text.
 * //from   w  w  w .j a  va  2s.  c  o  m
 * @return See above.
 */
private JComponent createDebugTab() {
    debugTextPane = new JTextPane();
    debugTextPane.setEditable(false);
    StyledDocument doc = (StyledDocument) debugTextPane.getDocument();

    Style style = doc.addStyle(STYLE, null);
    StyleConstants.setForeground(style, Color.black);
    StyleConstants.setFontFamily(style, "SansSerif");
    StyleConstants.setFontSize(style, 12);
    StyleConstants.setBold(style, false);

    JScrollPane sp = new JScrollPane(debugTextPane);
    sp.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
        public void adjustmentValueChanged(AdjustmentEvent e) {
            try {
                debugTextPane.setCaretPosition(debugTextPane.getDocument().getLength());
            } catch (IllegalArgumentException ex) {
                //
            }
        }
    });
    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.add(sp, BorderLayout.CENTER);
    return panel;
}

From source file:org.openmicroscopy.shoola.util.ui.UIUtilities.java

/** Builds the UI component displaying the exception.*/
public static JTextPane buildExceptionArea() {
    StyleContext context = new StyleContext();
    StyledDocument document = new DefaultStyledDocument(context);

    JTextPane textPane = new JTextPane(document);
    textPane.setOpaque(false);//from w ww.j  a  v  a 2 s.  co m
    textPane.setEditable(false);

    // Create one of each type of tab stop
    List<TabStop> list = new ArrayList<TabStop>();

    // Create a left-aligned tab stop at 100 pixels from the left margin
    float pos = 15;
    int align = TabStop.ALIGN_LEFT;
    int leader = TabStop.LEAD_NONE;
    TabStop tstop = new TabStop(pos, align, leader);
    list.add(tstop);

    // Create a right-aligned tab stop at 200 pixels from the left margin
    pos = 15;
    align = TabStop.ALIGN_RIGHT;
    leader = TabStop.LEAD_NONE;
    tstop = new TabStop(pos, align, leader);
    list.add(tstop);

    // Create a center-aligned tab stop at 300 pixels from the left margin
    pos = 15;
    align = TabStop.ALIGN_CENTER;
    leader = TabStop.LEAD_NONE;
    tstop = new TabStop(pos, align, leader);
    list.add(tstop);

    // Create a decimal-aligned tab stop at 400 pixels from the left margin
    pos = 15;
    align = TabStop.ALIGN_DECIMAL;
    leader = TabStop.LEAD_NONE;
    tstop = new TabStop(pos, align, leader);
    list.add(tstop);

    // Create a tab set from the tab stops
    TabSet tabs = new TabSet(list.toArray(new TabStop[0]));

    // Add the tab set to the logical style;
    // the logical style is inherited by all paragraphs
    Style style = textPane.getLogicalStyle();
    StyleConstants.setTabSet(style, tabs);
    textPane.setLogicalStyle(style);
    Style debugStyle = document.addStyle("StyleName", null);
    StyleConstants.setForeground(debugStyle, Color.BLACK);
    StyleConstants.setFontFamily(debugStyle, "SansSerif");
    StyleConstants.setFontSize(debugStyle, 12);
    StyleConstants.setBold(debugStyle, false);
    return textPane;
}

From source file:org.smart.migrate.ui.ImportThread.java

private 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);

    s = doc.addStyle("icon", regular);
    StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);

    s = doc.addStyle("info", regular);
    StyleConstants.setForeground(s, Color.blue);

    s = doc.addStyle("error", regular);
    StyleConstants.setForeground(s, Color.red);

    s = doc.addStyle("warning", regular);
    StyleConstants.setForeground(s, Color.orange);

    s = doc.addStyle("gray", regular);
    StyleConstants.setForeground(s, Color.gray);

    s = doc.addStyle("success", regular);
    StyleConstants.setForeground(s, Color.green);
}

From source file:org.thdl.tib.input.TibetanConverter.java

/** Reads from in, closes in, converts (or finds some/all
non-TM/TMW), writes the result to out, does not close out.
The action taken depends on ct, which must be one of a set
number of strings -- see the code.  Uses short error and
warning messages if shortMessages is true; gives no warnings
or many warnings depending on warningLevel.  Returns an
appropriate return code so that TibetanConverter's usage
message is honored. *///  w  ww  .j ava 2 s  . c o  m
static int reallyConvert(InputStream in, PrintStream out, String ct, String warningLevel, boolean shortMessages,
        boolean colors) {
    if (UNI_TO_WYLIE_TEXT == ct || WYLIE_TO_ACIP_TEXT == ct || ACIP_TO_WYLIE_TEXT == ct) {
        try {
            /*String uniText;
            {
            // TODO(dchandler): use, here and elsewhere in the
            // codebase,
            // org.apache.commons.io.IOUtils.toString(InputStream,
            // encoding)
            StringBuffer s = new StringBuffer();
            char ch[] = new char[8192];
            BufferedReader bin
                = new BufferedReader(new InputStreamReader(in,
                                                           "UTF-8"));
            int amt;
            while (-1 != (amt = bin.read(ch))) {
                s.append(ch, 0, amt);
            }
            bin.close();
            uniText = s.toString();
            }
            StringBuffer errors = new StringBuffer();
            // TODO(dchandler): DLC: use human-friendly EWTS, not
            // computer-friendly!
            String ewtsText = Converter.convertToEwtsForComputers(uniText,
                                                              errors);
            // TODO(dchandler): is 51 the right choice?
            return (errors.length() > 0) ? 51 : 0;*/
            BasicTibetanTranscriptionConverter bc = null;
            if (UNI_TO_WYLIE_TEXT == ct)
                bc = new BasicTibetanTranscriptionConverter(
                        new BufferedReader(new InputStreamReader(in, "UTF16")), new PrintWriter(out));
            else
                bc = new BasicTibetanTranscriptionConverter(new BufferedReader(new InputStreamReader(in)),
                        new PrintWriter(out));
            bc.run(ct);
            return 0;
        } catch (IOException e) {
            // TODO(dchandler): print it?  where to?
            return 48;
        }
    } else if (ACIP_TO_UNI_TEXT == ct || ACIP_TO_TMW == ct || WYLIE_TO_UNI_TEXT == ct || WYLIE_TO_TMW == ct) {
        try {
            ArrayList al = ((ACIP_TO_UNI_TEXT == ct || ACIP_TO_TMW == ct) ? (TTraits) ACIPTraits.instance()
                    : (TTraits) EWTSTraits.instance()).scanner().scanStream(
                            in, null,
                            ThdlOptions.getIntegerOption((ACIP_TO_UNI_TEXT == ct || ACIP_TO_TMW == ct)
                                    ? "thdl.most.errors.a.tibetan.acip.document.can.have"
                                    : "thdl.most.errors.a.tibetan.ewts.document.can.have", 1000 - 1),
                            shortMessages, warningLevel);
            if (null == al)
                return 47;
            boolean embeddedWarnings = (warningLevel != "None");
            boolean hasWarnings[] = new boolean[] { false };
            if (ACIP_TO_UNI_TEXT == ct || WYLIE_TO_UNI_TEXT == ct) {
                if (!TConverter.convertToUnicodeText(
                        (WYLIE_TO_UNI_TEXT == ct) ? (TTraits) EWTSTraits.instance()
                                : (TTraits) ACIPTraits.instance(),
                        al, out, null, null, hasWarnings, embeddedWarnings, warningLevel, shortMessages))
                    return 46;
            } else {
                if (!TConverter.convertToTMW(
                        (WYLIE_TO_TMW == ct) ? (TTraits) EWTSTraits.instance()
                                : (TTraits) ACIPTraits.instance(),
                        al, out, null, null, hasWarnings, embeddedWarnings, warningLevel, shortMessages,
                        colors))
                    return 46;
            }
            if (embeddedWarnings && hasWarnings[0])
                return 45;
            else
                return 0;
        } catch (IOException e) {
            // TODO(dchandler): print it?  where to?
            return 48;
        }
    } else {
        TibetanDocument tdoc = new TibetanDocument();
        {
            SimpleAttributeSet ras = new SimpleAttributeSet();
            StyleConstants.setFontFamily(ras,
                    ThdlOptions.getStringOption("thdl.default.roman.font.face", "Serif"));
            StyleConstants.setFontSize(ras, ThdlOptions.getIntegerOption("thdl.default.roman.font.size", 14));
            tdoc.setRomanAttributeSet(ras);
        }
        try {
            // Read in the rtf file.
            if (debug)
                System.err.println("Start: reading in old RTF file");
            if (!ThdlOptions.getBooleanOption("thdl.do.not.fix.rtf.hex.escapes"))
                in = new RTFFixerInputStream(in);
            (new RTFEditorKit()).read(in, tdoc, 0);
            if (debug)
                System.err.println("End  : reading in old RTF file");
        } catch (Exception e) {
            out.println("TibetanConverter:\n" + rtfErrorMessage);
            return 3;
        }
        try {
            in.close();
        } catch (IOException e) {
            // silently ignore; we don't care about the input so much...
            ThdlDebug.noteIffyCode();
        }

        if (FIND_ALL_NON_TMW == ct) {
            // 0, -1 is the entire document.
            int exitCode = tdoc.findAllNonTMWCharacters(0, -1, out);
            if (out.checkError())
                exitCode = 41;
            return exitCode;
        } else if (FIND_SOME_NON_TMW == ct) {
            // 0, -1 is the entire document.
            int exitCode = tdoc.findSomeNonTMWCharacters(0, -1, out);
            if (out.checkError())
                exitCode = 41;
            return exitCode;
        } else if (FIND_SOME_NON_TM == ct) {
            // 0, -1 is the entire document.
            int exitCode = tdoc.findSomeNonTMCharacters(0, -1, out);
            if (out.checkError())
                exitCode = 41;
            return exitCode;
        } else if (FIND_ALL_NON_TM == ct) {
            // 0, -1 is the entire document.
            int exitCode = tdoc.findAllNonTMCharacters(0, -1, out);
            if (out.checkError())
                exitCode = 41;
            return exitCode;
        } else { // conversion {to Wylie or TM} mode
            // Fix curly braces in the entire document if the input is TMW:
            if (TM_TO_TMW != ct) {
                // DLC make me optional
                if (debug)
                    System.err.println("Start: solving curly brace problem");
                tdoc.replaceTahomaCurlyBracesAndBackslashes(0, -1);
                if (debug)
                    System.err.println("End  : solving curly brace problem");
            }

            int exitCode = 0;
            ThdlDebug.verify(((TMW_TO_TM == ct) ? 1 : 0) + ((TMW_TO_SAME_TMW == ct) ? 1 : 0)
                    + ((TMW_TO_UNI == ct) ? 1 : 0) + ((TM_TO_TMW == ct) ? 1 : 0) + ((TMW_TO_ACIP == ct) ? 1 : 0)
                    + ((TMW_TO_ACIP_TEXT == ct) ? 1 : 0) + ((TMW_TO_WYLIE == ct) ? 1 : 0)
                    + ((TMW_TO_WYLIE_TEXT == ct) ? 1 : 0) == 1);
            long numAttemptedReplacements[] = new long[] { 0 };
            if (TMW_TO_SAME_TMW == ct) {
                // Identity conversion for testing
                if (tdoc.identityTmwToTmwConversion(0, tdoc.getLength(), numAttemptedReplacements)) {
                    exitCode = 50;
                }
            } else if (TMW_TO_WYLIE == ct || TMW_TO_WYLIE_TEXT == ct) {
                // Convert to THDL Wylie:
                if (!tdoc.toWylie(0, tdoc.getLength(), numAttemptedReplacements)) {
                    exitCode = 44;
                }
            } else if (TMW_TO_ACIP == ct || TMW_TO_ACIP_TEXT == ct) {
                // Convert to ACIP:
                if (!tdoc.toACIP(0, tdoc.getLength(), numAttemptedReplacements)) {
                    exitCode = 49;
                }
            } else if (TMW_TO_UNI == ct) {
                StringBuffer errors = new StringBuffer();
                // Convert to Unicode:
                if (tdoc.convertToUnicode(0, tdoc.getLength(), errors,
                        ThdlOptions.getStringOption("thdl.tmw.to.unicode.font").intern(),
                        numAttemptedReplacements)) {
                    System.err.println(errors);
                    exitCode = 42;
                }
            } else if (TM_TO_TMW == ct) {
                StringBuffer errors = new StringBuffer();
                // Convert to TibetanMachineWeb:
                if (tdoc.convertToTMW(0, tdoc.getLength(), errors, numAttemptedReplacements)) {
                    System.err.println(errors);
                    exitCode = 42;
                }
            } else {
                ThdlDebug.verify(TMW_TO_TM == ct);
                StringBuffer errors = new StringBuffer();
                // Convert to TibetanMachine:
                if (tdoc.convertToTM(0, tdoc.getLength(), errors, numAttemptedReplacements)) {
                    System.err.println(errors);
                    exitCode = 42;
                }
            }

            // Write to standard output the result:
            if (TMW_TO_WYLIE_TEXT == ct || TMW_TO_ACIP_TEXT == ct) {
                try {
                    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
                    tdoc.writeTextOutput(bw);
                    bw.flush();
                } catch (IOException e) {
                    exitCode = 40;
                }
            } else {
                try {
                    tdoc.writeRTFOutputStream(out);
                } catch (IOException e) {
                    exitCode = 40;
                }
            }
            if (out.checkError())
                exitCode = 41;
            if (numAttemptedReplacements[0] < 1)
                exitCode = 43;

            return exitCode;
        }
    }
}