001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.cnd.completion.cplusplus.hyperlink;
043:
044: import java.awt.Toolkit;
045: import java.text.MessageFormat;
046: import javax.swing.text.DefaultCaret;
047: import javax.swing.text.Document;
048: import javax.swing.text.JTextComponent;
049: import org.netbeans.editor.BaseDocument;
050: import org.netbeans.editor.Utilities;
051: import org.netbeans.lib.editor.hyperlink.spi.HyperlinkProvider;
052: import org.netbeans.modules.cnd.api.model.CsmModelAccessor;
053: import org.netbeans.modules.cnd.api.model.CsmOffsetable;
054: import org.netbeans.modules.cnd.completion.cplusplus.NbCsmSyntaxSupport;
055: import org.netbeans.modules.cnd.completion.cplusplus.utils.Token;
056: import org.netbeans.modules.cnd.completion.cplusplus.utils.TokenUtilities;
057: import org.netbeans.modules.cnd.modelutil.CsmUtilities;
058: import org.openide.util.NbBundle;
059:
060: /**
061: * base hyperlink provider for Csm elements
062: * @author Vladimir Voskresensky
063: */
064: public abstract class CsmAbstractHyperlinkProvider implements
065: HyperlinkProvider {
066:
067: protected CsmAbstractHyperlinkProvider() {
068: DefaultCaret caret = new DefaultCaret();
069: caret.setMagicCaretPosition(null);
070: }
071:
072: protected abstract void performAction(
073: final BaseDocument originalDoc,
074: final JTextComponent target, final int offset);
075:
076: public void performClickAction(Document originalDoc,
077: final int offset) {
078: if (!(originalDoc instanceof BaseDocument))
079: return;
080:
081: final BaseDocument doc = (BaseDocument) originalDoc;
082: final JTextComponent target = Utilities.getFocusedComponent();
083:
084: if (target == null || target.getDocument() != doc)
085: return;
086:
087: Runnable run = new Runnable() {
088: public void run() {
089: performAction(doc, target, offset);
090: }
091: };
092: CsmModelAccessor.getModel().enqueue(run, "Following hyperlink"); //NOI18N
093: }
094:
095: public boolean isHyperlinkPoint(Document doc, int offset) {
096: Token token = getToken(doc, offset);
097: return isValidToken(token);
098: }
099:
100: protected abstract boolean isValidToken(Token token);
101:
102: public int[] getHyperlinkSpan(Document doc, int offset) {
103: Token token = getToken(doc, offset);
104: if (isValidToken(token)) {
105: return new int[] { token.getStartOffset(),
106: token.getEndOffset() };
107: } else {
108: return null;
109: }
110: }
111:
112: protected static Token getToken(Document doc, int offset) {
113: return TokenUtilities.getToken(doc, offset);
114: }
115:
116: private Token jumpToken = null;
117:
118: protected boolean preJump(BaseDocument doc, JTextComponent target,
119: int offset, String msgKey) {
120: if (doc == null || target == null || offset < 0
121: || offset > doc.getLength()) {
122: return false;
123: }
124: jumpToken = getToken(doc, offset);
125: if (!isValidToken(jumpToken)) {
126: return false;
127: }
128: String name = jumpToken.getText();
129: String msg = NbBundle.getBundle(NbCsmSyntaxSupport.class)
130: .getString(msgKey); //NOI18N
131: msg = MessageFormat.format(msg, new Object[] { name });
132: org.openide.awt.StatusDisplayer.getDefault().setStatusText(msg);//NOI18N
133: return true;
134: }
135:
136: protected boolean postJump(CsmOffsetable item, String existItemKey,
137: String noItemKey) {
138: if (jumpToken == null) {
139: return false;
140: }
141: if (item == null || !CsmUtilities.openSource(item)) {
142: //nothing found (item == null) or no source found (itemDesc != null)
143: //inform user, that we were not able to open the resource.
144: Toolkit.getDefaultToolkit().beep();
145: String key;
146: String name;
147: String itemDesc = CsmUtilities.getElementJumpName(item);
148: if (itemDesc != null && itemDesc.length() > 0) {
149: key = "goto_source_source_not_found"; // NOI18N
150: name = itemDesc;
151: } else {
152: key = "cannot-open-csm-element";// NOI18N
153: name = jumpToken.getText();
154: }
155:
156: String msg = NbBundle.getBundle(NbCsmSyntaxSupport.class)
157: .getString(key);
158:
159: org.openide.awt.StatusDisplayer.getDefault().setStatusText(
160: MessageFormat.format(msg, new Object[] { name }));
161: return false;
162: }
163: return true;
164: }
165:
166: protected Token getJumpToken() {
167: return this.jumpToken;
168: }
169:
170: }
|