| /** |
| * |
| * 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 TabBackwardFocusingStrategy. |
| */ |
| public class TabBackwardFocusingStrategy 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(yLayout, yCurrentFocus); |
| yView.setCurrentFocus((YFocusable) yNextFocus); |
| } |
| |
| /** |
| * Looks for the next element to be focused. Therefore it uses a round robin approach. If the first element is reached, we start at the end again. |
| * |
| * @param yLayout |
| * the y layout |
| * @param yElement |
| * the y element |
| * @return the y embeddable |
| */ |
| protected YEmbeddable findNextElementToFocus(YStrategyLayout yLayout, YEmbeddable yElement) { |
| if (yElement == null) { |
| return null; |
| } |
| YLayout yParent = (YLayout) yElement.eContainer(); |
| int index = yParent.getElements().indexOf(yElement); |
| if (index == 0) { |
| // element is the first -> Start again |
| index = yParent.getElements().size() - 1; |
| } else { |
| // use the previous element |
| index = index - 1; |
| } |
| |
| YEmbeddable nextFocusElement = yParent.getElements().get(index); |
| if (nextFocusElement instanceof YEditable) { |
| if (((YEditable) nextFocusElement).isEditable()) { |
| return nextFocusElement; |
| } else { |
| return findNextElementToFocus(yLayout, nextFocusElement); |
| } |
| } |
| return nextFocusElement; |
| |
| // YSuspectInfo info = yLayout.findInfoFor(yElement); |
| // YSuspectInfo previous = info != null ? info.getPreviousFocus() : |
| // null; |
| // YEmbeddable target = previous != null ? previous.getTarget() : null; |
| // |
| // // use the last element |
| // if (target == null && yLayout.getLayoutingInfo() != null |
| // && yLayout.findLastFocus() != null) { |
| // target = yLayout.findLastFocus().getTarget(); |
| // } |
| // |
| // return target; |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see org.eclipse.osbp.ecview.extension.api.IFocusingStrategy#getKeyStrokeDefinition() |
| */ |
| @Override |
| public KeyStrokeDefinition getKeyStrokeDefinition() { |
| KeyStrokeDefinition def = new KeyStrokeDefinition("", KeyCode.TAB, new int[0]); |
| return def; |
| } |
| |
| } |