KickJava   Java API By Example, From Geeks To Geeks.

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


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.jface.text;
12
13 import org.eclipse.core.runtime.Assert;
14
15
16 /**
17  * Standard implementation of a generic
18  * {@link org.eclipse.jface.text.ILineTracker}.
19  * <p>
20  * The line tracker can be configured with the set of legal line delimiters.
21  * Line delimiters are unconstrained. The line delimiters are used to compute
22  * the tracker's line structure. In the case of overlapping line delimiters, the
23  * longest line delimiter is given precedence of the shorter ones.
24  * <p>
25  * This class is not intended to be subclassed.
26  * </p>
27  */

28 public class ConfigurableLineTracker extends AbstractLineTracker {
29
30
31     /** The strings which are considered being the line delimiter */
32     private String JavaDoc[] fDelimiters;
33     /** A predefined delimiter information which is always reused as return value */
34     private DelimiterInfo fDelimiterInfo= new DelimiterInfo();
35
36
37     /**
38      * Creates a standard line tracker for the given line delimiters.
39      *
40      * @param legalLineDelimiters the tracker's legal line delimiters,
41      * may not be <code>null</code> and must be longer than 0
42      */

43     public ConfigurableLineTracker(String JavaDoc[] legalLineDelimiters) {
44         Assert.isTrue(legalLineDelimiters != null && legalLineDelimiters.length > 0);
45         fDelimiters= TextUtilities.copy(legalLineDelimiters);
46     }
47
48     /*
49      * @see org.eclipse.jface.text.ILineTracker#getLegalLineDelimiters()
50      */

51     public String JavaDoc[] getLegalLineDelimiters() {
52         return TextUtilities.copy(fDelimiters);
53     }
54
55     /*
56      * @see org.eclipse.jface.text.AbstractLineTracker#nextDelimiterInfo(java.lang.String, int)
57      */

58     protected DelimiterInfo nextDelimiterInfo(String JavaDoc text, int offset) {
59         if (fDelimiters.length > 1) {
60             int[] info= TextUtilities.indexOf(fDelimiters, text, offset);
61             if (info[0] == -1)
62                 return null;
63             fDelimiterInfo.delimiterIndex= info[0];
64             fDelimiterInfo.delimiter= fDelimiters[info[1]];
65         } else {
66             int index= text.indexOf(fDelimiters[0], offset);
67             if (index == -1)
68                 return null;
69             fDelimiterInfo.delimiterIndex= index;
70             fDelimiterInfo.delimiter= fDelimiters[0];
71         }
72
73         fDelimiterInfo.delimiterLength= fDelimiterInfo.delimiter.length();
74         return fDelimiterInfo;
75     }
76 }
77
Popular Tags