KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search > internal > core > text > DocumentCharSequence


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 package org.eclipse.search.internal.core.text;
12
13 import org.eclipse.jface.text.BadLocationException;
14 import org.eclipse.jface.text.IDocument;
15
16 /**
17  * Adapting a document to a CharSequence
18  */

19 public class DocumentCharSequence implements CharSequence JavaDoc {
20
21     private final IDocument fDocument;
22
23     /**
24      * @param document The document to wrap
25      */

26     public DocumentCharSequence(IDocument document) {
27         fDocument= document;
28     }
29
30     /* (non-Javadoc)
31      * @see java.lang.CharSequence#length()
32      */

33     public int length() {
34         return fDocument.getLength();
35     }
36
37     /* (non-Javadoc)
38      * @see java.lang.CharSequence#charAt(int)
39      */

40     public char charAt(int index) {
41         try {
42             return fDocument.getChar(index);
43         } catch (BadLocationException e) {
44             throw new IndexOutOfBoundsException JavaDoc();
45         }
46     }
47
48     /* (non-Javadoc)
49      * @see java.lang.CharSequence#subSequence(int, int)
50      */

51     public CharSequence JavaDoc subSequence(int start, int end) {
52         try {
53             return fDocument.get(start, end - start);
54         } catch (BadLocationException e) {
55             throw new IndexOutOfBoundsException JavaDoc();
56         }
57     }
58
59 }
60
Popular Tags