KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > DefaultLineTracker


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.jface.text;
12
13
14 /**
15  * Standard implementation of {@link org.eclipse.jface.text.ILineTracker}.
16  * <p>
17  * The line tracker considers the three common line delimiters which are '\n',
18  * '\r', '\r\n'.
19  * <p>
20  * This class is not intended to be subclassed.
21  * </p>
22  */

23 public class DefaultLineTracker extends AbstractLineTracker {
24
25     /** The predefined delimiters of this tracker */
26     public final static String JavaDoc[] DELIMITERS= { "\r", "\n", "\r\n" }; //$NON-NLS-3$ //$NON-NLS-1$ //$NON-NLS-2$
27
/** A predefined delimiter information which is always reused as return value */
28     private DelimiterInfo fDelimiterInfo= new DelimiterInfo();
29
30
31     /**
32      * Creates a standard line tracker.
33      */

34     public DefaultLineTracker() {
35     }
36
37     /*
38      * @see org.eclipse.jface.text.ILineTracker#getLegalLineDelimiters()
39      */

40     public String JavaDoc[] getLegalLineDelimiters() {
41         return TextUtilities.copy(DELIMITERS);
42     }
43
44     /*
45      * @see org.eclipse.jface.text.AbstractLineTracker#nextDelimiterInfo(java.lang.String, int)
46      */

47     protected DelimiterInfo nextDelimiterInfo(String JavaDoc text, int offset) {
48
49         char ch;
50         int length= text.length();
51         for (int i= offset; i < length; i++) {
52
53             ch= text.charAt(i);
54             if (ch == '\r') {
55
56                 if (i + 1 < length) {
57                     if (text.charAt(i + 1) == '\n') {
58                         fDelimiterInfo.delimiter= DELIMITERS[2];
59                         fDelimiterInfo.delimiterIndex= i;
60                         fDelimiterInfo.delimiterLength= 2;
61                         return fDelimiterInfo;
62                     }
63                 }
64
65                 fDelimiterInfo.delimiter= DELIMITERS[0];
66                 fDelimiterInfo.delimiterIndex= i;
67                 fDelimiterInfo.delimiterLength= 1;
68                 return fDelimiterInfo;
69
70             } else if (ch == '\n') {
71
72                 fDelimiterInfo.delimiter= DELIMITERS[1];
73                 fDelimiterInfo.delimiterIndex= i;
74                 fDelimiterInfo.delimiterLength= 1;
75                 return fDelimiterInfo;
76             }
77         }
78
79         return null;
80     }
81 }
82
Popular Tags