Java Code Examples for org.eclipse.ui.IWorkbenchPage#getActiveEditor()

The following examples show how to use org.eclipse.ui.IWorkbenchPage#getActiveEditor() . 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: XLIFFEditorImplWithNatTable.java    From tmxeditor8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 得到当前活动的 XLIFF 编辑器实例
 * @return ;
 */
public static XLIFFEditorImplWithNatTable getCurrent() {
	try {
		IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		if (window != null) {
			IWorkbenchPage page = window.getActivePage();
			if (page != null) {
				IEditorPart editor = page.getActiveEditor();
				if (editor != null && editor instanceof XLIFFEditorImplWithNatTable) {
					return (XLIFFEditorImplWithNatTable) editor;
				}
			}
		}
	} catch (NullPointerException e) {
		LOGGER.error("", e);
		e.printStackTrace();
	}
	return null;
}
 
Example 2
Source File: ShowHidenNonPrintingCharacterHandler.java    From tmxeditor8 with GNU General Public License v2.0 6 votes vote down vote up
public Object execute(ExecutionEvent event) throws ExecutionException {
	isSelected = !isSelected;				
	try {
		IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		if (window != null) {
			IWorkbenchPage page = window.getActivePage();
			if (page != null) {
				IEditorPart editor = page.getActiveEditor();
				if (editor != null && editor instanceof IXliffEditor) {
					((IXliffEditor) editor).refreshWithNonprinttingCharacter(isSelected);
				}
			}
		}
	} catch (NullPointerException e) {
		e.printStackTrace();
	}
	
	return null;
}
 
Example 3
Source File: XLIFFEditorSelectionPropertyTester.java    From tmxeditor8 with GNU General Public License v2.0 6 votes vote down vote up
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
	boolean enabled = false;
	IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
	if (window != null) {
		IWorkbenchPage page = window.getActivePage();
		if (page != null) {
			IEditorPart editor = page.getActiveEditor();
			if (editor != null && editor instanceof XLIFFEditorImplWithNatTable) {
				XLIFFEditorImplWithNatTable xliffEditor = (XLIFFEditorImplWithNatTable) editor;
				if (xliffEditor != null && xliffEditor.getTable() != null) {
					List<String> selectedRowIds = xliffEditor.getSelectedRowIds();
					enabled = (selectedRowIds != null && selectedRowIds.size() > 0);
				}
			}
		}
	}

	return enabled;
}
 
Example 4
Source File: TexlipsePlugin.java    From texlipse with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the reference to the project that owns the
 * file currently open in editor.
 * @return reference to the currently active project 
 */
public static IProject getCurrentProject() {
    IWorkbenchPage page = TexlipsePlugin.getCurrentWorkbenchPage();
    IEditorPart actEditor = null;
    if (page.isEditorAreaVisible()
         && page.getActiveEditor() != null) {
        actEditor = page.getActiveEditor();
    }
    else {
        return null;
    }
    IEditorInput editorInput = actEditor.getEditorInput();
    
    IFile aFile = (IFile)editorInput.getAdapter(IFile.class);
    if (aFile != null) return aFile.getProject();
    // If the first way does not gonna work...
    // actually this returns the file of the editor that was last selected
    IResource res = SelectedResourceManager.getDefault().getSelectedResource();
    return res == null ? null : res.getProject();
}
 
Example 5
Source File: OpenSymbolAction.java    From typescript.java with MIT License 6 votes vote down vote up
@Override
public void runWithEvent(Event event) {
	Shell parent = TypeScriptUIPlugin.getActiveWorkbenchShell();

	IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
	if (window != null) {
		IWorkbenchPage page = window.getActivePage();
		if (page != null) {
			if (page.getActiveEditor() instanceof TypeScriptEditor) {
				ITypeScriptFile tsFile = ((TypeScriptEditor) page.getActiveEditor()).getTypeScriptFile();
				OpenSymbolSelectionDialog dialog = new OpenSymbolSelectionDialog(tsFile, parent, true);
				int result = dialog.open();
				if (result == 0) {
					Object[] resources = dialog.getResult();
					if (resources != null && resources.length > 0) {
						NavtoItem item = (NavtoItem) resources[0];
						EditorUtils.openInEditor(item);
					}
				}
			}
		}
	}

}
 
Example 6
Source File: SaveAsImageHandler.java    From bonita-studio with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
    final IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    //Remove Selection
    final DiagramEditor editor = (DiagramEditor) activePage.getActiveEditor();
    editor.getDiagramGraphicalViewer().setSelection(new StructuredSelection());
    final CopyToImageAction act = new CopyToImageAction(activePage) {

        @Override
        protected List createOperationSet() {
            if (getWorkbenchPart() instanceof DiagramEditor) {
                return Collections.singletonList(((DiagramEditor) getWorkbenchPart()).getDiagramEditPart());
            }
            return Collections.emptyList();
        }

        @Override
        protected void setWorkbenchPart(final IWorkbenchPart workbenchPart) {
            super.setWorkbenchPart(getWorkbenchPage().getActiveEditor());
        }

    };
    act.init();
    act.run();
    return null;
}
 
Example 7
Source File: EmacsPlusUtils.java    From e4macs with Eclipse Public License 1.0 5 votes vote down vote up
public static ITextEditor getCurrentEditor() {
	ITextEditor result = null;
	IWorkbenchPage page = getWorkbenchPage();
	if (page != null) {
		IEditorPart activeEditor = page.getActiveEditor();
		result = getActiveTextEditor(activeEditor);
	}
	return result;
}
 
Example 8
Source File: JavaContentAssistHandler.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private ITextEditor getActiveEditor() {
	IWorkbenchWindow window= PlatformUI.getWorkbench().getActiveWorkbenchWindow();
	if (window != null) {
		IWorkbenchPage page= window.getActivePage();
		if (page != null) {
			IEditorPart editor= page.getActiveEditor();
			if (editor instanceof ITextEditor)
				return (JavaEditor) editor;
		}
	}
	return null;
}
 
Example 9
Source File: SseUtilities.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @return the active structured text viewer, or null
 */
public static StructuredTextViewer getActiveTextViewer() {
  // Need to get the workbench window from the UI thread
  final IWorkbenchWindow[] windowHolder = new IWorkbenchWindow[1];
  Display.getDefault().syncExec(new Runnable() {
    public void run() {
      windowHolder[0] = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    }
  });
 
  IWorkbenchWindow window = windowHolder[0];
  if (window == null) {
    return null;
  }
  
  IWorkbenchPage page = window.getActivePage();
  if (page == null) {
    return null;
  }

  IEditorPart editor = page.getActiveEditor();
  if (editor == null) {
    return null;
  }

  /*
   * TODO: change the following to use AdapterUtilities.getAdapter()
   * and either a) have GWTD register an adapter factory or b) add a direct
   * IAdaptable.getAdapter() call to AdapterUtilities.getAdapter().
   */
  StructuredTextEditor structuredEditor = (StructuredTextEditor) editor.getAdapter(StructuredTextEditor.class);
  if (structuredEditor == null) {
    return null;
  }

  return structuredEditor.getTextViewer();
}
 
Example 10
Source File: JavadocHover.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private IWorkbenchSite getSite() {
	IEditorPart editor= getEditor();
	if (editor == null) {
		IWorkbenchPage page= JavaPlugin.getActivePage();
		if (page != null)
			editor= page.getActiveEditor();
	}
	if (editor != null)
		return editor.getSite();

	return null;
}
 
Example 11
Source File: UIHelper.java    From tlaplus with MIT License 5 votes vote down vote up
/**
 * @return The currently active editor
 */
public static IEditorPart getActiveEditor() {
	final IWorkbenchPage activePage = getActivePage();
	if (activePage != null) {
		// At Toolbox startup, activePage can be null.
		return activePage.getActiveEditor();
	}
	return null;
}
 
Example 12
Source File: FieldInitializerUtil.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public IJavaElement getSelectedResource(IStructuredSelection selection) {
	IJavaElement elem = null;
	if(selection != null && !selection.isEmpty()){
		Object o = selection.getFirstElement();
		elem = Adapters.adapt(o, IJavaElement.class);
		if(elem == null){
			elem = getPackage(o);
		}
	}
	if (elem == null) {
		IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		IWorkbenchPart part = activePage.getActivePart();
		if (part instanceof ContentOutline) {
			part= activePage.getActiveEditor();
		}
		if (part instanceof XtextEditor) {
			IXtextDocument doc = ((XtextEditor)part).getDocument();
			IFile file = Adapters.adapt(doc, IFile.class);
			elem = getPackage(file);
		}
	}
	if (elem == null || elem.getElementType() == IJavaElement.JAVA_MODEL) {
		try {
			IJavaProject[] projects= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects();
			if (projects.length == 1) {
				elem= projects[0];
			}
		} catch (JavaModelException e) {
			throw new RuntimeException(e.getMessage());
		}
	}
	return elem;
}
 
Example 13
Source File: UIUtils.java    From statecharts with Eclipse Public License 1.0 5 votes vote down vote up
public static DiagramDocumentEditor getActiveEditor() {
	final IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
	final IWorkbenchPage workbenchPage = workbenchWindow.getActivePage();
	if (workbenchPage != null) {
		final IEditorPart editor = workbenchPage.getActiveEditor();
		if (editor instanceof DiagramDocumentEditor) {
			return (DiagramDocumentEditor)editor;
		}
	}
	return null;
}
 
Example 14
Source File: WorkbenchUtils.java    From xds-ide with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Fetches the editor currently in use. If the active editor is a multi page
 * editor and the param getSubEditor is true, then the editor of the active
 * page is returned instead of the multi page editor.
 * 
 * @param getSubEditor
 *            indicates that a sub editor should be returned
 * @return the active editor or <code>null</code> if no editor is active
 */
public static IEditorPart getActiveEditor(boolean getSubEditor) {
    IWorkbenchPage page = getActivePage();
    if (page != null) {
        IEditorPart result = page.getActiveEditor();
        if (getSubEditor && (result instanceof FormEditor)) {
            result = ((FormEditor) result).getActiveEditor();
        }
        return result;
    }
    return null;
}
 
Example 15
Source File: Utilities.java    From jbt with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the currently active BTEditor, or null if no BTEditor is active.
 */
public static BTEditor getActiveBTEditor() {
	IWorkbenchPage page = getMainWindowActivePage();

	if (page != null) {
		IEditorPart editor = page.getActiveEditor();
		if (editor instanceof BTEditor)
			return (BTEditor) editor;
		else
			return null;
	}

	return null;
}
 
Example 16
Source File: WorkbenchHelper.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
public static IGamlEditor getActiveEditor() {
	final IWorkbenchPage page = getPage();
	if (page != null) {
		final IEditorPart editor = page.getActiveEditor();
		if (editor instanceof IGamlEditor) { return (IGamlEditor) editor; }
	}
	return null;
}
 
Example 17
Source File: WizardUtils.java    From XPagesExtensionLibrary with Apache License 2.0 5 votes vote down vote up
public static String getXPageFileName() {
    IWorkbenchWindow win = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    IWorkbenchPage page = win.getActivePage();
    if (page != null) {
        IEditorPart editor = page.getActiveEditor();
        if (editor != null) {
            IEditorInput input = editor.getEditorInput();
            if (input instanceof IFileEditorInput) {
                return ((IFileEditorInput) input).getFile().getLocation().lastSegment();
            }
        }
    }
    return null;
}
 
Example 18
Source File: ExportAsBPMNHandler.java    From bonita-studio with GNU General Public License v2.0 4 votes vote down vote up
@CanExecute
public boolean isDiagramEditorActive(IWorkbenchPage activePage) {
    return activePage != null && activePage.getActiveEditor() instanceof ProcessDiagramEditor;
}
 
Example 19
Source File: ImportURINavigationTest.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
protected void doTestNavigation(IUnitOfWork<URI, IFile> uriComputation, boolean expectFQN) throws Exception {
	IJavaProject project = JavaProjectSetupUtil.createJavaProject("importuriuitestlanguage.project");
	try {
		IFile first = project.getProject().getFile("src/first.importuriuitestlanguage");
		first.create(new StringInputStream("type ASimpleType"), true, null);
		
		ResourceSet resourceSet = resourceSetProvider.get(project.getProject());
		
		Resource resource = resourceFactory.createResource(URI.createURI("synthetic://second.importuriuitestlanguage"));
		resourceSet.getResources().add(resource);
		String model = "import '" + uriComputation.exec(first) + "' type MyType extends ASimpleType";
		resource.load(new StringInputStream(model), null);
		EcoreUtil.resolveAll(resource);
		Assert.assertTrue(resource.getErrors().isEmpty());
		
		IHyperlink[] hyperlinks = helper.createHyperlinksByOffset((XtextResource) resource, model.indexOf("SimpleType"), false);
		Assert.assertEquals(1, hyperlinks.length);
		IWorkbenchPage activePage = workbench.getActiveWorkbenchWindow().getActivePage();
		Assert.assertNull(activePage.getActiveEditor());
		if (expectFQN) {
			Assert.assertEquals(URI.createURI(first.getLocationURI().toString()), ((XtextHyperlink)hyperlinks[0]).getURI().trimFragment());
		} else {
			Assert.assertEquals(URI.createPlatformResourceURI(first.getFullPath().toString(), true), ((XtextHyperlink)hyperlinks[0]).getURI().trimFragment());
		}
		hyperlinks[0].open();
		IEditorPart editor = activePage.getActiveEditor();
		Assert.assertNotNull(editor);
		IXtextDocument document = xtextDocumentUtil.getXtextDocument(editor);
		document.readOnly(new IUnitOfWork.Void<XtextResource>() {
			@Override
			public void process(XtextResource state) throws Exception {
				Assert.assertEquals("platform:/resource/importuriuitestlanguage.project/src/first.importuriuitestlanguage", state.getURI().toString());
			}
		});
		Assert.assertEquals("type ASimpleType", document.get());
		IEditorPart newPart = IDE.openEditor(activePage, first);
		Assert.assertEquals(1, activePage.getEditorReferences().length);
		Assert.assertEquals(editor, newPart);
	} finally {
		project.getProject().delete(true, null);
	}
}
 
Example 20
Source File: UIUtil.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
public static IProject getCurrentProject( )
{
    IWorkbench iworkbench = PlatformUI.getWorkbench( );
    if ( iworkbench == null )
    {
        return null;
    }

    IWorkbenchWindow iworkbenchwindow = iworkbench
            .getActiveWorkbenchWindow( );
    if ( iworkbenchwindow == null )
    {
        return null;
    }

    IWorkbenchPage iworkbenchpage = iworkbenchwindow.getActivePage( );
    if ( iworkbenchpage != null )
    {
        IEditorPart ieditorpart = iworkbenchpage.getActiveEditor( );
        if ( ieditorpart != null )
        {
            IEditorInput input = ieditorpart.getEditorInput( );
            if ( input != null )
            {
                IProject project = (IProject) ElementAdapterManager
                        .getAdapter( input, IProject.class );
                if ( project != null )
                {
                    return project;
                }
            }
        }
    }

    ISelection selection = iworkbenchwindow.getSelectionService( )
            .getSelection( );
    if ( selection instanceof IStructuredSelection )
    {
        Object element = ( (IStructuredSelection) selection )
                .getFirstElement( );
        if ( element instanceof IResource )
        {
            return ( (IResource) element ).getProject( );
        }
    }
    return null;
}