下面列出了org.eclipse.ui.IWorkbenchPage#getActiveEditor ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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;
}
/**
* 得到当前活动的 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;
}
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;
}
/**
* 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();
}
@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);
}
}
}
}
}
}
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;
}
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;
}
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;
}
/**
* 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;
}
/**
* 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;
}
public static ITextEditor getCurrentEditor() {
ITextEditor result = null;
IWorkbenchPage page = getWorkbenchPage();
if (page != null) {
IEditorPart activeEditor = page.getActiveEditor();
result = getActiveTextEditor(activeEditor);
}
return result;
}
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;
}
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;
}
/**
* @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;
}
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;
}
/**
* @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();
}
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;
}
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;
}
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);
}
}
@CanExecute
public boolean isDiagramEditorActive(IWorkbenchPage activePage) {
return activePage != null && activePage.getActiveEditor() instanceof ProcessDiagramEditor;
}