View | Details | Raw Unified | Return to bug 203430
Collapse All | Expand All

(-)ui/org/eclipse/jdt/internal/ui/compare/JavaMergeViewer.java (-2 / +11 lines)
Lines 10-15 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.jdt.internal.ui.compare;
11
package org.eclipse.jdt.internal.ui.compare;
12
12
13
13
import java.util.ArrayList;
14
import java.util.ArrayList;
14
import java.util.Iterator;
15
import java.util.Iterator;
15
16
Lines 28-36 Link Here
28
import org.eclipse.jface.preference.PreferenceConverter;
29
import org.eclipse.jface.preference.PreferenceConverter;
29
30
30
import org.eclipse.compare.CompareConfiguration;
31
import org.eclipse.compare.CompareConfiguration;
32
import org.eclipse.compare.CompareUI;
31
import org.eclipse.compare.IResourceProvider;
33
import org.eclipse.compare.IResourceProvider;
32
import org.eclipse.compare.contentmergeviewer.ITokenComparator;
34
import org.eclipse.compare.contentmergeviewer.ITokenComparator;
33
import org.eclipse.compare.contentmergeviewer.TextMergeViewer;
35
import org.eclipse.compare.contentmergeviewer.TextMergeViewer;
36
import org.eclipse.compare.internal.ComparePreferencePage;
34
import org.eclipse.compare.structuremergeviewer.*;
37
import org.eclipse.compare.structuremergeviewer.*;
35
import org.eclipse.compare.ITypedElement;
38
import org.eclipse.compare.ITypedElement;
36
39
Lines 57-62 Link Here
57
	private boolean fUseSystemColors;
60
	private boolean fUseSystemColors;
58
	private JavaSourceViewerConfiguration fSourceViewerConfiguration;
61
	private JavaSourceViewerConfiguration fSourceViewerConfiguration;
59
	private ArrayList fSourceViewer;
62
	private ArrayList fSourceViewer;
63
	private boolean fIgnoreWhitespace;
60
	
64
	
61
		
65
		
62
	public JavaMergeViewer(Composite parent, int styles, CompareConfiguration mp) {
66
	public JavaMergeViewer(Composite parent, int styles, CompareConfiguration mp) {
Lines 117-123 Link Here
117
121
118
    public void setInput(Object input) {
122
    public void setInput(Object input) {
119
    	
123
    	
120
    	if (input instanceof ICompareInput) {    		
124
    	if (input instanceof ICompareInput) {
125
    		if (fIgnoreWhitespace) {
126
    			input = new FormattedCompareInput((ICompareInput)input);
127
    		}
121
    		IJavaProject project= getJavaProject((ICompareInput)input);
128
    		IJavaProject project= getJavaProject((ICompareInput)input);
122
			if (project != null) {
129
			if (project != null) {
123
				setPreferenceStore(createChainedPreferenceStore(project));
130
				setPreferenceStore(createChainedPreferenceStore(project));
Lines 136-147 Link Here
136
    }
143
    }
137
    
144
    
138
    private ChainedPreferenceStore createChainedPreferenceStore(IJavaProject project) {
145
    private ChainedPreferenceStore createChainedPreferenceStore(IJavaProject project) {
139
    	ArrayList stores= new ArrayList(4);
146
    	ArrayList stores= new ArrayList(5);
140
    	if (project != null)
147
    	if (project != null)
141
    		stores.add(new EclipsePreferencesAdapter(new ProjectScope(project.getProject()), JavaCore.PLUGIN_ID));
148
    		stores.add(new EclipsePreferencesAdapter(new ProjectScope(project.getProject()), JavaCore.PLUGIN_ID));
142
		stores.add(JavaPlugin.getDefault().getPreferenceStore());
149
		stores.add(JavaPlugin.getDefault().getPreferenceStore());
143
		stores.add(new PreferencesAdapter(JavaCore.getPlugin().getPluginPreferences()));
150
		stores.add(new PreferencesAdapter(JavaCore.getPlugin().getPluginPreferences()));
144
		stores.add(EditorsUI.getPreferenceStore());
151
		stores.add(EditorsUI.getPreferenceStore());
152
		stores.add(new PreferencesAdapter(CompareUI.getPlugin().getPluginPreferences()));
145
		return new ChainedPreferenceStore((IPreferenceStore[]) stores.toArray(new IPreferenceStore[stores.size()]));
153
		return new ChainedPreferenceStore((IPreferenceStore[]) stores.toArray(new IPreferenceStore[stores.size()]));
146
    }
154
    }
147
155
Lines 416-421 Link Here
416
				}
424
				}
417
			};
425
			};
418
			fPreferenceStore.addPropertyChangeListener(fPreferenceChangeListener);
426
			fPreferenceStore.addPropertyChangeListener(fPreferenceChangeListener);
427
			this.fIgnoreWhitespace = fPreferenceStore.getBoolean(ComparePreferencePage.IGNORE_WHITESPACE);
419
		}
428
		}
420
	}
429
	}
421
}
430
}
(-)ui/org/eclipse/jdt/internal/ui/compare/FormattedCompareInput.java (+179 lines)
Added Link Here
1
package org.eclipse.jdt.internal.ui.compare;
2
3
import java.io.ByteArrayInputStream;
4
import java.io.ByteArrayOutputStream;
5
import java.io.IOException;
6
import java.io.InputStream;
7
import java.io.UnsupportedEncodingException;
8
import java.util.Map;
9
10
import org.eclipse.compare.IEncodedStreamContentAccessor;
11
import org.eclipse.compare.IStreamContentAccessor;
12
import org.eclipse.compare.ITypedElement;
13
import org.eclipse.compare.structuremergeviewer.ICompareInput;
14
import org.eclipse.compare.structuremergeviewer.ICompareInputChangeListener;
15
import org.eclipse.jdt.core.JavaCore;
16
import org.eclipse.jdt.core.ToolFactory;
17
import org.eclipse.jdt.core.formatter.CodeFormatter;
18
19
import org.eclipse.jdt.internal.ui.JavaPlugin;
20
21
import org.eclipse.jface.text.Document;
22
import org.eclipse.swt.graphics.Image;
23
import org.eclipse.text.edits.TextEdit;
24
25
import org.eclipse.core.runtime.CoreException;
26
import org.eclipse.core.runtime.IStatus;
27
import org.eclipse.core.runtime.Status;
28
29
30
31
public class FormattedCompareInput implements ICompareInput{
32
	class StringInput implements ITypedElement,
33
	IEncodedStreamContentAccessor {
34
		
35
		private String fContents;
36
		
37
		private String fType;
38
		
39
		private String fName;
40
		
41
		public StringInput(final String type, final String name, final String contents) {
42
			this.fType = type;
43
			this.fName = name;
44
			this.fContents = contents;
45
		}
46
		
47
		public Image getImage() {
48
			return null;
49
		}
50
		
51
		public String getName() {
52
			return this.fName;
53
		}
54
		
55
		public String getType() {
56
			return this.fType;
57
		}
58
		
59
		public String getCharset() throws CoreException {
60
			return "UTF-16"; //$NON-NLS-1$
61
		}
62
		
63
		public InputStream getContents() throws CoreException {
64
			byte[] bytes;
65
			try {
66
				bytes = this.fContents.getBytes("UTF-16"); //$NON-NLS-1$
67
			} catch (final UnsupportedEncodingException e) {
68
				bytes = this.fContents.getBytes();
69
			}
70
			return new ByteArrayInputStream(bytes);
71
		}
72
		
73
	}
74
75
	private ICompareInput fCompareInput;
76
77
78
	public FormattedCompareInput(final ICompareInput compareInput) {
79
		this.fCompareInput = compareInput;
80
	}
81
82
	public void addCompareInputChangeListener(
83
			final ICompareInputChangeListener listener) {
84
		this.fCompareInput.addCompareInputChangeListener(listener);
85
	}
86
87
	public void copy(final boolean leftToRight) {
88
		this.fCompareInput.copy(leftToRight);
89
	}
90
91
	public ITypedElement getAncestor() {
92
		return this.fCompareInput.getAncestor();
93
	}
94
95
	public Image getImage() {
96
		return this.fCompareInput.getImage();
97
	}
98
99
	public int getKind() {
100
		return this.fCompareInput.getKind();
101
	}
102
103
	public ITypedElement getLeft() {
104
		final ITypedElement leftElement = this.fCompareInput.getLeft();
105
106
		return format(leftElement);
107
	}
108
109
	public String getName() {
110
		return this.fCompareInput.getName();
111
	}
112
113
	public ITypedElement getRight() {
114
		final ITypedElement rightElement = this.fCompareInput.getRight();
115
		
116
		return format(rightElement); 
117
	}
118
119
	public void removeCompareInputChangeListener(
120
			final ICompareInputChangeListener listener) {
121
		this.fCompareInput.removeCompareInputChangeListener(listener);
122
	}
123
124
125
126
	private ITypedElement format(final ITypedElement elementToFormat) {
127
		try {
128
			if (elementToFormat instanceof IStreamContentAccessor) {
129
				final String contentsString = fromIStreamContentAccessorToString((IStreamContentAccessor) elementToFormat);
130
				final Map options = JavaCore.getOptions();
131
				
132
				final CodeFormatter codeFormatter = ToolFactory
133
						.createCodeFormatter(options);
134
				final TextEdit tmpOutputFromFormatter = codeFormatter.format(
135
						CodeFormatter.K_COMPILATION_UNIT, contentsString, 0, contentsString
136
								.length(), 0, null);
137
				if (tmpOutputFromFormatter != null) {
138
					//to convert from TextEdit to String we must pass it to a Document
139
					final Document tempDoc = new Document(contentsString);
140
					tmpOutputFromFormatter.apply(tempDoc);
141
					final String formattedText = tempDoc.get();
142
					final StringInput toReturn = new StringInput(elementToFormat
143
							.getType(), elementToFormat.getName(), formattedText);
144
					return toReturn;
145
				} else {
146
					JavaPlugin.getDefault().getLog().log(new Status(IStatus.WARNING,JavaPlugin.getPluginId(), 0, "Cannot format the source code",null)); //$NON-NLS-1$
147
					return elementToFormat;
148
				}
149
	
150
			}
151
	
152
		} catch (final Exception e) {
153
			JavaPlugin.getDefault().getLog().log(new Status(IStatus.WARNING,JavaPlugin.getPluginId(), 0, "Cannot format the source code",e)); //$NON-NLS-1$
154
		}
155
		return elementToFormat;
156
	}
157
158
	private String fromIStreamContentAccessorToString(IStreamContentAccessor isca) throws CoreException, IOException {
159
		String charset = null; 
160
		if (isca instanceof IEncodedStreamContentAccessor) { //try to get the charset from the inputstream
161
			charset = ((IEncodedStreamContentAccessor)isca).getCharset();
162
		}
163
		if (charset == null || "".equals(charset)) { //$NON-NLS-1$
164
			//set default charset to UTF-8
165
			charset = "UTF-8"; //$NON-NLS-1$
166
		}
167
		final InputStream contentIs = isca.getContents();
168
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
169
		byte[] buffer = new byte[4096];
170
		int readBytes = 0;
171
		while((readBytes = contentIs.read(buffer)) >= 0) {
172
			bos.write(buffer,0,readBytes);
173
		}
174
		return new String(bos.toByteArray(),charset); 
175
	}
176
177
178
179
}

Return to bug 203430