]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 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; | |
15 | ||
16 | import org.eclipse.swt.widgets.Composite; | |
17 | ||
18 | import org.eclipse.core.runtime.IStatus; | |
19 | import org.eclipse.core.runtime.Status; | |
20 | ||
21 | import org.eclipse.jface.text.BadLocationException; | |
22 | import org.eclipse.jface.text.Region; | |
23 | import org.eclipse.jface.text.formatter.FormattingContextProperties; | |
24 | import org.eclipse.jface.text.formatter.IContentFormatter; | |
25 | import org.eclipse.jface.text.formatter.IContentFormatterExtension; | |
26 | import org.eclipse.jface.text.formatter.IFormattingContext; | |
27 | ||
28 | import org.eclipse.jdt.core.JavaCore; | |
29 | ||
30 | import org.eclipse.jdt.ui.cleanup.ICleanUpConfigurationUI; | |
31 | ||
32 | import org.eclipse.jdt.internal.ui.IJavaStatusConstants; | |
33 | import org.eclipse.jdt.internal.ui.JavaPlugin; | |
34 | import org.eclipse.jdt.internal.ui.actions.IndentAction; | |
35 | import org.eclipse.jdt.internal.ui.fix.MultiFixMessages; | |
36 | import org.eclipse.jdt.internal.ui.preferences.formatter.JavaPreview; | |
37 | import org.eclipse.jdt.internal.ui.text.java.JavaFormattingContext; | |
38 | ||
39 | ||
40 | public class CleanUpPreview extends JavaPreview { | |
41 | ||
42 | private ICleanUpConfigurationUI fPage; | |
43 | private boolean fFormat; | |
44 | private boolean fCorrectIndentation; | |
45 | ||
46 | public CleanUpPreview(Composite parent, ICleanUpConfigurationUI page) { | |
47 | super(JavaCore.getDefaultOptions(), parent); | |
48 | fPage= page; | |
49 | fFormat= false; | |
50 | } | |
51 | ||
52 | public void setFormat(boolean enable) { | |
53 | fFormat= enable; | |
54 | } | |
55 | ||
56 | public void setCorrectIndentation(boolean enabled) { | |
57 | fCorrectIndentation= enabled; | |
58 | } | |
59 | ||
60 | /** | |
61 | * {@inheritDoc} | |
62 | */ | |
63 | @Override | |
64 | protected void doFormatPreview() { | |
65 | format(fPage.getPreview()); | |
66 | } | |
67 | ||
68 | private void format(String text) { | |
69 | if (text == null) { | |
70 | fPreviewDocument.set(""); //$NON-NLS-1$ | |
71 | return; | |
72 | } | |
73 | fPreviewDocument.set(text); | |
74 | ||
75 | if (!fFormat) { | |
76 | if (!fCorrectIndentation) | |
77 | return; | |
78 | ||
79 | fSourceViewer.setRedraw(false); | |
80 | try { | |
81 | IndentAction.indent(fPreviewDocument, null); | |
82 | } catch (BadLocationException e) { | |
83 | JavaPlugin.log(e); | |
84 | } finally { | |
85 | fSourceViewer.setRedraw(true); | |
86 | } | |
87 | ||
88 | return; | |
89 | } | |
90 | ||
91 | fSourceViewer.setRedraw(false); | |
92 | final IFormattingContext context = new JavaFormattingContext(); | |
93 | try { | |
94 | final IContentFormatter formatter = fViewerConfiguration.getContentFormatter(fSourceViewer); | |
95 | if (formatter instanceof IContentFormatterExtension) { | |
96 | final IContentFormatterExtension extension = (IContentFormatterExtension) formatter; | |
97 | context.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES, JavaCore.getOptions()); | |
98 | context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.valueOf(true)); | |
99 | extension.format(fPreviewDocument, context); | |
100 | } else | |
101 | formatter.format(fPreviewDocument, new Region(0, fPreviewDocument.getLength())); | |
102 | } catch (Exception e) { | |
103 | final IStatus status= new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IJavaStatusConstants.INTERNAL_ERROR, | |
104 | MultiFixMessages.CleanUpRefactoringWizard_formatterException_errorMessage, e); | |
105 | JavaPlugin.log(status); | |
106 | } finally { | |
107 | context.dispose(); | |
108 | fSourceViewer.setRedraw(true); | |
109 | } | |
110 | } | |
111 | ||
112 | @Override | |
113 | public void setWorkingValues(Map<String, String> workingValues) { | |
114 | //Don't change the formatter settings | |
115 | } | |
116 | ||
117 | } |