Java Code Examples for org.eclipse.jdt.ui.JavaUI#getEditorInputTypeRoot()

The following examples show how to use org.eclipse.jdt.ui.JavaUI#getEditorInputTypeRoot() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavadocView.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Compute the textual representation of a 'static' 'final' field's constant initializer value.
 *
 * @param activePart the part that triggered the computation, or <code>null</code>
 * @param selection the selection that references the field, or <code>null</code>
 * @param resolvedField the filed whose constant value will be computed
 * @param monitor the progress monitor
 *
 * @return the textual representation of the constant, or <code>null</code> if the
 *   field is not a constant field, the initializer value could not be computed, or
 *   the progress monitor was cancelled
 * @since 3.4
 */
private String computeFieldConstant(IWorkbenchPart activePart, ISelection selection, IField resolvedField, IProgressMonitor monitor) {

	if (!JavadocHover.isStaticFinal(resolvedField))
		return null;

	Object constantValue;
	IJavaProject preferenceProject;

	if (selection instanceof ITextSelection && activePart instanceof JavaEditor) {
		IEditorPart editor= (IEditorPart) activePart;
		ITypeRoot activeType= JavaUI.getEditorInputTypeRoot(editor.getEditorInput());
		preferenceProject= activeType.getJavaProject();
		constantValue= getConstantValueFromActiveEditor(activeType, resolvedField, (ITextSelection) selection, monitor);
		if (constantValue == null) // fall back - e.g. when selection is inside Javadoc of the element
			constantValue= computeFieldConstantFromTypeAST(resolvedField, monitor);
	} else {
		constantValue= computeFieldConstantFromTypeAST(resolvedField, monitor);
		preferenceProject= resolvedField.getJavaProject();
	}

	if (constantValue != null)
		return JavadocHover.getFormattedAssignmentOperator(preferenceProject) + formatCompilerConstantValue(constantValue);

	return null;
}
 
Example 2
Source File: OccurrencesSearchResultPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void installOnActiveEditor(IWorkbenchPage page) {
	IEditorPart activeEditor= page.getActiveEditor();
	if (activeEditor instanceof ITextEditor) {
		editorActive(activeEditor);
		ISelection selection= activeEditor.getSite().getSelectionProvider().getSelection();
		ITypeRoot typeRoot= JavaUI.getEditorInputTypeRoot(activeEditor.getEditorInput());
		if (typeRoot != null && selection instanceof ITextSelection) {
			CompilationUnit astRoot= SharedASTProvider.getAST(typeRoot, SharedASTProvider.WAIT_ACTIVE_ONLY, null);
			if (astRoot != null) {
				preformEditorSelectionChanged((ITextSelection) selection, astRoot);
			}
		}
	}
}
 
Example 3
Source File: ClipboardOperationAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
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();
	}
}
 
Example 4
Source File: FindImplementOccurrencesAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static ITypeRoot getEditorInput(JavaEditor editor) {
	return JavaUI.getEditorInputTypeRoot(editor.getEditorInput());
}
 
Example 5
Source File: FindExceptionOccurrencesAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static ITypeRoot getEditorInput(JavaEditor editor) {
	return JavaUI.getEditorInputTypeRoot(editor.getEditorInput());
}
 
Example 6
Source File: FindOccurrencesInFileAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static ITypeRoot getEditorInput(JavaEditor editor) {
	return JavaUI.getEditorInputTypeRoot(editor.getEditorInput());
}
 
Example 7
Source File: FindMethodExitOccurrencesAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static ITypeRoot getEditorInput(JavaEditor editor) {
	return JavaUI.getEditorInputTypeRoot(editor.getEditorInput());
}
 
Example 8
Source File: FindBreakContinueTargetOccurrencesAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static ITypeRoot getEditorInput(JavaEditor editor) {
	return JavaUI.getEditorInputTypeRoot(editor.getEditorInput());
}
 
Example 9
Source File: RefactorActionGroup.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private ITypeRoot getEditorInput() {
	return JavaUI.getEditorInputTypeRoot(fEditor.getEditorInput());
}
 
Example 10
Source File: NLSKeyHyperlinkDetector.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private ITypeRoot getInputJavaElement(ITextEditor editor) {
	return JavaUI.getEditorInputTypeRoot(editor.getEditorInput());
}
 
Example 11
Source File: ClipboardOperationAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
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();
		}
	}
}
 
Example 12
Source File: CopyQualifiedNameAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private Object getSelectedElement(JavaEditor editor) {
	ISourceViewer viewer= editor.getViewer();
	if (viewer == null)
		return null;

	Point selectedRange= viewer.getSelectedRange();
	int length= selectedRange.y;
	int offset= selectedRange.x;

	ITypeRoot element= JavaUI.getEditorInputTypeRoot(editor.getEditorInput());
	if (element == null)
		return null;

	CompilationUnit ast= SharedASTProvider.getAST(element, SharedASTProvider.WAIT_YES, null);
	if (ast == null)
		return null;

	NodeFinder finder= new NodeFinder(ast, offset, length);
	ASTNode node= finder.getCoveringNode();

	IBinding binding= null;
	if (node instanceof Name) {
		binding= getConstructorBindingIfAvailable((Name)node);
		if (binding != null)
			return binding;
		binding= ((Name)node).resolveBinding();
	} else if (node instanceof MethodInvocation) {
		binding= ((MethodInvocation)node).resolveMethodBinding();
	} else if (node instanceof MethodDeclaration) {
		binding= ((MethodDeclaration)node).resolveBinding();
	} else if (node instanceof Type) {
		binding= ((Type)node).resolveBinding();
	} else if (node instanceof AnonymousClassDeclaration) {
		binding= ((AnonymousClassDeclaration)node).resolveBinding();
	} else if (node instanceof TypeDeclaration) {
		binding= ((TypeDeclaration)node).resolveBinding();
	} else if (node instanceof CompilationUnit) {
		return ((CompilationUnit)node).getJavaElement();
	} else if (node instanceof Expression) {
		binding= ((Expression)node).resolveTypeBinding();
	} else if (node instanceof ImportDeclaration) {
		binding= ((ImportDeclaration)node).resolveBinding();
	} else if (node instanceof MemberRef) {
		binding= ((MemberRef)node).resolveBinding();
	} else if (node instanceof MemberValuePair) {
		binding= ((MemberValuePair)node).resolveMemberValuePairBinding();
	} else if (node instanceof PackageDeclaration) {
		binding= ((PackageDeclaration)node).resolveBinding();
	} else if (node instanceof TypeParameter) {
		binding= ((TypeParameter)node).resolveBinding();
	} else if (node instanceof VariableDeclaration) {
		binding= ((VariableDeclaration)node).resolveBinding();
	}

	if (binding != null)
		return binding.getJavaElement();

	return null;
}