下面列出了org.eclipse.jface.text.IEditingSupport#org.eclipse.jdt.internal.ui.javaeditor.EditorUtility 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public CompletableFuture<List<? extends ICodeMining>> provideCodeMinings(ITextViewer viewer,
IProgressMonitor monitor) {
return CompletableFuture.supplyAsync(() -> {
monitor.isCanceled();
ITextEditor textEditor = super.getAdapter(ITextEditor.class);
ITypeRoot unit = EditorUtility.getEditorInputJavaElement(textEditor, true);
if (unit == null) {
return Collections.emptyList();
}
try {
IJavaElement[] elements = unit.getChildren();
List<ICodeMining> minings = new ArrayList<>(elements.length);
collectMinings(unit, textEditor, unit.getChildren(), minings, viewer, monitor);
monitor.isCanceled();
return minings;
} catch (JavaModelException e) {
// Should never occur
}
return Collections.emptyList();
});
}
@Override
public CompletableFuture<List<? extends ICodeMining>> provideCodeMinings(ITextViewer viewer,
IProgressMonitor monitor) {
this.viewer = viewer;
return CompletableFuture.supplyAsync(() -> {
monitor.isCanceled();
ITextEditor textEditor = super.getAdapter(ITextEditor.class);
ITypeRoot unit = EditorUtility.getEditorInputJavaElement(textEditor, true);
if (unit == null) {
return null;
}
try {
IJavaElement[] elements = unit.getChildren();
List<ICodeMining> minings = new ArrayList<>(elements.length);
collectCodeMinings(unit, elements, minings, viewer, monitor);
monitor.isCanceled();
return minings;
} catch (JavaModelException e) {
// TODO: what should we done when there are some errors?
}
return null;
});
}
@Override
public CompletableFuture<List<? extends ICodeMining>> provideCodeMinings(ITextViewer viewer,
IProgressMonitor monitor) {
return CompletableFuture.supplyAsync(() -> {
monitor.isCanceled();
ITextEditor textEditor = super.getAdapter(ITextEditor.class);
ITypeRoot unit = EditorUtility.getEditorInputJavaElement(textEditor, true);
if (unit == null) {
return null;
}
List<ICodeMining> minings = new ArrayList<>();
collectCodeMinings(unit, textEditor, viewer, minings);
monitor.isCanceled();
return minings;
});
}
@Override
public CompletableFuture<List<? extends ICodeMining>> provideCodeMinings(ITextViewer viewer,
IProgressMonitor monitor) {
return CompletableFuture.supplyAsync(() -> {
monitor.isCanceled();
ITextEditor textEditor = super.getAdapter(ITextEditor.class);
ITypeRoot unit = EditorUtility.getEditorInputJavaElement(textEditor, true);
if (unit == null) {
return Collections.emptyList();
}
fViewer = viewer;
fUnit = unit;
try {
List<ICodeMining> minings = new ArrayList<>();
collectMinings(unit, textEditor, unit.getChildren(), minings, viewer, monitor);
monitor.isCanceled();
return minings;
} catch (JavaModelException e) {
// TODO: what should we done when there are some errors?
}
return Collections.emptyList();
});
}
/**
* This method is used by the test-framework, to catch the thrown exceptions
* and report it to the user.
*
* @see #run(IMarker)
*/
private void runInternal(IMarker marker) throws CoreException {
Assert.isNotNull(marker);
PendingRewrite pending = resolveWithoutWriting(marker);
if (pending == null) {
return;
}
try {
IRegion region = completeRewrite(pending);
if (region == null) {
return;
}
IEditorPart part = EditorUtility.isOpenInEditor(pending.originalUnit);
if (part instanceof ITextEditor) {
((ITextEditor) part).selectAndReveal(region.getOffset(), region.getLength());
}
} finally {
pending.originalUnit.discardWorkingCopy();
}
}
@Override
public void run(IMarker marker) {
try {
IEditorPart part = EditorUtility.isOpenInEditor(cu);
if (part == null) {
part = JavaUI.openInEditor(cu, true, false);
if (part instanceof ITextEditor) {
((ITextEditor) part).selectAndReveal(offset, length);
}
}
if (part != null) {
IEditorInput input = part.getEditorInput();
IDocument doc = JavaPlugin.getDefault().getCompilationUnitDocumentProvider().getDocument(
input);
proposal.apply(doc);
}
} catch (CoreException e) {
CorePluginLog.logError(e);
}
}
@Override
public void run(IStructuredSelection selection) {
IMember[] members= getSelectedMembers(selection);
if (members == null || members.length == 0) {
return;
}
try {
ICompilationUnit cu= members[0].getCompilationUnit();
if (!ActionUtil.isEditable(getShell(), cu)) {
return;
}
// open the editor, forces the creation of a working copy
IEditorPart editor= JavaUI.openInEditor(cu);
if (ElementValidator.check(members, getShell(), getDialogTitle(), false))
run(members);
JavaModelUtil.reconcile(cu);
EditorUtility.revealInEditor(editor, members[0]);
} catch (CoreException e) {
ExceptionHandler.handle(e, getShell(), getDialogTitle(), ActionMessages.AddJavaDocStubsAction_error_actionFailed);
}
}
public void init(IWorkbench workbench, IStructuredSelection structuredSelection) {
IWorkbenchWindow window= workbench.getActiveWorkbenchWindow();
List<?> selected= Collections.EMPTY_LIST;
if (window != null) {
ISelection selection= window.getSelectionService().getSelection();
if (selection instanceof IStructuredSelection) {
selected= ((IStructuredSelection) selection).toList();
} else {
IJavaElement element= EditorUtility.getActiveEditorJavaInput();
if (element != null) {
selected= Arrays.asList(element);
}
}
}
fStore= new JavadocOptionsManager(fXmlJavadocFile, getDialogSettings(), selected);
}
private void open(KeyReference keyReference) {
if (keyReference == null)
return;
try {
IEditorPart part= keyReference.element != null ? EditorUtility.openInEditor(keyReference.element, true) : EditorUtility.openInEditor(keyReference.resource, true);
if (part != null)
EditorUtility.revealInEditor(part, keyReference.offset, keyReference.length);
} catch (PartInitException x) {
String message= null;
IWorkbenchAdapter wbAdapter= (IWorkbenchAdapter)((IAdaptable)keyReference).getAdapter(IWorkbenchAdapter.class);
if (wbAdapter != null)
message= Messages.format(PropertiesFileEditorMessages.OpenAction_error_messageArgs,
new String[] { wbAdapter.getLabel(keyReference), x.getLocalizedMessage() } );
if (message == null)
message= Messages.format(PropertiesFileEditorMessages.OpenAction_error_message, x.getLocalizedMessage());
MessageDialog.openError(fShell,
PropertiesFileEditorMessages.OpenAction_error_messageProblems,
message);
}
}
private void revealElementInEditor(Object elem, StructuredViewer originViewer) {
// only allow revealing when the type hierarchy is the active page
// no revealing after selection events due to model changes
if (getSite().getPage().getActivePart() != this) {
return;
}
if (fSelectionProviderMediator.getViewerInFocus() != originViewer) {
return;
}
IEditorPart editorPart= EditorUtility.isOpenInEditor(elem);
if (editorPart != null && (elem instanceof IJavaElement)) {
getSite().getPage().removePartListener(fPartListener);
getSite().getPage().bringToTop(editorPart);
EditorUtility.revealInEditor(editorPart, (IJavaElement) elem);
getSite().getPage().addPartListener(fPartListener);
}
}
/**
* Computes the state mask for the given modifier string.
*
* @param modifiers the string with the modifiers, separated by '+', '-', ';', ',' or '.'
* @return the state mask or -1 if the input is invalid
*/
public static int computeStateMask(String modifiers) {
if (modifiers == null)
return -1;
if (modifiers.length() == 0)
return SWT.NONE;
int stateMask= 0;
StringTokenizer modifierTokenizer= new StringTokenizer(modifiers, ",;.:+-* "); //$NON-NLS-1$
while (modifierTokenizer.hasMoreTokens()) {
int modifier= EditorUtility.findLocalizedModifier(modifierTokenizer.nextToken());
if (modifier == 0 || (stateMask & modifier) == modifier)
return -1;
stateMask= stateMask | modifier;
}
return stateMask;
}
public void run(IMarker marker) {
try {
IEditorPart part= EditorUtility.isOpenInEditor(fCompilationUnit);
if (part == null) {
part= JavaUI.openInEditor(fCompilationUnit, true, false);
if (part instanceof ITextEditor) {
((ITextEditor) part).selectAndReveal(fOffset, fLength);
}
}
if (part != null) {
IEditorInput input= part.getEditorInput();
IDocument doc= JavaPlugin.getDefault().getCompilationUnitDocumentProvider().getDocument(input);
fProposal.apply(doc);
}
} catch (CoreException e) {
JavaPlugin.log(e);
}
}
private String getProjectPath(IStructuredSelection selection) {
Object selectedElement= null;
if (selection == null || selection.isEmpty()) {
selectedElement= EditorUtility.getActiveEditorJavaInput();
} else if (selection.size() == 1) {
selectedElement= selection.getFirstElement();
}
if (selectedElement instanceof IResource) {
IProject proj= ((IResource)selectedElement).getProject();
if (proj != null) {
return proj.getFullPath().makeRelative().toString();
}
} else if (selectedElement instanceof IJavaElement) {
IJavaProject jproject= ((IJavaElement)selectedElement).getJavaProject();
if (jproject != null) {
return jproject.getProject().getFullPath().makeRelative().toString();
}
}
return null;
}
@Override
protected List provideCodeMinings(ITextViewer viewer, IJavaStackFrame frame, IProgressMonitor monitor) {
List<ICodeMining> minings = new ArrayList<>();
ITextEditor textEditor = super.getAdapter(ITextEditor.class);
ITypeRoot unit = EditorUtility.getEditorInputJavaElement(textEditor, true);
if (unit == null) {
return minings;
}
CompilationUnit cu = SharedASTProvider.getAST(unit, SharedASTProvider.WAIT_YES, null);
JavaDebugElementCodeMiningASTVisitor visitor = new JavaDebugElementCodeMiningASTVisitor(frame, cu, viewer,
minings, this);
cu.accept(visitor);
return minings;
}
public static IEditorReference[] getOpenJavaEditors(IProject project) {
List<IEditorReference> projectOpenJavaEditors = new ArrayList<IEditorReference>();
try {
IWorkbenchPage page = JavaPlugin.getActivePage();
if (page != null) {
// Iterate through all the open editors
IEditorReference[] openEditors = page.getEditorReferences();
for (IEditorReference openEditor : openEditors) {
IEditorPart editor = openEditor.getEditor(false);
// Only look for Java Editor and subclasses
if (editor instanceof CompilationUnitEditor) {
IEditorInput input = openEditor.getEditorInput();
IJavaProject inputProject = EditorUtility.getJavaProject(input);
// See if the editor is editing a file in this project
if (inputProject != null && inputProject.getProject().equals(project)) {
projectOpenJavaEditors.add(openEditor);
}
}
}
}
} catch (PartInitException e) {
GWTPluginLog.logError(e);
}
return projectOpenJavaEditors.toArray(new IEditorReference[0]);
}
@Override
protected void doSetInput(IEditorInput input) throws CoreException {
IJavaProject javaProject = EditorUtility.getJavaProject(input);
if (javaProject != null && GWTNature.isGWTProject(javaProject.getProject())) {
// Use GWT partitioning if the compilation unit is in a GWT project
inputPartitioning = GWTPartitions.GWT_PARTITIONING;
} else {
// Otherwise, use Java partitioning to emulate the Java editor
inputPartitioning = IJavaPartitions.JAVA_PARTITIONING;
}
super.doSetInput(input);
}
/**
* Note: This constructor is for internal use only. Clients should not call this constructor.
* @param editor the Java editor
*
* @noreference This constructor is not intended to be referenced by clients.
*/
public OpenAction(JavaEditor editor) {
this(editor.getEditorSite());
fEditor= editor;
setText(ActionMessages.OpenAction_declaration_label);
setEnabled(EditorUtility.getEditorInputJavaElement(fEditor, false) != null);
}
@Override
public void apply(IDocument document) {
try {
ICompilationUnit unit= getCompilationUnit();
IEditorPart part= null;
if (unit.getResource().exists()) {
boolean canEdit= performValidateEdit(unit);
if (!canEdit) {
return;
}
part= EditorUtility.isOpenInEditor(unit);
if (part == null) {
part= JavaUI.openInEditor(unit);
if (part != null) {
fSwitchedEditor= true;
document= JavaUI.getDocumentProvider().getDocument(part.getEditorInput());
}
}
IWorkbenchPage page= JavaPlugin.getActivePage();
if (page != null && part != null) {
page.bringToTop(part);
}
if (part != null) {
part.setFocus();
}
}
performChange(part, document);
} catch (CoreException e) {
ExceptionHandler.handle(e, CorrectionMessages.CUCorrectionProposal_error_title, CorrectionMessages.CUCorrectionProposal_error_message);
}
}
/**
* Links to editor (if option enabled)
* @param selection the selection
*/
private void linkToEditor(ISelection selection) {
Object obj= SelectionUtil.getSingleElement(selection);
if (obj != null) {
IEditorPart part= EditorUtility.isOpenInEditor(obj);
if (part != null) {
IWorkbenchPage page= getSite().getPage();
page.bringToTop(part);
if (obj instanceof IJavaElement)
EditorUtility.revealInEditor(part, (IJavaElement)obj);
}
}
}
private boolean inputIsSelected(IEditorInput input) {
IStructuredSelection selection= (IStructuredSelection)fViewer.getSelection();
if (selection.size() != 1)
return false;
IEditorInput selectionAsInput= EditorUtility.getEditorInput(selection.getFirstElement());
return input.equals(selectionAsInput);
}
public static IEditorPart isOpenInEditor(Object elem) {
IJavaElement javaElement= null;
if (elem instanceof MethodWrapper) {
javaElement= ((MethodWrapper) elem).getMember();
} else if (elem instanceof CallLocation) {
javaElement= ((CallLocation) elem).getMember();
}
if (javaElement != null) {
return EditorUtility.isOpenInEditor(javaElement);
}
return null;
}
@Override
public void dragStart(DragSourceEvent event) {
fEditorInputDatas= new ArrayList<EditorInputData>();
ISelection selection= fProvider.getSelection();
if (selection instanceof IStructuredSelection) {
IStructuredSelection structuredSelection= (IStructuredSelection) selection;
for (Iterator<?> iter= structuredSelection.iterator(); iter.hasNext();) {
Object element= iter.next();
IEditorInput editorInput= EditorUtility.getEditorInput(element);
if (editorInput != null && editorInput.getPersistable() != null) {
try {
String editorId= EditorUtility.getEditorID(editorInput);
// see org.eclipse.ui.internal.ide.EditorAreaDropAdapter.openNonExternalEditor(..):
IEditorRegistry editorReg= PlatformUI.getWorkbench().getEditorRegistry();
IEditorDescriptor editorDesc= editorReg.findEditor(editorId);
if (editorDesc != null && !editorDesc.isOpenExternal()) {
fEditorInputDatas.add(EditorInputTransfer.createEditorInputData(editorId, editorInput));
}
} catch (PartInitException e) {
JavaPlugin.log(e);
}
}
}
}
event.doit= fEditorInputDatas.size() > 0;
}
public IEditorPart openElement(Object element) throws PartInitException {
IWorkbenchPage wbPage= JavaPlugin.getActivePage();
IEditorPart editor;
if (NewSearchUI.reuseEditor())
editor= showWithReuse(element, wbPage);
else
editor= showWithoutReuse(element);
if (element instanceof IJavaElement)
EditorUtility.revealInEditor(editor, (IJavaElement) element);
return editor;
}
private IEditorPart showWithoutReuse(Object element) throws PartInitException {
try {
return EditorUtility.openInEditor(element, false);
} catch (PartInitException e) {
if (e.getStatus().getCode() != IJavaStatusConstants.EDITOR_NO_EDITOR_INPUT) {
throw e;
}
}
return null;
}
private IEditorPart showWithReuse(Object element, IWorkbenchPage wbPage) throws PartInitException {
IEditorInput input= EditorUtility.getEditorInput(element);
if (input == null)
return null;
String editorID= EditorUtility.getEditorID(input);
return showInEditor(wbPage, input, editorID);
}
@Override
public final void run() {
IJavaElement inputElement= EditorUtility.getEditorInputJavaElement(fEditor, false);
if (!(inputElement instanceof ITypeRoot && inputElement.exists()))
return;
ITypeRoot typeRoot= (ITypeRoot) inputElement;
ISourceRange sourceRange;
try {
sourceRange= typeRoot.getSourceRange();
if (sourceRange == null || sourceRange.getLength() == 0) {
MessageDialog.openInformation(fEditor.getEditorSite().getShell(),
SelectionActionMessages.StructureSelect_error_title,
SelectionActionMessages.StructureSelect_error_message);
return;
}
} catch (JavaModelException e) {
}
ITextSelection selection= getTextSelection();
ISourceRange newRange= getNewSelectionRange(createSourceRange(selection), typeRoot);
// Check if new selection differs from current selection
if (selection.getOffset() == newRange.getOffset() && selection.getLength() == newRange.getLength())
return;
fSelectionHistory.remember(new SourceRange(selection.getOffset(), selection.getLength()));
try {
fSelectionHistory.ignoreSelectionChanges();
fEditor.selectAndReveal(newRange.getOffset(), newRange.getLength());
} finally {
fSelectionHistory.listenToSelectionChanges();
}
}
public static IEditorPart openDeclaration(IJavaElement element) throws PartInitException, JavaModelException {
if (!(element instanceof IPackageFragment)) {
return JavaUI.openInEditor(element);
}
IPackageFragment packageFragment= (IPackageFragment) element;
ITypeRoot typeRoot;
IPackageFragmentRoot root= (IPackageFragmentRoot) packageFragment.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
if (root.getKind() == IPackageFragmentRoot.K_BINARY) {
typeRoot= packageFragment.getClassFile(JavaModelUtil.PACKAGE_INFO_CLASS);
} else {
typeRoot= packageFragment.getCompilationUnit(JavaModelUtil.PACKAGE_INFO_JAVA);
}
// open the package-info file in editor if one exists
if (typeRoot.exists())
return JavaUI.openInEditor(typeRoot);
// open the package.html file in editor if one exists
Object[] nonJavaResources= packageFragment.getNonJavaResources();
for (Object nonJavaResource : nonJavaResources) {
if (nonJavaResource instanceof IFile) {
IFile file= (IFile) nonJavaResource;
if (file.exists() && JavaModelUtil.PACKAGE_HTML.equals(file.getName())) {
return EditorUtility.openInEditor(file, true);
}
}
}
// select the package in the Package Explorer if there is no associated package Javadoc file
PackageExplorerPart view= (PackageExplorerPart) JavaPlugin.getActivePage().showView(JavaUI.ID_PACKAGES);
view.tryToReveal(packageFragment);
return null;
}
@Override
public void install(ITextViewer textViewer) {
super.install(textViewer);
fPartListener= new PartListener();
IWorkbenchPartSite site= fTextEditor.getSite();
IWorkbenchWindow window= site.getWorkbenchWindow();
window.getPartService().addPartListener(fPartListener);
fActivationListener= new ActivationListener(textViewer.getTextWidget());
Shell shell= window.getShell();
shell.addShellListener(fActivationListener);
fJavaElementChangedListener= new ElementChangedListener();
JavaCore.addElementChangedListener(fJavaElementChangedListener);
fResourceChangeListener= new ResourceChangeListener();
IWorkspace workspace= JavaPlugin.getWorkspace();
workspace.addResourceChangeListener(fResourceChangeListener);
fPropertyChangeListener= new IPropertyChangeListener() {
public void propertyChange(PropertyChangeEvent event) {
if (SpellingService.PREFERENCE_SPELLING_ENABLED.equals(event.getProperty()) || SpellingService.PREFERENCE_SPELLING_ENGINE.equals(event.getProperty()))
forceReconciling();
}
};
JavaPlugin.getDefault().getCombinedPreferenceStore().addPropertyChangeListener(fPropertyChangeListener);
fReconciledElement= EditorUtility.getEditorInputJavaElement(fTextEditor, false);
}
private void gotoSelectedElement() {
Object selectedElement= getSelectedElement();
if (selectedElement != null) {
try {
dispose();
IEditorPart part= EditorUtility.openInEditor(selectedElement, true);
if (part != null && selectedElement instanceof IJavaElement)
EditorUtility.revealInEditor(part, (IJavaElement) selectedElement);
} catch (CoreException ex) {
JavaPlugin.log(ex);
}
}
}
private static IMarkerResolution[] internalGetResolutions(IMarker marker) {
if (!internalHasResolutions(marker)) {
return NO_RESOLUTIONS;
}
ICompilationUnit cu= getCompilationUnit(marker);
if (cu != null) {
IEditorInput input= EditorUtility.getEditorInput(cu);
if (input != null) {
IProblemLocation location= findProblemLocation(input, marker);
if (location != null) {
IInvocationContext context= new AssistContext(cu, location.getOffset(), location.getLength());
if (!hasProblem (context.getASTRoot().getProblems(), location))
return NO_RESOLUTIONS;
ArrayList<IJavaCompletionProposal> proposals= new ArrayList<IJavaCompletionProposal>();
JavaCorrectionProcessor.collectCorrections(context, new IProblemLocation[] { location }, proposals);
Collections.sort(proposals, new CompletionProposalComparator());
int nProposals= proposals.size();
IMarkerResolution[] resolutions= new IMarkerResolution[nProposals];
for (int i= 0; i < nProposals; i++) {
resolutions[i]= new CorrectionMarkerResolution(context.getCompilationUnit(), location.getOffset(), location.getLength(), proposals.get(i), marker);
}
return resolutions;
}
}
}
return NO_RESOLUTIONS;
}