Search in sources :

Example 21 with HTMLEditorKit

use of javax.swing.text.html.HTMLEditorKit in project Spark by igniterealtime.

the class CheckUpdates method downloadUpdate.

public void downloadUpdate(final File downloadedFile, final SparkVersion version) {
    final java.util.Timer timer = new java.util.Timer();
    // Prepare HTTP post
    final GetMethod post = new GetMethod(version.getDownloadURL());
    // Get HTTP client
    Protocol.registerProtocol("https", new Protocol("https", new EasySSLProtocolSocketFactory(), 443));
    final HttpClient httpclient = new HttpClient();
    String proxyHost = System.getProperty("http.proxyHost");
    String proxyPort = System.getProperty("http.proxyPort");
    if (ModelUtil.hasLength(proxyHost) && ModelUtil.hasLength(proxyPort)) {
        try {
            httpclient.getHostConfiguration().setProxy(proxyHost, Integer.parseInt(proxyPort));
        } catch (NumberFormatException e) {
            Log.error(e);
        }
    }
    try {
        int result = httpclient.executeMethod(post);
        if (result != 200) {
            return;
        }
        long length = post.getResponseContentLength();
        int contentLength = (int) length;
        bar = new JProgressBar(0, contentLength);
    } catch (IOException e) {
        Log.error(e);
    }
    final JFrame frame = new JFrame(Res.getString("title.downloading.im.client"));
    frame.setIconImage(SparkRes.getImageIcon(SparkRes.SMALL_MESSAGE_IMAGE).getImage());
    titlePanel = new TitlePanel(Res.getString("title.upgrading.client"), Res.getString("message.version", version.getVersion()), SparkRes.getImageIcon(SparkRes.SEND_FILE_24x24), true);
    final Thread thread = new Thread(() -> {
        try {
            InputStream stream = post.getResponseBodyAsStream();
            long size = post.getResponseContentLength();
            ByteFormat formater = new ByteFormat();
            sizeText = formater.format(size);
            titlePanel.setDescription(Res.getString("message.version", version.getVersion()) + " \n" + Res.getString("message.file.size", sizeText));
            downloadedFile.getParentFile().mkdirs();
            FileOutputStream out = new FileOutputStream(downloadedFile);
            copy(stream, out);
            out.close();
            if (!cancel) {
                downloadComplete = true;
                promptForInstallation(downloadedFile, Res.getString("title.download.complete"), Res.getString("message.restart.spark"));
            } else {
                out.close();
                downloadedFile.delete();
            }
            UPDATING = false;
            frame.dispose();
        } catch (Exception ex) {
        // Nothing to do
        } finally {
            timer.cancel();
            // Release current connection to the connection pool once you are done
            post.releaseConnection();
        }
    });
    frame.getContentPane().setLayout(new GridBagLayout());
    frame.getContentPane().add(titlePanel, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
    frame.getContentPane().add(bar, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
    JEditorPane pane = new JEditorPane();
    boolean displayContentPane = version.getChangeLogURL() != null || version.getDisplayMessage() != null;
    try {
        pane.setEditable(false);
        if (version.getChangeLogURL() != null) {
            pane.setEditorKit(new HTMLEditorKit());
            pane.setPage(version.getChangeLogURL());
        } else if (version.getDisplayMessage() != null) {
            pane.setText(version.getDisplayMessage());
        }
        if (displayContentPane) {
            frame.getContentPane().add(new JScrollPane(pane), new GridBagConstraints(0, 2, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
        }
    } catch (IOException e) {
        Log.error(e);
    }
    frame.getContentPane().setBackground(Color.WHITE);
    frame.pack();
    if (displayContentPane) {
        frame.setSize(600, 400);
    } else {
        frame.setSize(400, 100);
    }
    frame.setLocationRelativeTo(SparkManager.getMainWindow());
    GraphicUtils.centerWindowOnScreen(frame);
    frame.addWindowListener(new WindowAdapter() {

        public void windowClosing(WindowEvent windowEvent) {
            thread.interrupt();
            cancel = true;
            UPDATING = false;
            if (!downloadComplete) {
                UIManager.put("OptionPane.okButtonText", Res.getString("ok"));
                JOptionPane.showMessageDialog(SparkManager.getMainWindow(), Res.getString("message.updating.cancelled"), Res.getString("title.cancelled"), JOptionPane.ERROR_MESSAGE);
            }
        }
    });
    frame.setVisible(true);
    thread.start();
    timer.scheduleAtFixedRate(new TimerTask() {

        int seconds = 1;

        public void run() {
            ByteFormat formatter = new ByteFormat();
            long value = bar.getValue();
            long average = value / seconds;
            String text = formatter.format(average) + "/Sec";
            String total = formatter.format(value);
            titlePanel.setDescription(Res.getString("message.version", version.getVersion()) + " \n" + Res.getString("message.file.size", sizeText) + "\n" + Res.getString("message.transfer.rate") + ": " + text + "\n" + Res.getString("message.total.downloaded") + ": " + total);
            seconds++;
        }
    }, 1000, 1000);
}
Also used : GridBagConstraints(java.awt.GridBagConstraints) Insets(java.awt.Insets) GridBagLayout(java.awt.GridBagLayout) JProgressBar(javax.swing.JProgressBar) WindowAdapter(java.awt.event.WindowAdapter) TimerTask(java.util.TimerTask) JFrame(javax.swing.JFrame) ByteFormat(org.jivesoftware.spark.util.ByteFormat) Protocol(org.apache.commons.httpclient.protocol.Protocol) JScrollPane(javax.swing.JScrollPane) InputStream(java.io.InputStream) HTMLEditorKit(javax.swing.text.html.HTMLEditorKit) IOException(java.io.IOException) TitlePanel(org.jivesoftware.spark.component.TitlePanel) IOException(java.io.IOException) HttpClient(org.apache.commons.httpclient.HttpClient) FileOutputStream(java.io.FileOutputStream) WindowEvent(java.awt.event.WindowEvent) GetMethod(org.apache.commons.httpclient.methods.GetMethod) JEditorPane(javax.swing.JEditorPane)

Example 22 with HTMLEditorKit

use of javax.swing.text.html.HTMLEditorKit in project MtgDesktopCompanion by nicho92.

the class StoriesGUI method initGUI.

private void initGUI() {
    JScrollPane scrollList = new JScrollPane();
    JScrollPane scrollEditor = new JScrollPane();
    setLayout(new BorderLayout(0, 0));
    resultListModel = new DefaultListModel<>();
    listResult = new JList<>(resultListModel);
    listResult.setCellRenderer(new MTGStoryListRenderer());
    listResult.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    listResult.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent evt) {
            if (evt.getClickCount() == 1) {
                evt.consume();
                ThreadManager.getInstance().execute(() -> {
                    lblLoading.setVisible(true);
                    try {
                        editorPane.setText(Jsoup.connect(listResult.getSelectedValue().getUrl().toString()).get().select("div#content-detail-page-of-an-article").html());
                    } catch (Exception e) {
                        JOptionPane.showMessageDialog(null, e.getMessage(), MTGControler.getInstance().getLangService().getError(), JOptionPane.ERROR_MESSAGE);
                    }
                    lblLoading.setVisible(false);
                }, "Load story");
            } else {
                try {
                    Desktop.getDesktop().browse(listResult.getSelectedValue().getUrl().toURI());
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null, e.getMessage(), MTGControler.getInstance().getLangService().getError(), JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    });
    scrollList.setViewportView(listResult);
    JPanel panel = new JPanel();
    add(panel, BorderLayout.NORTH);
    JButton btnLoadNext = new JButton("Load Next");
    btnLoadNext.addActionListener(ae -> initStories());
    panel.add(btnLoadNext);
    lblLoading = new JLabel(MTGConstants.ICON_LOADING);
    lblLoading.setVisible(false);
    panel.add(lblLoading);
    editorPane = new JEditorPane();
    editorPane.setEditable(false);
    editorPane.setContentType("text/html");
    HTMLEditorKit kit = new HTMLEditorKit();
    editorPane.setEditorKit(kit);
    Document doc = kit.createDefaultDocument();
    editorPane.setDocument(doc);
    scrollEditor.setViewportView(editorPane);
    JSplitPane splitPane = new JSplitPane();
    splitPane.setLeftComponent(scrollList);
    splitPane.setRightComponent(scrollEditor);
    add(splitPane, BorderLayout.CENTER);
}
Also used : JScrollPane(javax.swing.JScrollPane) JPanel(javax.swing.JPanel) MouseEvent(java.awt.event.MouseEvent) MTGStoryListRenderer(org.magic.gui.renderer.MTGStoryListRenderer) MouseAdapter(java.awt.event.MouseAdapter) JButton(javax.swing.JButton) JLabel(javax.swing.JLabel) HTMLEditorKit(javax.swing.text.html.HTMLEditorKit) Document(javax.swing.text.Document) IOException(java.io.IOException) BorderLayout(java.awt.BorderLayout) JEditorPane(javax.swing.JEditorPane) JSplitPane(javax.swing.JSplitPane)

Example 23 with HTMLEditorKit

use of javax.swing.text.html.HTMLEditorKit in project blue by kunstmusik.

the class Installer method restored.

@Override
public void restored() {
    // override HTMLEditorKit's link color so that documentation that shows
    // in CodeCompletion has this color
    HTMLEditorKit kit = new HTMLEditorKit();
    kit.getStyleSheet().addRule("a { color: #F8BB00; }");
}
Also used : HTMLEditorKit(javax.swing.text.html.HTMLEditorKit)

Example 24 with HTMLEditorKit

use of javax.swing.text.html.HTMLEditorKit in project UniversalMediaServer by UniversalMediaServer.

the class HelpTab method build.

/**
 * Set up the panel for the help tab and load its contents from a file.
 * @return The component containing the help tab and its contents
 */
public JComponent build() {
    FormLayout layout = new FormLayout("left:pref, 0:grow", "pref, fill:default:grow");
    PanelBuilder builder = new PanelBuilder(layout);
    builder.opaque(true);
    CellConstraints cc = new CellConstraints();
    editorPane = new JEditorPane();
    editorPane.setEditable(false);
    editorPane.setContentType("text/html");
    editorPane.setBackground(Color.WHITE);
    HTMLEditorKit editorKit = new HTMLEditorKit();
    StyleSheet styleSheet = ((HTMLDocument) editorKit.createDefaultDocument()).getStyleSheet();
    buildStyleSheet(styleSheet);
    editorKit.setStyleSheet(styleSheet);
    editorPane.setEditorKit(editorKit);
    updateContents();
    // Enable internal anchor links
    editorPane.addHyperlinkListener(new HyperlinkListener() {

        @Override
        public void hyperlinkUpdate(HyperlinkEvent event) {
            try {
                if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                    String urlString = event.getURL().toExternalForm();
                    if (urlString.startsWith("http://") || urlString.startsWith("https://") || urlString.startsWith("ftp://")) {
                        // Open external links in the desktop web browser
                        URI uri = new URI(urlString);
                        Desktop.getDesktop().browse(uri);
                    } else {
                        // Open anchor links in the editorPane
                        editorPane.setPage(event.getURL());
                    }
                }
            } catch (IOException | URISyntaxException e) {
                LOGGER.debug("Caught exception", e);
            }
        }
    });
    JScrollPane pane = new JScrollPane(editorPane, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    pane.setPreferredSize(new Dimension(500, 400));
    pane.setBorder(BorderFactory.createEmptyBorder());
    builder.add(pane, cc.xy(2, 2));
    return builder.getPanel();
}
Also used : FormLayout(com.jgoodies.forms.layout.FormLayout) JScrollPane(javax.swing.JScrollPane) PanelBuilder(com.jgoodies.forms.builder.PanelBuilder) HyperlinkEvent(javax.swing.event.HyperlinkEvent) HTMLDocument(javax.swing.text.html.HTMLDocument) HTMLEditorKit(javax.swing.text.html.HTMLEditorKit) Dimension(java.awt.Dimension) URI(java.net.URI) StyleSheet(javax.swing.text.html.StyleSheet) HyperlinkListener(javax.swing.event.HyperlinkListener) JEditorPane(javax.swing.JEditorPane) CellConstraints(com.jgoodies.forms.layout.CellConstraints)

Example 25 with HTMLEditorKit

use of javax.swing.text.html.HTMLEditorKit in project freeplane by freeplane.

the class XHTMLWriter method html2xhtml.

/**
 * Read HTML from the Reader, and send XHTML to the writer. Common mistakes
 * in the HTML code will also be corrected. The result is pretty-printed.
 *
 * @param reader
 *            HTML source
 * @param writer
 *            XHTML target
 */
public static void html2xhtml(final Reader reader, final Writer writer) throws IOException, BadLocationException {
    final HTMLEditorKit kit = new HTMLEditorKit();
    final Document doc = kit.createDefaultDocument();
    kit.read(reader, doc, doc.getLength());
    final XHTMLWriter xhw = new XHTMLWriter(writer, (HTMLDocument) doc);
    xhw.write();
}
Also used : HTMLEditorKit(javax.swing.text.html.HTMLEditorKit) HTMLDocument(javax.swing.text.html.HTMLDocument) Document(javax.swing.text.Document)

Aggregations

HTMLEditorKit (javax.swing.text.html.HTMLEditorKit)47 HTMLDocument (javax.swing.text.html.HTMLDocument)18 JEditorPane (javax.swing.JEditorPane)10 StyleSheet (javax.swing.text.html.StyleSheet)9 IOException (java.io.IOException)8 JScrollPane (javax.swing.JScrollPane)8 Dimension (java.awt.Dimension)7 URL (java.net.URL)7 Document (javax.swing.text.Document)6 MouseEvent (java.awt.event.MouseEvent)5 StringReader (java.io.StringReader)5 HyperlinkEvent (javax.swing.event.HyperlinkEvent)5 ActionListener (java.awt.event.ActionListener)4 HyperlinkListener (javax.swing.event.HyperlinkListener)4 BorderLayout (java.awt.BorderLayout)3 ActionEvent (java.awt.event.ActionEvent)3 InputStream (java.io.InputStream)3 BadLocationException (javax.swing.text.BadLocationException)3 MouseAdapter (java.awt.event.MouseAdapter)2 InputStreamReader (java.io.InputStreamReader)2