下面列出了org.eclipse.jface.text.ITextOperationTarget#canDoOperation ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void run() {
try {
// Move offset to the line of the annotation, if necessary
IDocument document = getDocument();
int annotationLine = ruler.getLineOfLastMouseButtonActivity();
int annotationLineOffet = document.getLineOffset(annotationLine);
Point currentSelection = textEditor.getInternalSourceViewer().getSelectedRange();
int currentLine = document.getLineOfOffset(currentSelection.x);
if (currentLine != annotationLine)
textEditor.getInternalSourceViewer().setSelectedRange(annotationLineOffet, 0);
// show QuickFix dialog
ITextOperationTarget operation = textEditor.getAdapter(ITextOperationTarget.class);
final int opCode = ISourceViewer.QUICK_ASSIST;
if (operation != null && operation.canDoOperation(opCode))
operation.doOperation(opCode);
} catch (BadLocationException e) {
// Ignore -> do nothing
}
}
@Override
public void runWithEvent(Event event) {
if (fAnnotation instanceof OverrideIndicatorManager.OverrideIndicator) {
((OverrideIndicatorManager.OverrideIndicator)fAnnotation).open();
return;
}
if (fHasCorrection) {
ITextOperationTarget operation= (ITextOperationTarget) fTextEditor.getAdapter(ITextOperationTarget.class);
final int opCode= ISourceViewer.QUICK_ASSIST;
if (operation != null && operation.canDoOperation(opCode)) {
fTextEditor.selectAndReveal(fPosition.getOffset(), fPosition.getLength());
operation.doOperation(opCode);
}
return;
}
super.run();
}
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
ITextSelection selection = WorkbenchUtils.getActiveTextSelection();
IDocument document = WorkbenchUtils.getActiveDocument();
IEditorInput input = WorkbenchUtils.getActiveInput();
IEditorPart editor = WorkbenchUtils.getActiveEditor(false);
boolean isTextOperationAllowed = (selection != null) && (document != null)
&& (input != null) && (editor != null)
&& (editor instanceof SourceCodeTextEditor);
if (isTextOperationAllowed) {
final ITextOperationTarget operationTarget = (ITextOperationTarget) editor.getAdapter(ITextOperationTarget.class);
String commentPrefix = ((SourceCodeTextEditor)editor).getEOLCommentPrefix();
isTextOperationAllowed = (operationTarget != null)
&& (operationTarget instanceof TextViewer)
&& (validateEditorInputState((ITextEditor)editor))
&& (commentPrefix != null);
if ((isTextOperationAllowed)) {
final int operation = isSelectionCommented(document, selection, commentPrefix)
? ITextOperationTarget.STRIP_PREFIX
: ITextOperationTarget.PREFIX;
if (operationTarget.canDoOperation(operation)) {
BusyIndicator.showWhile(Display.getDefault(), new Runnable() {
public void run() {
// Really processed in TextViewer.doOperation:
operationTarget.doOperation(operation);
}
});
}
}
}
return null;
}
@Override
public void annotationDefaultSelected(VerticalRulerEvent event) {
Annotation annotation= event.getSelectedAnnotation();
IAnnotationModel model= getAnnotationModel();
if (isOverrideIndicator(annotation)) {
((OverrideIndicatorManager.OverrideIndicator)annotation).open();
return;
}
if (isBreakpoint(annotation))
triggerAction(ITextEditorActionConstants.RULER_DOUBLE_CLICK, event.getEvent());
Position position= model.getPosition(annotation);
if (position == null)
return;
if (isQuickFixTarget(annotation)) {
ITextOperationTarget operation= (ITextOperationTarget) getTextEditor().getAdapter(ITextOperationTarget.class);
final int opCode= ISourceViewer.QUICK_ASSIST;
if (operation != null && operation.canDoOperation(opCode)) {
getTextEditor().selectAndReveal(position.getOffset(), position.getLength());
operation.doOperation(opCode);
return;
}
}
// default:
super.annotationDefaultSelected(event);
}
private boolean computeEnablement(ITextEditor editor) {
if (editor == null)
return false;
ITextOperationTarget target= (ITextOperationTarget) editor.getAdapter(ITextOperationTarget.class);
if (target == null || ! target.canDoOperation(ISourceViewer.CONTENTASSIST_PROPOSALS))
return false;
IJavaProject javaProject = EditorUtility.getJavaProject(editor.getEditorInput());
if (! fCategory.matches(javaProject))
return false;
ISelection selection= editor.getSelectionProvider().getSelection();
return isValidSelection(selection);
}
private void disable(ITextOperationTarget textOperationTarget, ITextOperationTargetExtension targetExtension,
int operation, String preference) {
if (textOperationTarget.canDoOperation(operation)) {
fMemento.put(preference, true);
targetExtension.enableOperation(operation, false);
}
}
protected boolean isTargetOperationEnabled() {
ITextEditor editor = getTextEditor();
if(editor != null) {
fOperationTarget = (ITextOperationTarget) editor.getAdapter(ITextOperationTarget.class);
}
return fOperationTarget != null &&
fOperationTarget.canDoOperation(ITextOperationTarget.PREFIX) &&
fOperationTarget.canDoOperation(ITextOperationTarget.STRIP_PREFIX);
}