]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/preferences/cleanup/CleanUpPreview.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / preferences / cleanup / CleanUpPreview.java
CommitLineData
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
12package org.eclipse.jdt.internal.ui.preferences.cleanup;
13
14import java.util.Map;
15
16import org.eclipse.swt.widgets.Composite;
17
18import org.eclipse.core.runtime.IStatus;
19import org.eclipse.core.runtime.Status;
20
21import org.eclipse.jface.text.BadLocationException;
22import org.eclipse.jface.text.Region;
23import org.eclipse.jface.text.formatter.FormattingContextProperties;
24import org.eclipse.jface.text.formatter.IContentFormatter;
25import org.eclipse.jface.text.formatter.IContentFormatterExtension;
26import org.eclipse.jface.text.formatter.IFormattingContext;
27
28import org.eclipse.jdt.core.JavaCore;
29
30import org.eclipse.jdt.ui.cleanup.ICleanUpConfigurationUI;
31
32import org.eclipse.jdt.internal.ui.IJavaStatusConstants;
33import org.eclipse.jdt.internal.ui.JavaPlugin;
34import org.eclipse.jdt.internal.ui.actions.IndentAction;
35import org.eclipse.jdt.internal.ui.fix.MultiFixMessages;
36import org.eclipse.jdt.internal.ui.preferences.formatter.JavaPreview;
37import org.eclipse.jdt.internal.ui.text.java.JavaFormattingContext;
38
39
40public 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}