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.impl.xref;
043:
044: import javax.swing.text.BadLocationException;
045: import org.netbeans.editor.BaseDocument;
046: import org.netbeans.editor.Utilities;
047: import org.netbeans.modules.cnd.api.model.CsmOffsetable;
048:
049: /**
050: * (line, col, offset) based CsmOffsetable.Position implementation
051: * @author Vladimir Voskresensky
052: */
053: public class DocOffsPositionImpl implements CsmOffsetable.Position {
054: private int line;
055: private int col;
056: private final int offset;
057: private final BaseDocument doc;
058:
059: public DocOffsPositionImpl(BaseDocument doc, int offset) {
060: this (-1, -1, offset, doc);
061: }
062:
063: public DocOffsPositionImpl(CsmOffsetable.Position pos) {
064: if (pos != null) {
065: this .line = pos.getLine();
066: this .col = pos.getColumn();
067: this .offset = pos.getOffset();
068: } else {
069: this .line = -1;
070: this .col = -1;
071: this .offset = 0;
072: }
073: this .doc = null;
074: }
075:
076: public DocOffsPositionImpl(int line, int col, int offset,
077: BaseDocument doc) {
078: this .line = line;
079: this .col = col;
080: this .offset = offset;
081: this .doc = doc;
082: }
083:
084: public int getOffset() {
085: return offset;
086: }
087:
088: public int getLine() {
089: return getLine(true);
090: }
091:
092: public int getColumn() {
093: return getColumn(true);
094: }
095:
096: public int getLine(boolean create) {
097: if (create && this .line == -1 && this .doc != null) {
098: try {
099: this .line = Utilities.getLineOffset(this .doc,
100: this .offset) + 1;
101: } catch (BadLocationException ex) {
102: this .line = -1;
103: }
104: }
105: return this .line;
106: }
107:
108: public int getColumn(boolean create) {
109: if (create && this .col == -1 && this .doc != null) {
110: try {
111: this .col = Utilities.getVisualColumn(this .doc,
112: this .offset) + 1;
113: } catch (BadLocationException ex) {
114: this .col = -1;
115: }
116: }
117: return this .col;
118: }
119:
120: /*package*/BaseDocument getDocument() {
121: return this .doc;
122: }
123:
124: public String toString() {
125: return "" + getLine(true) + ':' + getColumn(true) + '/'
126: + getOffset();
127: }
128: }
|