Search in sources :

Example 1 with ContextWrapper

use of eu.esdihumboldt.hale.ui.common.help.internal.ContextWrapper in project hale by halestudio.

the class SelectionContextProvider method getContext.

/**
 * @see IContextProvider#getContext(Object)
 */
@Override
public IContext getContext(Object target) {
    // provide a context based on the selection
    ISelection selection = selectionProvider.getSelection();
    IContext defaultContext = null;
    if (defaultContextId != null) {
        defaultContext = HelpSystem.getContext(defaultContextId);
    }
    List<IContext> contexts = new ArrayList<IContext>();
    if (selection instanceof IStructuredSelection) {
        for (Object object : ((IStructuredSelection) selection).toList()) {
            IContext context = getSelectionContext(object);
            if (context != null) {
                contexts.add(context);
            }
        }
    }
    if (contexts.size() == 1) {
        if (defaultContext == null) {
            return contexts.get(0);
        } else {
            // create context enhanced with default topics
            return new ContextWrapper(contexts.get(0), Arrays.asList(defaultContext.getRelatedTopics()));
        }
    } else if (!contexts.isEmpty()) {
        LinkedHashSet<IHelpResource> topics = new LinkedHashSet<IHelpResource>();
        Set<String> hrefs = new HashSet<String>();
        // collect topics
        for (IContext context : contexts) {
            for (IHelpResource topic : context.getRelatedTopics()) {
                if (!hrefs.contains(topic.getHref())) {
                    // ensure that the
                    // same topic is
                    // only added once
                    topics.add(topic);
                    hrefs.add(topic.getHref());
                }
            }
        }
        if (!topics.isEmpty()) {
            if (defaultContext == null) {
                return new ContextImpl(// XXX
                "Multiple selected objects, see below for related topics.", // improve?!
                topics.toArray(new IHelpResource[topics.size()]));
            } else {
                // create a context enhanced with the selection topics
                return new ContextWrapper(defaultContext, topics);
            }
        }
    }
    // by default, get the view context
    return defaultContext;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) IContext(org.eclipse.help.IContext) Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) ContextImpl(eu.esdihumboldt.hale.ui.common.help.internal.ContextImpl) ISelection(org.eclipse.jface.viewers.ISelection) IHelpResource(org.eclipse.help.IHelpResource) ContextWrapper(eu.esdihumboldt.hale.ui.common.help.internal.ContextWrapper)

Aggregations

ContextImpl (eu.esdihumboldt.hale.ui.common.help.internal.ContextImpl)1 ContextWrapper (eu.esdihumboldt.hale.ui.common.help.internal.ContextWrapper)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Set (java.util.Set)1 IContext (org.eclipse.help.IContext)1 IHelpResource (org.eclipse.help.IHelpResource)1 ISelection (org.eclipse.jface.viewers.ISelection)1 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)1