private void doPasteWithImportsOperation() { ITextEditor editor = getTextEditor(); IJavaElement inputElement = JavaUI.getEditorInputTypeRoot(editor.getEditorInput()); Clipboard clipboard = new Clipboard(getDisplay()); try { ClipboardData importsData = (ClipboardData) clipboard.getContents(fgTransferInstance); if (importsData != null && inputElement instanceof ICompilationUnit && !importsData.isFromSame(inputElement)) { // combine operation and adding of imports IRewriteTarget target = (IRewriteTarget) editor.getAdapter(IRewriteTarget.class); if (target != null) { target.beginCompoundChange(); } try { fOperationTarget.doOperation(fOperationCode); addImports((ICompilationUnit) inputElement, importsData); } catch (CoreException e) { JavaPlugin.log(e); } finally { if (target != null) { target.endCompoundChange(); } } } else { fOperationTarget.doOperation(fOperationCode); } } finally { clipboard.dispose(); } }
/** * 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. */ public void update() { boolean isEnabled = (fOperationTarget != null && fOperationTarget.canDoOperation(ITextOperationTarget.PREFIX) && fOperationTarget.canDoOperation(ITextOperationTarget.STRIP_PREFIX)); setEnabled(isEnabled); }
@Override public void runWithEvent(Event event) { if (mCanFix) { ITextOperationTarget operation = (ITextOperationTarget) mTextEditor.getAdapter(ITextOperationTarget.class); final int opCode = ISourceViewer.QUICK_ASSIST; if (operation != null && operation.canDoOperation(opCode)) { mTextEditor.selectAndReveal(mPosition.getOffset(), mPosition.getLength()); operation.doOperation(opCode); } return; } super.run(); }
/** * 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. */ public void update() { super.update(); if (!canModifyEditor()) { setEnabled(false); return; } ITextEditor editor = getTextEditor(); if (fOperationTarget == null && editor != null) fOperationTarget = (ITextOperationTarget) editor.getAdapter(ITextOperationTarget.class); boolean isEnabled = (fOperationTarget != null && fOperationTarget.canDoOperation(ITextOperationTarget.PREFIX) && fOperationTarget.canDoOperation(ITextOperationTarget.STRIP_PREFIX)); setEnabled(isEnabled); }
/** * 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, // JavaEditorMessages.getString("ToggleComment.error.title"), // JavaEditorMessages.getString("ToggleComment.error.message")); // //$NON-NLS-1$ //$NON-NLS-2$ return; } Display display = null; if (shell != null && !shell.isDisposed()) display = shell.getDisplay(); BusyIndicator.showWhile( display, new Runnable() { public void run() { fOperationTarget.doOperation(operationCode); } }); }
/** * 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; final int operationCode; if (isSelectionCommented(fViewer.getSelectionProvider().getSelection())) operationCode = ITextOperationTarget.STRIP_PREFIX; else operationCode = ITextOperationTarget.PREFIX; Shell shell = fViewer.getTextWidget().getShell(); if (!fOperationTarget.canDoOperation(operationCode)) { if (shell != null) MessageDialog.openError( shell, "An error occured", "ToggleComment_error_message=An error occurred while toggling comments."); //$NON-NLS-1$//$NON-NLS-2$ return; } Display display = null; if (shell != null && !shell.isDisposed()) display = shell.getDisplay(); BusyIndicator.showWhile( display, new Runnable() { public void run() { fOperationTarget.doOperation(operationCode); } }); }
/* * @see org.eclipse.jface.action.IAction#runWithEvent(org.eclipse.swt.widgets.Event) * @since 3.2 */ 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(); }
private void doCutCopyWithImportsOperation() { ITextEditor editor = getTextEditor(); ITypeRoot inputElement = JavaUI.getEditorInputTypeRoot(editor.getEditorInput()); ISelection selection = editor.getSelectionProvider().getSelection(); Object clipboardData = null; if (inputElement != null && selection instanceof ITextSelection && !selection.isEmpty()) { ITextSelection textSelection = (ITextSelection) selection; if (isNonTrivialSelection(textSelection)) { clipboardData = getClipboardData(inputElement, textSelection.getOffset(), textSelection.getLength()); } } fOperationTarget.doOperation(fOperationCode); if (clipboardData != null) { /* * We currently make assumptions about what the styled text widget sets, * see https://bugs.eclipse.org/bugs/show_bug.cgi?id=61876 */ Clipboard clipboard = new Clipboard(getDisplay()); try { Object textData = clipboard.getContents(TextTransfer.getInstance()); /* * Don't add if we didn't get any text data from the clipboard, see: * - https://bugs.eclipse.org/bugs/show_bug.cgi?id=70077 * - https://bugs.eclipse.org/bugs/show_bug.cgi?id=200743 */ if (textData == null) return; ArrayList<Object> datas = new ArrayList<Object>(3); ArrayList<ByteArrayTransfer> transfers = new ArrayList<ByteArrayTransfer>(3); datas.add(textData); transfers.add(TextTransfer.getInstance()); Object rtfData = clipboard.getContents(RTFTransfer.getInstance()); if (rtfData != null) { datas.add(rtfData); transfers.add(RTFTransfer.getInstance()); } datas.add(clipboardData); transfers.add(fgTransferInstance); Transfer[] dataTypes = transfers.toArray(new Transfer[transfers.size()]); Object[] data = datas.toArray(); setClipboardContents(clipboard, data, dataTypes); } finally { clipboard.dispose(); } } }
protected final void internalDoOperation() { if (PreferenceConstants.getPreferenceStore() .getBoolean(PreferenceConstants.EDITOR_IMPORTS_ON_PASTE) && isSmartInsertMode()) { if (fOperationCode == ITextOperationTarget.PASTE) { doPasteWithImportsOperation(); } else { doCutCopyWithImportsOperation(); } } else { fOperationTarget.doOperation(fOperationCode); } }
/** * Updates the enabled state of the action. Fires a property change if the enabled state * changes. * * @see Action#firePropertyChange(String, Object, Object) */ public void update() { // XXX: workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=206111 if (fOperationCode == ITextOperationTarget.REDO) return; boolean wasEnabled = isEnabled(); boolean isEnabled = (fOperationTarget != null && fOperationTarget.canDoOperation(fOperationCode)); setEnabled(isEnabled); if (wasEnabled != isEnabled) firePropertyChange( ENABLED, wasEnabled ? Boolean.TRUE : Boolean.FALSE, isEnabled ? Boolean.TRUE : Boolean.FALSE); }
/* (non-Javadoc) * @see org.eclipse.ui.texteditor.IUpdate#update() */ @Override public void update() { super.update(); if (!isReadOnlyOperation() && !canModifyEditor()) { setEnabled(false); return; } ITextEditor editor = getTextEditor(); if (fOperationTarget == null && editor != null && fOperationCode != -1) fOperationTarget = (ITextOperationTarget) editor.getAdapter(ITextOperationTarget.class); boolean isEnabled = (fOperationTarget != null && fOperationTarget.canDoOperation(fOperationCode)); setEnabled(isEnabled); }
@Override public void run() { ITextOperationTarget textOperationTarget = this.getTextOperationTarget(); textOperationTarget.doOperation(ISourceViewer.FORMAT); }
/** @see Action#run() */ @Override public void run() { if (fOperationCode != -1 && fOperationTarget != null) fOperationTarget.doOperation(fOperationCode); }
/** @see org.eclipse.jface.action.Action#run() */ public void run() { target.doOperation(ITextOperationTarget.SELECT_ALL); }