Index: ui/org/eclipse/jdt/internal/ui/compare/JavaMergeViewer.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/compare/JavaMergeViewer.java,v retrieving revision 1.19 diff -u -r1.19 JavaMergeViewer.java --- ui/org/eclipse/jdt/internal/ui/compare/JavaMergeViewer.java 29 May 2007 18:41:42 -0000 1.19 +++ ui/org/eclipse/jdt/internal/ui/compare/JavaMergeViewer.java 21 Sep 2007 08:19:42 -0000 @@ -10,6 +10,7 @@ *******************************************************************************/ package org.eclipse.jdt.internal.ui.compare; + import java.util.ArrayList; import java.util.Iterator; @@ -28,9 +29,11 @@ import org.eclipse.jface.preference.PreferenceConverter; import org.eclipse.compare.CompareConfiguration; +import org.eclipse.compare.CompareUI; import org.eclipse.compare.IResourceProvider; import org.eclipse.compare.contentmergeviewer.ITokenComparator; import org.eclipse.compare.contentmergeviewer.TextMergeViewer; +import org.eclipse.compare.internal.ComparePreferencePage; import org.eclipse.compare.structuremergeviewer.*; import org.eclipse.compare.ITypedElement; @@ -57,6 +60,7 @@ private boolean fUseSystemColors; private JavaSourceViewerConfiguration fSourceViewerConfiguration; private ArrayList fSourceViewer; + private boolean fIgnoreWhitespace; public JavaMergeViewer(Composite parent, int styles, CompareConfiguration mp) { @@ -117,7 +121,10 @@ public void setInput(Object input) { - if (input instanceof ICompareInput) { + if (input instanceof ICompareInput) { + if (fIgnoreWhitespace) { + input = new FormattedCompareInput((ICompareInput)input); + } IJavaProject project= getJavaProject((ICompareInput)input); if (project != null) { setPreferenceStore(createChainedPreferenceStore(project)); @@ -136,12 +143,13 @@ } private ChainedPreferenceStore createChainedPreferenceStore(IJavaProject project) { - ArrayList stores= new ArrayList(4); + ArrayList stores= new ArrayList(5); if (project != null) stores.add(new EclipsePreferencesAdapter(new ProjectScope(project.getProject()), JavaCore.PLUGIN_ID)); stores.add(JavaPlugin.getDefault().getPreferenceStore()); stores.add(new PreferencesAdapter(JavaCore.getPlugin().getPluginPreferences())); stores.add(EditorsUI.getPreferenceStore()); + stores.add(new PreferencesAdapter(CompareUI.getPlugin().getPluginPreferences())); return new ChainedPreferenceStore((IPreferenceStore[]) stores.toArray(new IPreferenceStore[stores.size()])); } @@ -416,6 +424,7 @@ } }; fPreferenceStore.addPropertyChangeListener(fPreferenceChangeListener); + this.fIgnoreWhitespace = fPreferenceStore.getBoolean(ComparePreferencePage.IGNORE_WHITESPACE); } } } Index: ui/org/eclipse/jdt/internal/ui/compare/FormattedCompareInput.java =================================================================== RCS file: ui/org/eclipse/jdt/internal/ui/compare/FormattedCompareInput.java diff -N ui/org/eclipse/jdt/internal/ui/compare/FormattedCompareInput.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ ui/org/eclipse/jdt/internal/ui/compare/FormattedCompareInput.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,179 @@ +package org.eclipse.jdt.internal.ui.compare; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.UnsupportedEncodingException; +import java.util.Map; + +import org.eclipse.compare.IEncodedStreamContentAccessor; +import org.eclipse.compare.IStreamContentAccessor; +import org.eclipse.compare.ITypedElement; +import org.eclipse.compare.structuremergeviewer.ICompareInput; +import org.eclipse.compare.structuremergeviewer.ICompareInputChangeListener; +import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jdt.core.ToolFactory; +import org.eclipse.jdt.core.formatter.CodeFormatter; + +import org.eclipse.jdt.internal.ui.JavaPlugin; + +import org.eclipse.jface.text.Document; +import org.eclipse.swt.graphics.Image; +import org.eclipse.text.edits.TextEdit; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; + + + +public class FormattedCompareInput implements ICompareInput{ + class StringInput implements ITypedElement, + IEncodedStreamContentAccessor { + + private String fContents; + + private String fType; + + private String fName; + + public StringInput(final String type, final String name, final String contents) { + this.fType = type; + this.fName = name; + this.fContents = contents; + } + + public Image getImage() { + return null; + } + + public String getName() { + return this.fName; + } + + public String getType() { + return this.fType; + } + + public String getCharset() throws CoreException { + return "UTF-16"; //$NON-NLS-1$ + } + + public InputStream getContents() throws CoreException { + byte[] bytes; + try { + bytes = this.fContents.getBytes("UTF-16"); //$NON-NLS-1$ + } catch (final UnsupportedEncodingException e) { + bytes = this.fContents.getBytes(); + } + return new ByteArrayInputStream(bytes); + } + + } + + private ICompareInput fCompareInput; + + + public FormattedCompareInput(final ICompareInput compareInput) { + this.fCompareInput = compareInput; + } + + public void addCompareInputChangeListener( + final ICompareInputChangeListener listener) { + this.fCompareInput.addCompareInputChangeListener(listener); + } + + public void copy(final boolean leftToRight) { + this.fCompareInput.copy(leftToRight); + } + + public ITypedElement getAncestor() { + return this.fCompareInput.getAncestor(); + } + + public Image getImage() { + return this.fCompareInput.getImage(); + } + + public int getKind() { + return this.fCompareInput.getKind(); + } + + public ITypedElement getLeft() { + final ITypedElement leftElement = this.fCompareInput.getLeft(); + + return format(leftElement); + } + + public String getName() { + return this.fCompareInput.getName(); + } + + public ITypedElement getRight() { + final ITypedElement rightElement = this.fCompareInput.getRight(); + + return format(rightElement); + } + + public void removeCompareInputChangeListener( + final ICompareInputChangeListener listener) { + this.fCompareInput.removeCompareInputChangeListener(listener); + } + + + + private ITypedElement format(final ITypedElement elementToFormat) { + try { + if (elementToFormat instanceof IStreamContentAccessor) { + final String contentsString = fromIStreamContentAccessorToString((IStreamContentAccessor) elementToFormat); + final Map options = JavaCore.getOptions(); + + final CodeFormatter codeFormatter = ToolFactory + .createCodeFormatter(options); + final TextEdit tmpOutputFromFormatter = codeFormatter.format( + CodeFormatter.K_COMPILATION_UNIT, contentsString, 0, contentsString + .length(), 0, null); + if (tmpOutputFromFormatter != null) { + //to convert from TextEdit to String we must pass it to a Document + final Document tempDoc = new Document(contentsString); + tmpOutputFromFormatter.apply(tempDoc); + final String formattedText = tempDoc.get(); + final StringInput toReturn = new StringInput(elementToFormat + .getType(), elementToFormat.getName(), formattedText); + return toReturn; + } else { + JavaPlugin.getDefault().getLog().log(new Status(IStatus.WARNING,JavaPlugin.getPluginId(), 0, "Cannot format the source code",null)); //$NON-NLS-1$ + return elementToFormat; + } + + } + + } catch (final Exception e) { + JavaPlugin.getDefault().getLog().log(new Status(IStatus.WARNING,JavaPlugin.getPluginId(), 0, "Cannot format the source code",e)); //$NON-NLS-1$ + } + return elementToFormat; + } + + private String fromIStreamContentAccessorToString(IStreamContentAccessor isca) throws CoreException, IOException { + String charset = null; + if (isca instanceof IEncodedStreamContentAccessor) { //try to get the charset from the inputstream + charset = ((IEncodedStreamContentAccessor)isca).getCharset(); + } + if (charset == null || "".equals(charset)) { //$NON-NLS-1$ + //set default charset to UTF-8 + charset = "UTF-8"; //$NON-NLS-1$ + } + final InputStream contentIs = isca.getContents(); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + byte[] buffer = new byte[4096]; + int readBytes = 0; + while((readBytes = contentIs.read(buffer)) >= 0) { + bos.write(buffer,0,readBytes); + } + return new String(bos.toByteArray(),charset); + } + + + +}