Java Code Examples for org.eclipse.ui.IEditorSite#getService()

The following examples show how to use org.eclipse.ui.IEditorSite#getService() . 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: ServiceUtils.java    From google-cloud-eclipse with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an OSGi service from {@link ExecutionEvent}. It looks up a service in the following
 * locations (if exist) in the given order:
 *
 * {@code HandlerUtil.getActiveSite(event)}
 * {@code HandlerUtil.getActiveEditor(event).getEditorSite()}
 * {@code HandlerUtil.getActiveEditor(event).getSite()}
 * {@code HandlerUtil.getActiveWorkbenchWindow(event)}
 * {@code PlatformUI.getWorkbench()}
 */
public static <T> T getService(ExecutionEvent event, Class<T> api) {
  IWorkbenchSite activeSite = HandlerUtil.getActiveSite(event);
  if (activeSite != null) {
    return activeSite.getService(api);
  }

  IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
  if (activeEditor != null) {
    IEditorSite editorSite = activeEditor.getEditorSite();
    if (editorSite != null) {
      return editorSite.getService(api);
    }
    IWorkbenchPartSite site = activeEditor.getSite();
    if (site != null) {
      return site.getService(api);
    }
  }

  IWorkbenchWindow workbenchWindow = HandlerUtil.getActiveWorkbenchWindow(event);
  if (workbenchWindow != null) {
    return workbenchWindow.getService(api);
  }

  return PlatformUI.getWorkbench().getService(api);
}
 
Example 2
Source File: JsonEditor.java    From KaiZen-OpenAPI-Editor with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void init(IEditorSite site, IEditorInput input) throws PartInitException {
    super.init(site, input);
    IContextService contextService = (IContextService) site.getService(IContextService.class);
    contextService.activateContext(CONTEXT);
}
 
Example 3
Source File: ICEFormEditor.java    From ice with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * This operation overrides init so that the ICE Form, passed as an
 * IEditorInput, can be stored.
 * 
 * @param site
 *            the site on the workbench where the Form is drawn
 * @param input
 *            the input for this editor
 */
@Override
public void init(IEditorSite site, IEditorInput input)
		throws RuntimeException {

	// Get the E4 Context. This is how you get into the E4 application model
	// if you are running from a 3.x part and don't have your own
	// application model. See bugs.eclipse.org/bugs/show_bug.cgi?id=376486
	// and chapter 101 of Lar Vogel's e4 book.
	e4Context = site.getService(IEclipseContext.class);

	// Instruct the framework to perform dependency injection for
	// this Form using the ContextInjectionFactory.
	ContextInjectionFactory.inject(this, e4Context);

	// Get the Client Reference
	IClient client = null;
	try {
		client = IClient.getClient();
	} catch (CoreException e1) {
		e1.printStackTrace();
	}

	// Set the site
	setSite(site);

	// Grab the form from the input or the client depending on the type of
	// the input. This should only be a temporary switch until we remove the
	// ICEFormInput and redirect the way the client works.
	if (input instanceof ICEFormInput) {
		ICEFormInput = (ICEFormInput) input;
		iceDataForm = ICEFormInput.getForm();

		// Set the part name to be the file name
		setPartName(iceDataForm.getName() + ".xml");

		// Set the input
		setInput(input);
	} else if (input instanceof FileEditorInput && client != null) {
		// Grab the file and load the form
		IFile formFile = ((FileEditorInput) input).getFile();
		// try {
		// IClient client = IClient.getClient();
		iceDataForm = client.loadItem(formFile);
		logger.info("IClient and Form loaded.");
		// Set *correct* input via a little short circuit.
		ICEFormInput = new ICEFormInput(iceDataForm);
		setInput(ICEFormInput);

		// Set the IFormWidget on the IClient
		client.addFormWidget(new EclipseFormWidget(this));

		// Set the part name to be the file name
		setPartName(input.getName());

		// Register the client as a listener
		// of specific form editor events.
		try {
			registerUpdateListener(
					IUpdateEventListener.getUpdateEventListener());
			registerProcessListener(
					IProcessEventListener.getProcessEventListener());
			registerResourceProvider(
					ISimpleResourceProvider.getSimpleResourceProvider());
		} catch (CoreException e) {
			// Complain
			logger.error(
					"Unable to get register the update, process, or simpleresource implementations!",
					e);
		}

	} else {
		// Throw errors if the type is wrong
		logger.error("Unable to load Form Editor!");
		throw new RuntimeException("Input passed to ICEFormEditor.init()"
				+ " is not of type ICEFormInput or FileEditorInput, or the IClient instance is null.");
	}

	// Get the Item Name for the Form Header.
	for (Identifiable i : client.getItems()) {
		if (iceDataForm.getItemID() == i.getId()) {
			itemName = i.getClass().getSimpleName() + " Item " + i.getId();
			break;
		}
	}

	// Register this ICEFormEditor with the provided Form
	iceDataForm.register(this);

	return;
}