public class TextMergeViewer extends ContentMergeViewer implements IAdaptable
RangeDifferencer
to perform a
textual, line-by-line comparison of two (or three) input documents.
It is based on the ContentMergeViewer
and uses TextViewer
s
to implement the ancestor, left, and right content areas.
In the three-way compare case ranges of differing lines are highlighted and framed
with different colors to show whether the difference is an incoming, outgoing, or conflicting change.
The TextMergeViewer
supports the notion of a current "differing range"
and provides toolbar buttons to navigate from one range to the next (or previous).
If there is a current "differing range" and the underlying document is editable
the TextMergeViewer
enables actions in context menu and toolbar to
copy a range from one side to the other side, thereby performing a merge operation.
In addition to a line-by-line comparison the TextMergeViewer
uses a token based compare on differing lines.
The token compare is activated when navigating into
a range of differing lines. At first the lines are selected as a block.
When navigating into this block the token compare shows for every line
the differing token by selecting them.
The TextMergeViewer
's default token compare works on characters separated
by whitespace. If a different strategy is needed (for example, Java tokens in
a Java-aware merge viewer), clients can create their own token
comparators by implementing the ITokenComparator
interface and overriding the
TextMergeViewer.createTokenComparator
factory method).
Access to the TextMergeViewer
's model is by means of an
IMergeViewerContentProvider
. Its get
Content methods must return
either an IDocument
, an IDocumentRange
, or an IStreamContentAccessor
.
In the IDocumentRange
case the TextMergeViewer
works on a subrange of a document. In the IStreamContentAccessor
case
a document is created internally and initialized from the stream.
A TextMergeViewer
can be used as is. However clients may subclass
to customize the behavior. For example a MergeTextViewer
for Java would override
the configureTextViewer
method to configure the TextViewer
for Java source code,
the createTokenComparator
method to create a Java specific tokenizer.
In 3.5 a new API has been introduced to let clients provide their own source viewers implementation with an option to configure them basing on a corresponding editor input.
WIDGET_DATA_KEY
Constructor and Description |
---|
TextMergeViewer(Composite parent,
CompareConfiguration configuration)
Creates a text merge viewer under the given parent control.
|
TextMergeViewer(Composite parent,
int style,
CompareConfiguration configuration)
Creates a text merge viewer under the given parent control.
|
Modifier and Type | Method and Description |
---|---|
protected void |
configureTextViewer(TextViewer textViewer)
Configures the passed text viewer.
|
protected void |
copy(boolean leftToRight)
Copies the content of one side to the other side.
|
protected Control |
createCenterControl(Composite parent)
Create the control that divides the left and right sides of the merge viewer.
|
protected void |
createControls(Composite composite)
Creates the SWT controls for the ancestor, left, and right
content areas of this compare viewer.
|
protected SourceViewer |
createSourceViewer(Composite parent,
int textOrientation)
Creates a new source viewer.
|
protected ITokenComparator |
createTokenComparator(String line)
Creates an
ITokenComparator which is used to show the
intra line differences. |
protected void |
createToolItems(ToolBarManager tbm)
Contributes items to the given
ToolBarManager . |
protected boolean |
doSave(Object newInput,
Object oldInput)
Overridden to prevent save confirmation if new input is sub document of current input.
|
protected int |
findInsertionPosition(char type,
ICompareInput input) |
protected void |
flushContent(Object oldInput,
IProgressMonitor monitor)
Flush the modified content back to input elements via the content provider.
|
Object |
getAdapter(Class adapter)
Returns an object which is an instance of the given class
associated with this object.
|
protected int |
getCenterWidth()
Return the desired width of the center control.
|
protected byte[] |
getContents(boolean left)
Returns the contents of the underlying document as an array of bytes using the current workbench encoding.
|
protected IDocumentPartitioner |
getDocumentPartitioner()
Returns a document partitioner which is suitable for the underlying content type.
|
protected String |
getDocumentPartitioning()
Return the partitioning to which the partitioner returned from
getDocumentPartitioner() is to be associated. |
protected IEditorInput |
getEditorInput(ISourceViewer sourceViewer)
Returns an editor input for the given source viewer.
|
protected void |
handleCompareInputChange()
Handle a change to the given input reported from an
ICompareInputChangeListener . |
protected void |
handleDispose(DisposeEvent event)
Called on the viewer disposal.
|
protected void |
handlePropertyChangeEvent(PropertyChangeEvent event)
Callback that is invoked when a property in the compare configuration
(
ContentMergeViewer.getCompareConfiguration() changes. |
protected void |
handleResizeAncestor(int x,
int y,
int width,
int height)
Lays out the ancestor area of the compare viewer.
|
protected void |
handleResizeLeftRight(int x,
int y,
int width1,
int centerWidth,
int width2,
int height)
Lays out the left and right areas of the compare viewer.
|
protected boolean |
handleSetFocus()
Override to give focus to the pane that previously had focus or to a suitable
default pane.
|
void |
invalidateTextPresentation()
Invalidates the current presentation by invalidating the three text viewers.
|
protected boolean |
isEditorBacked(ITextViewer textViewer)
Tells whether the given text viewer is backed by an editor.
|
protected void |
setActionsActivated(SourceViewer sourceViewer,
boolean state)
Activates or deactivates actions of the given source viewer.
|
void |
setBackgroundColor(RGB background)
Sets the viewer's background color to the given RGB value.
|
protected void |
setEditable(ISourceViewer sourceViewer,
boolean state)
Sets the editable state of the given source viewer.
|
void |
setForegroundColor(RGB foreground)
Sets the viewer's foreground color to the given RGB value.
|
protected void |
setupDocument(IDocument document)
Setup the given document for use with this viewer.
|
protected void |
updateContent(Object ancestor,
Object left,
Object right)
Initializes the controls of the three content areas with the given input objects.
|
protected void |
updateHeader()
Updates the headers of the three areas
by querying the content provider for a name and image for
the three sides of the input object.
|
protected void |
updateToolItems()
Updates the enabled state of the toolbar items.
|
addPropertyChangeListener, buildControl, flush, flushLeft, flushRight, getCenterControl, getCompareConfiguration, getControl, getResourceBundle, getSelection, getTitle, getToolBarManager, inputChanged, internalIsLeftDirty, internalIsRightDirty, isAncestorVisible, isLeftDirty, isRightDirty, isThreeWay, refresh, removePropertyChangeListener, save, setConfirmSave, setContentProvider, setLeftDirty, setRightDirty, setSelection
getContentProvider, getInput, getLabelProvider, handleLabelProviderChanged, hookControl, labelProviderChanged, setInput, setLabelProvider
addHelpListener, addSelectionChangedListener, fireHelpRequested, fireSelectionChanged, getData, handleHelpRequest, removeHelpListener, removeSelectionChangedListener, scrollDown, scrollUp, setData, setSelection
public TextMergeViewer(Composite parent, CompareConfiguration configuration)
parent
- the parent controlconfiguration
- the configuration objectpublic TextMergeViewer(Composite parent, int style, CompareConfiguration configuration)
parent
- the parent controlstyle
- SWT style bits for top level composite of this viewerconfiguration
- the configuration objectpublic void setBackgroundColor(RGB background)
null
the system's default background color is used.background
- the background color or null
to use the system's default background colorpublic void setForegroundColor(RGB foreground)
null
the system's default foreground color is used.foreground
- the foreground color or null
to use the system's default foreground colorpublic void invalidateTextPresentation()
protected void configureTextViewer(TextViewer textViewer)
TextMergeViewer
implementation of this method will
configure the viewer with a SourceViewerConfiguration
.
Subclasses may reimplement to provide a specific configuration for the
text viewer.textViewer
- the text viewer to configureprotected ITokenComparator createTokenComparator(String line)
ITokenComparator
which is used to show the
intra line differences.
The TextMergeViewer
implementation of this method returns a
tokenizer that breaks a line into words separated by whitespace.
Subclasses may reimplement to provide a specific tokenizer.line
- the line for which to create the ITokenComparator
protected void setupDocument(IDocument document)
getDocumentPartitioner()
is registered as the default partitioner for the document.
Subclasses that return a partitioner must also override
getDocumentPartitioning()
if they wish to be able to use shared
documents (i.e. file buffers).document
- the document to be set upprotected IDocumentPartitioner getDocumentPartitioner()
IStreamContentAccessor
and an internal document must be obtained. This
document is initialized with the partitioner returned from this method.
The TextMergeViewer
implementation of this method returns
null
. Subclasses may reimplement to create a partitioner for a
specific content type. Subclasses that do return a partitioner should also
return a partitioning from getDocumentPartitioning()
in order to make
use of shared documents (e.g. file buffers).
null
protected String getDocumentPartitioning()
getDocumentPartitioner()
is to be associated. Return null
only if partitioning is not needed or if the subclass
overrode setupDocument(IDocument)
directly.
By default, null
is returned which means that shared
documents that return a partitioner from getDocumentPartitioner()
will not be able to use shared documents.IDocumentExtension3
protected void handleDispose(DisposeEvent event)
handleDispose
in class ContentMergeViewer
event
- ContentViewer.handleDispose(org.eclipse.swt.events.DisposeEvent)
protected void createControls(Composite composite)
ContentMergeViewer
updateContent
.createControls
in class ContentMergeViewer
composite
- the container for the three areasprotected boolean handleSetFocus()
handleSetFocus
in class ContentMergeViewer
ContentMergeViewer.handleSetFocus()
protected final Control createCenterControl(Composite parent)
ContentMergeViewer
createCenterControl
in class ContentMergeViewer
parent
- the parent compositeprotected final int getCenterWidth()
ContentMergeViewer
getCenterWidth
in class ContentMergeViewer
ContentMergeViewer.handleResizeLeftRight(int, int, int, int, int, int)
,
ContentMergeViewer.handleResizeAncestor(int, int, int, int)
protected SourceViewer createSourceViewer(Composite parent, int textOrientation)
createControls(Composite)
). It is called three
times for each text part of the comparison: ancestor, left, right.
Clients may implement to provide their own type of source viewers. The
viewer is not expected to be configured with a source viewer
configuration.parent
- the parent of the viewer's controltextOrientation
- style constant bit for text orientationSourceViewer
.protected boolean isEditorBacked(ITextViewer textViewer)
textViewer
- the text viewer to checktrue
if the viewer is backed by an editorprotected IEditorInput getEditorInput(ISourceViewer sourceViewer)
null
when no input is available, for example when the input
for the merge viewer has not been set yet.sourceViewer
- the source viewer to get input fornull
when no input is
availableprotected void setActionsActivated(SourceViewer sourceViewer, boolean state)
The default implementation does nothing, but clients should override to properly react to viewers switching.
sourceViewer
- the source viewerstate
- true
if activatedprotected boolean doSave(Object newInput, Object oldInput)
doSave
in class ContentMergeViewer
newInput
- the new input of this viewer, or null
if there is no new inputoldInput
- the old input element, or null
if there was previously no inputtrue
if saving was successful, or if the user didn't want to save (by pressing 'NO' in the confirmation dialog).protected void updateContent(Object ancestor, Object left, Object right)
ContentMergeViewer
updateContent
in class ContentMergeViewer
ancestor
- the input for the ancestor arealeft
- the input for the left arearight
- the input for the right areaprotected void setEditable(ISourceViewer sourceViewer, boolean state)
sourceViewer
- the source viewerstate
- the stateprotected int findInsertionPosition(char type, ICompareInput input)
protected byte[] getContents(boolean left)
getContents
in class ContentMergeViewer
left
- if true
the contents of the left side is returned; otherwise the right sideprotected final void handleResizeAncestor(int x, int y, int width, int height)
ContentMergeViewer
handleResizeAncestor
in class ContentMergeViewer
x
- the horizontal position of the ancestor area within its containery
- the vertical position of the ancestor area within its containerwidth
- the width of the ancestor areaheight
- the height of the ancestor areaprotected final void handleResizeLeftRight(int x, int y, int width1, int centerWidth, int width2, int height)
ContentMergeViewer
handleResizeLeftRight
in class ContentMergeViewer
x
- the horizontal position of the left area within its containery
- the vertical position of the left and right area within its containerwidth1
- the width of the left areacenterWidth
- the width of the gap between the left and right areaswidth2
- the width of the right areaheight
- the height of the left and right areasprotected void updateHeader()
ContentMergeViewer
This method is called whenever the header must be updated.
Subclasses may extend this method, although this is generally not required.
updateHeader
in class ContentMergeViewer
protected void createToolItems(ToolBarManager tbm)
ContentMergeViewer
ToolBarManager
.
It is called when this viewer is installed in its container and if the container
has a ToolBarManager
.
The ContentMergeViewer
implementation of this method does nothing.
Subclasses may reimplement.createToolItems
in class ContentMergeViewer
tbm
- the toolbar manager to contribute toprotected void handlePropertyChangeEvent(PropertyChangeEvent event)
ContentMergeViewer
ContentMergeViewer.getCompareConfiguration()
changes.handlePropertyChangeEvent
in class ContentMergeViewer
event
- the property change eventprotected void updateToolItems()
ContentMergeViewer
This method is called whenever the state of the items needs updating.
Subclasses may extend this method, although this is generally not required.
updateToolItems
in class ContentMergeViewer
protected void copy(boolean leftToRight)
ContentMergeViewer
copy
in class ContentMergeViewer
leftToRight
- if true
, the left side is copied to the right side;
if false
, the right side is copied to the left sideprotected void flushContent(Object oldInput, IProgressMonitor monitor)
ContentMergeViewer
flushContent
in class ContentMergeViewer
oldInput
- the compare inputmonitor
- a progress monitor or null
if the method
was call from a place where a progress monitor was not available.public Object getAdapter(Class adapter)
IAdaptable
null
if
no such object can be found.getAdapter
in interface IAdaptable
adapter
- the adapter class to look upnull
if this object does not
have an adapter for the given classprotected void handleCompareInputChange()
ContentMergeViewer
ICompareInputChangeListener
.
This class registers a listener with its input and reports any change events through
this method. By default, this method prompts for any unsaved changes and then refreshes
the viewer. Subclasses may override.handleCompareInputChange
in class ContentMergeViewer
Copyright (c) 2000, 2014 Eclipse Contributors and others. All rights reserved.Guidelines for using Eclipse APIs.