Example usage for javax.swing JEditorPane setFont

List of usage examples for javax.swing JEditorPane setFont

Introduction

In this page you can find the example usage for javax.swing JEditorPane setFont.

Prototype

@BeanProperty(preferred = true, visualUpdate = true, description = "The font for the component.")
public void setFont(Font font) 

Source Link

Document

Sets the font for this component.

Usage

From source file:Main.java

/**
 * Enforces JEditorPane font.//from   w  ww  .  j  ava  2  s.c o  m
 * Once the content type of a JEditorPane is set to text/html the font on the Pane starts to be managed by Swing.
 * This method forces using provided font.
 */
public static void enforceJEditorPaneFont(JEditorPane jEditorPane, Font font) {
    jEditorPane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
    jEditorPane.setFont(font);
}

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);
    //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);/* www .j a  v a  2  s . c o  m*/

    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.squidy.designer.knowledgebase.RepositoryItem.java

private void initPane() {
    // Add Zoomable Component
    new Thread(new Runnable() {
        public void run() {

            // ProgressIndicator indicator = new
            // ProgressIndicator(InformationShape.this);

            if (information == null || "".equals(information)) {
                return;
            }// w  ww  . jav  a2 s  .co  m

            URL url = null;
            try {
                try {
                    if (information.endsWith(".pdf")) {
                        url = InformationShape.class.getResource(information);
                    } else if (information.endsWith(".html")) {
                        try {
                            url = new URL(information);
                        } catch (Exception e) {
                            url = InformationShape.class.getResource(information);
                        }
                    } else {
                        url = new URL(information);
                    }
                } catch (Exception e) {
                    // do nothing
                }

                PNode cropNode;
                // PDF
                if (information.endsWith(".pdf")) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Display information as PDF.");
                    }
                    cropNode = new PDFPane(url.getFile());
                }
                // HTML
                else {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Display information as HTML.");
                    }

                    JEditorPane editorPane = new JEditorPane();
                    editorPane.setFont(internalFont.deriveFont(10f));

                    FontMetrics fm = editorPane.getFontMetrics(editorPane.getFont());
                    int editorWidth = 400;
                    editorPane.setPreferredSize(new Dimension(editorWidth,
                            FontUtils.getLineCount(information, editorWidth) * fm.getHeight()));

                    cropNode = JComponentWrapper.create(editorPane);
                    editorPane.setEditable(false);

                    if (information.endsWith(".html")) {
                        HTMLEditorKit editorKit = new HTMLEditorKit();
                        editorPane.setEditorKit(editorKit);
                        editorPane.setPage(url);
                        editorPane.setPreferredSize(new Dimension(800, 2000));
                    } else {
                        editorPane.setText(information);
                    }

                    // Prepare HTML Kit
                    // HTMLParser editorKit = new HTMLParser();
                    // HTMLParserCallback callback = new
                    // HTMLParserCallback();
                    // getComponentEditorPane().setEditorKit(editorKit);
                    // //Open connection
                    // InputStreamReader reader = new
                    // InputStreamReader(url.openStream());
                    // //Start parse process
                    // editorKit.getParser().parse(reader, callback, true);
                    // Wait until parsing process has finished
                    // try {
                    // Thread.sleep(2000);
                    // }
                    // catch (InterruptedException e) {
                    // if (LOG.isErrorEnabled()) {
                    // LOG.error("Error in " +
                    // InformationShape.class.getName() + ".", e);
                    // }
                    // }
                }

                cropScroll = new CropScroll(cropNode, new Dimension(1000, 700), 0.2);
                cropScroll.setOffset(
                        getBoundsReference().getCenterX() - cropScroll.getBoundsReference().getCenterX(), 250);
                addChild(cropScroll);
                invalidateFullBounds();
                invalidateLayout();
                invalidatePaint();
            } catch (MalformedURLException e) {
                if (LOG.isErrorEnabled()) {
                    LOG.error("Could not parse URL from input string: " + e.getMessage() + " in "
                            + RepositoryItem.class.getName() + ".\nInput was: " + information);
                }
            } catch (IOException e) {
                if (LOG.isErrorEnabled()) {
                    LOG.error("Could not create HTMLPane in " + RepositoryItem.class.getName(), e);
                }
            }

            // indicator.done();
        }
    }).start();
}

From source file:org.squidy.designer.zoom.impl.InformationShape.java

private void initPane() {
    // Add Zoomable Component
    new Thread(new Runnable() {
        public void run() {

            // ProgressIndicator indicator = new
            // ProgressIndicator(InformationShape.this);

            if (informationSource == null || "".equals(informationSource)) {
                return;
            }//from w w  w. j  av  a2  s . c  om

            try {
                if (informationSource.endsWith(".pdf")) {
                    url = InformationShape.class.getResource(informationSource);
                } else if (informationSource.endsWith(".html")) {
                    try {
                        url = new URL(informationSource);
                    } catch (Exception e) {
                        url = InformationShape.class.getResource(informationSource);
                    }
                } else {
                    url = new URL(informationSource);
                }
            } catch (Exception e) {
                // do nothing
            }

            PNode cropNode;
            // PDF
            if (informationSource.endsWith(".pdf")) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Display information as PDF.");
                }
                cropNode = new PDFPane(url.getFile());
            }
            // HTML
            else {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Display information as HTML.");
                }

                if (informationSource.startsWith("http") || informationSource.endsWith(".html")) {

                    XHTMLPanel xhtmlPanel = new XHTMLPanel();
                    try {
                        xhtmlPanel.setDocument(url.toURI().toString());
                    } catch (URISyntaxException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    xhtmlPanel.setPreferredSize(new Dimension(800, 800));
                    //                  xhtmlPanel.addPropertyChangeListener("preferredSize", new PropertyChangeListener() {
                    //                     
                    //                     public void propertyChange(PropertyChangeEvent evt) {
                    //                        cropScroll.updateScroller();
                    //                        cropNode.
                    //                     }
                    //                  });
                    cropNode = JComponentWrapper.create(xhtmlPanel);
                } else {
                    JEditorPane editorPane = new JEditorPane();
                    editorPane.setFont(internalFont.deriveFont(10f));

                    FontMetrics fm = editorPane.getFontMetrics(editorPane.getFont());
                    int editorWidth = 400;
                    editorPane.setPreferredSize(new Dimension(editorWidth,
                            FontUtils.getLineCount(informationSource, editorWidth) * fm.getHeight()));

                    cropNode = JComponentWrapper.create(editorPane);
                    editorPane.setEditable(false);

                    editorPane.setText(informationSource);
                }

                // Prepare HTML Kit
                // HTMLParser editorKit = new HTMLParser();
                // HTMLParserCallback callback = new
                // HTMLParserCallback();
                // getComponentEditorPane().setEditorKit(editorKit);
                // //Open connection
                // InputStreamReader reader = new
                // InputStreamReader(url.openStream());
                // //Start parse process
                // editorKit.getParser().parse(reader, callback, true);
                // Wait until parsing process has finished
                // try {
                // Thread.sleep(2000);
                // }
                // catch (InterruptedException e) {
                // if (LOG.isErrorEnabled()) {
                // LOG.error("Error in " +
                // InformationShape.class.getName() + ".", e);
                // }
                // }
            }

            cropScroll = new CropScroll(cropNode, new Dimension(1000, 700), 0.2);
            cropScroll.setOffset(
                    getBoundsReference().getCenterX() - cropScroll.getBoundsReference().getCenterX(), 250);
            addChild(cropScroll);
            invalidateFullBounds();
            invalidateLayout();
            invalidatePaint();

            // indicator.done();
        }
    }).start();
}

From source file:uk.ac.ucl.cs.cmic.giftcloud.uploadapp.GiftCloudDialogs.java

public void showMessage(final String message) throws HeadlessException {

    final JPanel messagePanel = new JPanel(new GridBagLayout());
    final JEditorPane textField = new JEditorPane();
    textField.setContentType("text/html");
    textField.setText(message);//from ww  w.j a  va2 s .c  o m
    textField.setEditable(false);
    textField.setBackground(null);
    textField.setBorder(null);
    textField.setEditable(false);
    textField.setForeground(UIManager.getColor("Label.foreground"));
    textField.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, true);
    textField.setFont(UIManager.getFont("Label.font"));
    textField.addHyperlinkListener(new HyperlinkListener() {
        public void hyperlinkUpdate(HyperlinkEvent e) {
            if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                if (Desktop.isDesktopSupported()) {
                    try {
                        Desktop.getDesktop().browse(e.getURL().toURI());
                    } catch (IOException e1) {
                        // Ignore failure to launch URL
                    } catch (URISyntaxException e1) {
                        // Ignore failure to launch URL
                    }
                }
            }
        }
    });

    messagePanel.add(textField);
    textField.setAlignmentX(SwingConstants.CENTER);

    JOptionPane.showMessageDialog(mainFrame.getContainer(), messagePanel, applicationName,
            JOptionPane.INFORMATION_MESSAGE, icon);
}

From source file:util.ui.UiUtilities.java

/**
 * Creates a Html EditorPane that holds a HTML-Help Text.
 *
 * Add a Listener if you want to have clickable Links
 *
 * @param html/*w ww . j ava 2 s  .c o m*/
 *          HTML-Text to display
 * @param listener
 *          Link-Listener for this HelpText
 * @param background The color for the background.
 * @return EditorPane that holds a Help Text
 * @since 2.7.2
 */
public static JEditorPane createHtmlHelpTextArea(String html, HyperlinkListener listener, Color background) {
    // Quick "hack". Remove HTML-Code and replace it with Code that includes the
    // correct Font
    if (html.indexOf("<html>") >= 0) {
        html = StringUtils.substringBetween(html, "<html>", "</html>");
    }
    JLabel label = new JLabel();
    Font font = label.getFont();
    html = "<html><div style=\"color:" + UiUtilities.getHTMLColorCode(label.getForeground()) + ";font-family:"
            + font.getName() + "; font-size:" + font.getSize() + ";background-color:rgb(" + background.getRed()
            + "," + background.getGreen() + "," + background.getBlue() + ");\">" + html + "</div></html>";

    final JEditorPane pane = new JEditorPane("text/html", html);
    pane.setBorder(BorderFactory.createEmptyBorder());
    pane.setEditable(false);
    pane.setFont(font);
    pane.setOpaque(false);
    pane.setFocusable(false);

    if (listener != null) {
        pane.addHyperlinkListener(listener);
    }
    return pane;
}