001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.languages.css;
021:
022: import javax.swing.text.AbstractDocument;
023: import org.netbeans.api.languages.ASTNode;
024: import org.netbeans.api.languages.SyntaxContext;
025: import org.netbeans.api.languages.Context;
026: import org.netbeans.api.lexer.TokenHierarchy;
027: import org.netbeans.api.lexer.TokenSequence;
028: import org.openide.cookies.OpenCookie;
029: import org.openide.filesystems.FileObject;
030: import org.openide.loaders.DataObject;
031: import org.openide.loaders.DataObjectNotFoundException;
032: import org.openide.util.Lookup;
033: import org.openide.util.NbBundle;
034: import org.openide.windows.TopComponent;
035:
036: /**
037: *
038: * @author Jan Jancura
039: */
040: public class CSS {
041:
042: private static final String TOOLTIP_SAMPLE_TEXT = NbBundle
043: .getBundle("org/netbeans/modules/languages/css/Bundle")
044: .getString("LBL_Sample_Text");
045:
046: public static String navigatorTooltip(Context context) {
047: if (!(context instanceof SyntaxContext))
048: return null;
049: ASTNode n = (ASTNode) ((SyntaxContext) context).getASTPath()
050: .getLeaf();
051: StringBuilder sb = new StringBuilder();
052: sb.append("<html>");
053: sb.append("<p style=\"");
054: ASTNode body = n.getNode("body");
055: if (body == null)
056: return null;
057: ASTNode declarations = body.getNode("declarations");
058: if (declarations == null)
059: return null;
060: String s = declarations.getAsText();
061: s = s.replaceAll("\"", "");
062: sb.append(s);
063: sb.append("\">");
064: sb.append(TOOLTIP_SAMPLE_TEXT);
065: sb.append("</p>");
066: sb.append("</html>");
067: return sb.toString();
068: }
069:
070: public static Runnable hyperlink(Context context) {
071: AbstractDocument document = (AbstractDocument) context
072: .getDocument();
073: document.readLock();
074: try {
075: TokenHierarchy tokenHierarchy = TokenHierarchy
076: .get(document);
077: TokenSequence ts = tokenHierarchy.tokenSequence();
078: while (true) {
079: ts.move(context.getOffset());
080: if (!ts.moveNext())
081: break;
082: TokenSequence ts2 = ts.embedded();
083: if (ts2 == null)
084: break;
085: ts = ts2;
086: }
087: String s = ts.token().id().name();
088: s = s.substring(1, s.length() - 1);
089: s = s.replace("%20", " "); // HACK
090: Lookup l = TopComponent.getRegistry().getActivated()
091: .getLookup();
092: DataObject dob = l.lookup(DataObject.class);
093: FileObject fo = dob.getPrimaryFile().getParent();
094: final FileObject file = fo.getFileObject(s);
095: if (file != null)
096: return new Runnable() {
097: public void run() {
098: try {
099: DataObject d = DataObject.find(file);
100: OpenCookie oc = d
101: .getCookie(OpenCookie.class);
102: oc.open();
103: } catch (DataObjectNotFoundException ex) {
104: }
105: }
106: };
107: return null;
108: } finally {
109: document.readUnlock();
110: }
111: }
112: }
|