下面列出了org.eclipse.ui.PlatformUI#getWorkbench ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public MessageConsoleStream getMessageStream() {
MessageConsole myConsole = findConsole("Yang Console"); //calls function to find/create the Yang console
if (myConsole != null) {
IWorkbench wb = PlatformUI.getWorkbench();
IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
IWorkbenchPage page = win.getActivePage();
String id = IConsoleConstants.ID_CONSOLE_VIEW;
IConsoleView view;
try {
view = (IConsoleView) page.showView(id);
view.display(myConsole);
return myConsole.newMessageStream();
} catch (PartInitException e) {
e.printStackTrace();
}
}
return null;
}
/** {@inheritDoc} */
public XtextEditor getActiveXtextEditor() {
IWorkbench workbench = PlatformUI.getWorkbench();
IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
if (workbenchWindow == null) {
return null;
}
IWorkbenchPage activePage = workbenchWindow.getActivePage();
if (activePage == null) {
return null;
}
IEditorPart activeEditor = activePage.getActiveEditor();
if (activeEditor instanceof XtextEditor) {
return (XtextEditor) activeEditor;
// return null;
}
// XtextEditor xtextEditor = (XtextEditor) activeEditor.getAdapter(XtextEditor.class);
// return xtextEditor;
return null;
}
/**
* Create the table viewer to test
*
* @return the table viewer bot
*/
protected AbstractSegmentStoreTableView openTable() {
AbstractSegmentStoreTableView tableView = getTableView();
if (tableView != null) {
return tableView;
}
IViewPart vp = null;
final IWorkbench workbench = PlatformUI.getWorkbench();
vp = UIThreadRunnable.syncExec((Result<IViewPart>) () -> {
try {
return workbench.getActiveWorkbenchWindow().getActivePage().showView(TestSegmentStoreTableView.ID);
} catch (PartInitException e) {
return null;
}
});
assertNotNull(vp);
assertTrue(vp instanceof TestSegmentStoreTableView);
TestSegmentStoreTableView testSegmentStoreTableView = (TestSegmentStoreTableView) vp;
testSegmentStoreTableView.setTest(this);
fTableView = testSegmentStoreTableView;
return fTableView;
}
/**
* Switch to the perspective with id given as parameter
*
* @param perspectiveID
*/
public static synchronized void switchToPerspective(final String perspectiveID) {
final IWorkbench workbench = PlatformUI.getWorkbench();
final IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
if (window != null) {
final IWorkbenchPage activePage = window.getActivePage();
if (activePage != null) {
final IPerspectiveDescriptor activePerspective = activePage.getPerspective();
if (activePerspective == null || !activePerspective.getId().equals(perspectiveID)) {
final IPerspectiveRegistry registry = workbench.getPerspectiveRegistry();
final IWorkbenchPage page = window.getActivePage();
final IPerspectiveDescriptor desc = registry.findPerspectiveWithId(perspectiveID);
page.setPerspective(desc);
}
}
}
}
public static void openSarosView() {
/*
* TODO What to do if no WorkbenchWindows are are active?
*/
final IWorkbench workbench = PlatformUI.getWorkbench();
if (workbench == null) return;
final IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
if (window == null) return;
try {
window.getActivePage().showView(SarosView.ID, null, IWorkbenchPage.VIEW_CREATE);
} catch (PartInitException e) {
log.error("could not open Saros view (id: " + SarosView.ID + ")", e); // $NON-NLS-1$
}
}
public void run(String[] params, ICheatSheetManager manager) {
if ((params != null) && (params.length > 0)) {
IWorkbench workbench = PlatformUI.getWorkbench();
INewWizard wizard = getWizard(params[0]);
wizard.init(workbench, new StructuredSelection());
WizardDialog dialog = new WizardDialog(PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getShell(), wizard);
dialog.create();
dialog.open();
// did the wizard succeed ?
notifyResult(dialog.getReturnCode() == Window.OK);
}
}
@Override
public void stop() {
final IWorkbench workbench = PlatformUI.getWorkbench();
if (workbench == null) {
return;
}
final Display display = workbench.getDisplay();
display.syncExec(() -> {
if (!display.isDisposed()) {
workbench.close();
}
});
}
private static IWorkbenchPage getActivePage() {
final IWorkbench bench = PlatformUI.getWorkbench();
if (bench == null) return null;
final IWorkbenchWindow window = bench.getActiveWorkbenchWindow();
if (window == null) return null;
return window.getActivePage();
}
static private final IWorkbenchPage unsafeGetWorkbenchPage() {
IWorkbenchPage page = null;
if (PlatformUI.isWorkbenchRunning()) {
IWorkbench wb = PlatformUI.getWorkbench();
IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
page = win.getActivePage();
}
return page;
}
/**
* Switch current perspective
*
* @param perspectiveId
* @return
*/
public static IWorkbenchPage switchPerspective(String perspectiveId) {
Assert.isNotNull(perspectiveId, "PerspectiveId is null");
IWorkbench workbench = PlatformUI.getWorkbench();
IWorkbenchWindow window = getActiveWindow();
Assert.isNotNull(workbench, "Workbench is null");
Assert.isNotNull(window, "Window is null");
try {
IWorkbenchPage page = workbench.showPerspective(perspectiveId, window);
// show intro
if (InitialPerspective.ID.equals(perspectiveId) && workbench.getIntroManager().hasIntro()) {
page.resetPerspective();
// We are no longer showing the Intro view. The following will
// probably
// be replaced by something that shows the view we want. 09 Oct
// 2009
// workbench.getIntroManager().showIntro(window, false);
openView(ToolboxWelcomeView.ID);
}
return page;
} catch (WorkbenchException e) {
Activator.getDefault().logError("Error switching a perspective to " + perspectiveId, e);
}
return null;
}
public void stop() {
if (!PlatformUI.isWorkbenchRunning())
return;
final IWorkbench workbench = PlatformUI.getWorkbench();
final Display display = workbench.getDisplay();
display.syncExec(new Runnable() {
public void run() {
if (!display.isDisposed())
workbench.close();
}
});
}
protected WizardDialog createWizardDialog(NewElementWizard newXtendClassWizard) {
IWorkbench workbench = PlatformUI.getWorkbench();
Shell shell = workbench.getActiveWorkbenchWindow().getShell();
newXtendClassWizard.init(workbench, new StructuredSelection());
WizardDialog dialog = new WizardDialog(shell, newXtendClassWizard);
dialog.create();
return dialog;
}
@Test
public void testRunAppEngine_hiddenEvenIfAppEngineProjectFileIsOpen() throws CoreException {
IProject emptyProject = emptyProjectCreator.getProject();
// Create an empty file in the App Engine project, and open it in an editor.
IProject appEngineProject = appEngineProjectCreator.getProject();
IFile file = appEngineProject.getFile("textfile.txt");
file.create(new ByteArrayInputStream(new byte[0]), IFile.FORCE, null);
IWorkbench workbench = PlatformUI.getWorkbench();
assertNotNull(WorkbenchUtil.openInEditor(workbench, file));
assertFalse(appEngineMenuExists(emptyProject));
}
@Override
public void earlyStartup() {
final IWorkbench workbench = PlatformUI.getWorkbench();
GcpStatusMonitoringService service = workbench.getService(GcpStatusMonitoringService.class);
if (service != null) {
service.addStatusChangeListener(
result -> {
ICommandService commandService = workbench.getService(ICommandService.class);
if (commandService != null) {
commandService.refreshElements(
"com.google.cloud.tools.eclipse.ui.status.showGcpStatus", null);
}
});
}
}
public static IEclipseContext workbenchContext() {
if (PlatformUI.isWorkbenchRunning()) {
final Workbench workbench = (Workbench) PlatformUI.getWorkbench();
if (workbench != null) {
final IEclipseContext context = workbench.getContext();
checkNotNull(context, "Workbench eclipse context is null");
return context;
}
throw new IllegalStateException("No workbench available");
}
return headlessContext();
}
/**
* Create a new view. <br>
* If a view with the corresponding id already exists and no suffix were
* added the existing view will be given focus.
*
* @param viewId
* The id of the view to be created. <br>
* Format: primary_id[:secondary_id[&uuid]|:uuid]
* @param generateSuffix
* Add or replace a generated suffix id (UUID). This allows
* multiple views with the same id to be displayed.
* @return The view instance, or null if an error occurred.
*/
@NonNullByDefault
public static @Nullable IViewPart newView(String viewId, boolean generateSuffix) {
IViewPart viewPart = null;
String primaryId = null;
String secondaryId = null;
/* Parse the view id */
int index = viewId.indexOf(TmfView.VIEW_ID_SEPARATOR);
if (index != -1) {
primaryId = viewId.substring(0, index);
secondaryId = getBaseSecId(viewId.substring(index + 1));
} else {
primaryId = viewId;
}
if (generateSuffix) {
if (secondaryId == null) {
secondaryId = UUID.randomUUID().toString();
} else {
secondaryId += INTERNAL_SECONDARY_ID_SEPARATOR + UUID.randomUUID().toString();
}
}
IWorkbench workbench = PlatformUI.getWorkbench();
IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
IWorkbenchPage page = workbenchWindow.getActivePage();
try {
viewPart = page.showView(primaryId, secondaryId, IWorkbenchPage.VIEW_ACTIVATE);
page.activate(viewPart);
} catch (PartInitException e) {
/* Simply return null on error */
}
return viewPart;
}
protected IWorkbench getWorkbench() {
return PlatformUI.getWorkbench();
}
public static IWorkbench getWorkbench() {
return PlatformUI.getWorkbench();
}
public void dispose() {
final IWorkbench workbench = PlatformUI.getWorkbench();
unhookListeners(workbench);
}
public static IWorkbenchWindow[] getWorkbenchWindows() {
IWorkbench workbench = PlatformUI.getWorkbench();
return workbench.getWorkbenchWindows();
}