KickJava   Java API By Example, From Geeks To Geeks.

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


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  * This strategy always copies the indentation of the previous line.
16  * <p>
17  * This class is not intended to be subclassed.</p>
18  *
19  * @since 3.1
20  */

21 public class DefaultIndentLineAutoEditStrategy implements IAutoEditStrategy {
22
23     /**
24      * Creates a new indent line auto edit strategy which can be installed on
25      * text viewers.
26      */

27     public DefaultIndentLineAutoEditStrategy() {
28     }
29
30     /**
31      * Returns the first offset greater than <code>offset</code> and smaller than
32      * <code>end</code> whose character is not a space or tab character. If no such
33      * offset is found, <code>end</code> is returned.
34      *
35      * @param document the document to search in
36      * @param offset the offset at which searching start
37      * @param end the offset at which searching stops
38      * @return the offset in the specified range whose character is not a space or tab
39      * @exception BadLocationException if position is an invalid range in the given document
40      */

41     protected int findEndOfWhiteSpace(IDocument document, int offset, int end) throws BadLocationException {
42         while (offset < end) {
43             char c= document.getChar(offset);
44             if (c != ' ' && c != '\t') {
45                 return offset;
46             }
47             offset++;
48         }
49         return end;
50     }
51
52     /**
53      * Copies the indentation of the previous line.
54      *
55      * @param d the document to work on
56      * @param c the command to deal with
57      */

58     private void autoIndentAfterNewLine(IDocument d, DocumentCommand c) {
59
60         if (c.offset == -1 || d.getLength() == 0)
61             return;
62
63         try {
64             // find start of line
65
int p= (c.offset == d.getLength() ? c.offset - 1 : c.offset);
66             IRegion info= d.getLineInformationOfOffset(p);
67             int start= info.getOffset();
68
69             // find white spaces
70
int end= findEndOfWhiteSpace(d, start, c.offset);
71
72             StringBuffer JavaDoc buf= new StringBuffer JavaDoc(c.text);
73             if (end > start) {
74                 // append to input
75
buf.append(d.get(start, end - start));
76             }
77
78             c.text= buf.toString();
79
80         } catch (BadLocationException excp) {
81             // stop work
82
}
83     }
84
85     /*
86      * @see org.eclipse.jface.text.IAutoEditStrategy#customizeDocumentCommand(org.eclipse.jface.text.IDocument, org.eclipse.jface.text.DocumentCommand)
87      */

88     public void customizeDocumentCommand(IDocument d, DocumentCommand c) {
89         if (c.length == 0 && c.text != null && TextUtilities.endsWith(d.getLegalLineDelimiters(), c.text) != -1)
90             autoIndentAfterNewLine(d, c);
91     }
92 }
93
Popular Tags