下面列出了org.eclipse.ui.IWorkbenchPage#getEditorReferences ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
/**
* Close open editors associated with this experiment.
*/
public void closeEditors() {
IFile file = getBookmarksFile();
FileEditorInput input = new FileEditorInput(file);
IWorkbench wb = PlatformUI.getWorkbench();
for (IWorkbenchWindow wbWindow : wb.getWorkbenchWindows()) {
for (IWorkbenchPage wbPage : wbWindow.getPages()) {
for (IEditorReference editorReference : wbPage.getEditorReferences()) {
try {
if (editorReference.getEditorInput().equals(input)) {
wbPage.closeEditor(editorReference.getEditor(false), false);
}
} catch (PartInitException e) {
Activator.getDefault().logError(NLS.bind(Messages.TmfCommonProjectElement_ErrorClosingEditor, getName()), e);
}
}
}
}
}
/**
* Returns a List containing all the BTEditor that are currently open.
*/
public static List<BTEditor> getBTEditors() {
IWorkbenchPage activePage = getMainWindowActivePage();
if (activePage != null) {
IEditorReference[] editors = activePage.getEditorReferences();
if (editors.length == 0)
return new Vector<BTEditor>();
List<BTEditor> returnedEditors = new Vector<BTEditor>();
for (int i = 0; i < editors.length; i++) {
if (editors[i].getEditor(false) instanceof BTEditor) {
returnedEditors.add((BTEditor) editors[i].getEditor(false));
}
}
return returnedEditors;
}
return new LinkedList<BTEditor>();
}
public RenameSelectionState(Object element) {
fElement= element;
fParts= new ArrayList<IWorkbenchPart>();
fSelections= new ArrayList<IStructuredSelection>();
IWorkbenchWindow dw = JavaPlugin.getActiveWorkbenchWindow();
if (dw == null) {
fDisplay= null;
return;
}
fDisplay= dw.getShell().getDisplay();
IWorkbenchPage page = dw.getActivePage();
if (page == null)
return;
IViewReference vrefs[]= page.getViewReferences();
for(int i= 0; i < vrefs.length; i++) {
consider(vrefs[i].getPart(false));
}
IEditorReference refs[]= page.getEditorReferences();
for(int i= 0; i < refs.length; i++) {
consider(refs[i].getPart(false));
}
}
protected void cleanupWorkbench(IFile targetFile, IWorkbenchPage page) throws CoreException {
if (targetFile.exists()) {
IEditorReference[] refs = page.getEditorReferences();
for (IEditorReference ref : refs) {
IEditorPart part = ref.getEditor(false);
if (part != null) {
IEditorInput inp = part.getEditorInput();
if (inp instanceof FileEditorInput) {
if (((FileEditorInput) inp).getFile().equals(targetFile)) {
page.closeEditor(ref.getEditor(false), true);
}
}
}
}
}
}
protected DiagramEditPart retrieveEditPartFromOpenedEditors(final Diagram d) {
final IWorkbenchPage activePage = getActivePage();
if (activePage != null) {
for (final IEditorReference ep : activePage.getEditorReferences()) {
final IEditorPart editor = ep.getEditor(false);
if (editor instanceof DiagramEditor
&& ((DiagramEditor) editor).getDiagram().equals(d)
&& ((DiagramEditor) editor).getDiagramEditPart() != null) {
return ((DiagramEditor) editor).getDiagramEditPart();
}
}
}
return null;
}
/**
* {@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;
}
private IEditorReference activeEditorReference(final IWorkbenchPage activePage) {
final IEditorPart activeEditor = activePage.getActiveEditor();
for (final IEditorReference ref : activePage.getEditorReferences()) {
if (Objects.equal(activeEditor, ref.getPart(false))) {
return ref;
}
}
return null;
}
@Override
public void pageOpened(IWorkbenchPage page) {
IEditorReference[] rs = page.getEditorReferences();
for (IEditorReference r : rs) {
IEditorPart part = r.getEditor(false);
if (part != null) {
editorOpened(part);
}
}
page.addPartListener(this);
}
public static void showValidationPart() {
final IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
final IEditorPart part = activePage.getActiveEditor();
if (part != null && part instanceof DiagramEditor) {
final MainProcess proc = ModelHelper
.getMainProcess(((DiagramEditor) part).getDiagramEditPart().resolveSemanticElement());
final String partName = proc.getName() + " (" + proc.getVersion() + ")";
for (final IEditorReference ref : activePage.getEditorReferences()) {
if (partName.equals(ref.getPartName())) {
activePage.activate(ref.getPart(true));
break;
}
}
}
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
try {
activePage.showView("org.bonitasoft.studio.validation.view");
} catch (final PartInitException e) {
BonitaStudioLog.error(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;
}
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 static IEditorReference[] getOpenedFileRefs( )
{
IWorkbenchWindow window = org.eclipse.ui.PlatformUI.getWorkbench( )
.getActiveWorkbenchWindow( );
IWorkbenchPage page = window.getActivePage( );
return page.getEditorReferences( );
}
static IEditorReference[] getEditors(final IWorkbenchWindow w) {
if (w != null) {
final IWorkbenchPage page = w.getActivePage();
if (page != null) { return page.getEditorReferences(); }
}
return new IEditorReference[0];
}
/**
* This operation overrides the ViewPart.createPartControl method to create
* and draw the TreeViewer before registering it as a selection provider.
*
* @param parent
* The Composite used to create the TreeViewer.
*/
@Override
public void createPartControl(Composite parent) {
// Create a TabFolder to manage tabs
tabFolder = new TabFolder(parent, SWT.NONE);
// Create pages (TabItems) for text files and images
TabItem textTab = new TabItem(tabFolder, SWT.NONE, 0);
textTab.setText("Files");
TabItem imageTab = new TabItem(tabFolder, SWT.NONE, 1);
imageTab.setText("Images");
TabItem plotTab = new TabItem(tabFolder, SWT.NONE, 2);
plotTab.setText("Plots");
// Create the tool bar and buttons for the view
createActions();
// Initialize the TreeViewer
fileTreeViewer = new TreeViewer(tabFolder);
imageTreeViewer = new TreeViewer(tabFolder);
// Create content and label providers
initializeTreeViewer(fileTreeViewer);
initializeTreeViewer(imageTreeViewer);
// Register the tree to the tabs
textTab.setControl(fileTreeViewer.getControl());
imageTab.setControl(imageTreeViewer.getControl());
// Register this view as a SelectionProvider
getSite().setSelectionProvider(fileTreeViewer);
getSite().setSelectionProvider(imageTreeViewer);
// Registered the view as a double click listener of the TreeViewer
fileTreeViewer.addDoubleClickListener(this);
imageTreeViewer.addDoubleClickListener(this);
// Add a listener to catch tab selection changes.
// NOTE: In Windows, this event is fired instantly, so this listener
// needs to be declared after everything else is initialized!
tabFolder.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event event) {
// If tabs are changed while playing, stop playing.
if (playAction.isInPlayState()) {
playAction.stop();
}
// Set the TreeViewer input to the selected tab
setTreeContent(tabFolder.indexOf((TabItem) event.item));
}
});
// Create the Table and table viewer for the Plot tab
Table listTable = new Table(tabFolder, SWT.FLAT);
DefaultEventTableViewer<VizResource> listTableViewer = new DefaultEventTableViewer<VizResource>(
plotList, listTable, plotList);
// Register the table control with the plot tab
plotTab.setControl(listTable);
// Check if there is currently an active ICEFormEditor. If so, update
// the currently active editor and related UI pieces.
IEditorPart activeEditor = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage().getActiveEditor();
if (activeEditor != null && activeEditor instanceof ICEFormEditor) {
if (activeEditor != editor) {
setActiveEditor((ICEFormEditor) activeEditor);
}
} else {
// Get a list of all the currently open editors
IWorkbenchPage workbenchPage = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage();
IEditorReference[] editorRefs = workbenchPage.getEditorReferences();
if (editorRefs != null && editorRefs.length > 0) {
// Begin iterating through all the editors, looking for one
// that's an ICEFormEditor
for (IEditorReference e : editorRefs) {
// If it's an ICEFormEditor, set it as the active editor
if (e.getId().equals(ICEFormEditor.ID)) {
setActiveEditor((ICEFormEditor) e.getEditor(false));
break;
}
}
}
}
// Register as a listener to the part service so that the view can
// update when the active ICEFormEditor changes.
IPartService partService = getSite().getWorkbenchWindow()
.getPartService();
partService.addPartListener(this);
return;
}
/**
* 根据指定要合并打开的文件,获取其配置文件
* @param selectIFiles
* @param isActive 如果找到了符合的合并打开临时文件,是否激活当前nattable编辑器
* @return ;
*/
public IFile getMultiFilesTempIFile(boolean isActive){
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorReference[] editorRes = page.getEditorReferences();
for (int i = 0; i < editorRes.length; i++) {
if (editorRes[i].getId().equals(XLIFF_EDITOR_ID)) {
try {
IXliffEditor xlfEditor = (IXliffEditor) editorRes[i].getEditor(true);
IFile multiTempFile = ((FileEditorInput) editorRes[i].getEditorInput()).getFile();
List<File> openedFileList = xlfEditor.getMultiFileList();
boolean isExist = false;
if (selectIFiles.size() == openedFileList.size()) {
isExist = true;
for (IFile iFile : selectIFiles) {
if (openedFileList.indexOf(iFile.getFullPath().toFile()) == -1) {
continue;
}
}
}
// Map<String, Object> resultMap = handler.openFile(multiTempFile.getLocation().toOSString());
// if (resultMap == null
// || Constant.RETURNVALUE_RESULT_SUCCESSFUL != (Integer) resultMap.get(Constant.RETURNVALUE_RESULT)) {
// continue;
// }
// List<String> mergerFileList = handler.getMultiFiles(multiTempFile);
// if (mergerFileList.size() == selectIFiles.size()) {
// for (IFile iFile : selectIFiles) {
// if (mergerFileList.indexOf(iFile.getLocation().toOSString()) < 0) {
// continue;
// }
// }
// }
if (isActive) {
page.activate(editorRes[i].getEditor(true));
}
if (isExist) {
return multiTempFile;
}
} catch (PartInitException e) {
e.printStackTrace();
}
}
}
return null;
}
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;
}
@Override
protected void okPressed() {
String filterNameStr = filterNameTxt.getText();
if (filterNameStr == null || "".equals(filterNameStr)) {
MessageDialog.openInformation(getShell(), "", Messages.getString("dialog.CustomFilterDialog.msg6"));
return;
}
StringBuilder xpath = new StringBuilder();
String link = andBtn.getSelection() ? " and " : " or ";
ArrayList<String[]> tempValue = new ArrayList<String[]>();
for (DynaComposite comp : conditionList) { // 得到所有自定义条件组合的xpath
String tempXpath = comp.getXpath(true);
if (RESULT_FAILED.equals(tempXpath)) {
return;
}
xpath.append(tempXpath).append(link);
tempValue.add(comp.getTempIndex());
}
if (xpath.length() > 0) {
if (isAdd()) {
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (window != null) {
IWorkbenchPage page = window.getActivePage();
if (page != null) {
IEditorReference[] editors = page.getEditorReferences();
for(IEditorReference e : editors){
IEditorPart editor = e.getEditor(false);
if (editor != null && editor instanceof XLIFFEditorImplWithNatTable) {
Combo cb = ((XLIFFEditorImplWithNatTable) editor).getFilterCombo();
if(cb != null && !cb.isDisposed()){
cb.add(filterNameTxt.getText());
}
}
}
}
}
// cmbFilter.add(filterNameTxt.getText());
} else {
XLFHandler.getFilterMap().put(filterNameTxt.getText(), xpath.substring(0, xpath.lastIndexOf(link)));
}
customFilters.put(filterNameStr, xpath.substring(0, xpath.lastIndexOf(link)));
customFiltersAddition.put(filterNameStr, link.trim());
customFiltersIndex.put(filterNameStr, tempValue);
PreferenceStore.saveMap(IPreferenceConstants.FILTER_CONDITION, new TreeMap<String, String>(customFilters));
PreferenceStore.saveMap(IPreferenceConstants.FILTER_CONDITION_ADDITION, customFiltersAddition);
PreferenceStore.saveCustomCondition(IPreferenceConstants.FILTER_CONDITION_INDEX, customFiltersIndex);
reload();
}
}
@Override
protected void okPressed() {
String filterNameStr = filterNameTxt.getText();
if (filterNameStr == null || "".equals(filterNameStr)) {
MessageDialog.openInformation(getShell(), "", Messages.getString("dialog.CustomFilterDialog.msg6"));
return;
}
StringBuilder xpath = new StringBuilder();
String link = andBtn.getSelection() ? " and " : " or ";
ArrayList<String[]> tempValue = new ArrayList<String[]>();
for (DynaComposite comp : conditionList) { // 得到所有自定义条件组合的xpath
String tempXpath = comp.getXpath(true);
if (RESULT_FAILED.equals(tempXpath)) {
return;
}
xpath.append(tempXpath).append(link);
tempValue.add(comp.getTempIndex());
}
if (xpath.length() > 0) {
if (isAdd()) {
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (window != null) {
IWorkbenchPage page = window.getActivePage();
if (page != null) {
IEditorReference[] editors = page.getEditorReferences();
for(IEditorReference e : editors){
IEditorPart editor = e.getEditor(false);
if (editor != null && editor instanceof XLIFFEditorImplWithNatTable) {
Combo cb = ((XLIFFEditorImplWithNatTable) editor).getFilterCombo();
if(cb != null && !cb.isDisposed()){
cb.add(filterNameTxt.getText());
}
}
}
}
}
// cmbFilter.add(filterNameTxt.getText());
} else {
XLFHandler.getFilterMap().put(filterNameTxt.getText(), xpath.substring(0, xpath.lastIndexOf(link)));
}
customFilters.put(filterNameStr, xpath.substring(0, xpath.lastIndexOf(link)));
customFiltersAddition.put(filterNameStr, link.trim());
customFiltersIndex.put(filterNameStr, tempValue);
PreferenceStore.saveMap(IPreferenceConstants.FILTER_CONDITION, new TreeMap<String, String>(customFilters));
PreferenceStore.saveMap(IPreferenceConstants.FILTER_CONDITION_ADDITION, customFiltersAddition);
PreferenceStore.saveCustomCondition(IPreferenceConstants.FILTER_CONDITION_INDEX, customFiltersIndex);
reload();
}
}