下面列出了怎么用org.eclipse.ui.contexts.IContextService的API类实例代码及写法,或者点击链接到github查看源代码。
private void handleStateChange() {
boolean isActive = false;
for (IPyContextObserver obs : this.observers.getListeners()) {
if (obs.isPyContextActive()) {
isActive = true;
break;
}
}
IContextService contextService = (IContextService) PlatformUI.getWorkbench().getService(IContextService.class);
//May be null on shutdown on Eclipse 4.
if (contextService != null) {
if (isActive) {
if (activateContext == null) {
activateContext = contextService.activateContext("com.python.pydev.contexts.window");
}
} else {
if (activateContext != null) {
contextService.deactivateContext(activateContext);
}
activateContext = null;
}
}
}
@Override
protected void createControls(Composite composite) {
super.createControls(composite);
ICompareContainer container = getCompareConfiguration().getContainer();
IWorkbenchPart workbenchPart= container.getWorkbenchPart();
if (workbenchPart != null) {
IContextService service= workbenchPart.getSite().getService(IContextService.class);
if (service != null) {
service.activateContext(EditorSettings_Actual.EDITOR_CONTEXT_ID);
}
}
// TODO: activate Lang editor commands
// IHandlerService handlerSvc = container.getServiceLocator().getService(IHandlerService.class);
// handlerSvc.activateHandler(EditorCommandIds.OpenDef_ID, new OpenDefinitionHandler2());
}
/**
* Activate a context that this view uses. It will be tied to this view activation events and will be removed when
* the view is disposed.
*
*/
private void activateContext() {
IContextService contextService = getSite().getService(IContextService.class);
// this will get cleaned up automatically when the site
// is disposed
contextService.activateContext(VIEW_CONTEXT_ID);
}
private void initN4Context() {
if (isWorkbenchRunning()) {
final IWorkbench workbench = getWorkbench();
workbench.getDisplay().asyncExec(() -> {
final IContextService service = workbench.getService(IContextService.class);
service.activateContext(N4_CONTEXT_ID);
});
}
}
private void deactivateContext() {
if (contextActivation != null) {
IContextService contextService = getService(IContextService.class);
if (contextService != null) {
contextService.deactivateContext(contextActivation);
}
contextActivation = null;
}
}
/**
* Cancel overriding of standard 'Check for Updates' handler.
*/
public static void deactivateXdsUpdateContext() {
if (activateContext != null) {
IContextService contextService = (IContextService) PlatformUI.getWorkbench().getService(IContextService.class);
contextService.deactivateContext(activateContext);
activateContext = null;
}
}
@Override
protected void createControls(Composite composite) {
super.createControls(composite);
IWorkbenchPart workbenchPart = getCompareConfiguration().getContainer().getWorkbenchPart();
if (workbenchPart != null) {
IContextService service = workbenchPart.getSite().getService(IContextService.class);
if (service != null) {
service.activateContext("ts.eclipse.ide.jsdt.ui.typeScriptEditorScope"); //$NON-NLS-1$
}
}
}
/**
* Sets view part.
*
* @param viewSite The view part to set
*/
public void setSite(ViewPart viewSite) {
fSite = viewSite;
fSite.getSite().setSelectionProvider(fSelProvider);
Object serviceObject = fSite.getSite().getWorkbenchWindow().getService(IContextService.class);
IContextService service = (IContextService) serviceObject;
service.activateContext("org.eclipse.linuxtools.tmf.ui.view.uml2sd.context"); //$NON-NLS-1$
service.activateContext(IContextIds.CONTEXT_ID_WINDOW);
}
private void deactivateContextService() {
IContextService contextService = fContextService;
if (contextService != null) {
contextService.deactivateContexts(fActiveContexts);
fActiveContexts.clear();
}
}
@Override
protected void createControls(Composite composite) {
super.createControls(composite);
IWorkbenchPart workbenchPart = getCompareConfiguration().getContainer().getWorkbenchPart();
if (workbenchPart != null) {
IContextService service = (IContextService)workbenchPart.getSite().getService(IContextService.class);
if (service != null) {
service.activateContext("org.eclipse.jdt.ui.javaEditorScope"); //$NON-NLS-1$
}
}
}
@Override
public void createPartControl(Composite parent) {
// Open the context
final IContextService contextService = getSite().getService(IContextService.class);
contextService.activateContext(CONTEXT_ID);
// Overridden for setting the label provider, which is set in a private function in the super type.
super.createPartControl(parent);
internalResetLabelProvider();
restoreFilterAndSorter();
}
public void earlyStartup() {
final IWorkbench workbench = PlatformUI.getWorkbench();
workbench.getDisplay().asyncExec(new Runnable() {
@Override
public void run() {
IContextService contextService = (IContextService) workbench.getService(IContextService.class);
if(contextService != null){
contextService.activateContext("net.heartsome.cat.te.context");
}
}
});
}
private void activateDesignerEditPart( )
{
IContextService contextService = (IContextService) PlatformUI.getWorkbench( )
.getService( IContextService.class );
if ( contextActivation == null )
contextActivation = contextService.activateContext( VIEW_CONTEXT_ID );
}
private void deActivateDesignerEditPart( )
{
if ( !UIUtil.isReportEditorActivated( ) )
{
IContextService contextService = (IContextService) PlatformUI.getWorkbench( )
.getService( IContextService.class );
if ( contextActivation != null )
{
contextService.deactivateContext( contextActivation );
contextActivation = null;
}
}
}
/**
* Registers the shell as the same type as its parent with the context
* support. This ensures that it does not modify the current state of the
* application.
*/
private final void registerShellType() {
final Shell shell = getShell();
final IContextService contextService = (IContextService) editor.getEditorSite().getWorkbenchWindow().getWorkbench().getService(IContextService.class);
contextService.registerShell(shell, contextService.getShellType((Shell) shell.getParent()));
}
/**
* @see org.eclipse.ui.console.IConsolePageParticipant#deactivated()
*/
public void deactivated() {
if (fContextActivation != null){
IContextService contextService = (IContextService) PlatformUI.getWorkbench().getAdapter(IContextService.class);
contextService.deactivateContext(fContextActivation);
fContextActivation = null;
((EmacsPlusConsole)console).offLine();
}
}
public void getContexts(ITextEditor editor) {
IContextService contextService = (IContextService) editor.getSite().getService(IContextService.class);
@SuppressWarnings("unchecked") // Eclipse documents the collection type
Collection<String> col = contextService.getActiveContextIds();
Iterator<String> it = col.iterator();
while (it.hasNext()) {
System.out.println("context: " + it.next()); //$NON-NLS-1$
}
}
private void clearContext() {
if (this.repeatc != null) {
((IContextService)PlatformUI.getWorkbench().getService(IContextService.class)).deactivateContext(repeatc);
this.repeatc = null;
}
}
/**
* Registers the shell as the same type as its parent with the context
* support. This ensures that it does not modify the current state of the
* application.
*/
private final void registerShellType() {
final Shell shell = getShell();
final IContextService contextService = (IContextService) EditorUtils.getActiveWorkbenchWindow().getService(
IContextService.class);
contextService.registerShell(shell, contextService.getShellType((Shell) shell.getParent()));
}
@Override
public void initialize(final IWorkbenchConfigurer configurer) {
super.initialize(configurer);
configurer.setSaveAndRestore(true);
final IContextService contextService = PlatformUI.getWorkbench().getService(IContextService.class);
contextService.activateContext("org.bonitasoft.studio.context.id");
initializeIDEImages(configurer);
}
private void activateContext() {
IContextService contextService = getService(IContextService.class);
if (contextService != null) {
contextActivation = contextService.activateContext(CONTEXT_ID);
}
}
/**
* Override standard 'Check for Updates' handler.
*/
public static void activateXdsUpdateContext() {
IContextService contextService = (IContextService) PlatformUI.getWorkbench().getService(IContextService.class);
activateContext = contextService.activateContext( XDS_UPDATE_CONTEXT );
}
private void activateContextService() {
IContextService contextService = fContextService;
if (fActiveContexts.isEmpty() && contextService != null) {
fActiveContexts.add(Objects.requireNonNull(contextService.activateContext(TMF_VIEW_UI_CONTEXT)));
}
}
@Override
public void init(IEditorSite site, IEditorInput input) throws PartInitException {
super.init(site, input);
IContextService contextService = (IContextService) site.getService(IContextService.class);
contextService.activateContext(CONTEXT);
}
private void initBindings()
{
WorkbenchJob workbenchJob = new WorkbenchJob("Installing KeybindingsManager") //$NON-NLS-1$
{
@Override
public IStatus runInUIThread(IProgressMonitor monitor)
{
loadbindings();
// Insert our key listener before the Eclipse's key listeners
IBindingService bindingService = (IBindingService) workbench.getService(IBindingService.class);
if (bindingService instanceof BindingService)
{
final BindingService theBindingService = (BindingService) bindingService;
Display display = PlatformUI.getWorkbench().getDisplay();
final WorkbenchKeyboard keyboard = theBindingService.getKeyboard();
Listener keyDownFilter = keyboard.getKeyDownFilter();
try
{
if (keyDownFilter != null)
{
display.removeFilter(SWT.KeyDown, keyDownFilter);
display.removeFilter(SWT.Traverse, keyDownFilter);
}
display.addFilter(SWT.KeyDown, listener);
display.addFilter(SWT.Traverse, listener);
}
finally
{
if (keyDownFilter != null)
{
display.addFilter(SWT.KeyDown, keyDownFilter);
display.addFilter(SWT.Traverse, keyDownFilter);
}
}
}
// Set the initial enabled state of KeybindingsManager
IContextService contextService = (IContextService) workbench.getService(IContextService.class);
contextService.addContextManagerListener(contextManagerListener);
setEnabled(contextService.getActiveContextIds().contains(ScriptingUIPlugin.SCRIPTING_CONTEXT_ID));
return Status.OK_STATUS;
}
};
workbenchJob.setRule(MUTEX_RULE);
EclipseUtil.setSystemForJob(workbenchJob);
workbenchJob.setPriority(Job.LONG);
workbenchJob.schedule();
}
private boolean processKeyStroke(Event event, KeyStroke keyStroke)
{
IBindingService bindingService = (IBindingService) workbench.getService(IBindingService.class);
KeySequence sequenceBeforeKeyStroke = state.getCurrentSequence();
KeySequence sequenceAfterKeyStroke = KeySequence.getInstance(sequenceBeforeKeyStroke, keyStroke);
if (uniqueKeySequences.contains(sequenceAfterKeyStroke))
{
IEvaluationService evaluationService = (IEvaluationService) workbench.getService(IEvaluationService.class);
IEvaluationContext evaluationContext = evaluationService.getCurrentState();
IWorkbenchPart workbenchPart = (IWorkbenchPart) evaluationContext.getVariable(ISources.ACTIVE_PART_NAME);
ICommandElementsProvider commandElementsProvider = (ICommandElementsProvider) workbenchPart
.getAdapter(ICommandElementsProvider.class);
if (commandElementsProvider != null)
{
// Is there a Eclipse binding that matches the key sequence?
Binding binding = null;
if (bindingService.isPerfectMatch(sequenceAfterKeyStroke))
{
// Record it
binding = bindingService.getPerfectMatch(sequenceAfterKeyStroke);
}
List<CommandElement> commandElements = commandElementsProvider
.getCommandElements(sequenceAfterKeyStroke);
if (commandElements.size() == 0)
{
if (binding == null)
{
// Remember the prefix
incrementState(sequenceAfterKeyStroke);
}
else
{
// Reset our state
resetState();
}
// Do not consume the event. Let Eclipse handle it.
return false;
}
else
{
if (binding == null && commandElements.size() == 1)
{
// We have a unique scripting command to execute
executeCommandElement(commandElementsProvider, commandElements.get(0));
// Reset our state
resetState();
// The event should be consumed
return true;
}
else
{
// We need to show commands menu to the user
IContextService contextService = (IContextService) workbench.getService(IContextService.class);
popup(workbenchPart.getSite().getShell(), bindingService, contextService,
commandElementsProvider, commandElements, event, binding,
getInitialLocation(commandElementsProvider));
// Reset our state
resetState();
// The event should be consumed
return true;
}
}
}
}
else if (uniqueKeySequencesPrefixes.contains(sequenceAfterKeyStroke))
{
// Prefix match
// Is there a Eclipse command with a perfect match
if (bindingService.isPerfectMatch(sequenceAfterKeyStroke))
{
// Reset our state
resetState();
}
else
{
// Remember the prefix
incrementState(sequenceAfterKeyStroke);
}
}
else
{
// Reset our state
resetState();
}
// We did not handle the event. Do not consume the event. Let Eclipse handle it.
return false;
}
/**
* @see org.eclipse.ui.console.IConsolePageParticipant#activated()
*/
public void activated() {
IContextService contextService = (IContextService) PlatformUI.getWorkbench().getAdapter(IContextService.class);
fContextActivation = contextService.activateContext("org.eclipse.ui.textEditorScope"); //$NON-NLS-1$
((EmacsPlusConsole)console).onLine();
}
private void activateContext() {
if (this.repeatc == null) {
this.repeatc = ((IContextService)PlatformUI.getWorkbench().getService(IContextService.class)).activateContext(RCONTEXT);
}
}
/**
* @param contextId defines the keys context we'll work with...
*
* We'll only remove/add bindings to this context.
*/
public BindKeysHelper(String contextId) {
Assert.isNotNull(contextId);
this.contextId = contextId;
// Set the context we're working with.
Set<String> activeContextIds = new HashSet<>();
activeContextIds.add(contextId);
contextManager.setActiveContextIds(activeContextIds);
// Check that the context we're working with actually exists
IWorkbench workbench = PlatformUI.getWorkbench();
bindingService = (IBindingService) workbench.getService(IBindingService.class);
IContextService contextService = (IContextService) workbench.getService(IContextService.class);
Context context = contextService.getContext(contextId);
if (context == null || context.isDefined() == false) {
throw new RuntimeException("The context: " + contextId + " does not exist.");
}
Scheme activeScheme = bindingService.getActiveScheme();
final Scheme[] definedSchemes = bindingService.getDefinedSchemes();
// Make a copy we can work with locally (we'll apply changes later based on this copy).
try {
for (int i = 0; i < definedSchemes.length; i++) {
final Scheme scheme = definedSchemes[i];
final Scheme copy = localChangeManager.getScheme(scheme.getId());
copy.define(scheme.getName(), scheme.getDescription(), scheme.getParentId());
}
localChangeManager.setActiveScheme(activeScheme);
} catch (final NotDefinedException e) {
throw new Error("There is a programmer error in the bind keys helper"); //$NON-NLS-1$
}
localChangeManager.setLocale(bindingService.getLocale());
localChangeManager.setPlatform(bindingService.getPlatform());
Binding[] bindings = bindingService.getBindings();
for (Binding binding : bindings) {
initialState.add(binding);
}
localChangeManager.setBindings(bindings);
}