001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2008 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-2008 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: package org.netbeans.modules.cnd.highlight.semantic.actions;
042:
043: import javax.swing.JEditorPane;
044: import javax.swing.text.Document;
045: import org.netbeans.modules.cnd.highlight.semantic.MarkOccurrencesHighlighter;
046: import org.netbeans.modules.cnd.loaders.CCDataObject;
047: import org.netbeans.modules.cnd.loaders.CDataObject;
048: import org.netbeans.modules.cnd.model.tasks.CaretAwareCsmFileTaskFactory;
049: import org.netbeans.spi.editor.highlighting.HighlightsSequence;
050: import org.netbeans.spi.editor.highlighting.support.OffsetsBag;
051: import org.openide.awt.StatusDisplayer;
052: import org.openide.cookies.EditorCookie;
053: import org.openide.loaders.DataObject;
054: import org.openide.nodes.Node;
055: import org.openide.util.NbBundle;
056: import org.openide.windows.TopComponent;
057:
058: /**
059: *
060: * @author Sergey Grinev
061: */
062: public class SemanticUtils {
063:
064: @SuppressWarnings("empty-statement")
065: private static int findOccurrencePosition(boolean directionForward,
066: Document doc, int curPos) {
067: OffsetsBag bag = MarkOccurrencesHighlighter
068: .getHighlightsBag(doc);
069: HighlightsSequence hs = bag.getHighlights(0, doc.getLength());
070:
071: if (hs.moveNext()) {
072: if (directionForward) {
073: int firstStart = hs.getStartOffset(), firstEnd = hs
074: .getEndOffset();
075:
076: while (hs.getStartOffset() <= curPos && hs.moveNext())
077: ;
078:
079: if (hs.getStartOffset() > curPos) {
080: // we found next occurrence
081: return hs.getStartOffset();
082: } else if (!(firstEnd >= curPos && firstStart <= curPos)) {
083: // cyclic jump to first occurrence unless we already there
084: return firstStart;
085: }
086: } else {
087: int current = hs.getStartOffset(), last;
088: boolean stuck = false;
089: do {
090: last = current;
091: current = hs.getStartOffset();
092: } while (hs.getEndOffset() < curPos
093: && (stuck = hs.moveNext()));
094:
095: if (last == current) {
096: // we got no options to jump, cyclic jump to last in file unless we already there
097: while (hs.moveNext())
098: ;
099: if (!(hs.getEndOffset() >= curPos && hs
100: .getStartOffset() <= curPos)) {
101: return hs.getStartOffset();
102: }
103: } else if (stuck) {
104: // just move to previous occurence
105: return last;
106: } else {
107: // it was last occurence in the file
108: return current;
109: }
110: }
111: }
112: return -1;
113: }
114:
115: /*package*/static void navigateToOccurence(boolean next) {
116: final Node[] activatedNodes = TopComponent.getRegistry()
117: .getActivatedNodes();
118: // check whether current file is C++ Source file
119: DataObject dobj = activatedNodes[0].getLookup().lookup(
120: CCDataObject.class);
121: if (dobj == null) {
122: // check whether current file is C Source file
123: dobj = activatedNodes[0].getLookup().lookup(
124: CDataObject.class);
125: }
126: if (dobj != null) {
127: EditorCookie ec = dobj.getCookie(EditorCookie.class);
128: JEditorPane[] panes = ec.getOpenedPanes();
129: Document doc = ec.getDocument();
130:
131: int position = CaretAwareCsmFileTaskFactory
132: .getLastPosition(dobj.getPrimaryFile());
133: int goTo = findOccurrencePosition(next, doc, position);
134: if (goTo > 0) {
135: panes[0].setCaretPosition(goTo);
136: } else {
137: StatusDisplayer.getDefault().setStatusText(
138: NbBundle.getMessage(SemanticUtils.class,
139: "cpp-no-marked-occurrence"));
140: }
141: }
142:
143: }
144: }
|