Lines 1-5
Link Here
|
1 |
/******************************************************************************* |
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2000, 2008 IBM Corporation and others. |
2 |
* Copyright (c) 2000, 2009 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
5 |
* which accompanies this distribution, and is available at |
Lines 28-33
Link Here
|
28 |
import org.eclipse.compare.patch.IHunk; |
28 |
import org.eclipse.compare.patch.IHunk; |
29 |
import org.eclipse.compare.rangedifferencer.RangeDifference; |
29 |
import org.eclipse.compare.rangedifferencer.RangeDifference; |
30 |
import org.eclipse.compare.structuremergeviewer.*; |
30 |
import org.eclipse.compare.structuremergeviewer.*; |
|
|
31 |
import org.eclipse.core.resources.IResource; |
31 |
import org.eclipse.core.resources.ResourcesPlugin; |
32 |
import org.eclipse.core.resources.ResourcesPlugin; |
32 |
import org.eclipse.core.runtime.*; |
33 |
import org.eclipse.core.runtime.*; |
33 |
import org.eclipse.core.runtime.Assert; |
34 |
import org.eclipse.core.runtime.Assert; |
Lines 36-41
Link Here
|
36 |
import org.eclipse.jface.dialogs.MessageDialog; |
37 |
import org.eclipse.jface.dialogs.MessageDialog; |
37 |
import org.eclipse.jface.preference.IPreferenceStore; |
38 |
import org.eclipse.jface.preference.IPreferenceStore; |
38 |
import org.eclipse.jface.resource.ColorRegistry; |
39 |
import org.eclipse.jface.resource.ColorRegistry; |
|
|
40 |
import org.eclipse.jface.resource.ImageDescriptor; |
39 |
import org.eclipse.jface.resource.JFaceResources; |
41 |
import org.eclipse.jface.resource.JFaceResources; |
40 |
import org.eclipse.jface.text.*; |
42 |
import org.eclipse.jface.text.*; |
41 |
import org.eclipse.jface.text.Region; |
43 |
import org.eclipse.jface.text.Region; |
Lines 55-60
Link Here
|
55 |
import org.eclipse.swt.widgets.*; |
57 |
import org.eclipse.swt.widgets.*; |
56 |
import org.eclipse.ui.*; |
58 |
import org.eclipse.ui.*; |
57 |
import org.eclipse.ui.actions.ActionFactory; |
59 |
import org.eclipse.ui.actions.ActionFactory; |
|
|
60 |
import org.eclipse.ui.editors.text.IEncodingSupport; |
61 |
import org.eclipse.ui.editors.text.IStorageDocumentProvider; |
58 |
import org.eclipse.ui.texteditor.*; |
62 |
import org.eclipse.ui.texteditor.*; |
59 |
|
63 |
|
60 |
import com.ibm.icu.text.MessageFormat; |
64 |
import com.ibm.icu.text.MessageFormat; |
Lines 363-369
Link Here
|
363 |
} |
367 |
} |
364 |
} |
368 |
} |
365 |
|
369 |
|
366 |
class ContributorInfo implements IElementStateListener, VerifyListener, IDocumentListener { |
370 |
class ContributorInfo implements IElementStateListener, VerifyListener, IDocumentListener, IEncodingSupport { |
367 |
private final TextMergeViewer fViewer; |
371 |
private final TextMergeViewer fViewer; |
368 |
private final Object fElement; |
372 |
private final Object fElement; |
369 |
private char fLeg; |
373 |
private char fLeg; |
Lines 387-397
Link Here
|
387 |
} |
391 |
} |
388 |
} |
392 |
} |
389 |
} |
393 |
} |
390 |
|
394 |
|
|
|
395 |
public void setEncoding(String encoding) { |
396 |
if (fDocumentKey == null || fDocumentProvider == null) { |
397 |
return; |
398 |
} |
399 |
if (fDocumentProvider instanceof IStorageDocumentProvider) { |
400 |
IStorageDocumentProvider provider = (IStorageDocumentProvider) fDocumentProvider; |
401 |
String current = provider.getEncoding(fDocumentKey); |
402 |
boolean dirty = fDocumentProvider.canSaveDocument(fDocumentKey); |
403 |
if (!dirty) { |
404 |
String internal = encoding == null ? "" : encoding; //$NON-NLS-1$ |
405 |
if (!internal.equals(current)) { |
406 |
provider.setEncoding(fDocumentKey, encoding); |
407 |
try { |
408 |
fDocumentProvider.resetDocument(fDocumentKey); |
409 |
} catch (CoreException e) { |
410 |
CompareUIPlugin.log(e); |
411 |
} finally { |
412 |
update(true); |
413 |
} |
414 |
} |
415 |
} |
416 |
} |
417 |
} |
418 |
|
391 |
public String getEncoding() { |
419 |
public String getEncoding() { |
392 |
if (fEncoding == null) |
420 |
if (fDocumentProvider != null && fDocumentKey != null |
393 |
return ResourcesPlugin.getEncoding(); |
421 |
&& fDocumentProvider instanceof IStorageDocumentProvider) { |
394 |
return fEncoding; |
422 |
IStorageDocumentProvider provider = (IStorageDocumentProvider) fDocumentProvider; |
|
|
423 |
return provider.getEncoding(fDocumentKey); |
424 |
} |
425 |
return null; |
426 |
} |
427 |
|
428 |
public String getDefaultEncoding() { |
429 |
if (fDocumentProvider != null && fDocumentKey != null |
430 |
&& fDocumentProvider instanceof IStorageDocumentProvider) { |
431 |
IStorageDocumentProvider provider = (IStorageDocumentProvider) fDocumentProvider; |
432 |
return provider.getDefaultEncoding(); |
433 |
} |
434 |
return null; |
435 |
} |
436 |
|
437 |
private String internalGetEncoding() { |
438 |
if (fElement instanceof IEncodedStreamContentAccessor) { |
439 |
try { |
440 |
fEncoding = ((IEncodedStreamContentAccessor)fElement).getCharset(); |
441 |
} catch (CoreException e) { |
442 |
// silently ignored |
443 |
} |
444 |
} |
445 |
if (fEncoding != null) { |
446 |
return fEncoding; |
447 |
} |
448 |
return ResourcesPlugin.getEncoding(); |
395 |
} |
449 |
} |
396 |
|
450 |
|
397 |
public void setEncodingIfAbsent(ContributorInfo otherContributor) { |
451 |
public void setEncodingIfAbsent(ContributorInfo otherContributor) { |
Lines 582-588
Link Here
|
582 |
String s= null; |
636 |
String s= null; |
583 |
|
637 |
|
584 |
try { |
638 |
try { |
585 |
String encoding = getEncoding(); |
639 |
String encoding = internalGetEncoding(); |
586 |
s = Utilities.readString(sca, encoding); |
640 |
s = Utilities.readString(sca, encoding); |
587 |
} catch (CoreException ex) { |
641 |
} catch (CoreException ex) { |
588 |
this.fViewer.setError(fLeg, ex.getMessage()); |
642 |
this.fViewer.setError(fLeg, ex.getMessage()); |
Lines 2111-2117
Link Here
|
2111 |
|
2165 |
|
2112 |
// Add the find action to the popup menu of the viewer |
2166 |
// Add the find action to the popup menu of the viewer |
2113 |
contributeFindAction(part); |
2167 |
contributeFindAction(part); |
2114 |
|
2168 |
|
|
|
2169 |
contributeChangeEncodingAction(part); |
2170 |
|
2115 |
configureTextViewer(part); |
2171 |
configureTextViewer(part); |
2116 |
|
2172 |
|
2117 |
return part; |
2173 |
return part; |
Lines 2128-2133
Link Here
|
2128 |
viewer.addAction(MergeSourceViewer.FIND_ID, action); |
2184 |
viewer.addAction(MergeSourceViewer.FIND_ID, action); |
2129 |
} |
2185 |
} |
2130 |
|
2186 |
|
|
|
2187 |
private void contributeChangeEncodingAction(MergeSourceViewer viewer) { |
2188 |
ResourceBundle bundle = ResourceBundle.getBundle("org.eclipse.ui.texteditor.ConstructedTextEditorMessages"); //$NON-NLS-1$ |
2189 |
IAction action = new ChangeEncodingAction(bundle, "Editor.ChangeEncodingAction.", getTextEditorAdapter()) { //$NON-NLS-1$ |
2190 |
public void run() { |
2191 |
super.run(); |
2192 |
encodingChanged(); |
2193 |
} |
2194 |
}; |
2195 |
viewer.addAction(MergeSourceViewer.CHANGE_ENCODING_ID, action); |
2196 |
} |
2197 |
|
2198 |
private void encodingChanged() { |
2199 |
ContributorInfo info = null; |
2200 |
String key = null; |
2201 |
if (fFocusPart == fAncestor && fAncestorContributor != null) { |
2202 |
info = fAncestorContributor; |
2203 |
key = CompareConfiguration.ANCESTOR_ENCODING; |
2204 |
} else if (fFocusPart == fLeft && fLeftContributor != null) { |
2205 |
info = fLeftContributor; |
2206 |
key = CompareConfiguration.LEFT_ENCODING; |
2207 |
} else { |
2208 |
info = fRightContributor; |
2209 |
key = CompareConfiguration.RIGHT_ENCODING; |
2210 |
} |
2211 |
IEditorInput input = getEditorInput(info); |
2212 |
if (input != null && input.getAdapter(IResource.class) != null) { |
2213 |
refresh(); |
2214 |
} |
2215 |
getCompareConfiguration().setProperty(key, info.getEncoding()); |
2216 |
} |
2217 |
|
2131 |
private void connectGlobalActions(final MergeSourceViewer part) { |
2218 |
private void connectGlobalActions(final MergeSourceViewer part) { |
2132 |
if (fHandlerService != null) { |
2219 |
if (fHandlerService != null) { |
2133 |
if (part != null) |
2220 |
if (part != null) |
Lines 2575-2581
Link Here
|
2575 |
if (contents != null) { |
2662 |
if (contents != null) { |
2576 |
byte[] bytes; |
2663 |
byte[] bytes; |
2577 |
try { |
2664 |
try { |
2578 |
bytes= contents.getBytes(left ? fLeftContributor.getEncoding() : fRightContributor.getEncoding()); |
2665 |
bytes= contents.getBytes(left ? fLeftContributor.internalGetEncoding() : fRightContributor.internalGetEncoding()); |
2579 |
} catch(UnsupportedEncodingException ex) { |
2666 |
} catch(UnsupportedEncodingException ex) { |
2580 |
// use default encoding |
2667 |
// use default encoding |
2581 |
bytes= contents.getBytes(); |
2668 |
bytes= contents.getBytes(); |
Lines 4512-4515
Link Here
|
4512 |
return true; |
4599 |
return true; |
4513 |
return false; |
4600 |
return false; |
4514 |
} |
4601 |
} |
|
|
4602 |
|
4603 |
private ITextEditor getTextEditorAdapter() { |
4604 |
return new ITextEditor () { |
4605 |
public void close(boolean save) { |
4606 |
// Implementing interface method |
4607 |
} |
4608 |
public void doRevertToSaved() { |
4609 |
// Implementing interface method |
4610 |
} |
4611 |
public IAction getAction(String actionId) { |
4612 |
// Implementing interface method |
4613 |
return null; |
4614 |
} |
4615 |
public IDocumentProvider getDocumentProvider() { |
4616 |
// Implementing interface method |
4617 |
return null; |
4618 |
} |
4619 |
public IRegion getHighlightRange() { |
4620 |
// Implementing interface method |
4621 |
return null; |
4622 |
} |
4623 |
public ISelectionProvider getSelectionProvider() { |
4624 |
// Implementing interface method |
4625 |
return null; |
4626 |
} |
4627 |
public boolean isEditable() { |
4628 |
// Implementing interface method |
4629 |
return false; |
4630 |
} |
4631 |
public void removeActionActivationCode(String actionId) { |
4632 |
// Implementing interface method |
4633 |
} |
4634 |
public void resetHighlightRange() { |
4635 |
// Implementing interface method |
4636 |
} |
4637 |
public void selectAndReveal(int offset, int length) { |
4638 |
// Implementing interface method |
4639 |
} |
4640 |
public void setAction(String actionId, IAction action) { |
4641 |
// Implementing interface method |
4642 |
} |
4643 |
public void setActionActivationCode(String actionId, |
4644 |
char activationCharacter, int activationKeyCode, |
4645 |
int activationStateMask) { |
4646 |
// Implementing interface method |
4647 |
} |
4648 |
public void setHighlightRange(int offset, int length, |
4649 |
boolean moveCursor) { |
4650 |
// Implementing interface method |
4651 |
} |
4652 |
public void showHighlightRangeOnly(boolean showHighlightRangeOnly) { |
4653 |
// Implementing interface method |
4654 |
} |
4655 |
public boolean showsHighlightRangeOnly() { |
4656 |
// Implementing interface method |
4657 |
return false; |
4658 |
} |
4659 |
public IEditorInput getEditorInput() { |
4660 |
if (fFocusPart == fAncestor && fAncestorContributor != null) { |
4661 |
return TextMergeViewer.this |
4662 |
.getEditorInput(fAncestorContributor); |
4663 |
} else if (fFocusPart == fLeft && fLeftContributor != null) { |
4664 |
return TextMergeViewer.this |
4665 |
.getEditorInput(fLeftContributor); |
4666 |
} else if (fFocusPart == fRight && fRightContributor != null) { |
4667 |
return TextMergeViewer.this |
4668 |
.getEditorInput(fRightContributor); |
4669 |
} else { |
4670 |
return null; |
4671 |
} |
4672 |
} |
4673 |
public IEditorSite getEditorSite() { |
4674 |
// Implementing interface method |
4675 |
return null; |
4676 |
} |
4677 |
public void init(IEditorSite site, IEditorInput input) |
4678 |
throws PartInitException { |
4679 |
// Implementing interface method |
4680 |
} |
4681 |
public void addPropertyListener(IPropertyListener listener) { |
4682 |
// Implementing interface method |
4683 |
} |
4684 |
public void createPartControl(Composite parent) { |
4685 |
// Implementing interface method |
4686 |
} |
4687 |
public void dispose() { |
4688 |
// Implementing interface method |
4689 |
} |
4690 |
public IWorkbenchPartSite getSite() { |
4691 |
// Implementing interface method |
4692 |
return new IWorkbenchPartSite() { |
4693 |
public String getId() { |
4694 |
// Implementing interface method |
4695 |
return null; |
4696 |
} |
4697 |
public IKeyBindingService getKeyBindingService() { |
4698 |
// Implementing interface method |
4699 |
return null; |
4700 |
} |
4701 |
public IWorkbenchPart getPart() { |
4702 |
// Implementing interface method |
4703 |
return null; |
4704 |
} |
4705 |
public String getPluginId() { |
4706 |
// Implementing interface method |
4707 |
return null; |
4708 |
} |
4709 |
public String getRegisteredName() { |
4710 |
// Implementing interface method |
4711 |
return null; |
4712 |
} |
4713 |
public void registerContextMenu(MenuManager menuManager, |
4714 |
ISelectionProvider selectionProvider) { |
4715 |
// Implementing interface method |
4716 |
} |
4717 |
public void registerContextMenu(String menuId, |
4718 |
MenuManager menuManager, |
4719 |
ISelectionProvider selectionProvider) { |
4720 |
// Implementing interface method |
4721 |
} |
4722 |
public IWorkbenchPage getPage() { |
4723 |
// Implementing interface method |
4724 |
return null; |
4725 |
} |
4726 |
public ISelectionProvider getSelectionProvider() { |
4727 |
// Implementing interface method |
4728 |
return null; |
4729 |
} |
4730 |
public Shell getShell() { |
4731 |
return fComposite.getShell(); |
4732 |
} |
4733 |
public IWorkbenchWindow getWorkbenchWindow() { |
4734 |
// Implementing interface method |
4735 |
return null; |
4736 |
} |
4737 |
public void setSelectionProvider(ISelectionProvider provider) { |
4738 |
// Implementing interface method |
4739 |
} |
4740 |
public Object getAdapter(Class adapter) { |
4741 |
// Implementing interface method |
4742 |
return null; |
4743 |
} |
4744 |
public Object getService(Class api) { |
4745 |
// Implementing interface method |
4746 |
return null; |
4747 |
} |
4748 |
public boolean hasService(Class api) { |
4749 |
// Implementing interface method |
4750 |
return false; |
4751 |
} |
4752 |
}; |
4753 |
} |
4754 |
public String getTitle() { |
4755 |
// Implementing interface method |
4756 |
return null; |
4757 |
} |
4758 |
public Image getTitleImage() { |
4759 |
// Implementing interface method |
4760 |
return null; |
4761 |
} |
4762 |
public String getTitleToolTip() { |
4763 |
// Implementing interface method |
4764 |
return null; |
4765 |
} |
4766 |
public void removePropertyListener(IPropertyListener listener) { |
4767 |
// Implementing interface method |
4768 |
} |
4769 |
public void setFocus() { |
4770 |
// Implementing interface method |
4771 |
} |
4772 |
public Object getAdapter(Class adapter) { |
4773 |
if (adapter == IEncodingSupport.class) { |
4774 |
if (fFocusPart == fAncestor) { |
4775 |
return fAncestorContributor; |
4776 |
} else if (fFocusPart == fLeft) { |
4777 |
return fLeftContributor; |
4778 |
} else if (fFocusPart == fRight) { |
4779 |
return fRightContributor; |
4780 |
} else { |
4781 |
return null; |
4782 |
} |
4783 |
} |
4784 |
return null; |
4785 |
} |
4786 |
public void doSave(IProgressMonitor monitor) { |
4787 |
// Implementing interface method |
4788 |
} |
4789 |
public void doSaveAs() { |
4790 |
// Implementing interface method |
4791 |
} |
4792 |
public boolean isDirty() { |
4793 |
if (fFocusPart == fLeft) { |
4794 |
return isLeftDirty(); |
4795 |
} else if (fFocusPart == fRight) { |
4796 |
return isRightDirty(); |
4797 |
} |
4798 |
return false; |
4799 |
} |
4800 |
public boolean isSaveAsAllowed() { |
4801 |
// Implementing interface method |
4802 |
return false; |
4803 |
} |
4804 |
public boolean isSaveOnCloseNeeded() { |
4805 |
// Implementing interface method |
4806 |
return false; |
4807 |
} |
4808 |
}; |
4809 |
} |
4810 |
|
4811 |
private IEditorInput getEditorInput(final ContributorInfo contributor) { |
4812 |
IEditorInput ret = contributor.getDocumentKey(); |
4813 |
if (ret != null) { |
4814 |
return ret; |
4815 |
} |
4816 |
return new IEditorInput() { |
4817 |
public boolean exists() { |
4818 |
// Implementing interface method |
4819 |
return false; |
4820 |
} |
4821 |
public ImageDescriptor getImageDescriptor() { |
4822 |
// Implementing interface method |
4823 |
return null; |
4824 |
} |
4825 |
public String getName() { |
4826 |
// Implementing interface method |
4827 |
return null; |
4828 |
} |
4829 |
public IPersistableElement getPersistable() { |
4830 |
// Implementing interface method |
4831 |
return null; |
4832 |
} |
4833 |
public String getToolTipText() { |
4834 |
// Implementing interface method |
4835 |
return null; |
4836 |
} |
4837 |
public Object getAdapter(Class adapter) { |
4838 |
if (adapter == IResource.class) { |
4839 |
Object input = contributor.getElement(); |
4840 |
if (input instanceof ResourceNode) { |
4841 |
return ((ResourceNode) input).getResource(); |
4842 |
} |
4843 |
} |
4844 |
return null; |
4845 |
} |
4846 |
}; |
4847 |
} |
4515 |
} |
4848 |
} |