KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > preferences > cleanup > CleanUpPreview


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
12 package org.eclipse.jdt.internal.ui.preferences.cleanup;
13
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.Status;
18
19 import org.eclipse.swt.widgets.Composite;
20
21 import org.eclipse.jface.text.Region;
22 import org.eclipse.jface.text.formatter.FormattingContextProperties;
23 import org.eclipse.jface.text.formatter.IContentFormatter;
24 import org.eclipse.jface.text.formatter.IContentFormatterExtension;
25 import org.eclipse.jface.text.formatter.IFormattingContext;
26
27 import org.eclipse.jdt.core.JavaCore;
28
29 import org.eclipse.jdt.internal.ui.IJavaStatusConstants;
30 import org.eclipse.jdt.internal.ui.JavaPlugin;
31 import org.eclipse.jdt.internal.ui.fix.ICleanUp;
32 import org.eclipse.jdt.internal.ui.fix.MultiFixMessages;
33 import org.eclipse.jdt.internal.ui.preferences.formatter.JavaPreview;
34 import org.eclipse.jdt.internal.ui.text.comment.CommentFormattingContext;
35
36
37 public class CleanUpPreview extends JavaPreview {
38
39     private ICleanUp[] fPreviewCleanUps;
40     private boolean fFormat;
41     public CleanUpPreview(Composite parent, ICleanUp[] cleanUps) {
42         super(JavaCore.getDefaultOptions(), parent);
43         fPreviewCleanUps= cleanUps;
44         fFormat= false;
45     }
46     
47     public void setCleanUps(ICleanUp[] fCleanUps) {
48         fPreviewCleanUps= fCleanUps;
49     }
50     
51     public void setFormat(boolean enable) {
52         fFormat= enable;
53     }
54
55     /**
56      * {@inheritDoc}
57      */

58     protected void doFormatPreview() {
59     
60         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
61         for (int i= 0; i < fPreviewCleanUps.length; i++) {
62             buf.append(fPreviewCleanUps[i].getPreview());
63             buf.append("\n"); //$NON-NLS-1$
64
}
65         format(buf.toString());
66     }
67     
68     private void format(String JavaDoc text) {
69         if (text == null) {
70             fPreviewDocument.set(""); //$NON-NLS-1$
71
return;
72         }
73         fPreviewDocument.set(text);
74         
75         if (!fFormat)
76             return;
77         
78         fSourceViewer.setRedraw(false);
79         final IFormattingContext context = new CommentFormattingContext();
80         try {
81             final IContentFormatter formatter = fViewerConfiguration.getContentFormatter(fSourceViewer);
82             if (formatter instanceof IContentFormatterExtension) {
83                 final IContentFormatterExtension extension = (IContentFormatterExtension) formatter;
84                 context.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES, JavaCore.getOptions());
85                 context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.valueOf(true));
86                 extension.format(fPreviewDocument, context);
87             } else
88                 formatter.format(fPreviewDocument, new Region(0, fPreviewDocument.getLength()));
89         } catch (Exception JavaDoc e) {
90             final IStatus status= new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IJavaStatusConstants.INTERNAL_ERROR,
91                 MultiFixMessages.CleanUpRefactoringWizard_formatterException_errorMessage, e);
92             JavaPlugin.log(status);
93         } finally {
94             context.dispose();
95             fSourceViewer.setRedraw(true);
96         }
97     }
98
99     public void setWorkingValues(Map JavaDoc workingValues) {
100         //Don't change the formatter settings
101
}
102     
103 }
104
Popular Tags