KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > dom > rewrite > LineInformation


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.internal.core.dom.rewrite;
13
14 import org.eclipse.jdt.core.dom.CompilationUnit;
15 import org.eclipse.jface.text.BadLocationException;
16 import org.eclipse.jface.text.IDocument;
17
18 /**
19  *
20  */

21 public abstract class LineInformation {
22     
23     public static LineInformation create(final IDocument doc) {
24         return new LineInformation() {
25             public int getLineOfOffset(int offset) {
26                 try {
27                     return doc.getLineOfOffset(offset);
28                 } catch (BadLocationException e) {
29                     return -1;
30                 }
31             }
32
33             public int getLineOffset(int line) {
34                 try {
35                     return doc.getLineOffset(line);
36                 } catch (BadLocationException e) {
37                     return -1;
38                 }
39             }
40         };
41     }
42     
43     public static LineInformation create(final CompilationUnit astRoot) {
44         return new LineInformation() {
45             public int getLineOfOffset(int offset) {
46                 return astRoot.getLineNumber(offset) - 1;
47             }
48             public int getLineOffset(int line) {
49                 return astRoot.getPosition(line + 1, 0);
50             }
51         };
52     }
53     
54     
55     
56     public abstract int getLineOfOffset(int offset);
57     public abstract int getLineOffset(int line);
58     
59 }
60
Popular Tags