### Eclipse Workspace Patch 1.0 #P org.eclipse.compare Index: compare/org/eclipse/compare/internal/MergeSourceViewer.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.compare/compare/org/eclipse/compare/internal/Attic/MergeSourceViewer.java,v retrieving revision 1.44 diff -u -r1.44 MergeSourceViewer.java --- compare/org/eclipse/compare/internal/MergeSourceViewer.java 22 May 2007 20:19:52 -0000 1.44 +++ compare/org/eclipse/compare/internal/MergeSourceViewer.java 12 Jan 2009 14:02:11 -0000 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2007 IBM Corporation and others. + * Copyright (c) 2000, 2009 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -34,6 +34,7 @@ import org.eclipse.ui.PlatformUI; import org.eclipse.ui.editors.text.EditorsUI; import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants; +import org.eclipse.ui.texteditor.ChangeEncodingAction; import org.eclipse.ui.texteditor.FindReplaceAction; /** * Extends the JFace SourceViewer with some convenience methods. @@ -50,6 +51,7 @@ public static final String SELECT_ALL_ID= "selectAll"; //$NON-NLS-1$ public static final String SAVE_ID= "save"; //$NON-NLS-1$ public static final String FIND_ID= "find"; //$NON-NLS-1$ + public static final String CHANGE_ENCODING_ID= "changeEncoding"; //$NON-NLS-1$ class TextOperationAction extends MergeViewerAction { @@ -451,6 +453,7 @@ addMenu(menu, SELECT_ALL_ID); menu.add(new Separator("edit")); //$NON-NLS-1$ + addMenu(menu, CHANGE_ENCODING_ID); menu.add(new Separator("find")); //$NON-NLS-1$ addMenu(menu, FIND_ID); @@ -502,6 +505,9 @@ } if (next instanceof FindReplaceAction) { FindReplaceAction action = (FindReplaceAction) next; action.update(); + } else if (next instanceof ChangeEncodingAction) { + ChangeEncodingAction action = (ChangeEncodingAction) next; + action.update(); } } } Index: compare/org/eclipse/compare/contentmergeviewer/TextMergeViewer.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.compare/compare/org/eclipse/compare/contentmergeviewer/Attic/TextMergeViewer.java,v retrieving revision 1.231 diff -u -r1.231 TextMergeViewer.java --- compare/org/eclipse/compare/contentmergeviewer/TextMergeViewer.java 21 Apr 2008 08:53:25 -0000 1.231 +++ compare/org/eclipse/compare/contentmergeviewer/TextMergeViewer.java 12 Jan 2009 14:02:10 -0000 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2008 IBM Corporation and others. + * Copyright (c) 2000, 2009 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -55,6 +55,9 @@ import org.eclipse.swt.widgets.*; import org.eclipse.ui.*; import org.eclipse.ui.actions.ActionFactory; +import org.eclipse.ui.editors.text.IEncodingSupport; +import org.eclipse.ui.editors.text.IStorageDocumentProvider; +import org.eclipse.ui.progress.UIJob; import org.eclipse.ui.texteditor.*; import com.ibm.icu.text.MessageFormat; @@ -363,7 +366,7 @@ } } - class ContributorInfo implements IElementStateListener, VerifyListener, IDocumentListener { + class ContributorInfo implements IElementStateListener, VerifyListener, IDocumentListener, IEncodingSupport { private final TextMergeViewer fViewer; private final Object fElement; private char fLeg; @@ -387,11 +390,54 @@ } } } - + + public void setEncoding(String encoding) { + if (fDocumentKey == null || fDocumentProvider == null) { + return; + } + if (fDocumentProvider instanceof IStorageDocumentProvider) { + IStorageDocumentProvider provider = (IStorageDocumentProvider) fDocumentProvider; + String current = provider.getEncoding(fDocumentKey); + boolean dirty = fDocumentProvider.canSaveDocument(fDocumentKey); + if (!dirty) { + String internal = encoding == null ? "" : encoding; //$NON-NLS-1$ + if (!internal.equals(current)) { + provider.setEncoding(fDocumentKey, encoding); + try { + fDocumentProvider.resetDocument(fDocumentKey); + } catch (CoreException e) { + CompareUIPlugin.log(e); + } finally { + update(true); + } + } + } + } + } + public String getEncoding() { - if (fEncoding == null) - return ResourcesPlugin.getEncoding(); - return fEncoding; + if (fDocumentProvider != null && fDocumentKey != null + && fDocumentProvider instanceof IStorageDocumentProvider) { + IStorageDocumentProvider provider = (IStorageDocumentProvider) fDocumentProvider; + return provider.getEncoding(fDocumentKey); + } + return null; + } + + public String getDefaultEncoding() { + if (fDocumentProvider != null && fDocumentKey != null + && fDocumentProvider instanceof IStorageDocumentProvider) { + IStorageDocumentProvider provider = (IStorageDocumentProvider) fDocumentProvider; + return provider.getDefaultEncoding(); + } + return null; + } + + private String internalGetEncoding() { + if (fEncoding != null) { + return fEncoding; + } + return ResourcesPlugin.getEncoding(); } public void setEncodingIfAbsent(ContributorInfo otherContributor) { @@ -582,7 +628,7 @@ String s= null; try { - String encoding = getEncoding(); + String encoding = internalGetEncoding(); s = Utilities.readString(sca, encoding); } catch (CoreException ex) { this.fViewer.setError(fLeg, ex.getMessage()); @@ -812,6 +858,14 @@ IEditorInput input = getDocumentKey(); if (input != null && input.equals(element)) { this.fViewer.updateDirtyState(input, getDocumentProvider(), fLeg); + + // recalculate diffs and update controls + new UIJob(CompareMessages.DocumentMerger_0) { + public IStatus runInUIThread(IProgressMonitor monitor) { + update(true); + return Status.OK_STATUS; + } + }.schedule(); } } public void elementContentAboutToBeReplaced(Object element) { @@ -845,6 +899,7 @@ if (oldContributor != null) { fSelection = oldContributor.fSelection; fTopIndex = oldContributor.fTopIndex; + fEncoding = oldContributor.fEncoding; } } @@ -2111,7 +2166,9 @@ // Add the find action to the popup menu of the viewer contributeFindAction(part); - + + contributeChangeEncodingAction(part); + configureTextViewer(part); return part; @@ -2128,6 +2185,12 @@ viewer.addAction(MergeSourceViewer.FIND_ID, action); } + private void contributeChangeEncodingAction(MergeSourceViewer viewer) { + ResourceBundle bundle = ResourceBundle.getBundle("org.eclipse.ui.texteditor.ConstructedTextEditorMessages"); //$NON-NLS-1$ + IAction action = new ChangeEncodingAction(bundle, "Editor.ChangeEncodingAction.", getTextEditorAdapter()); //$NON-NLS-1$ + viewer.addAction(MergeSourceViewer.CHANGE_ENCODING_ID, action); + } + private void connectGlobalActions(final MergeSourceViewer part) { if (fHandlerService != null) { if (part != null) @@ -2575,7 +2638,7 @@ if (contents != null) { byte[] bytes; try { - bytes= contents.getBytes(left ? fLeftContributor.getEncoding() : fRightContributor.getEncoding()); + bytes= contents.getBytes(left ? fLeftContributor.internalGetEncoding() : fRightContributor.internalGetEncoding()); } catch(UnsupportedEncodingException ex) { // use default encoding bytes= contents.getBytes(); @@ -4512,4 +4575,209 @@ return true; return false; } + + private ITextEditor getTextEditorAdapter() { + return new ITextEditor () { + public void close(boolean save) { + // Implementing interface method + } + public void doRevertToSaved() { + // Implementing interface method + } + public IAction getAction(String actionId) { + // Implementing interface method + return null; + } + public IDocumentProvider getDocumentProvider() { + // Implementing interface method + return null; + } + public IRegion getHighlightRange() { + // Implementing interface method + return null; + } + public ISelectionProvider getSelectionProvider() { + // Implementing interface method + return null; + } + public boolean isEditable() { + // Implementing interface method + return false; + } + public void removeActionActivationCode(String actionId) { + // Implementing interface method + } + public void resetHighlightRange() { + // Implementing interface method + } + public void selectAndReveal(int offset, int length) { + // Implementing interface method + } + public void setAction(String actionId, IAction action) { + // Implementing interface method + } + public void setActionActivationCode(String actionId, + char activationCharacter, int activationKeyCode, + int activationStateMask) { + // Implementing interface method + } + public void setHighlightRange(int offset, int length, + boolean moveCursor) { + // Implementing interface method + } + public void showHighlightRangeOnly(boolean showHighlightRangeOnly) { + // Implementing interface method + } + public boolean showsHighlightRangeOnly() { + // Implementing interface method + return false; + } + public IEditorInput getEditorInput() { + if (fFocusPart == fAncestor && fAncestorContributor != null) { + return fAncestorContributor.getDocumentKey(); + } else if (fFocusPart == fLeft && fLeftContributor != null) { + return fLeftContributor.getDocumentKey(); + } else if (fFocusPart == fRight && fRightContributor != null) { + return fRightContributor.getDocumentKey(); + } else { + return null; + } + } + public IEditorSite getEditorSite() { + // Implementing interface method + return null; + } + public void init(IEditorSite site, IEditorInput input) + throws PartInitException { + // Implementing interface method + } + public void addPropertyListener(IPropertyListener listener) { + // Implementing interface method + } + public void createPartControl(Composite parent) { + // Implementing interface method + } + public void dispose() { + // Implementing interface method + } + public IWorkbenchPartSite getSite() { + // Implementing interface method + return new IWorkbenchPartSite() { + public String getId() { + // Implementing interface method + return null; + } + public IKeyBindingService getKeyBindingService() { + // Implementing interface method + return null; + } + public IWorkbenchPart getPart() { + // Implementing interface method + return null; + } + public String getPluginId() { + // Implementing interface method + return null; + } + public String getRegisteredName() { + // Implementing interface method + return null; + } + public void registerContextMenu(MenuManager menuManager, + ISelectionProvider selectionProvider) { + // Implementing interface method + } + public void registerContextMenu(String menuId, + MenuManager menuManager, + ISelectionProvider selectionProvider) { + // Implementing interface method + } + public IWorkbenchPage getPage() { + // Implementing interface method + return null; + } + public ISelectionProvider getSelectionProvider() { + // Implementing interface method + return null; + } + public Shell getShell() { + return fComposite.getShell(); + } + public IWorkbenchWindow getWorkbenchWindow() { + // Implementing interface method + return null; + } + public void setSelectionProvider(ISelectionProvider provider) { + // Implementing interface method + } + public Object getAdapter(Class adapter) { + // Implementing interface method + return null; + } + public Object getService(Class api) { + // Implementing interface method + return null; + } + public boolean hasService(Class api) { + // Implementing interface method + return false; + } + }; + } + public String getTitle() { + // Implementing interface method + return null; + } + public Image getTitleImage() { + // Implementing interface method + return null; + } + public String getTitleToolTip() { + // Implementing interface method + return null; + } + public void removePropertyListener(IPropertyListener listener) { + // Implementing interface method + } + public void setFocus() { + // Implementing interface method + } + public Object getAdapter(Class adapter) { + if (adapter == IEncodingSupport.class) { + if (fFocusPart == fAncestor) { + return fAncestorContributor; + } else if (fFocusPart == fLeft) { + return fLeftContributor; + } else if (fFocusPart == fRight) { + return fRightContributor; + } else { + return null; + } + } + return null; + } + public void doSave(IProgressMonitor monitor) { + // Implementing interface method + } + public void doSaveAs() { + // Implementing interface method + } + public boolean isDirty() { + if (fFocusPart == fLeft) { + return isLeftDirty(); + } else if (fFocusPart == fRight) { + return isRightDirty(); + } + return false; + } + public boolean isSaveAsAllowed() { + // Implementing interface method + return false; + } + public boolean isSaveOnCloseNeeded() { + // Implementing interface method + return false; + } + }; + } }