KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > preferences > formatter > CompilationUnitPreview


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.formatter;
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.internal.ui.IJavaStatusConstants;
28 import org.eclipse.jdt.internal.ui.JavaPlugin;
29 import org.eclipse.jdt.internal.ui.text.comment.CommentFormattingContext;
30
31
32 public class CompilationUnitPreview extends JavaPreview {
33
34     private String JavaDoc fPreviewText;
35
36     /**
37      * @param workingValues
38      * @param parent
39      */

40     public CompilationUnitPreview(Map JavaDoc workingValues, Composite parent) {
41
42         super(workingValues, parent);
43     }
44
45     protected void doFormatPreview() {
46         if (fPreviewText == null) {
47             fPreviewDocument.set(""); //$NON-NLS-1$
48
return;
49         }
50         fPreviewDocument.set(fPreviewText);
51         
52         fSourceViewer.setRedraw(false);
53         final IFormattingContext context = new CommentFormattingContext();
54         try {
55             final IContentFormatter formatter = fViewerConfiguration.getContentFormatter(fSourceViewer);
56             if (formatter instanceof IContentFormatterExtension) {
57                 final IContentFormatterExtension extension = (IContentFormatterExtension) formatter;
58                 context.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES, fWorkingValues);
59                 context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.valueOf(true));
60                 extension.format(fPreviewDocument, context);
61             } else
62                 formatter.format(fPreviewDocument, new Region(0, fPreviewDocument.getLength()));
63         } catch (Exception JavaDoc e) {
64             final IStatus status= new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IJavaStatusConstants.INTERNAL_ERROR,
65                 FormatterMessages.JavaPreview_formatter_exception, e);
66             JavaPlugin.log(status);
67         } finally {
68             context.dispose();
69             fSourceViewer.setRedraw(true);
70         }
71     }
72     
73     public void setPreviewText(String JavaDoc previewText) {
74 // if (previewText == null) throw new IllegalArgumentException();
75
fPreviewText= previewText;
76         update();
77     }
78 }
79
Popular Tags