下面列出了org.eclipse.ui.IEditorReference#getEditor ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void refreshDistProjects() {
try {
IEditorReference[] editorReferences = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
.getEditorReferences();
for (IEditorReference reference : editorReferences) {
if (DIST_EDITOR_ID.equals(reference.getId())) {
IEditorPart editor = reference.getEditor(false);
if (editor instanceof Refreshable) {
Refreshable refreshable = (Refreshable) editor;
refreshable.refresh();
}
}
}
} catch (Exception e) {
log.warn("Cannot refresh Carbon application project list", e);
}
}
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;
}
private void maintainListOfEditorsWithSameResourceSet(
final List<DiagramDocumentEditor> editorsWithSameResourceSet,
final IEditorReference[] editorReferences, final IEditorInput editorInput,
final ResourceSet resourceSet) {
for (final IEditorReference editorRef : editorReferences) {
try {
final IEditorInput currentEditorInput = editorRef.getEditorInput();
if (currentEditorInput != editorInput) {
final IEditorPart openEditor = editorRef.getEditor(false);
if (openEditor instanceof DiagramDocumentEditor) {
final DiagramDocumentEditor openDiagramEditor = (DiagramDocumentEditor) openEditor;
final ResourceSet diagramResourceSet = openDiagramEditor.getEditingDomain().getResourceSet();
if (diagramResourceSet == resourceSet) {
editorsWithSameResourceSet.add(openDiagramEditor);
}
}
}
} catch (final Exception ex) {
BonitaStudioLog.error(ex);
}
}
}
/**
* 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;
}
/**
* @return The last active editor with the given editor ID in the current
* active workbench page.
*/
public static IEditorPart getLastEditor(String editorId) {
if (INSTANCE == null) {
// not yet initialized, e.g. when another early startups blocks us!
// Let's try to get any editor with the specified id instead.
if (PlatformUI.getWorkbench() != null) {
final IWorkbenchWindow window = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow();
if (window != null) {
final IWorkbenchPage page = window.getActivePage();
if (page != null) {
for (IEditorReference ref : page.getEditorReferences()) {
if (ref.getId().equals(editorId)) {
return ref.getEditor(false);
}
}
}
}
}
return null;
}
return INSTANCE.getEditorById(editorId);
}
/**
* This method will return all editors open in all IWorkbenchWindows.
*
* <p>This method will ask Eclipse to restore editors which have not been loaded yet (if Eclipse
* is started editors are loaded lazily), which is why it must run in the SWT thread. So calling
* this method might cause partOpen events to be sent.
*
* @return all editors that are currently opened
* @swt
*/
public static Set<IEditorPart> getOpenEditors() {
Set<IEditorPart> editorParts = new HashSet<IEditorPart>();
for (IWorkbenchWindow window : PlatformUI.getWorkbench().getWorkbenchWindows()) {
for (IWorkbenchPage page : window.getPages()) {
for (IEditorReference reference : page.getEditorReferences()) {
IEditorPart editorPart = reference.getEditor(false);
if (editorPart == null) {
log.debug("editor part needs to be restored: " + reference.getTitle());
// Making this call might cause partOpen events
editorPart = reference.getEditor(true);
}
if (editorPart != null) {
editorParts.add(editorPart);
} else {
log.warn("editor part could not be restored: " + reference);
}
}
}
}
return editorParts;
}
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());
}
}
}
/**
* Creates a list of all {@link AnnotationEditor} which are currently opened.
*
* @return the annotation editors
*/
public static AnnotationEditor[] getAnnotationEditors() {
List<AnnotationEditor> dirtyParts = new ArrayList<>();
IWorkbenchWindow windows[] = PlatformUI.getWorkbench().getWorkbenchWindows();
for (IWorkbenchWindow element : windows) {
IWorkbenchPage pages[] = element.getPages();
for (IWorkbenchPage page : pages) {
IEditorReference[] references = page.getEditorReferences();
for (IEditorReference reference : references) {
IEditorPart part = reference.getEditor(false);
if (part instanceof AnnotationEditor) {
AnnotationEditor editor = (AnnotationEditor) part;
dirtyParts.add(editor);
}
}
}
}
return dirtyParts.toArray(new AnnotationEditor[dirtyParts.size()]);
}
/**
* {@inheritDoc}
*/
public Object execute(ExecutionEvent event) throws ExecutionException {
final Module m = getModuleFromContext((IEvaluationContext)event.getApplicationContext());
final Job j = new ToolboxJob("Removing module...") {
protected IStatus run(final IProgressMonitor monitor) {
try {
m.getResource().delete(IResource.NEVER_DELETE_PROJECT_CONTENT, monitor);
return Status.OK_STATUS;
} catch (Exception e) {
return Status.CANCEL_STATUS;
}
}
};
final IWorkbenchWindow iww = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
final IWorkbenchPage page = iww.getActivePage();
final IEditorReference[] refs = page.getEditorReferences();
final String tabName = m.getModuleName() + TLAConstants.Files.TLA_EXTENSION;
boolean removeModule = true;
for (final IEditorReference ier : refs) {
if (tabName.equals(ier.getName())) {
final IEditorPart editor = ier.getEditor(false);
if (editor != null) {
// If dirty and they cancel the closing, this will return false
removeModule = page.closeEditor(editor, true);
}
}
}
if (removeModule) {
j.schedule();
}
return null;
}
@Override
public void pageClosed(IWorkbenchPage page) {
IEditorReference[] rs = page.getEditorReferences();
for (IEditorReference r : rs) {
IEditorPart part = r.getEditor(false);
if (part != null) {
editorClosed(part);
}
}
page.removePartListener(this);
}
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]);
}
private IEditorPart showInEditor(IWorkbenchPage page, IEditorInput input, String editorId) {
IEditorPart editor= page.findEditor(input);
if (editor != null) {
page.bringToTop(editor);
return editor;
}
IEditorReference reusedEditorRef= fReusedEditor;
if (reusedEditorRef != null) {
boolean isOpen= reusedEditorRef.getEditor(false) != null;
boolean canBeReused= isOpen && !reusedEditorRef.isDirty() && !reusedEditorRef.isPinned();
if (canBeReused) {
boolean showsSameInputType= reusedEditorRef.getId().equals(editorId);
if (!showsSameInputType) {
page.closeEditors(new IEditorReference[] { reusedEditorRef }, false);
fReusedEditor= null;
} else {
editor= reusedEditorRef.getEditor(true);
if (editor instanceof IReusableEditor) {
((IReusableEditor) editor).setInput(input);
page.bringToTop(editor);
return editor;
}
}
}
}
// could not reuse
try {
editor= page.openEditor(input, editorId, false);
if (editor instanceof IReusableEditor) {
IEditorReference reference= (IEditorReference) page.getReference(editor);
fReusedEditor= reference;
} else {
fReusedEditor= null;
}
return editor;
} catch (PartInitException ex) {
MessageDialog.openError(JavaPlugin.getActiveWorkbenchShell(), SearchMessages.Search_Error_openEditor_title, SearchMessages.Search_Error_openEditor_message);
return null;
}
}
/**
* 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 boolean isResourceUsedElseWhere(final ResourceSet resourceSet, final IEditorInput editorInput) {
IEditorReference[] editorReferences;
if(PlatformUI.isWorkbenchRunning()){
editorReferences = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences();
}else{
return false;
}
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) {
return true;
}
}
}
} catch (final Exception e) {
BonitaStudioLog.error(e);
}
}
return false;
}
/**
* 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);
}
}
}
private ModelEditor getModelEditor(final IEvaluationContext context) {
// is current editor a model editor?
Object variable = context.getVariable(ISources.ACTIVE_EDITOR_ID_NAME);
final String id = (variable != IEvaluationContext.UNDEFINED_VARIABLE) ? (String)variable : null;
if ((id != null) && (id.startsWith(ModelEditor.ID))) {
variable = context.getVariable(ISources.ACTIVE_EDITOR_NAME);
if (variable instanceof IEditorPart) {
lastModelEditor = (ModelEditor)variable;
}
}
// If lastModelEditor is still null, it means we haven't run the model
// checker yet AND the model editor view is *not* active. Lets search
// through all editors to find a model checker assuming only a single one
// is open right now. If more than one model editor is open, randomly
// select one. In case it's not the one intended to be run by the user,
// she has to activate the correct model editor manually.
//
// It is tempting to store the name of the lastModelEditor
// in e.g. an IDialogSetting to persistently store even across Toolbox
// restarts. However, the only way to identify a model editor here is by
// its name and almost all model editors carry the name "Model_1" (the
// default name). So we might end up using Model_1 which was the last
// model that ran for spec A, but right now spec B and two of its model
// editors are open ("Model_1" and "Model_2"). It would launch Model_1,
// even though Model_2 might be what the user wants.
if (lastModelEditor == null) {
final IWorkbenchWindow workbenchWindow = (IWorkbenchWindow) context
.getVariable(ISources.ACTIVE_WORKBENCH_WINDOW_NAME);
for (final IWorkbenchPage page : workbenchWindow.getPages()) {
for (final IEditorReference editorRefs : page.getEditorReferences()) {
if (editorRefs.getId().equals(ModelEditor.ID)) {
lastModelEditor = (ModelEditor) editorRefs.getEditor(true);
break;
}
}
}
}
// Validate that the lastModelEditor still belongs to the current
// open spec. E.g. lastModelEditor might still be around from when
// the user ran a it on spec X, but has switched to spec Y in the
// meantime. Closing the spec nulls the ModelEditor
if ((lastModelEditor != null) && lastModelEditor.isDisposed()) {
lastModelEditor = null;
}
// If the previous two attempts to find a model editor have failed, lets
// return whatever we have... which might be null.
return lastModelEditor;
}
private IEditorPart showWithReuse(IFile file, IWorkbenchPage page, String editorId, boolean activate) throws PartInitException {
IEditorInput input= new FileEditorInput(file);
IEditorPart editor= page.findEditor(input);
if (editor != null) {
page.bringToTop(editor);
if (activate) {
page.activate(editor);
}
return editor;
}
IEditorReference reusedEditorRef= fReusedEditor;
if (reusedEditorRef != null) {
boolean isOpen= reusedEditorRef.getEditor(false) != null;
boolean canBeReused= isOpen && !reusedEditorRef.isDirty() && !reusedEditorRef.isPinned();
if (canBeReused) {
boolean showsSameInputType= reusedEditorRef.getId().equals(editorId);
if (!showsSameInputType) {
page.closeEditors(new IEditorReference[] { reusedEditorRef }, false);
fReusedEditor= null;
} else {
editor= reusedEditorRef.getEditor(true);
if (editor instanceof IReusableEditor) {
((IReusableEditor) editor).setInput(input);
page.bringToTop(editor);
if (activate) {
page.activate(editor);
}
return editor;
}
}
}
}
editor= page.openEditor(input, editorId, activate);
if (editor instanceof IReusableEditor) {
IEditorReference reference= (IEditorReference) page.getReference(editor);
fReusedEditor= reference;
} else {
fReusedEditor= null;
}
return editor;
}
private IEditorPart showWithReuse(IFile file, IWorkbenchPage page, String editorId, boolean activate)
throws PartInitException {
IEditorInput input = new FileEditorInput(file);
IEditorPart editor = page.findEditor(input);
if (editor != null) {
page.bringToTop(editor);
if (activate) {
page.activate(editor);
}
return editor;
}
IEditorReference reusedEditorRef = fReusedEditor;
if (reusedEditorRef != null) {
boolean isOpen = reusedEditorRef.getEditor(false) != null;
boolean canBeReused = isOpen && !reusedEditorRef.isDirty() && !reusedEditorRef.isPinned();
if (canBeReused) {
boolean showsSameInputType = reusedEditorRef.getId().equals(editorId);
if (!showsSameInputType) {
page.closeEditors(new IEditorReference[] { reusedEditorRef }, false);
fReusedEditor = null;
} else {
editor = reusedEditorRef.getEditor(true);
if (editor instanceof IReusableEditor) {
((IReusableEditor) editor).setInput(input);
page.bringToTop(editor);
if (activate) {
page.activate(editor);
}
return editor;
}
}
}
}
editor = page.openEditor(input, editorId, activate);
if (editor instanceof IReusableEditor) {
IEditorReference reference = (IEditorReference) page.getReference(editor);
fReusedEditor = reference;
} else {
fReusedEditor = null;
}
return editor;
}
public IEditorPart openBeanEditor(BeanItem beanItem, boolean readOnly) throws SystemException, PartInitException {
if (beanItem == null) {
return null;
}
ICodeGeneratorService service = (ICodeGeneratorService) GlobalServiceRegister.getDefault()
.getService(ICodeGeneratorService.class);
ECodeLanguage lang = ((RepositoryContext) CorePlugin.getContext().getProperty(Context.REPOSITORY_CONTEXT_KEY))
.getProject().getLanguage();
ITalendSynchronizer routineSynchronizer = service.createRoutineSynchronizer();
// check if the related editor is open.
IWorkbenchPage page = getActivePage();
IEditorReference[] editorParts = page.getEditorReferences();
String talendEditorID = "org.talend.designer.core.ui.editor.StandAloneTalend" + lang.getCaseName() + "Editor"; //$NON-NLS-1$ //$NON-NLS-2$
boolean found = false;
IEditorPart talendEditor = null;
for (IEditorReference reference : editorParts) {
IEditorPart editor = reference.getEditor(false);
if (talendEditorID.equals(editor.getSite().getId())) {
// TextEditor talendEditor = (TextEditor) editor;
RepositoryEditorInput editorInput = (RepositoryEditorInput) editor.getEditorInput();
if (editorInput.getItem().equals(beanItem)) {
page.bringToTop(editor);
found = true;
talendEditor = editor;
break;
}
}
}
if (!found) {
routineSynchronizer.syncRoutine(beanItem, true);
IFile file = routineSynchronizer.getFile(beanItem);
if (file == null) {
return null;
}
RepositoryEditorInput input = new BeanEditorInput(file, beanItem);
input.setReadOnly(readOnly);
talendEditor = page.openEditor(input, talendEditorID); // $NON-NLS-1$
}
return talendEditor;
}
/**
* 关闭指定文件的编辑器 -- robert 2013-04-01
* 备注:这里面的方法,是不能获取 nattable 的实例,故,在处理 合并打开的情况时,是通过 vtd 进行解析 合并临时文件从而获取相关文件的
* @param iFileList
*/
public static void closePointEditor(List<IFile> iFileList){
Map<IFile, IEditorPart> openedIfileMap = new HashMap<IFile, IEditorPart>();
IEditorReference[] referenceArray = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences();
for(IEditorReference reference : referenceArray){
IEditorPart editor = reference.getEditor(true);
IFile iFile = ((FileEditorInput)editor.getEditorInput()).getFile();
// 如果这是一个 nattable 编辑器
if (XLIFF_EDITOR_ID.equals(editor.getSite().getId())) {
String iFilePath = iFile.getLocation().toOSString();
String extension = iFile.getFileExtension();
if ("hsxliff".equals(extension)) {
openedIfileMap.put(iFile, editor);
}else if ("xlp".equals(extension)) {
// 这是合并打开的情况
// 开始解析这个合并打开临时文件,获取合并打开的文件。
VTDGen vg = new VTDGen();
if (vg.parseFile(iFilePath, true)) {
VTDNav vn = vg.getNav();
AutoPilot ap = new AutoPilot(vn);
try {
ap.selectXPath("/mergerFiles/mergerFile/@filePath");
int index = -1;
while ((index = ap.evalXPath()) != -1) {
String fileLC = vn.toString(index + 1);
if (fileLC != null && !"".equals(fileLC)) {
openedIfileMap.put(ResourceUtils.fileToIFile(fileLC), editor);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}else {
// 其他情况,直接将文件丢进去就行了
openedIfileMap.put(iFile, editor);
}
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
for(IFile curIfile : iFileList){
if (openedIfileMap.containsKey(curIfile)) {
page.closeEditor(openedIfileMap.get(curIfile), false);
}
}
}
}