下面列出了org.eclipse.ui.handlers.IHandlerActivation#org.eclipse.jface.text.ITextOperationTarget 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void validateCompletionProposals(String text, String[] expectedProposalTexts)
throws IOException, CoreException {
IProject project = getProject(BASIC_PROJECT_NAME);
IFile file = project.getFolder("src").getFile("main.rs");
IEditorPart editor = IDE.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), file);
((ITextEditor) editor).getDocumentProvider().getDocument(editor.getEditorInput()).set(text);
ICompletionProposal[] proposals = (new SnippetContentAssistProcessor()).computeCompletionProposals(
(ITextViewer) editor.getAdapter(ITextOperationTarget.class), text.length() - 1);
if (expectedProposalTexts == null) {
assertTrue(proposals == null || proposals.length == 0);
return;
}
assertNotNull(proposals);
for (int i = 0; i < proposals.length; i++) {
assertEquals(((LSCompletionProposal) proposals[i]).getItem().getTextEdit().getNewText(),
expectedProposalTexts[i]);
}
}
@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
}
}
/**
* Implementation of the <code>IUpdate</code> prototype method discovers
* the operation through the current editor's
* <code>ITextOperationTarget</code> adapter, and sets the enabled state
* accordingly.
*/
@Override
public void update() {
super.update();
if (!canModifyEditor()) {
setEnabled(false);
return;
}
ITextEditor editor= getTextEditor();
if (fOperationTarget == null && editor != null)
fOperationTarget= Adapters.adapt(editor, ITextOperationTarget.class);
boolean isEnabled= (fOperationTarget != null && fOperationTarget.canDoOperation(ITextOperationTarget.PREFIX) && fOperationTarget.canDoOperation(ITextOperationTarget.STRIP_PREFIX));
setEnabled(isEnabled);
}
/**
* Creates the action.
*
* @param bundle
* the resource bundle
* @param prefix
* a prefix to be prepended to the various resource keys (described in <code>ResourceAction</code>
* constructor), or <code>null</code> if none
* @param editor
* the text editor. May not be <code>null</code>.
* @param operationCode
* the operation code
*/
public ImportsAwareClipboardAction(ResourceBundle bundle, String prefix, ITextEditor editor,
final int operationCode) {
super(bundle, prefix, editor);
this.operationCode = operationCode;
if (operationCode == ITextOperationTarget.CUT) {
setHelpContextId(IAbstractTextEditorHelpContextIds.CUT_ACTION);
setActionDefinitionId(IWorkbenchCommandConstants.EDIT_CUT);
} else if (operationCode == ITextOperationTarget.COPY) {
setHelpContextId(IAbstractTextEditorHelpContextIds.COPY_ACTION);
setActionDefinitionId(IWorkbenchCommandConstants.EDIT_COPY);
} else if (operationCode == ITextOperationTarget.PASTE) {
setHelpContextId(IAbstractTextEditorHelpContextIds.PASTE_ACTION);
setActionDefinitionId(IWorkbenchCommandConstants.EDIT_PASTE);
} else {
Assert.isTrue(false, "Invalid operation code"); //$NON-NLS-1$
}
update();
}
/**
* Resets the state of the editor to what it was before macro record or playback
* started.
*/
public void leaveMacroMode() {
if (fEditorPart != null) {
ITextOperationTarget textOperationTarget = fEditorPart.getAdapter(ITextOperationTarget.class);
if (textOperationTarget instanceof ITextOperationTargetExtension) {
ITextOperationTargetExtension targetExtension = (ITextOperationTargetExtension) textOperationTarget;
if (textOperationTarget instanceof ITextOperationTargetExtension) {
restore(targetExtension, ISourceViewer.CONTENTASSIST_PROPOSALS, CONTENT_ASSIST_ENABLED);
restore(targetExtension, ISourceViewer.QUICK_ASSIST, QUICK_ASSIST_ENABLED);
}
}
if (fEditorPart instanceof ITextEditor) {
ITextEditor textEditor = (ITextEditor) fEditorPart;
restore(textEditor, ITextEditorActionConstants.CONTENT_ASSIST);
restore(textEditor, ITextEditorActionConstants.QUICK_ASSIST);
restore(textEditor, ITextEditorActionConstants.BLOCK_SELECTION_MODE);
}
}
}
private void handleVerifyKeyPressed(VerifyEvent event) {
if (!event.doit)
return;
if (event.stateMask != SWT.MOD1)
return;
switch (event.character) {
case ' ':
fPatternEditor.doOperation(ISourceViewer.CONTENTASSIST_PROPOSALS);
event.doit = false;
break;
// CTRL-Z
case 'z' - 'a' + 1:
fPatternEditor.doOperation(ITextOperationTarget.UNDO);
event.doit = false;
break;
}
}
@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();
}
/**
* Creates the action.
* @param bundle the resource bundle
* @param prefix a prefix to be prepended to the various resource keys
* (described in <code>ResourceAction</code> constructor), or
* <code>null</code> if none
* @param editor the text editor
* @param operationCode the operation code
*/
public ClipboardOperationAction(ResourceBundle bundle, String prefix, ITextEditor editor, int operationCode) {
super(bundle, prefix, editor);
fOperationCode= operationCode;
if (operationCode == ITextOperationTarget.CUT) {
setHelpContextId(IAbstractTextEditorHelpContextIds.CUT_ACTION);
setActionDefinitionId(IWorkbenchCommandConstants.EDIT_CUT);
} else if (operationCode == ITextOperationTarget.COPY) {
setHelpContextId(IAbstractTextEditorHelpContextIds.COPY_ACTION);
setActionDefinitionId(IWorkbenchCommandConstants.EDIT_COPY);
} else if (operationCode == ITextOperationTarget.PASTE) {
setHelpContextId(IAbstractTextEditorHelpContextIds.PASTE_ACTION);
setActionDefinitionId(IWorkbenchCommandConstants.EDIT_PASTE);
} else {
Assert.isTrue(false, "Invalid operation code"); //$NON-NLS-1$
}
update();
}
@Override
public void doOperation(int operation) {
if (operation == ITextOperationTarget.PASTE && isAutoFormattingEnable()) {
final IRewriteTarget target = getRewriteTarget();
target.beginCompoundChange();
final IDocumentAutoFormatter formatter = getDocumentAutoFormatter();
formatter.beginAutoFormat();
try {
super.doOperation(operation);
} finally {
formatter.endAutoFormat();
target.endCompoundChange();
}
} else {
super.doOperation(operation);
}
}
/**
* 执行复制时对标记的处理,复制后在OS系统中不能包含标记占位符 ;
*/
private void copy() {
super.doOperation(ITextOperationTarget.COPY);
TextTransfer plainTextTransfer = TextTransfer.getInstance();
XLiffTextTransfer hsTextTransfer = XLiffTextTransfer.getInstance();
Clipboard clipboard = new Clipboard(getTextWidget().getDisplay());
String plainText = (String) clipboard.getContents(plainTextTransfer);
if (plainText == null || plainText.length() == 0) {
return;
}
plainText = plainText.replaceAll(Utils.getLineSeparator(), "\n");
plainText = plainText.replaceAll(Constants.LINE_SEPARATOR_CHARACTER + "", "");
plainText = plainText.replaceAll(Constants.TAB_CHARACTER + "", "\t");
plainText = plainText.replaceAll(Constants.SPACE_CHARACTER + "", " ");
plainText = plainText.replaceAll("\u200B", "");
clipboard.clearContents();
Object[] data = new Object[] { PATTERN.matcher(plainText).replaceAll(""), plainText };
Transfer[] types = new Transfer[] { plainTextTransfer, hsTextTransfer };
clipboard.setContents(data, types, DND.CLIPBOARD);
clipboard.dispose();
}
/**
* 执行复制时对标记的处理,复制后在OS系统中不能包含标记占位符 ;
*/
private void copy() {
super.doOperation(ITextOperationTarget.COPY);
TextTransfer plainTextTransfer = TextTransfer.getInstance();
HSTextTransfer hsTextTransfer = HSTextTransfer.getInstance();
Clipboard clipboard = new Clipboard(getTextWidget().getDisplay());
String plainText = (String) clipboard.getContents(plainTextTransfer);
if (plainText == null || plainText.length() == 0) {
return;
}
plainText = plainText.replaceAll(System.getProperty("line.separator"), "\n");
plainText = plainText.replaceAll(TmxEditorConstanst.LINE_SEPARATOR_CHARACTER + "", "");
plainText = plainText.replaceAll(TmxEditorConstanst.TAB_CHARACTER + "", "\t");
plainText = plainText.replaceAll(TmxEditorConstanst.SPACE_CHARACTER + "", " ");
plainText = plainText.replaceAll("\u200B", "");
clipboard.clearContents();
Object[] data = new Object[] { PATTERN.matcher(plainText).replaceAll(""), plainText };
Transfer[] types = new Transfer[] { plainTextTransfer, hsTextTransfer };
clipboard.setContents(data, types);
clipboard.dispose();
}
public static TMPresentationReconciler getTMPresentationReconciler(IEditorPart editorPart) {
if (editorPart == null) {
return null;
}
@Nullable ITextOperationTarget target = editorPart.getAdapter(ITextOperationTarget.class);
if (target instanceof ITextViewer) {
ITextViewer textViewer = ((ITextViewer) target);
return TMPresentationReconciler.getTMPresentationReconciler(textViewer);
}
return null;
}
static ITextViewer getViewer(IFile file) {
IWorkbench workbench = PlatformUI.getWorkbench();
IWorkbenchWindow activeWorkbenchWindow = workbench.getActiveWorkbenchWindow();
IWorkbenchPage activePage = activeWorkbenchWindow.getActivePage();
IEditorPart editorPart = ResourceUtil.findEditor(activePage, file);
ITextOperationTarget target = editorPart.getAdapter(ITextOperationTarget.class);
if (target instanceof ITextViewer) {
return (ITextViewer) target;
}
return null;
}
/**
* Implementation of the <code>IAction</code> prototype. Checks if the selected
* lines are all commented or not and uncomments/comments them respectively.
*/
@Override
public void run() {
if (fOperationTarget == null || fDocumentPartitioning == null || fPrefixesMap == null)
return;
ITextEditor editor= getTextEditor();
if (editor == null)
return;
if (!validateEditorInputState())
return;
final int operationCode;
if (isSelectionCommented(editor.getSelectionProvider().getSelection()))
operationCode= ITextOperationTarget.STRIP_PREFIX;
else
operationCode= ITextOperationTarget.PREFIX;
Shell shell= editor.getSite().getShell();
if (!fOperationTarget.canDoOperation(operationCode)) {
if (shell != null)
MessageDialog.openError(shell, Messages.ToggleSLCommentAction_0, Messages.ToggleSLCommentAction_1);
return;
}
Display display= null;
if (shell != null && !shell.isDisposed())
display= shell.getDisplay();
BusyIndicator.showWhile(display, new Runnable() {
@Override
public void run() {
fOperationTarget.doOperation(operationCode);
}
});
}
/**
* Update the state.
*/
public void updateEnabledState() {
if (viewer != null && !viewer.getTextWidget().isDisposed()) {
setEnabled(viewer.canDoOperation(ITextOperationTarget.COPY));
return;
}
if (copyAction != null) {
setEnabled(copyAction.isEnabled());
return;
}
setEnabled(false);
}
protected void internalDoOperation() {
if (operationCode == ITextOperationTarget.PASTE) {
doPasteWithImportsOperation();
} else {
doCutCopyWithImportsOperation();
}
}
public void runWithEvent(Event event) {
if (viewer != null && !viewer.getTextWidget().isDisposed()) {
viewer.doOperation(ITextOperationTarget.CUT);
updateActionsEnableState();
return;
}
if (cutAction != null) {
cutAction.runWithEvent(event);
return;
}
}
/**
* replace default cut/copy/paste actions with a version that provided by the factory, if one is injected
*/
protected void createClipboardActions() {
IClipboardActionFactory actionsFactory = getClipboardActionFactory();
if (actionsFactory != null) {
ResourceBundle bundle = XbaseEditorMessages.getBundleForConstructedKeys();
TextEditorAction action = actionsFactory.create(bundle, "Editor.Cut.", this, ITextOperationTarget.CUT); //$NON-NLS-1$
setAction(ITextEditorActionConstants.CUT, action);
action = actionsFactory.create(bundle, "Editor.Copy.", this, ITextOperationTarget.COPY); //$NON-NLS-1$
setAction(ITextEditorActionConstants.COPY, action);
action = actionsFactory.create(bundle, "Editor.Paste.", this, ITextOperationTarget.PASTE); //$NON-NLS-1$
setAction(ITextEditorActionConstants.PASTE, action);
}
}
@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;
}
/**
* Implementation of the <code>IAction</code> prototype. Checks if the selected
* lines are all commented or not and uncomments/comments them respectively.
*/
public void run()
{
if (fOperationTarget == null || fDocumentPartitioning == null || fPrefixesMap == null)
return;
ITextEditor editor = getTextEditor();
if (editor == null)
return;
if (!validateEditorInputState())
return;
final int operationCode;
if (isSelectionCommented(editor.getSelectionProvider().getSelection()))
operationCode = ITextOperationTarget.STRIP_PREFIX;
else
operationCode = ITextOperationTarget.PREFIX;
Shell shell = editor.getSite().getShell();
if (!fOperationTarget.canDoOperation(operationCode))
{
if (shell != null)
MessageDialog.openError(shell, TLAEditorMessages.getString("ToggleComment.error.title"),
TLAEditorMessages.getString("ToggleComment.error.message"));
return;
}
Display display = null;
if (shell != null && !shell.isDisposed())
display = shell.getDisplay();
BusyIndicator.showWhile(display, new Runnable() {
public void run()
{
fOperationTarget.doOperation(operationCode);
}
});
}
public void runWithEvent(Event event) {
if (viewer != null && !viewer.getTextWidget().isDisposed()) {
// 使用TextViewer组件的撤销功能
viewer.doOperation(ITextOperationTarget.UNDO);
updateActionsEnableState();
return;
}
if (undoAction != null) {
undoAction.runWithEvent(event);
return;
}
}
public void runWithEvent(Event event) {
if (viewer != null && !viewer.getTextWidget().isDisposed()) {
viewer.doOperation(ITextOperationTarget.COPY);
updateActionsEnableState();
return;
}
if (copyAction != null) {
copyAction.runWithEvent(event);
return;
}
}
@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);
}
/**
* Update the state.
*/
public void updateEnabledState() {
if (viewer != null && !viewer.getTextWidget().isDisposed()) {
setEnabled(viewer.canDoOperation(ITextOperationTarget.REDO));
return;
}
if (undoAction != null) {
setEnabled(redoAction.isEnabled());
return;
}
setEnabled(false);
}
/**
* Implementation of the <code>IAction</code> prototype. Checks if the selected
* lines are all commented or not and uncomments/comments them respectively.
*/
@Override
public void run() {
if (fOperationTarget == null || fDocumentPartitioning == null || fPrefixesMap == null)
return;
ITextEditor editor= getTextEditor();
if (editor == null)
return;
if (!validateEditorInputState())
return;
final int operationCode;
if (isSelectionCommented(editor.getSelectionProvider().getSelection()))
operationCode= ITextOperationTarget.STRIP_PREFIX;
else
operationCode= ITextOperationTarget.PREFIX;
Shell shell= editor.getSite().getShell();
if (!fOperationTarget.canDoOperation(operationCode)) {
if (shell != null)
MessageDialog.openError(shell, JavaEditorMessages.ToggleComment_error_title, JavaEditorMessages.ToggleComment_error_message);
return;
}
Display display= null;
if (shell != null && !shell.isDisposed())
display= shell.getDisplay();
BusyIndicator.showWhile(display, new Runnable() {
public void run() {
fOperationTarget.doOperation(operationCode);
}
});
}
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);
}
public void runWithEvent(Event event) {
if (viewer != null && !viewer.getTextWidget().isDisposed()) {
viewer.doOperation(ITextOperationTarget.CUT);
updateActionsEnableState();
return;
}
if (cutAction != null) {
cutAction.runWithEvent(event);
return;
}
}
/**
* Update the state.
*/
public void updateEnabledState() {
if (viewer != null && !viewer.getTextWidget().isDisposed()) {
setEnabled(viewer.canDoOperation(ITextOperationTarget.UNDO));
return;
}
if (undoAction != null) {
setEnabled(undoAction.isEnabled());
return;
}
setEnabled(false);
}
@Override
public void runWithEvent(Event event) {
if (viewer != null && !viewer.getTextWidget().isDisposed()) {
XLIFFEditorImplWithNatTable xliffEditor = XLIFFEditorImplWithNatTable.getCurrent();
// 先保存在撤销,除非以后取消两种模式,否则不要删除此判断
if (viewer.canDoOperation(ITextOperationTarget.UNDO)) {
HsMultiActiveCellEditor.commit(true);
}
IOperationHistory history = OperationHistoryFactory.getOperationHistory();
IUndoContext undoContext = (IUndoContext) xliffEditor.getTable().getData(IUndoContext.class.getName());
if (history.canUndo(undoContext)) {
try {
history.undo(undoContext, null, null);
undoBean.setCrosseStep(undoBean.getCrosseStep() + 1);
} catch (ExecutionException e) {
e.printStackTrace();
}
}
XLIFFEditorImplWithNatTable.getCurrent().redraw();
updateActionsEnableState();
return;
}
if (undoAction != null) {
undoAction.runWithEvent(event);
return;
}
}
public void runWithEvent(Event event) {
if (viewer != null && !viewer.getTextWidget().isDisposed()) {
viewer.doOperation(ITextOperationTarget.COPY);
updateActionsEnableState();
return;
}
if (copyAction != null) {
copyAction.runWithEvent(event);
return;
}
}