Skip to content

Instantly share code, notes, and snippets.

@paulweb515
Created November 9, 2012 15:53
Show Gist options
  • Save paulweb515/4046458 to your computer and use it in GitHub Desktop.
Save paulweb515/4046458 to your computer and use it in GitHub Desktop.
/*******************************************************************************
* Copyright (c) 2009, 2012 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.e4.ui.internal.services;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Set;
import org.eclipse.core.commands.contexts.Context;
import org.eclipse.core.commands.contexts.ContextManager;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.ui.services.EContextService;
import org.eclipse.e4.ui.services.IServiceConstants;
public class ContextContextService implements EContextService {
static final String LOCAL_CONTEXTS = "localContexts";
private IEclipseContext eclipseContext;
private ContextManager contextManager;
private boolean deferUpdates = false;
public ContextContextService(IEclipseContext context) {
eclipseContext = context;
contextManager = (ContextManager) context.get(ContextManager.class
.getName());
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.e4.ui.services.EContextService#activateContext(java.lang.
* String)
*/
public void activateContext(String id) {
if (deferUpdates) {
deferActivateContext(id);
return;
}
LinkedList<String> locals = (LinkedList<String>) eclipseContext
.getLocal(LOCAL_CONTEXTS);
if (locals == null) {
locals = new LinkedList<String>();
locals.add(id);
eclipseContext.set(LOCAL_CONTEXTS, locals);
} else {
boolean contained = locals.contains(id);
if (locals.add(id) && !contained) {
// copy the set so the change is propagated
eclipseContext.set(LOCAL_CONTEXTS, locals.clone());
}
}
}
private void deferActivateContext(String id) {
LinkedList<String> locals = (LinkedList<String>) eclipseContext
.getLocal(LOCAL_CONTEXTS+".a");
if (locals == null) {
locals = new LinkedList<String>();
eclipseContext.set(LOCAL_CONTEXTS+ ".a", locals);
}
locals.add(id);
}
private void setEventCaching(boolean cache) {
if (cache) {
deferUpdates = true;
return;
}
LinkedList<String> locals = (LinkedList<String>) eclipseContext
.getLocal(LOCAL_CONTEXTS);
if (locals == null) {
locals = new LinkedList<String>();
}
LinkedList<String> activates = (LinkedList<String>) eclipseContext
.getLocal(LOCAL_CONTEXTS + ".a");
if (activates != null) {
eclipseContext.remove(LOCAL_CONTEXTS + ".a");
}
LinkedList<String> deactivates = (LinkedList<String>) eclipseContext
.getLocal(LOCAL_CONTEXTS + ".d");
if (deactivates != null) {
eclipseContext.remove(LOCAL_CONTEXTS + ".d");
}
for (String id : activates) {
locals.add(id);
}
for (String id : deactivates) {
locals.remove(id);
}
eclipseContext.set(LOCAL_CONTEXTS, locals.clone());
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.e4.ui.services.EContextService#deactivateContext(java.lang
* .String)
*/
public void deactivateContext(String id) {
if (deferUpdates) {
deferDeactivateContext(id);
return;
}
LinkedList<String> locals = (LinkedList<String>) eclipseContext
.getLocal(LOCAL_CONTEXTS);
if (locals != null && locals.remove(id)) {
boolean contained = locals.contains(id);
if (!contained) {
// copy the set so the change is propagated
eclipseContext.set(LOCAL_CONTEXTS, locals.clone());
}
}
}
private void deferDeactivateContext(String id) {
LinkedList<String> locals = (LinkedList<String>) eclipseContext
.getLocal(LOCAL_CONTEXTS+".d");
if (locals == null) {
locals = new LinkedList<String>();
eclipseContext.set(LOCAL_CONTEXTS+ ".d", locals);
}
locals.add(id);
}
/*
* (non-Javadoc)
*
* @see org.eclipse.e4.ui.services.EContextService#getActiveContextIds()
*/
public Collection<String> getActiveContextIds() {
Set<String> set = (Set<String>) eclipseContext
.get(IServiceConstants.ACTIVE_CONTEXTS);
if (set != null) {
contextManager.setActiveContextIds(set);
}
return set;
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.e4.ui.services.EContextService#getContext(java.lang.String)
*/
public Context getContext(String id) {
Context ctx = contextManager.getContext(id);
return ctx;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment