in org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/CommitAndDiffComponent.java [91:331]
public CommitAndDiffComponent(Composite parent, IWorkbenchPartSite site) {
commentAndDiffScrolledComposite = new ScrolledComposite(parent,
SWT.H_SCROLL | SWT.V_SCROLL);
commentAndDiffScrolledComposite.setExpandHorizontal(true);
commentAndDiffScrolledComposite.setExpandVertical(true);
commentAndDiffComposite = new Composite(commentAndDiffScrolledComposite,
SWT.NONE);
commentAndDiffScrolledComposite.setContent(commentAndDiffComposite);
commentAndDiffComposite
.setLayout(GridLayoutFactory.fillDefaults().create());
commentViewer = new CommitMessageViewer(commentAndDiffComposite, site);
commentViewer.getControl().setLayoutData(
GridDataFactory.fillDefaults().grab(true, false).create());
commentViewer.addTextInputListener(new ITextInputListener() {
@Override
public void inputDocumentChanged(IDocument oldInput,
IDocument newInput) {
commentCaret = new Point(0, 0);
}
@Override
public void inputDocumentAboutToBeChanged(IDocument oldInput,
IDocument newInput) {
// Nothing
}
});
commentViewer.addTextListener(
event -> resizeCommentAndDiffScrolledComposite());
StyledText commentWidget = commentViewer.getTextWidget();
commentWidget.addVerifyKeyListener(event -> {
// Get the current caret position *before* it moves (if it does)
int offset = commentWidget.getCaretOffset();
commentCaret = new Point(commentWidget.getLineAtOffset(offset),
offset);
});
commentWidget.addCaretListener(event -> {
Point location = commentWidget
.getLocationAtOffset(event.caretOffset);
scrollCommentAndDiff(location,
commentWidget.getLineHeight(event.caretOffset));
});
commentAndDiffComposite
.setBackground(commentViewer.getControl().getBackground());
HyperlinkSourceViewer.Configuration configuration = new HyperlinkSourceViewer.Configuration(
EditorsUI.getPreferenceStore()) {
@Override
public int getHyperlinkStateMask(ISourceViewer sourceViewer) {
return SWT.NONE;
}
@Override
protected IHyperlinkDetector[] internalGetHyperlinkDetectors(
ISourceViewer sourceViewer) {
IHyperlinkDetector[] registered = super.internalGetHyperlinkDetectors(
sourceViewer);
// Always add our special detector for commit hyperlinks; we
// want those to show always.
if (registered == null) {
return new IHyperlinkDetector[] {
new CommitMessageViewer.KnownHyperlinksDetector() };
} else {
IHyperlinkDetector[] result = new IHyperlinkDetector[registered.length
+ 1];
System.arraycopy(registered, 0, result, 0,
registered.length);
result[registered.length] = new CommitMessageViewer.KnownHyperlinksDetector();
return result;
}
}
@Override
public String[] getConfiguredContentTypes(
ISourceViewer sourceViewer) {
return new String[] { IDocument.DEFAULT_CONTENT_TYPE,
CommitMessageViewer.HEADER_CONTENT_TYPE,
CommitMessageViewer.FOOTER_CONTENT_TYPE };
}
@Override
public IPresentationReconciler getPresentationReconciler(
ISourceViewer viewer) {
PresentationReconciler reconciler = new PresentationReconciler();
reconciler.setDocumentPartitioning(
getConfiguredDocumentPartitioning(viewer));
DefaultDamagerRepairer hyperlinkDamagerRepairer = new DefaultDamagerRepairer(
new HyperlinkTokenScanner(this, viewer));
reconciler.setDamager(hyperlinkDamagerRepairer,
IDocument.DEFAULT_CONTENT_TYPE);
reconciler.setRepairer(hyperlinkDamagerRepairer,
IDocument.DEFAULT_CONTENT_TYPE);
Token headerDefault = new Token(new TextAttribute(
PlatformUI.getWorkbench().getDisplay()
.getSystemColor(SWT.COLOR_DARK_GRAY)));
DefaultDamagerRepairer headerDamagerRepairer = new DefaultDamagerRepairer(
new HyperlinkTokenScanner(this, viewer, headerDefault));
reconciler.setDamager(headerDamagerRepairer,
CommitMessageViewer.HEADER_CONTENT_TYPE);
reconciler.setRepairer(headerDamagerRepairer,
CommitMessageViewer.HEADER_CONTENT_TYPE);
DefaultDamagerRepairer footerDamagerRepairer = new DefaultDamagerRepairer(
new FooterTokenScanner(this, viewer));
reconciler.setDamager(footerDamagerRepairer,
CommitMessageViewer.FOOTER_CONTENT_TYPE);
reconciler.setRepairer(footerDamagerRepairer,
CommitMessageViewer.FOOTER_CONTENT_TYPE);
return reconciler;
}
};
commentViewer.configure(configuration);
diffViewer = new DiffViewer(commentAndDiffComposite, null, SWT.NONE);
diffViewer.configure(
new DiffViewer.Configuration(EditorsUI.getPreferenceStore()));
diffViewer.getControl().setLayoutData(
GridDataFactory.fillDefaults().grab(true, false).create());
diffViewer.addTextInputListener(new ITextInputListener() {
@Override
public void inputDocumentChanged(IDocument oldInput,
IDocument newInput) {
diffCaret = new Point(0, 0);
}
@Override
public void inputDocumentAboutToBeChanged(IDocument oldInput,
IDocument newInput) {
// Nothing
}
});
diffViewer.addTextListener(
event -> resizeCommentAndDiffScrolledComposite());
ActionUtils.UpdateableAction selectAll = ActionUtils.createGlobalAction(
ActionFactory.SELECT_ALL,
() -> diffViewer.doOperation(ITextOperationTarget.SELECT_ALL),
() -> diffViewer
.canDoOperation(ITextOperationTarget.SELECT_ALL));
ActionUtils.UpdateableAction copy = ActionUtils.createGlobalAction(
ActionFactory.COPY,
() -> diffViewer.doOperation(ITextOperationTarget.COPY),
() -> diffViewer.canDoOperation(ITextOperationTarget.COPY));
ActionUtils.setGlobalActions(diffViewer.getControl(), copy, selectAll);
ShowWhitespaceAction showWhitespaceAction = new ShowWhitespaceAction(
diffViewer);
diffViewer.addSelectionChangedListener(e -> copy.update());
MenuManager contextMenu = new MenuManager();
contextMenu.setRemoveAllWhenShown(true);
contextMenu.addMenuListener(manager -> {
if (diffViewer.getDocument().getLength() > 0) {
manager.add(copy);
manager.add(selectAll);
manager.add(new Separator());
manager.add(showWhitespaceAction);
}
});
StyledText diffWidget = diffViewer.getTextWidget();
diffWidget.setMenu(contextMenu.createContextMenu(diffWidget));
diffWidget.addDisposeListener(e -> showWhitespaceAction.dispose());
diffWidget.addVerifyKeyListener(event -> {
// Get the current caret position *before* it moves (if it does)
int offset = diffWidget.getCaretOffset();
diffCaret = new Point(diffWidget.getLineAtOffset(offset), offset);
});
diffWidget.addCaretListener(event -> {
Point location = diffWidget.getLocationAtOffset(event.caretOffset);
location.y += diffViewer.getControl().getLocation().y;
scrollCommentAndDiff(location,
diffWidget.getLineHeight(event.caretOffset));
});
commentAndDiffScrolledComposite
.addControlListener(new ControlAdapter() {
@Override
public void controlResized(ControlEvent e) {
if (!resizing && commentViewer.getTextWidget()
.getWordWrap()) {
resizeCommentAndDiffScrolledComposite();
}
}
});
// Continuous cursor navigation over the two viewers with the arrow keys
commentWidget.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (diffWidget.getCharCount() == 0) {
return;
}
if (e.keyCode == SWT.ARROW_DOWN) {
int lastLine = commentWidget.getLineCount() - 1;
if (commentCaret.x == lastLine
&& commentWidget.getLineAtOffset(commentWidget
.getCaretOffset()) == lastLine) {
diffWidget.setFocus();
diffWidget.setCaretOffset(0);
}
} else if (e.keyCode == SWT.ARROW_RIGHT) {
int chars = commentWidget.getCharCount();
if (commentCaret.y == chars
&& commentWidget.getCaretOffset() == chars) {
diffWidget.setFocus();
diffWidget.setCaretOffset(0);
}
}
}
});
diffWidget.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.keyCode == SWT.ARROW_UP) {
if (diffCaret.x == 0 && diffWidget.getLineAtOffset(
diffWidget.getCaretOffset()) == 0) {
commentWidget.setFocus();
commentWidget
.setCaretOffset(commentWidget.getOffsetAtLine(
commentWidget.getLineCount() - 1));
}
} else if (e.keyCode == SWT.ARROW_LEFT) {
if (diffCaret.y == 0
&& diffWidget.getCaretOffset() == 0) {
commentWidget.setFocus();
commentWidget
.setCaretOffset(commentWidget.getCharCount());
}
}
}
});
}