David Green の記事は良いスタートでしたが、すべてを機能させるには少し掘り下げました。完全なサンプル プロジェクトを GitHub に公開しました。いくつかのスニペットをここに投稿します。
このTextViewerSupport
クラスは、エクストラ テキスト ビューアに委譲するコマンドごとに新しいアクション ハンドラを関連付けます。複数のテキスト ビューアがあるTextViewerSupport
場合は、それぞれに対してオブジェクトをインスタンス化するだけです。コンストラクターですべてを結び付けます。
public TextViewerSupport(TextViewer textViewer) {
this.textViewer = textViewer;
StyledText textWidget = textViewer.getTextWidget();
textWidget.addFocusListener(this);
textWidget.addDisposeListener(this);
IWorkbenchWindow window = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow();
handlerService = (IHandlerService) window
.getService(IHandlerService.class);
if (textViewer.getTextWidget().isFocusControl()) {
activateContext();
}
}
このactivateContext()
メソッドには、委譲するすべてのコマンドのリストがあり、それぞれに新しいハンドラーを登録します。これは David の記事からの変更の 1 つです。thisITextEditorActionDefinitionIds
は廃止され、 に置き換えられましたIWorkbenchCommandConstants
。
protected void activateContext() {
if (handlerActivations.isEmpty()) {
activateHandler(ITextOperationTarget.COPY,
IWorkbenchCommandConstants.EDIT_COPY);
}
}
// Add a single handler.
protected void activateHandler(int operation, String actionDefinitionId) {
StyledText textWidget = textViewer.getTextWidget();
IHandler actionHandler = createActionHandler(operation,
actionDefinitionId);
IHandlerActivation handlerActivation = handlerService.activateHandler(
actionDefinitionId, actionHandler,
new ActiveFocusControlExpression(textWidget));
handlerActivations.add(handlerActivation);
}
// Create a handler that delegates to the text viewer.
private IHandler createActionHandler(final int operation,
String actionDefinitionId) {
Action action = new Action() {
@Override
public void run() {
if (textViewer.canDoOperation(operation)) {
textViewer.doOperation(operation);
}
}
};
action.setActionDefinitionId(actionDefinitionId);
return new ActionHandler(action);
}
これActiveFocusControlExpression
により、新しいハンドラーに十分な優先度が与えられ、標準のハンドラーが置き換えられます。これは、David のバージョンとほとんど同じです。ただし、コンパイルするには、プラグイン マニフェストに追加の依存関係を追加する必要がありました。パッケージorg.eclipse.core.expressions
とorg.eclipse.ui.texteditor
.