blob: f722a8ffd994958bb014f5688c7d4bac3c8a27a7 [file] [log] [blame]
/**
*
* 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 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* 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 EnterBackwardFocusingStrategy.
*/
public class EnterBackwardFocusingStrategy 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 the first element is reached, we start at the end 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 == 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(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;
}
}