KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2007 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  * Auto edit strategy that converts tabs into spaces.
16  * <p>
17  * Clients usually instantiate and configure this class but
18  * can also extend it in their own subclass.
19  * </p>
20  *
21  * @since 3.3
22  */

23 public class TabsToSpacesConverter implements IAutoEditStrategy {
24
25     private int fTabRatio;
26     private ILineTracker fLineTracker;
27
28
29     public void setNumberOfSpacesPerTab(int ratio) {
30         fTabRatio= ratio;
31     }
32
33     public void setLineTracker(ILineTracker lineTracker) {
34         fLineTracker= lineTracker;
35     }
36
37     private int insertTabString(StringBuffer JavaDoc buffer, int offsetInLine) {
38
39         if (fTabRatio == 0)
40             return 0;
41
42         int remainder= offsetInLine % fTabRatio;
43         remainder= fTabRatio - remainder;
44         for (int i= 0; i < remainder; i++)
45             buffer.append(' ');
46         return remainder;
47     }
48
49     public void customizeDocumentCommand(IDocument document, DocumentCommand command) {
50         String JavaDoc text= command.text;
51         if (text == null)
52             return;
53
54         int index= text.indexOf('\t');
55         if (index > -1) {
56
57             StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
58
59             fLineTracker.set(command.text);
60             int lines= fLineTracker.getNumberOfLines();
61
62             try {
63
64                 for (int i= 0; i < lines; i++) {
65
66                     int offset= fLineTracker.getLineOffset(i);
67                     int endOffset= offset + fLineTracker.getLineLength(i);
68                     String JavaDoc line= text.substring(offset, endOffset);
69
70                     int position= 0;
71                     if (i == 0) {
72                         IRegion firstLine= document.getLineInformationOfOffset(command.offset);
73                         position= command.offset - firstLine.getOffset();
74                     }
75
76                     int length= line.length();
77                     for (int j= 0; j < length; j++) {
78                         char c= line.charAt(j);
79                         if (c == '\t') {
80                             position += insertTabString(buffer, position);
81                         } else {
82                             buffer.append(c);
83                             ++ position;
84                         }
85                     }
86
87                 }
88
89                 command.text= buffer.toString();
90
91             } catch (BadLocationException x) {
92             }
93         }
94     }
95 }
96
Popular Tags