KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > java > JavaStringAutoIndentStrategy


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.jdt.internal.ui.text.java;
12
13 import org.eclipse.jface.preference.IPreferenceStore;
14
15 import org.eclipse.jface.text.BadLocationException;
16 import org.eclipse.jface.text.DefaultIndentLineAutoEditStrategy;
17 import org.eclipse.jface.text.DocumentCommand;
18 import org.eclipse.jface.text.IDocument;
19 import org.eclipse.jface.text.IRegion;
20 import org.eclipse.jface.text.ITypedRegion;
21 import org.eclipse.jface.text.TextUtilities;
22
23 import org.eclipse.ui.IEditorPart;
24 import org.eclipse.ui.IWorkbenchPage;
25 import org.eclipse.ui.texteditor.ITextEditorExtension3;
26
27 import org.eclipse.jdt.ui.PreferenceConstants;
28
29 import org.eclipse.jdt.internal.ui.JavaPlugin;
30
31 /**
32  * Auto indent strategy for java strings
33  */

34 public class JavaStringAutoIndentStrategy extends DefaultIndentLineAutoEditStrategy {
35
36     private String JavaDoc fPartitioning;
37
38     /**
39      * The input string doesn't contain any line delimiter.
40      *
41      * @param inputString the given input string
42      * @return the displayable string.
43      */

44     private String JavaDoc displayString(String JavaDoc inputString, String JavaDoc indentation, String JavaDoc delimiter) {
45
46         int length = inputString.length();
47         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(length);
48         java.util.StringTokenizer JavaDoc tokenizer = new java.util.StringTokenizer JavaDoc(inputString, "\n\r", true); //$NON-NLS-1$
49
while (tokenizer.hasMoreTokens()){
50
51             String JavaDoc token = tokenizer.nextToken();
52             if (token.equals("\r")) { //$NON-NLS-1$
53
buffer.append("\\r"); //$NON-NLS-1$
54
if (tokenizer.hasMoreTokens()) {
55                     token = tokenizer.nextToken();
56                     if (token.equals("\n")) { //$NON-NLS-1$
57
buffer.append("\\n"); //$NON-NLS-1$
58
buffer.append("\" + " + delimiter); //$NON-NLS-1$
59
buffer.append(indentation);
60                         buffer.append("\""); //$NON-NLS-1$
61
continue;
62                     } else {
63                         buffer.append("\" + " + delimiter); //$NON-NLS-1$
64
buffer.append(indentation);
65                         buffer.append("\""); //$NON-NLS-1$
66
}
67                 } else {
68                     continue;
69                 }
70             } else if (token.equals("\n")) { //$NON-NLS-1$
71
buffer.append("\\n"); //$NON-NLS-1$
72
buffer.append("\" + " + delimiter); //$NON-NLS-1$
73
buffer.append(indentation);
74                 buffer.append("\""); //$NON-NLS-1$
75
continue;
76             }
77
78             StringBuffer JavaDoc tokenBuffer = new StringBuffer JavaDoc();
79             for (int i = 0; i < token.length(); i++){
80                 char c = token.charAt(i);
81                 switch (c) {
82                     case '\r' :
83                         tokenBuffer.append("\\r"); //$NON-NLS-1$
84
break;
85                     case '\n' :
86                         tokenBuffer.append("\\n"); //$NON-NLS-1$
87
break;
88                     case '\b' :
89                         tokenBuffer.append("\\b"); //$NON-NLS-1$
90
break;
91                     case '\t' :
92                         // keep tabs verbatim
93
tokenBuffer.append("\t"); //$NON-NLS-1$
94
break;
95                     case '\f' :
96                         tokenBuffer.append("\\f"); //$NON-NLS-1$
97
break;
98                     case '\"' :
99                         tokenBuffer.append("\\\""); //$NON-NLS-1$
100
break;
101                     case '\'' :
102                         tokenBuffer.append("\\'"); //$NON-NLS-1$
103
break;
104                     case '\\' :
105                         tokenBuffer.append("\\\\"); //$NON-NLS-1$
106
break;
107                     default :
108                         tokenBuffer.append(c);
109                 }
110             }
111             buffer.append(tokenBuffer);
112         }
113         return buffer.toString();
114     }
115
116     /**
117      * Creates a new Java string auto indent strategy for the given document partitioning.
118      *
119      * @param partitioning the document partitioning
120      */

121     public JavaStringAutoIndentStrategy(String JavaDoc partitioning) {
122         super();
123         fPartitioning= partitioning;
124     }
125
126     private boolean isLineDelimiter(IDocument document, String JavaDoc text) {
127         String JavaDoc[] delimiters= document.getLegalLineDelimiters();
128         if (delimiters != null)
129             return TextUtilities.equals(delimiters, text) > -1;
130         return false;
131     }
132
133     private String JavaDoc getLineIndentation(IDocument document, int offset) throws BadLocationException {
134
135         // find start of line
136
int adjustedOffset= (offset == document.getLength() ? offset - 1 : offset);
137         IRegion line= document.getLineInformationOfOffset(adjustedOffset);
138         int start= line.getOffset();
139
140         // find white spaces
141
int end= findEndOfWhiteSpace(document, start, offset);
142
143         return document.get(start, end - start);
144     }
145
146     private String JavaDoc getModifiedText(String JavaDoc string, String JavaDoc indentation, String JavaDoc delimiter) {
147         return displayString(string, indentation, delimiter);
148     }
149
150     private void javaStringIndentAfterNewLine(IDocument document, DocumentCommand command) throws BadLocationException {
151
152         ITypedRegion partition= TextUtilities.getPartition(document, fPartitioning, command.offset, true);
153         int offset= partition.getOffset();
154         int length= partition.getLength();
155
156         if (command.offset == offset + length && document.getChar(offset + length - 1) == '\"')
157             return;
158
159         String JavaDoc indentation= getLineIndentation(document, command.offset);
160         String JavaDoc delimiter= TextUtilities.getDefaultLineDelimiter(document);
161
162         IRegion line= document.getLineInformationOfOffset(offset);
163         String JavaDoc string= document.get(line.getOffset(), offset - line.getOffset());
164         if (string.trim().length() != 0)
165             indentation += String.valueOf("\t\t"); //$NON-NLS-1$
166

167         IPreferenceStore preferenceStore= JavaPlugin.getDefault().getPreferenceStore();
168         if (isLineDelimiter(document, command.text))
169             command.text= "\" +" + command.text + indentation + "\""; //$NON-NLS-1$//$NON-NLS-2$
170
else if (command.text.length() > 1 && preferenceStore.getBoolean(PreferenceConstants.EDITOR_ESCAPE_STRINGS))
171             command.text= getModifiedText(command.text, indentation, delimiter);
172     }
173
174     private boolean isSmartMode() {
175         IWorkbenchPage page= JavaPlugin.getActivePage();
176         if (page != null) {
177             IEditorPart part= page.getActiveEditor();
178             if (part instanceof ITextEditorExtension3) {
179                 ITextEditorExtension3 extension= (ITextEditorExtension3) part;
180                 return extension.getInsertMode() == ITextEditorExtension3.SMART_INSERT;
181             }
182         }
183         return false;
184     }
185
186     /*
187      * @see org.eclipse.jface.text.IAutoIndentStrategy#customizeDocumentCommand(IDocument, DocumentCommand)
188      */

189     public void customizeDocumentCommand(IDocument document, DocumentCommand command) {
190         try {
191             if (command.text == null)
192                 return;
193
194             IPreferenceStore preferenceStore= JavaPlugin.getDefault().getPreferenceStore();
195
196             if (preferenceStore.getBoolean(PreferenceConstants.EDITOR_WRAP_STRINGS) && isSmartMode()) {
197                 javaStringIndentAfterNewLine(document, command);
198             }
199
200         } catch (BadLocationException e) {
201         }
202     }
203 }
204
Popular Tags