KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > fix > CodeFormatFix


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.jdt.internal.ui.fix;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.text.edits.DeleteEdit;
17 import org.eclipse.text.edits.MultiTextEdit;
18 import org.eclipse.text.edits.TextEdit;
19
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Status;
23
24 import org.eclipse.jface.text.BadLocationException;
25 import org.eclipse.jface.text.Document;
26 import org.eclipse.jface.text.IRegion;
27 import org.eclipse.jface.text.TextUtilities;
28
29 import org.eclipse.ltk.core.refactoring.CategorizedTextEditGroup;
30 import org.eclipse.ltk.core.refactoring.GroupCategory;
31 import org.eclipse.ltk.core.refactoring.GroupCategorySet;
32 import org.eclipse.ltk.core.refactoring.TextChange;
33
34 import org.eclipse.jdt.core.ICompilationUnit;
35 import org.eclipse.jdt.core.formatter.CodeFormatter;
36
37 import org.eclipse.jdt.internal.corext.fix.IFix;
38 import org.eclipse.jdt.internal.corext.refactoring.changes.CompilationUnitChange;
39 import org.eclipse.jdt.internal.corext.util.CodeFormatterUtil;
40
41 import org.eclipse.jdt.internal.ui.JavaPlugin;
42 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
43
44 public class CodeFormatFix implements IFix {
45     
46     public static IFix createCleanUp(ICompilationUnit cu, boolean format, boolean removeTrailingWhitespacesAll, boolean removeTrailingWhitespacesIgnorEmpty) throws CoreException {
47         if (!format && !removeTrailingWhitespacesAll && !removeTrailingWhitespacesIgnorEmpty)
48             return null;
49         
50         if (format) {
51             Map JavaDoc fomatterSettings= new HashMap JavaDoc(cu.getJavaProject().getOptions(true));
52             
53             String JavaDoc content= cu.getBuffer().getContents();
54             Document document= new Document(content);
55             
56             TextEdit edit= CodeFormatterUtil.reformat(CodeFormatter.K_COMPILATION_UNIT, content, 0, TextUtilities.getDefaultLineDelimiter(document), fomatterSettings);
57             if (edit == null || !edit.hasChildren())
58                 return null;
59             
60             String JavaDoc label= MultiFixMessages.CodeFormatFix_description;
61             TextChange change= new CompilationUnitChange(label, cu);
62             change.setEdit(edit);
63             
64             CategorizedTextEditGroup group= new CategorizedTextEditGroup(label, new GroupCategorySet(new GroupCategory(label, label, label)));
65             group.addTextEdit(edit);
66             change.addTextEditGroup(group);
67             
68             return new CodeFormatFix(change, cu);
69         } else if (removeTrailingWhitespacesAll || removeTrailingWhitespacesIgnorEmpty) {
70             try {
71                 MultiTextEdit multiEdit= new MultiTextEdit();
72                 Document document= new Document(cu.getBuffer().getContents());
73                 
74                 int lineCount= document.getNumberOfLines();
75                 for (int i= 0; i < lineCount; i++) {
76                     
77                     IRegion region= document.getLineInformation(i);
78                     if (region.getLength() == 0)
79                         continue;
80                     
81                     int lineStart= region.getOffset();
82                     int lineExclusiveEnd= lineStart + region.getLength();
83                     int j= getIndexOfRightMostNoneWhitspaceCharacter(lineStart, lineExclusiveEnd - 1, document);
84                     
85                     if (removeTrailingWhitespacesAll) {
86                         j++;
87                         if (j < lineExclusiveEnd)
88                             multiEdit.addChild(new DeleteEdit(j, lineExclusiveEnd - j));
89                     } else if (removeTrailingWhitespacesIgnorEmpty) {
90                         if (j >= lineStart) {
91                             if (document.getChar(j) == '*' && getIndexOfRightMostNoneWhitspaceCharacter(lineStart, j - 1, document) < lineStart) {
92                                 j++;
93                             }
94                             j++;
95                             if (j < lineExclusiveEnd)
96                                 multiEdit.addChild(new DeleteEdit(j, lineExclusiveEnd - j));
97                         }
98                     }
99                 }
100                 
101                 if (multiEdit.getChildrenSize() == 0)
102                     return null;
103                 
104                 String JavaDoc label= MultiFixMessages.CodeFormatFix_RemoveTrailingWhitespace_changeDescription;
105                 CompilationUnitChange change= new CompilationUnitChange(label, cu);
106                 change.setEdit(multiEdit);
107                 
108                 CategorizedTextEditGroup group= new CategorizedTextEditGroup(label, new GroupCategorySet(new GroupCategory(label, label, label)));
109                 group.addTextEdit(multiEdit);
110                 change.addTextEditGroup(group);
111                 
112                 return new CodeFormatFix(change, cu);
113             } catch (BadLocationException x) {
114                 throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, "", x)); //$NON-NLS-1$
115
}
116         }
117         
118         return null;
119     }
120     
121     /**
122      * Returns the index in document of a none whitespace character
123      * between start (inclusive) and end (inclusive) such that if
124      * more then one such character the index returned is the largest
125      * possible (closest to end). Returns start - 1 if no such character.
126      *
127      * @param start
128      * @param end
129      * @param document
130      * @return the position or start - 1
131      * @throws BadLocationException
132      */

133     private static int getIndexOfRightMostNoneWhitspaceCharacter(int start, int end, Document document) throws BadLocationException {
134         int position= end;
135         while (position >= start && Character.isWhitespace(document.getChar(position)))
136             position--;
137         
138         return position;
139     }
140
141     private final ICompilationUnit fCompilationUnit;
142     private final TextChange fChange;
143     
144     public CodeFormatFix(TextChange change, ICompilationUnit compilationUnit) {
145         fChange= change;
146         fCompilationUnit= compilationUnit;
147     }
148     
149     /* (non-Javadoc)
150      * @see org.eclipse.jdt.internal.corext.fix.IFix#createChange()
151      */

152     public TextChange createChange() throws CoreException {
153         return fChange;
154     }
155     
156     /* (non-Javadoc)
157      * @see org.eclipse.jdt.internal.corext.fix.IFix#getCompilationUnit()
158      */

159     public ICompilationUnit getCompilationUnit() {
160         return fCompilationUnit;
161     }
162     
163     /* (non-Javadoc)
164      * @see org.eclipse.jdt.internal.corext.fix.IFix#getDescription()
165      */

166     public String JavaDoc getDescription() {
167         return MultiFixMessages.CodeFormatFix_description;
168     }
169     
170     /* (non-Javadoc)
171      * @see org.eclipse.jdt.internal.corext.fix.IFix#getStatus()
172      */

173     public IStatus getStatus() {
174         return StatusInfo.OK_STATUS;
175     }
176     
177 }
178
Popular Tags