| package org.eclipse.osbp.runtime.common.event; |
| |
| import java.util.HashMap; |
| import java.util.Map; |
| |
| import org.eclipse.e4.core.contexts.IEclipseContext; |
| import org.eclipse.e4.ui.model.application.ui.MUIElement; |
| import org.eclipse.e4.ui.model.application.ui.advanced.MPerspective; |
| import org.eclipse.e4.ui.model.application.ui.basic.MPart; |
| |
| public class SelectionStore { |
| private Map<String, Object> selections = new HashMap<>(); |
| |
| public void setSelection(String fqn, Object id) { |
| if(id != null) { |
| selections.put(fqn, id); |
| } else { |
| selections.remove(fqn); |
| } |
| } |
| |
| public Object getSelection(String fqn) { |
| if(selections.containsKey(fqn)) { |
| return selections.get(fqn); |
| } |
| return null; |
| } |
| |
| /** |
| * Store a selection by id to perspective context. |
| * |
| * @param key the fully qualified name of the id field |
| * @param value the id object |
| * @return true, if successful |
| */ |
| public static boolean putSelectionToPerspectiveContext(MPart part, String key, Object value) { |
| MUIElement element = part; |
| while(element != null && !(element instanceof MPerspective)) { |
| element = element.getParent(); |
| } |
| if(element != null) { |
| IEclipseContext context = ((MPerspective)element).getContext(); |
| if(context.get(SelectionStore.class) == null) { |
| context.set(SelectionStore.class, new SelectionStore()); |
| } |
| context.get(SelectionStore.class).setSelection(key, value); |
| return true; |
| } |
| return false; |
| } |
| |
| /** |
| * Gets a selection by id from perspective context. |
| * |
| * @param key the fully qualified name of the id field |
| * @return the selected id object from perspective context |
| */ |
| public static Object getSelectionFromPerspectiveContext(MPart part, String key) { |
| MUIElement element = part; |
| while(element != null && !(element instanceof MPerspective)) { |
| element = element.getParent(); |
| } |
| if(element != null) { |
| IEclipseContext context = ((MPerspective)element).getContext(); |
| if(context.get(SelectionStore.class) != null) { |
| return context.get(SelectionStore.class).getSelection(key); |
| } |
| } |
| return null; |
| } |
| } |