Source code file content

Revision: 185 (of 185)

Tagging rcp\tags\release67\options.api of NB 6.7
» Project Revision History

» Checkout URL

incubator / nbplugins / trunk / ahrefhyperlink.revamped / src / com / wp / nbguru / ahrefhyperlink / revamped / AHrefHyperlinkProvider.java

Size: 4975 bytes, 1 line
package com.wp.nbguru.ahrefhyperlink.revamped;

// <editor-fold defaultstate="collapsed" desc="Java APIs Imports..">
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import javax.swing.text.StyledDocument;
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="NetBeans APIs Imports..">
import org.netbeans.api.editor.EditorRegistry;
import org.netbeans.api.html.lexer.HTMLTokenId;
import org.netbeans.api.lexer.Token;
import org.netbeans.api.lexer.TokenHierarchy;
import org.netbeans.api.lexer.TokenSequence;
import org.netbeans.lib.editor.hyperlink.spi.HyperlinkProvider;
import org.openide.awt.HtmlBrowser.URLDisplayer;
import org.openide.util.RequestProcessor;
// </editor-fold>

/**
 *
 * @author Geertjan Wielenga, Varun Nischal
 * @version 1.1
 */
public class AHrefHyperlinkProvider implements HyperlinkProvider {

    private static String AHREF_IDENTIFIER = "href";
    private int startOffset;
    private int endOffset;
    private String identifier;
    private final Logger logger =
            Logger.getLogger(AHrefHyperlinkProvider.class.getName());

    // <editor-fold defaultstate="collapsed" desc="Implemented methods...">
    public boolean isHyperlinkPoint(Document doc, int offset) {
        JTextComponent target = EditorRegistry.lastFocusedComponent();
        final StyledDocument styledDoc = (StyledDocument) target.getDocument();
        if (styledDoc == null) {
            return false;
        }

        // Work only with the open editor
        //and the editor has to be the active component:
        if ((target == null) || (target.getDocument() != doc)) {
            return false;
        }

        TokenHierarchy hi = TokenHierarchy.get(doc);
        TokenSequence ts = hi.tokenSequence(HTMLTokenId.language());

        ts.move(offset);
        ts.moveNext();

        Token<HTMLTokenId> tok = ts.token();

        if (tok != null) {
            int tokOffset = ts.offset();
            switch (tok.id()) {
                case VALUE:
                    while (ts.movePrevious()) {
                        Token<HTMLTokenId> prev = ts.token();
                        switch (prev.id()) {
                            case ARGUMENT:
                                if (AHREF_IDENTIFIER.equals(prev.text().toString())) {
                                    startOffset = tokOffset;
                                    endOffset = startOffset +
                                            tok.text().length();
                                    ++startOffset;
                                    --endOffset;
                                    return true;
                                }
                            case OPERATOR:
                            case EOL:
                            case ERROR:
                            case WS:
                                continue;
                            default:
                                return false;
                        }
                    }
                    return false;
            }
            return false;
        }
        return false;
    }

    public int[] getHyperlinkSpan(Document doc, int offset) {
        JTextComponent target = EditorRegistry.lastFocusedComponent();
        final StyledDocument styledDoc = (StyledDocument) target.getDocument();
        if (styledDoc == null) {
            return null;
        }

        // Return the position, which was set in the isHyperlink method:
        return new int[]{startOffset, endOffset};
    }

    public void performClickAction(Document doc, int offset) {
        try {
            JTextComponent target = EditorRegistry.lastFocusedComponent();
            final StyledDocument styledDoc =
                    (StyledDocument) target.getDocument();

            if (styledDoc == null) {
                return;
            }
            identifier = doc.getText(0, doc.getLength());
            identifier = identifier.substring(startOffset, endOffset);

            if (identifier.contains("://")) {
                try {
                    URLDisplayer.getDefault().showURL(new URL(identifier));
                } catch (MalformedURLException ex) {
                    logger.log(Level.SEVERE, null, ex);
                }
            } else {
                //Start a new thread for opening the HTML document-
                OpenThreadImpl thread = new OpenThreadImpl();
                thread.assignMembers(doc, identifier);

                RequestProcessor.getDefault().post(thread);
            }
        } catch (BadLocationException ex) {
            logger.log(Level.SEVERE, null, ex);
        }
    }
    // </editor-fold>
}
  • Mysql
  • Glassfish
  • Jruby
  • Rails
  • Nblogo
Terms of Use; Privacy Policy;
© 2010, Oracle Corporation and/or its affiliates
(revision 20120518.3c65429)
 
 
Close
loading
Please Confirm
Close