KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.text.edits.MalformedTreeException;
18 import org.eclipse.text.edits.MultiTextEdit;
19 import org.eclipse.text.edits.ReplaceEdit;
20 import org.eclipse.text.edits.TextEdit;
21
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IStatus;
24
25 import org.eclipse.jface.text.BadLocationException;
26 import org.eclipse.jface.text.Document;
27 import org.eclipse.jface.text.IDocument;
28 import org.eclipse.jface.text.ITypedRegion;
29 import org.eclipse.jface.text.TextUtilities;
30 import org.eclipse.jface.text.TypedPosition;
31 import org.eclipse.jface.text.formatter.FormattingContextProperties;
32 import org.eclipse.jface.text.formatter.IFormattingContext;
33
34 import org.eclipse.ltk.core.refactoring.CategorizedTextEditGroup;
35 import org.eclipse.ltk.core.refactoring.GroupCategory;
36 import org.eclipse.ltk.core.refactoring.GroupCategorySet;
37 import org.eclipse.ltk.core.refactoring.TextChange;
38
39 import org.eclipse.jdt.core.ICompilationUnit;
40 import org.eclipse.jdt.core.JavaCore;
41
42 import org.eclipse.jdt.internal.corext.fix.IFix;
43 import org.eclipse.jdt.internal.corext.refactoring.changes.CompilationUnitChange;
44
45 import org.eclipse.jdt.ui.text.IJavaPartitions;
46
47 import org.eclipse.jdt.internal.ui.JavaPlugin;
48 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
49 import org.eclipse.jdt.internal.ui.text.comment.CommentFormattingContext;
50 import org.eclipse.jdt.internal.ui.text.comment.CommentFormattingStrategy;
51
52 public class CommentFormatFix implements IFix {
53     
54     public static IFix createCleanUp(ICompilationUnit unit, boolean singleLine, boolean multiLine, boolean javaDoc, HashMap JavaDoc preferences) throws CoreException {
55         if (!singleLine && !multiLine && !javaDoc)
56             return null;
57         
58         String JavaDoc content= unit.getBuffer().getContents();
59         Document document= new Document(content);
60         
61         final List JavaDoc edits= format(document, singleLine, multiLine, javaDoc, preferences);
62         
63         if (edits.size() == 0)
64             return null;
65         
66         MultiTextEdit resultEdit= new MultiTextEdit();
67         resultEdit.addChildren((TextEdit[])edits.toArray(new TextEdit[edits.size()]));
68         
69         TextChange change= new CompilationUnitChange(MultiFixMessages.CommentFormatFix_description, unit);
70         change.setEdit(resultEdit);
71         
72         String JavaDoc label= MultiFixMessages.CommentFormatFix_description;
73         CategorizedTextEditGroup group= new CategorizedTextEditGroup(label, new GroupCategorySet(new GroupCategory(label, label, label)));
74         group.addTextEdit(resultEdit);
75         change.addTextEditGroup(group);
76         
77         return new CommentFormatFix(change, unit);
78     }
79     
80     static String JavaDoc format(String JavaDoc input, boolean singleLine, boolean multiLine, boolean javaDoc) {
81         if (!singleLine && !multiLine && !javaDoc)
82             return input;
83         
84         HashMap JavaDoc preferences= new HashMap JavaDoc(JavaCore.getOptions());
85         Document document= new Document(input);
86         List JavaDoc edits= format(document, singleLine, multiLine, javaDoc, preferences);
87         
88         if (edits.size() == 0)
89             return input;
90         
91         MultiTextEdit resultEdit= new MultiTextEdit();
92         resultEdit.addChildren((TextEdit[])edits.toArray(new TextEdit[edits.size()]));
93         
94         try {
95             resultEdit.apply(document);
96         } catch (MalformedTreeException e) {
97             JavaPlugin.log(e);
98         } catch (BadLocationException e) {
99             JavaPlugin.log(e);
100         }
101         return document.get();
102     }
103     
104     private static List JavaDoc format(IDocument document, boolean singleLine, boolean multiLine, boolean javaDoc, HashMap JavaDoc preferences) {
105         final List JavaDoc edits= new ArrayList JavaDoc();
106         
107         JavaPlugin.getDefault().getJavaTextTools().setupJavaDocumentPartitioner(document, IJavaPartitions.JAVA_PARTITIONING);
108         
109         String JavaDoc content= document.get();
110         
111         CommentFormattingStrategy formattingStrategy= new CommentFormattingStrategy();
112         
113         IFormattingContext context= new CommentFormattingContext();
114         context.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES, preferences);
115         context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.TRUE);
116         context.setProperty(FormattingContextProperties.CONTEXT_MEDIUM, document);
117         
118         try {
119             ITypedRegion[] regions= TextUtilities.computePartitioning(document, IJavaPartitions.JAVA_PARTITIONING, 0, document.getLength(), false);
120             for (int i= 0; i < regions.length; i++) {
121                 ITypedRegion region= regions[i];
122                 if (singleLine && region.getType().equals(IJavaPartitions.JAVA_SINGLE_LINE_COMMENT)) {
123                     TextEdit edit= format(region, context, formattingStrategy, content);
124                     if (edit != null)
125                         edits.add(edit);
126                 } else if (multiLine && region.getType().equals(IJavaPartitions.JAVA_MULTI_LINE_COMMENT)) {
127                     TextEdit edit= format(region, context, formattingStrategy, content);
128                     if (edit != null)
129                         edits.add(edit);
130                 } else if (javaDoc && region.getType().equals(IJavaPartitions.JAVA_DOC)) {
131                     TextEdit edit= format(region, context, formattingStrategy, content);
132                     if (edit != null)
133                         edits.add(edit);
134                 }
135             }
136         } catch (BadLocationException e) {
137             JavaPlugin.log(e);
138         } finally {
139             context.dispose();
140         }
141         
142         return edits;
143     }
144     
145     private static TextEdit format(ITypedRegion region, IFormattingContext context, CommentFormattingStrategy formattingStrategy, String JavaDoc content) {
146         TypedPosition typedPosition= new TypedPosition(region.getOffset(), region.getLength(), region.getType());
147         context.setProperty(FormattingContextProperties.CONTEXT_PARTITION, typedPosition);
148         formattingStrategy.formatterStarts(context);
149         TextEdit edit= formattingStrategy.calculateTextEdit();
150         formattingStrategy.formatterStops();
151         if (edit == null)
152             return null;
153         
154         if (!edit.hasChildren())
155             return null;
156         
157         // Filter out noops
158
TextEdit[] children= edit.getChildren();
159         for (int i= 0; i < children.length; i++) {
160             if (!(children[i] instanceof ReplaceEdit))
161                 return edit;
162         }
163         
164         IDocument doc= new Document(content);
165         try {
166             edit.copy().apply(doc, TextEdit.NONE);
167             if (content.equals(doc.get()))
168                 return null;
169         } catch (MalformedTreeException e) {
170         } catch (BadLocationException e) {
171         }
172         
173         return edit;
174     }
175     
176     private final ICompilationUnit fCompilationUnit;
177     private final TextChange fChange;
178     
179     public CommentFormatFix(TextChange change, ICompilationUnit compilationUnit) {
180         fChange= change;
181         fCompilationUnit= compilationUnit;
182     }
183     
184     /**
185      * {@inheritDoc}
186      */

187     public TextChange createChange() throws CoreException {
188         return fChange;
189     }
190     
191     /**
192      * {@inheritDoc}
193      */

194     public ICompilationUnit getCompilationUnit() {
195         return fCompilationUnit;
196     }
197     
198     /**
199      * {@inheritDoc}
200      */

201     public String JavaDoc getDescription() {
202         return MultiFixMessages.CommentFormatFix_description;
203     }
204     
205     /**
206      * {@inheritDoc}
207      */

208     public IStatus getStatus() {
209         return StatusInfo.OK_STATUS;
210     }
211 }
212
Popular Tags