Sheribaby / jtorchat

Automatically exported from code.google.com/p/jtorchat
0 stars 0 forks source link

Enhancement: Clickable Links #15

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Implemented the method provided by hardik.rajani at 
http://www.daniweb.com/software-development/java/threads/192209/869230#post86923
0

New class:
    package net.tsu.TCPort.Gui;

    import java.awt.Cursor;
    import java.awt.Point;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionListener;

    import javax.swing.JTextPane;
    import javax.swing.text.AttributeSet;
    import javax.swing.text.DefaultStyledDocument;
    import javax.swing.text.Document;
    import javax.swing.text.Element;
    import javax.swing.text.html.HTML;

    public class LinkController extends MouseAdapter implements MouseMotionListener {

        public void mouseClicked(MouseEvent e) {
            java.awt.Desktop desktop = java.awt.Desktop.getDesktop();
            JTextPane editor = (JTextPane) e.getSource();
            Document doc = editor.getDocument();
            Point pt = new Point(e.getX(), e.getY());
            int pos = editor.viewToModel(pt);

            if (pos >= 0) {
                if (doc instanceof DefaultStyledDocument) {
                    DefaultStyledDocument hdoc = (DefaultStyledDocument) doc;
                    Element el = hdoc.getCharacterElement(pos);
                    AttributeSet a = el.getAttributes();
                    String href = (String) a.getAttribute(HTML.Attribute.HREF);

                    if (href != null) {
                        try {
                            java.net.URI uri = new java.net.URI(href);
                            desktop.browse(uri);
                        } catch (Exception ev) {
                            System.err.println(ev.getMessage());
                        }
                    }
                }
            }
        }

        public void mouseMoved(MouseEvent ev) {

            Cursor handCursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
            Cursor defaultCursor = Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR);
            JTextPane editor = (JTextPane) ev.getSource();
            Point pt = new Point(ev.getX(), ev.getY());
            int pos = editor.viewToModel(pt);

            if (pos >= 0) {
                Document doc = editor.getDocument();

                if (doc instanceof DefaultStyledDocument) {
                    DefaultStyledDocument hdoc = (DefaultStyledDocument) doc;
                    Element e = hdoc.getCharacterElement(pos);
                    AttributeSet a = e.getAttributes();
                    String href = (String) a.getAttribute(HTML.Attribute.HREF);

                    if (href != null) {
                        if (editor.getCursor() != handCursor) {
                            editor.setCursor(handCursor);
                        }
                    } else {
                        editor.setCursor(defaultCursor);
                    }
                }
            } else {
                editor.setToolTipText(null);
            }
        }
    }
// End class

Added to ChatWindow.java
    public static final String reg = "([a-z0-9_\\-]{1,5}:\\/\\/)?(([a-z0-9_\\-]{1,}):([a-z0-9_\\-]{1,})\\@)?((www\\.)|([a-z0-9_\\-]{1,}\\.)+)?([a-z0-9_\\-]{3,})(\\.[a-z]{2,4})(\\/([a-z0-9_\\-]{1,}\\/)+)?([a-z0-9_\\-]{1,})?(\\.[a-z]{2,})?(\\?)?(((\\&)?[a-z0-9_\\-]{1,}(\\=[a-z0-9_\\-]{1,})?)+)?"; // credits for this regex go to Timmaeh.de - http://RegExr.com?2sct4

    public void addUrlText(String text) {
        Matcher m = Pattern.compile(reg).matcher(text);
        int start = 0;
        while (m.find()) {
            append("Plain", text.substring(start, m.start()));
            try {
                addHyperlink(new URL(m.group()), m.group(), Color.blue);
            } catch (MalformedURLException e) {
                boolean worked = false;
                try {
                    addHyperlink(new URL("http://" + m.group()), m.group(), Color.blue); // if the original doesnt have a protocol specified, insert http:// at the beggining
                    worked = true;
                } catch (MalformedURLException e1) {
                    e1.printStackTrace();
                }
                if (!worked)
                    e.printStackTrace();
            }
            start = m.end();
        }
        append("Plain", text.substring(start, text.length()));
    }

    public void addHyperlink(URL url, String text, Color color) {
        try {
            Document doc = textPane1.getDocument();
            SimpleAttributeSet attrs = new SimpleAttributeSet();
            StyleConstants.setUnderline(attrs, true);
            StyleConstants.setForeground(attrs, colo
            attrs.addAttribute(HTML.Attribute.HREF, url.toString());
            doc.insertString(doc.getLength(), text, attrs);
        } catch (BadLocationException e) {
            e.printStackTrace(System.err);
        }
    }

Added to the ChatWindow(Buddy) constructor:
    LinkController lc = new LinkController();
    textPane1.addMouseListener(lc);
    textPane1.addMouseMotionListener(lc);

Replace in ChatWindow.java:
    append("Plain", (flag ? "" : "[Delayed] ") + textArea4.getText().trim() + "\n");
with
    addUrlText((flag ? "" : "[Delayed] ") + textArea4.getText().trim() + "\n");

Replace in Gui.java
    w.append("Plain", s.replaceAll("\\\\n", "\n").trim() + "\n");
with
    w.addUrlText(s.replaceAll("\\\\n", "\n").trim() + "\n");

Original issue reported on code.google.com by Porling...@gmail.com on 2 Mar 2012 at 12:39

GoogleCodeExporter commented 8 years ago
Looks really good. Will definitely recommend for inclusion

Original comment by jtorc...@gmail.com on 2 Mar 2012 at 1:25

GoogleCodeExporter commented 8 years ago
            StyleConstants.setUnderline(attrs, true);
            StyleConstants.setForeground(attrs, colo
            attrs.addAttribute(HTML.Attribute.HREF, url.toString());
must be:
            StyleConstants.setUnderline(attrs, true);
            StyleConstants.setForeground(attrs, color);
            attrs.addAttribute(HTML.Attribute.HREF, url.toString());
and:
                    addHyperlink(new URL("http:// + m.group()), m.group(), Color.blue); // if the original doesnt have a protocol specified, insert http:// at the beggining

must be:
                    addHyperlink(new URL("http://" + m.group()), m.group(), Color.blue); // if the original doesnt have a protocol specified, insert http:// at the beggining

But with this fix it works perfect, thanks.

Original comment by dau...@googlemail.com on 17 Mar 2012 at 11:52

GoogleCodeExporter commented 8 years ago

Original comment by dau...@googlemail.com on 17 Mar 2012 at 11:52

GoogleCodeExporter commented 8 years ago

Original comment by dau...@googlemail.com on 25 Mar 2012 at 9:12