| /** |
| * |
| * Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * |
| * 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: |
| * Florian Pirchner - Initial implementation |
| * |
| */ |
| package org.eclipse.osbp.ecview.extension.strategy; |
| |
| import java.util.Map; |
| |
| import org.eclipse.osbp.ecview.core.common.context.IViewContext; |
| import org.eclipse.osbp.ecview.core.common.model.core.YEditable; |
| import org.eclipse.osbp.ecview.core.common.model.core.YElement; |
| import org.eclipse.osbp.ecview.core.common.model.core.YEmbeddable; |
| import org.eclipse.osbp.ecview.core.common.model.core.YFocusable; |
| import org.eclipse.osbp.ecview.core.common.model.core.YLayout; |
| import org.eclipse.osbp.ecview.core.common.model.core.YView; |
| import org.eclipse.osbp.ecview.core.common.services.IWidgetAssocationsService; |
| import org.eclipse.osbp.ecview.core.util.emf.ModelUtil; |
| import org.eclipse.osbp.ecview.extension.api.IFocusingStrategy; |
| import org.eclipse.osbp.ecview.extension.model.YStrategyLayout; |
| import org.eclipse.osbp.runtime.common.keystroke.KeyStrokeDefinition; |
| |
| import com.vaadin.event.ShortcutAction.KeyCode; |
| |
| /** |
| * The Class EnterForwardFocusingStrategy. |
| */ |
| public class EnterForwardFocusingStrategy implements IFocusingStrategy { |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see org.eclipse.osbp.ecview.extension.api.IFocusingStrategy#focus(java.lang.Object, java.lang.Object, org.eclipse.osbp.ecview.extension.model.YStrategyLayout) |
| */ |
| @Override |
| public void focus(Object source, Object target, YStrategyLayout yLayout) { |
| YView yView = yLayout.getView(); |
| IViewContext context = ModelUtil.getViewContext(yView); |
| IWidgetAssocationsService<Object, ? extends YElement> service = context.getService(IWidgetAssocationsService.ID); |
| |
| YEmbeddable yCurrentFocus = (YEmbeddable) service.getModelElement(target); |
| YEmbeddable yNextFocus = findNextElementToFocus(yCurrentFocus); |
| yView.setCurrentFocus((YFocusable) yNextFocus); |
| } |
| |
| /** |
| * Looks for the next element to be focused. Therefore it uses a round robin approach. If end of elements are reached, we start by index = 0 again. |
| * |
| * @param yElement |
| * the y element |
| * @return the y embeddable |
| */ |
| protected YEmbeddable findNextElementToFocus(YEmbeddable yElement) { |
| if (yElement == null) { |
| return null; |
| } |
| YLayout yParent = (YLayout) yElement.eContainer(); |
| int index = yParent.getElements().indexOf(yElement); |
| if (index == yParent.getElements().size() - 1) { |
| // element is the last -> Start again |
| index = 0; |
| } else { |
| // use the next element |
| index = index + 1; |
| } |
| |
| YEmbeddable nextFocusElement = yParent.getElements().get(index); |
| if (nextFocusElement instanceof YEditable) { |
| if (((YEditable) nextFocusElement).isEditable()) { |
| return nextFocusElement; |
| } else { |
| return findNextElementToFocus(nextFocusElement); |
| } |
| } |
| return nextFocusElement; |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see org.eclipse.osbp.ecview.extension.api.IFocusingStrategy#getKeyStrokeDefinition() |
| */ |
| @Override |
| public KeyStrokeDefinition getKeyStrokeDefinition() { |
| KeyStrokeDefinition def = new KeyStrokeDefinition("", KeyCode.ENTER, new int[0]); |
| return def; |
| } |
| |
| } |