下面列出了org.eclipse.ui.IEditorReference#getEditorInput ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* @generated
*/
public boolean matches(IEditorReference editorRef, IEditorInput input) {
IEditorInput editorInput;
try {
editorInput = editorRef.getEditorInput();
} catch (PartInitException e) {
return false;
}
if (editorInput.equals(input)) {
return true;
}
if (editorInput instanceof URIEditorInput && input instanceof URIEditorInput) {
return ((URIEditorInput) editorInput).getURI().equals(((URIEditorInput) input).getURI());
}
return false;
}
private static LSPDocumentInfo infoFromOpenEditors() {
for (IWorkbenchWindow window : PlatformUI.getWorkbench().getWorkbenchWindows()) {
for (IWorkbenchPage page : window.getPages()) {
for (IEditorReference editor : page.getEditorReferences()) {
IEditorInput input;
try {
input = editor.getEditorInput();
} catch (PartInitException e) {
continue;
}
if (input.getName().endsWith(".rs") && editor.getEditor(false) instanceof ITextEditor) { //$NON-NLS-1$
IDocument document = (((ITextEditor) editor.getEditor(false)).getDocumentProvider())
.getDocument(input);
Collection<LSPDocumentInfo> infos = LanguageServiceAccessor.getLSPDocumentInfosFor(document,
capabilities -> Boolean.TRUE.equals(capabilities.getReferencesProvider()));
if (!infos.isEmpty()) {
return infos.iterator().next();
}
}
}
}
}
return null;
}
/**
* Returns the editor with the specified input. Returns null if there is no
* opened editor with that input. If restore is requested, the method finds and
* returns the editor even if it is not restored yet after a restart.
*
* @param input
* the editor input
* @param restore
* true if the editor should be restored
* @return an editor with input equals to <code>input</code>
*/
private static IEditorPart findEditor(IEditorInput input, boolean restore) {
final IWorkbench wb = PlatformUI.getWorkbench();
final IWorkbenchPage activePage = wb.getActiveWorkbenchWindow().getActivePage();
for (IEditorReference editorReference : activePage.getEditorReferences()) {
try {
IEditorInput editorInput = editorReference.getEditorInput();
if (editorInput.equals(input)) {
return editorReference.getEditor(restore);
}
} catch (PartInitException e) {
// do nothing
}
}
return null;
}
public static void reopenWithGWTJavaEditor(IEditorReference[] openEditors) {
IWorkbenchPage page = JavaPlugin.getActivePage();
for (IEditorReference editorRef : openEditors) {
try {
IEditorPart editor = editorRef.getEditor(false);
IEditorInput input = editorRef.getEditorInput();
// Close the editor, prompting the user to save if document is dirty
if (page.closeEditor(editor, true)) {
// Re-open the .java file in the GWT Java Editor
IEditorPart gwtEditor = page.openEditor(input, GWTJavaEditor.EDITOR_ID);
// Save the file from the new editor if the Java editor's
// auto-format-on-save action screwed up the JSNI formatting
gwtEditor.doSave(null);
}
} catch (PartInitException e) {
GWTPluginLog.logError(e, "Could not open GWT Java editor on {0}", editorRef.getTitleToolTip());
}
}
}
/**
* Closes all editors (except start page, Log editor and script editors)
*/
public static boolean closeAll() {
try {
List<IEditorReference> rest = new ArrayList<>();
for (IEditorReference ref : getReferences()) {
if (ref.getEditorInput() instanceof SimpleEditorInput) {
SimpleEditorInput input = (SimpleEditorInput) ref.getEditorInput();
List<String> preventClosing = Arrays.asList(PREVENT_FROM_CLOSING);
if (preventClosing.contains(input.type))
continue;
}
rest.add(ref);
}
if (rest.size() == 0)
return true;
IEditorReference[] restArray = rest.toArray(
new IEditorReference[rest.size()]);
return getActivePage().closeEditors(restArray, true);
} catch (Exception e) {
log.error("Failed to close editors", e);
return false;
}
}
/**
* @generated
*/
public boolean matches(IEditorReference editorRef, IEditorInput input) {
IEditorInput editorInput;
try {
editorInput = editorRef.getEditorInput();
} catch (PartInitException e) {
return false;
}
if (editorInput.equals(input)) {
return true;
}
if (editorInput instanceof URIEditorInput && input instanceof URIEditorInput) {
return ((URIEditorInput) editorInput).getURI().equals(((URIEditorInput) input).getURI());
}
return false;
}
private static IEditorReference findEditor(Descriptor d) {
if (d == null)
return null;
for (IEditorReference ref : Editors.getReferences()) {
try {
IEditorInput inp = ref.getEditorInput();
if (!(inp instanceof ModelEditorInput))
continue;
ModelEditorInput minp = (ModelEditorInput) inp;
if (Objects.equals(minp.getDescriptor(), d))
return ref;
} catch (Exception e) {
log.error("editor search failed", e);
}
}
return null;
}
public static ManifestMultiPageEditor getManifestEditor(IDominoDesignerProject project) {
for (IEditorReference ref : PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences()) {
try {
if (ref.getEditorInput() instanceof BluemixManifestEditorInput) {
if (((BluemixManifestEditorInput)ref.getEditorInput()).getDesignerProject() == project) {
return (ManifestMultiPageEditor) ref.getEditor(false);
}
}
} catch (PartInitException e) {
if (BluemixLogger.BLUEMIX_LOGGER.isErrorEnabled()) {
BluemixLogger.BLUEMIX_LOGGER.errorp(BluemixUtil.class, "getManifestEditor", e, "Failed to get manifest editor"); // $NON-NLS-1$ $NLE-BluemixUtil.Failedtogetmanifesteditor-2$
}
}
}
return null;
}
private void closeRelatedEditorIfOpened(final ICompilationUnit compilationUnit) throws PartInitException {
Optional<IWorkbenchPage> activePage = Optional.ofNullable(PlatformUI.getWorkbench().getActiveWorkbenchWindow())
.map(IWorkbenchWindow::getActivePage);
if (activePage.isPresent()) {
if (editorPart != null) {
if (PlatformUI.isWorkbenchRunning()) {
activePage.get().closeEditor(editorPart, false);
}
} else {
if (PlatformUI.isWorkbenchRunning()) {
for (final IEditorReference editorReference : activePage.get().getEditorReferences()) {
final IEditorInput editorInput = editorReference.getEditorInput();
if (compilationUnit.getResource()
.equals(EditorUtil.retrieveResourceFromEditorInput(editorInput))) {
activePage.get().closeEditors(new IEditorReference[] { editorReference }, false);
break;
}
}
}
}
}
}
@Override
public void activateEditor(IWorkbenchPage page, IStructuredSelection selection) {
if (!(selection.getFirstElement() instanceof ModelElement))
return;
ModelElement element = (ModelElement) selection.getFirstElement();
for (IEditorReference ref : Editors.getReferences()) {
try {
if (!(ref.getEditorInput() instanceof ModelEditorInput))
continue;
ModelEditorInput input = (ModelEditorInput) ref.getEditorInput();
if (element.getContent().equals(input.getDescriptor())) {
App.openEditor(element.getContent());
}
} catch (PartInitException e) {
var log = LoggerFactory.getLogger(getClass());
log.error("Error activating editor", e);
}
}
}
/**
* Forces the user to close all opened editors for subdiagrams that are inlined.
*
* @return true if all editors were closed, false otherwise
*/
public static boolean closeSubdiagramEditors(State state) {
Diagram diagram = DiagramPartitioningUtil.getSubDiagram(state);
if (diagram == null)
return true;
IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorReference[] refs = activePage.getEditorReferences();
for (IEditorReference ref : refs) {
try {
if (ref.getEditorInput() instanceof IDiagramEditorInput) {
IDiagramEditorInput diagramInput = (IDiagramEditorInput) ref.getEditorInput();
if (diagramInput.getDiagram().equals(diagram)) {
boolean close = MessageDialog.openQuestion(activePage.getActivePart().getSite().getShell(),
"Close subdiagram editor?",
"The subdiagram is still open in another editor. Do you want to close it?");
if (close) {
activePage.closeEditor(ref.getEditor(false), false);
}
return close;
}
}
} catch (PartInitException e) {
e.printStackTrace();
}
}
return true;
}
private static IEditorPart getOpenEditor(ICompilationUnit cu) {
// Need to get the workbench window from the UI thread
final IWorkbenchWindow[][] windows = new IWorkbenchWindow[1][];
Display.getDefault().syncExec(new Runnable() {
public void run() {
windows[0] = PlatformUI.getWorkbench().getWorkbenchWindows();
}
});
for (IWorkbenchWindow window : windows[0]) {
for (IWorkbenchPage page : window.getPages()) {
for (IEditorReference editorRef : page.getEditorReferences()) {
try {
IEditorInput editorInput = editorRef.getEditorInput();
// See if this editor has the compilation unit resource open
if (editorInput instanceof FileEditorInput) {
IFile file = ((FileEditorInput) editorInput).getFile();
if (file.equals(cu.getResource())) {
return editorRef.getEditor(false);
}
}
} catch (PartInitException e) {
CorePluginLog.logError(e);
}
}
}
}
return null;
}
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]);
}
public static boolean isPropertiesOpenInEditor(DesignerProject dproject) {
boolean openInEditor = false;
IFile ifile = dproject.getProject().getFile("/WebContent/WEB-INF/xsp.properties"); //$NON-NLS-1$
// check if its already open
IEditorReference[] er = ExtLibPanelUtil.getActiveWorkbenchPage().getEditorReferences();
for (IEditorReference ref : er) {
try {
IEditorInput ei = ref.getEditorInput();
IFile f = (IFile)ei.getAdapter(IFile.class);
if (null != f) {
if (f.equals(ifile)) {
openInEditor = true;
break;
}
else {
IPath proppath = ifile.getFullPath();
IPath edpath = f.getFullPath();
if (edpath.segmentCount() >= 3 && proppath.segmentCount() > 1) {
String[] segs = edpath.segments();
String nsfname = proppath.segment(0);
if (StringUtil.equalsIgnoreCase(nsfname, segs[0]) && StringUtil.equalsIgnoreCase("AppProperties", segs[1])) { //$NON-NLS-1$
if (StringUtil.equalsIgnoreCase("database.properties", segs[2])) { //$NON-NLS-1$
openInEditor = true;
break;
}
}
}
}
}
}
catch(PartInitException pe) {
ExtLibToolingLogger.EXT_LIB_TOOLING_LOGGER.warn(pe, "exception trying to find open property editors"); // $NLW-ExtLibToolingUtil.exceptiontryingtofind-1$
}
}
return openInEditor;
}
public static void close() {
for (IEditorReference ref : Editors.getReferences()) {
try {
if (!(ref.getEditorInput() instanceof SimpleEditorInput))
continue;
SimpleEditorInput input = (SimpleEditorInput) ref.getEditorInput();
if (!TYPE.equals(input.type))
continue;
Editors.close(ref);
} catch (PartInitException e) {
log.error("Error closing editor " + ref.getId());
}
}
}
/**
* @return a set with the currently opened files in the PyEdit editors.
*/
public static Set<IFile> getOpenFiles() {
Set<IFile> ret = new HashSet<IFile>();
IWorkbenchWindow activeWorkbenchWindow = EditorUtils.getActiveWorkbenchWindow();
if (activeWorkbenchWindow == null) {
return ret;
}
IWorkbenchPage[] pages = activeWorkbenchWindow.getPages();
for (int i = 0; i < pages.length; i++) {
IEditorReference[] editorReferences = pages[i].getEditorReferences();
for (int j = 0; j < editorReferences.length; j++) {
IEditorReference iEditorReference = editorReferences[j];
if (!PyEdit.EDITOR_ID.equals(iEditorReference.getId())) {
continue; //Only PyDev editors...
}
try {
IEditorInput editorInput = iEditorReference.getEditorInput();
if (editorInput == null) {
continue;
}
IFile file = (IFile) editorInput.getAdapter(IFile.class);
if (file != null) {
ret.add(file);
}
} catch (Exception e1) {
Log.log(e1);
}
}
}
return ret;
}
/**
* Updates the image of the passed editor.
*/
private void updateImage(PyEdit pyEdit, IEditorReference iEditorReference, IPath path) {
String lastSegment = path.lastSegment();
if (lastSegment != null) {
if (lastSegment.startsWith("__init__.")) {
Image initIcon = ImageCache.asImage(PyTitlePreferencesPage.getInitIcon());
if (initIcon != null) {
if (pyEdit != null) {
pyEdit.setEditorImage(initIcon);
} else {
setEditorReferenceImage(iEditorReference, initIcon);
}
}
} else if (PyTitlePreferencesPage.isDjangoModuleToDecorate(lastSegment)) {
try {
IEditorInput editorInput;
if (pyEdit != null) {
editorInput = pyEdit.getEditorInput();
} else {
editorInput = iEditorReference.getEditorInput();
}
if (isDjangoHandledModule(PyTitlePreferencesPage.getDjangoModulesHandling(), editorInput,
lastSegment)) {
Image image = ImageCache.asImage(PyTitlePreferencesPage.getDjangoModuleIcon(lastSegment));
if (pyEdit != null) {
pyEdit.setEditorImage(image);
} else {
setEditorReferenceImage(iEditorReference, image);
}
}
} catch (PartInitException e) {
//ignore
}
}
}
}
/**
* @return a list of all the editors that have the last segment as 'currentName'
*/
private Map<IPath, List<IEditorReference>> removeEditorsNotMatchingCurrentName(String currentName,
List<IEditorReference> editorReferences) {
Map<IPath, List<IEditorReference>> ret = new HashMap<IPath, List<IEditorReference>>();
for (Iterator<IEditorReference> it = editorReferences.iterator(); it.hasNext();) {
IEditorReference iEditorReference = it.next();
try {
IEditorInput otherInput = iEditorReference.getEditorInput();
//Always get the 'original' name and not the currently set name, because
//if we previously had an __init__.py editor which we renamed to package/__init__.py
//and we open a new __init__.py, we want it renamed to new_package/__init__.py
IPath pathFromOtherInput = getPathFromInput(otherInput);
if (pathFromOtherInput == null) {
continue;
}
String lastSegment = pathFromOtherInput.lastSegment();
if (lastSegment == null) {
continue;
}
if (!currentName.equals(lastSegment)) {
continue;
}
List<IEditorReference> list = ret.get(pathFromOtherInput);
if (list == null) {
list = new ArrayList<IEditorReference>();
ret.put(pathFromOtherInput, list);
}
list.add(iEditorReference);
} catch (Throwable e) {
Log.log(e);
}
}
return ret;
}
public Object[] getElements(Object parent) {
CloneGroup[] cloneGroupTable = null;
if (cloneGroupList != null) {
if (filterBasedOnOpenedDocuments) {
Set<String> locationOfOpenedFiles = new HashSet<String>();
IEditorReference[] editorReferences = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences();
for (IEditorReference editorReference : editorReferences) {
try {
IEditorInput editorInput = editorReference.getEditorInput();
if (editorInput instanceof IFileEditorInput) {
IFileEditorInput iFileEditorInput = (IFileEditorInput) editorInput;
locationOfOpenedFiles.add(iFileEditorInput.getFile().getLocation().toPortableString());
}
} catch (PartInitException e) {
e.printStackTrace();
}
}
CloneGroupList filteredCloneGroupList = new CloneGroupList(selectedProject);
for (CloneGroup cloneGroup : cloneGroupList.getCloneGroups()) {
CloneGroup filteredCloneGroup = new CloneGroup(cloneGroup.getCloneGroupID());
for (CloneInstance cloneInstance : cloneGroup.getCloneInstances()) {
String cloneInstanceFilePath = cloneInstance.getLocationInfo().getContainingFilePath();
if (locationOfOpenedFiles.contains(new Path(cloneInstanceFilePath).toPortableString())) {
filteredCloneGroup.addClone(cloneInstance);
}
}
if (filteredCloneGroup.getCloneGroupSize() > 0) {
filteredCloneGroupList.add(cloneGroup);
}
}
cloneGroupTable = filteredCloneGroupList.getCloneGroups();
} else {
cloneGroupTable = cloneGroupList.getCloneGroups();
}
}
if(cloneGroupTable != null) {
return cloneGroupTable;
} else {
return new CloneGroup[] {};
}
}
/**
* called to dispose the resource set when closing the editor in parameter.
* The resource set will be unloaded only if it's not in use
* @param resourceSet
* @param editorInput
*/
public static void disposeEditorInput(final ResourceSet resourceSet, final IEditorInput editorInput) {
final EList<Resource> allResources = resourceSet.getResources();
final List<Resource> resourcesToDispose = new ArrayList<Resource>(allResources);
IEditorReference[] editorReferences;
if(PlatformUI.isWorkbenchRunning()){
final IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
if(activePage != null){
editorReferences = activePage.getEditorReferences();
} else {
return;
}
}else{
return;
}
for (final IEditorReference editorRef : editorReferences) {
try {
final IEditorInput currentEditorInput = editorRef.getEditorInput();
if (currentEditorInput != editorInput) {
final IEditorPart openEditor = editorRef.getEditor(false);
if (openEditor instanceof DiagramEditor) {
final DiagramEditor openDiagramEditor = (DiagramEditor) openEditor;
final ResourceSet diagramResourceSet = openDiagramEditor.getEditingDomain().getResourceSet();
if (diagramResourceSet == resourceSet) {
final Resource diagramResource = EditorUtil.getDiagramResource(diagramResourceSet, currentEditorInput);
if(diagramResource != null){
resourcesToDispose.remove(diagramResource);
final Collection<?> imports = EMFCoreUtil.getImports(diagramResource);
resourcesToDispose.removeAll(imports);
}
}
}
}
} catch (final Exception e) {
BonitaStudioLog.error(e);
}
}
for (final Resource resource : resourcesToDispose) {
try {
resource.unload();
allResources.remove(resource);
} catch (final Exception t) {
BonitaStudioLog.error(t);
}
}
}