blob: 4d0a00e43d81ba00b1d0b11f87d1e200448d41e0 [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:
* Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
*
*
* This copyright notice shows up in the generated Java code
*/
package org.eclipse.osbp.xtext.strategy.jvmmodel;
import com.google.common.base.Objects;
import javax.inject.Inject;
import org.eclipse.emf.common.util.EList;
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.services.IWidgetAssocationsService;
import org.eclipse.osbp.ecview.extension.api.IFocusingStrategy;
import org.eclipse.osbp.ecview.extension.api.IFocusingStrategyProvider;
import org.eclipse.osbp.ecview.extension.model.YStrategyLayout;
import org.eclipse.osbp.runtime.common.keystroke.KeyStrokeDefinition;
import org.eclipse.osbp.utils.annotation.CommonUtils;
import org.eclipse.osbp.xtext.strategy.FocusingEnum;
import org.eclipse.osbp.xtext.strategy.FocusingStrategy;
import org.eclipse.osbp.xtext.strategy.StrategyPackage;
import org.eclipse.osbp.xtext.strategy.constants.StrategyConstants;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.common.types.JvmAnnotationReference;
import org.eclipse.xtext.common.types.JvmField;
import org.eclipse.xtext.common.types.JvmFormalParameter;
import org.eclipse.xtext.common.types.JvmGenericType;
import org.eclipse.xtext.common.types.JvmMember;
import org.eclipse.xtext.common.types.JvmOperation;
import org.eclipse.xtext.common.types.JvmVisibility;
import org.eclipse.xtext.common.types.util.TypeReferences;
import org.eclipse.xtext.xbase.compiler.output.ITreeAppendable;
import org.eclipse.xtext.xbase.jvmmodel.IJvmDeclaredTypeAcceptor;
import org.eclipse.xtext.xbase.jvmmodel.JvmAnnotationReferenceBuilder;
import org.eclipse.xtext.xbase.jvmmodel.JvmTypeReferenceBuilder;
import org.eclipse.xtext.xbase.jvmmodel.JvmTypesBuilder;
import org.eclipse.xtext.xbase.lib.Conversions;
import org.eclipse.xtext.xbase.lib.Extension;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.osgi.service.component.annotations.Component;
@SuppressWarnings("all")
public class StrategyDSLFocusingStrategyCreator {
@Inject
@Extension
private JvmTypesBuilder _jvmTypesBuilder;
@Inject
@Extension
private StrategyConstants _strategyConstants;
@Inject
@Extension
private CommonUtils _commonUtils;
@Inject
private TypeReferences references;
private JvmAnnotationReferenceBuilder _annotationTypesBuilder;
private JvmTypeReferenceBuilder _typeReferenceBuilder;
public void createFocusingStrategy(final StrategyPackage pckg, final FocusingStrategy focusingStrategy, final IJvmDeclaredTypeAcceptor acceptor, final JvmAnnotationReferenceBuilder annotationTypesBuilder, final JvmTypeReferenceBuilder typeReferenceBuilder) {
this._annotationTypesBuilder = annotationTypesBuilder;
this._typeReferenceBuilder = typeReferenceBuilder;
StringConcatenation _builder = new StringConcatenation();
String _name = pckg.getName();
_builder.append(_name);
_builder.append(".");
CharSequence _strategyClassName = this.getStrategyClassName(focusingStrategy);
_builder.append(_strategyClassName);
JvmGenericType clsStrategy = this._jvmTypesBuilder.toClass(focusingStrategy, _builder.toString());
clsStrategy.getSuperTypes().add(this._typeReferenceBuilder.typeRef(IFocusingStrategy.class));
final Procedure1<JvmGenericType> _function = (JvmGenericType it) -> {
this.toFields(it, focusingStrategy);
this.toOperations(it, focusingStrategy);
};
acceptor.<JvmGenericType>accept(clsStrategy, _function);
StringConcatenation _builder_1 = new StringConcatenation();
String _name_1 = pckg.getName();
_builder_1.append(_name_1);
_builder_1.append(".");
CharSequence _strategyProviderClassName = this.getStrategyProviderClassName(focusingStrategy);
_builder_1.append(_strategyProviderClassName);
JvmGenericType clsProvider = this._jvmTypesBuilder.toClass(focusingStrategy, _builder_1.toString());
clsProvider.getSuperTypes().add(this._typeReferenceBuilder.typeRef(IFocusingStrategyProvider.class));
final Procedure1<JvmGenericType> _function_1 = (JvmGenericType it) -> {
JvmAnnotationReference annotationRef = this._annotationTypesBuilder.annotationRef(Component.class);
this._commonUtils.addAnnAttr(annotationRef, focusingStrategy, "immediate", Boolean.valueOf(true));
this._commonUtils.addAnnAttr(annotationRef, focusingStrategy, "service", this._typeReferenceBuilder.typeRef(IFocusingStrategyProvider.class));
StringConcatenation _builder_2 = new StringConcatenation();
_builder_2.append("ecview.focusing.id=");
String _ecviewFocusingId = focusingStrategy.getEcviewFocusingId();
_builder_2.append(_ecviewFocusingId);
String propContent = _builder_2.toString();
this._commonUtils.addAnnAttr(annotationRef, focusingStrategy, "property", propContent);
EList<JvmAnnotationReference> _annotations = it.getAnnotations();
this._jvmTypesBuilder.<JvmAnnotationReference>operator_add(_annotations, annotationRef);
this.toProviderFields(it, focusingStrategy);
this.toProviderOperations(it, focusingStrategy);
};
acceptor.<JvmGenericType>accept(clsProvider, _function_1);
}
public void toFields(final JvmGenericType type, final FocusingStrategy focusingStrategy) {
JvmField field = null;
StringConcatenation _builder = new StringConcatenation();
_builder.append("down");
field = this._jvmTypesBuilder.toField(focusingStrategy, _builder.toString(), this._typeReferenceBuilder.typeRef(boolean.class));
field.setVisibility(JvmVisibility.PRIVATE);
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("Now fix, but it has to come from the StrategyDSL");
this._jvmTypesBuilder.setDocumentation(field, _builder_1.toString());
final Procedure1<ITreeAppendable> _function = (ITreeAppendable it) -> {
StringConcatenation _builder_2 = new StringConcatenation();
_builder_2.append("false");
it.append(_builder_2);
};
this._jvmTypesBuilder.setInitializer(field, _function);
EList<JvmMember> _members = type.getMembers();
this._jvmTypesBuilder.<JvmField>operator_add(_members, field);
}
public void toOperations(final JvmGenericType type, final FocusingStrategy focusingStrategy) {
EList<JvmMember> _members = type.getMembers();
final Procedure1<JvmOperation> _function = (JvmOperation it) -> {
EList<JvmAnnotationReference> _annotations = it.getAnnotations();
JvmAnnotationReference _annotationRef = this._annotationTypesBuilder.annotationRef(Override.class);
this._jvmTypesBuilder.<JvmAnnotationReference>operator_add(_annotations, _annotationRef);
it.setVisibility(JvmVisibility.PUBLIC);
EList<JvmFormalParameter> _parameters = it.getParameters();
JvmFormalParameter _parameter = this._jvmTypesBuilder.toParameter(focusingStrategy, "source", this._typeReferenceBuilder.typeRef(Object.class));
this._jvmTypesBuilder.<JvmFormalParameter>operator_add(_parameters, _parameter);
EList<JvmFormalParameter> _parameters_1 = it.getParameters();
JvmFormalParameter _parameter_1 = this._jvmTypesBuilder.toParameter(focusingStrategy, "target", this._typeReferenceBuilder.typeRef(Object.class));
this._jvmTypesBuilder.<JvmFormalParameter>operator_add(_parameters_1, _parameter_1);
EList<JvmFormalParameter> _parameters_2 = it.getParameters();
JvmFormalParameter _parameter_2 = this._jvmTypesBuilder.toParameter(focusingStrategy, "yLayout", this._typeReferenceBuilder.typeRef(YStrategyLayout.class));
this._jvmTypesBuilder.<JvmFormalParameter>operator_add(_parameters_2, _parameter_2);
final Procedure1<ITreeAppendable> _function_1 = (ITreeAppendable it_1) -> {
StringConcatenation _builder = new StringConcatenation();
_builder.append("YView yView = yLayout.getView();");
_builder.newLine();
_builder.append("IViewContext context = ModelUtil.getViewContext(yView);");
_builder.newLine();
_builder.append("IWidgetAssocationsService<Object, ? extends YElement> service = context.getService(IWidgetAssocationsService.ID);");
_builder.newLine();
_builder.append("YEmbeddable yCurrentFocus = (YEmbeddable) service.getModelElement(target);");
_builder.newLine();
_builder.append("// YEmbeddable yNextFocus = findElementToFocus(yCurrentFocus, yLayout);");
_builder.newLine();
_builder.append("YEmbeddable yNextFocus = findElementToFocus(yCurrentFocus, service);");
_builder.newLine();
_builder.append("yView.setCurrentFocus((YFocusable) yNextFocus);");
_builder.newLine();
it_1.append(_builder);
};
this._jvmTypesBuilder.setBody(it, _function_1);
};
JvmOperation _method = this._jvmTypesBuilder.toMethod(focusingStrategy, "focus", this._typeReferenceBuilder.typeRef(Void.TYPE), _function);
this._jvmTypesBuilder.<JvmOperation>operator_add(_members, _method);
EList<JvmMember> _members_1 = type.getMembers();
final Procedure1<JvmOperation> _function_1 = (JvmOperation it) -> {
it.setVisibility(JvmVisibility.PROTECTED);
EList<JvmFormalParameter> _parameters = it.getParameters();
JvmFormalParameter _parameter = this._jvmTypesBuilder.toParameter(focusingStrategy, "yElement", this._typeReferenceBuilder.typeRef(YEmbeddable.class));
this._jvmTypesBuilder.<JvmFormalParameter>operator_add(_parameters, _parameter);
EList<JvmFormalParameter> _parameters_1 = it.getParameters();
JvmFormalParameter _parameter_1 = this._jvmTypesBuilder.toParameter(focusingStrategy, "service", this._typeReferenceBuilder.typeRef(IWidgetAssocationsService.class, this._typeReferenceBuilder.typeRef(Object.class), this._typeReferenceBuilder.wildcardExtends(this._typeReferenceBuilder.typeRef(YElement.class))));
this._jvmTypesBuilder.<JvmFormalParameter>operator_add(_parameters_1, _parameter_1);
final Procedure1<ITreeAppendable> _function_2 = (ITreeAppendable it_1) -> {
it_1.append(this.getFindElementToFocus(focusingStrategy));
};
this._jvmTypesBuilder.setBody(it, _function_2);
};
JvmOperation _method_1 = this._jvmTypesBuilder.toMethod(focusingStrategy, "findElementToFocus", this._typeReferenceBuilder.typeRef(YEmbeddable.class), _function_1);
this._jvmTypesBuilder.<JvmOperation>operator_add(_members_1, _method_1);
EList<JvmMember> _members_2 = type.getMembers();
final Procedure1<JvmOperation> _function_2 = (JvmOperation it) -> {
EList<JvmAnnotationReference> _annotations = it.getAnnotations();
JvmAnnotationReference _annotationRef = this._annotationTypesBuilder.annotationRef(Override.class);
this._jvmTypesBuilder.<JvmAnnotationReference>operator_add(_annotations, _annotationRef);
it.setVisibility(JvmVisibility.PUBLIC);
final Procedure1<ITreeAppendable> _function_3 = (ITreeAppendable it_1) -> {
it_1.append(this.getKeyStrokeDefinitionBody(focusingStrategy));
};
this._jvmTypesBuilder.setBody(it, _function_3);
};
JvmOperation _method_2 = this._jvmTypesBuilder.toMethod(focusingStrategy, "getKeyStrokeDefinition", this._typeReferenceBuilder.typeRef(KeyStrokeDefinition.class), _function_2);
this._jvmTypesBuilder.<JvmOperation>operator_add(_members_2, _method_2);
EList<JvmMember> _members_3 = type.getMembers();
final Procedure1<JvmOperation> _function_3 = (JvmOperation it) -> {
it.setVisibility(JvmVisibility.PRIVATE);
EList<JvmFormalParameter> _parameters = it.getParameters();
JvmFormalParameter _parameter = this._jvmTypesBuilder.toParameter(focusingStrategy, "focusable", this._typeReferenceBuilder.typeRef(YFocusable.class));
this._jvmTypesBuilder.<JvmFormalParameter>operator_add(_parameters, _parameter);
final Procedure1<ITreeAppendable> _function_4 = (ITreeAppendable it_1) -> {
it_1.append(this.findColumnIdxFocusable(focusingStrategy));
};
this._jvmTypesBuilder.setBody(it, _function_4);
};
JvmOperation _method_3 = this._jvmTypesBuilder.toMethod(focusingStrategy, "findNextFocusableColumnIdx", this._typeReferenceBuilder.typeRef(int.class), _function_3);
this._jvmTypesBuilder.<JvmOperation>operator_add(_members_3, _method_3);
EList<JvmMember> _members_4 = type.getMembers();
final Procedure1<JvmOperation> _function_4 = (JvmOperation it) -> {
it.setVisibility(JvmVisibility.PRIVATE);
EList<JvmFormalParameter> _parameters = it.getParameters();
JvmFormalParameter _parameter = this._jvmTypesBuilder.toParameter(focusingStrategy, "idx", this._typeReferenceBuilder.typeRef(int.class));
this._jvmTypesBuilder.<JvmFormalParameter>operator_add(_parameters, _parameter);
EList<JvmFormalParameter> _parameters_1 = it.getParameters();
JvmFormalParameter _parameter_1 = this._jvmTypesBuilder.toParameter(focusingStrategy, "columns", this._typeReferenceBuilder.typeRef(int.class));
this._jvmTypesBuilder.<JvmFormalParameter>operator_add(_parameters_1, _parameter_1);
final Procedure1<ITreeAppendable> _function_5 = (ITreeAppendable it_1) -> {
it_1.append(this.findColumnIdx(focusingStrategy));
};
this._jvmTypesBuilder.setBody(it, _function_5);
};
JvmOperation _method_4 = this._jvmTypesBuilder.toMethod(focusingStrategy, "findNextColumnIdx", this._typeReferenceBuilder.typeRef(int.class), _function_4);
this._jvmTypesBuilder.<JvmOperation>operator_add(_members_4, _method_4);
}
public CharSequence findColumnIdxFocusable(final FocusingStrategy focusingStrategy) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("return findNextColumnIdx(focusable.getLayoutIdx(), focusable.getLayoutColumns());");
_builder.newLine();
return _builder;
}
public CharSequence findColumnIdx(final FocusingStrategy focusingStrategy) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("if (idx % columns > columns) {");
_builder.newLine();
_builder.append("\t");
_builder.append("return findNextColumnIdx(idx % columns, columns);");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.append("return idx % columns;");
_builder.newLine();
return _builder;
}
public CharSequence getFindElementToFocus(final FocusingStrategy focusingStrategy) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("if (yElement == null) {");
_builder.newLine();
_builder.append("\t");
_builder.append("return null;");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.append("if (yElement instanceof YFocusable) {");
_builder.newLine();
_builder.append("\t");
_builder.append("YEmbeddable nextFocusElement = null;");
_builder.newLine();
_builder.append("\t");
_builder.append("int idx = ((YFocusable) yElement).getLayoutIdx();");
_builder.newLine();
_builder.append("\t");
_builder.append("// desired focus strategy to the next element is \'down\'");
_builder.newLine();
_builder.append("\t");
_builder.append("if (down) {");
_builder.newLine();
{
boolean _equals = focusingStrategy.getFocus().getLiteral().equals(FocusingEnum.FORWARD.getLiteral());
if (_equals) {
_builder.append("\t\t");
_builder.append("// next element below on forward focus is adding the number of layout columns to the current index ");
_builder.newLine();
_builder.append("\t\t");
_builder.append("idx = idx + ((YFocusable) yElement).getLayoutColumns();");
_builder.newLine();
_builder.append("\t\t");
_builder.append("nextFocusElement = (YEmbeddable) service.getModelElement(idx);");
_builder.newLine();
_builder.append("\t\t");
_builder.append("if (nextFocusElement == null) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("\t");
_builder.append("// if no element was found to focus search for elements in the next column");
_builder.newLine();
_builder.append("\t\t");
_builder.append("\t");
_builder.append("nextFocusElement = (YEmbeddable) service.getModelElement(findNextFocusableColumnIdx(((YFocusable) yElement)) + 1);");
_builder.newLine();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
} else {
boolean _equals_1 = focusingStrategy.getFocus().getLiteral().equals(FocusingEnum.BACKWARD.getLiteral());
if (_equals_1) {
_builder.append("\t\t");
_builder.append("if (idx == 1) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("\t");
_builder.append("// the current position is the first focusable element so the next focusable element backwards is");
_builder.newLine();
_builder.append("\t\t");
_builder.append("\t");
_builder.append("// the last focusable element of the last layout column");
_builder.newLine();
_builder.append("\t\t");
_builder.append("\t");
_builder.append("// finding the index of the last focusable element");
_builder.newLine();
_builder.append("\t\t");
_builder.append("\t");
CharSequence _findLastElementToFocus = this.getFindLastElementToFocus(focusingStrategy);
_builder.append(_findLastElementToFocus, "\t\t\t");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("\t");
_builder.append("// the last position of a focusable element in the last layout column");
_builder.newLine();
_builder.append("\t\t");
_builder.append("\t");
_builder.append("// subtracting the modulo operation between the last element index and the number of layout columns from the last element index");
_builder.newLine();
_builder.append("\t\t");
_builder.append("\t");
_builder.append("lastElementIdx = lastElementIdx - (lastElementIdx % ((YFocusable) yElement).getLayoutColumns());");
_builder.newLine();
_builder.append("\t\t");
_builder.append("\t");
_builder.append("nextFocusElement = (YEmbeddable) service.getModelElement(lastElementIdx);");
_builder.newLine();
_builder.append("\t\t");
_builder.append("} else {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("\t");
_builder.append("// next element above on backward focus is subtracting the number of layout columns to the current index");
_builder.newLine();
_builder.append("\t\t");
_builder.append("\t");
_builder.append("int prevElementIdx = idx - ((YFocusable) yElement).getLayoutColumns();");
_builder.newLine();
_builder.append("\t\t");
_builder.append("\t");
_builder.append("nextFocusElement = (YEmbeddable) service.getModelElement(prevElementIdx);");
_builder.newLine();
_builder.append("\t\t");
_builder.append("\t");
_builder.append("if (nextFocusElement == null) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("\t\t");
_builder.append("// if no element was found to focus the current index is the first element of the current column");
_builder.newLine();
_builder.append("\t\t");
_builder.append("\t\t");
_builder.append("// so search for the last element in the previous column");
_builder.newLine();
_builder.append("\t\t");
_builder.append("\t\t");
_builder.append("// go to the index of the first element of the previous column");
_builder.newLine();
_builder.append("\t\t");
_builder.append("\t\t");
_builder.append("idx = idx - 1;");
_builder.newLine();
_builder.append("\t\t");
_builder.append("\t\t");
_builder.append("// find the last focusable element in this column");
_builder.newLine();
_builder.append("\t\t");
_builder.append("\t\t");
_builder.append("while ((YEmbeddable) service.getModelElement(idx) != null) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("\t\t\t");
_builder.append("nextFocusElement = (YEmbeddable) service.getModelElement(idx);");
_builder.newLine();
_builder.append("\t\t");
_builder.append("\t\t\t");
_builder.append("idx = idx + ((YFocusable) yElement).getLayoutColumns();");
_builder.newLine();
_builder.append("\t\t");
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t\t");
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
}
}
}
_builder.append("\t\t");
_builder.append("// desired focus strategy to the next element is \'right\'");
_builder.newLine();
_builder.append("\t");
_builder.append("} else {");
_builder.newLine();
{
boolean _equals_2 = focusingStrategy.getFocus().getLiteral().equals(FocusingEnum.FORWARD.getLiteral());
if (_equals_2) {
_builder.append("\t\t");
_builder.append("nextFocusElement = (YEmbeddable) service.getModelElement(++idx);");
_builder.newLine();
} else {
boolean _equals_3 = focusingStrategy.getFocus().getLiteral().equals(FocusingEnum.BACKWARD.getLiteral());
if (_equals_3) {
_builder.append("\t\t");
_builder.append("nextFocusElement = (YEmbeddable) service.getModelElement(--idx);");
_builder.newLine();
}
}
}
_builder.append("\t\t");
_builder.append("if (nextFocusElement == null) {");
_builder.newLine();
{
boolean _equals_4 = focusingStrategy.getFocus().getLiteral().equals(FocusingEnum.FORWARD.getLiteral());
if (_equals_4) {
_builder.append("\t\t\t");
_builder.append("// if no element was found to focus go back to the first element");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("nextFocusElement = (YEmbeddable) service.getModelElement(0);");
_builder.newLine();
} else {
boolean _equals_5 = focusingStrategy.getFocus().getLiteral().equals(FocusingEnum.BACKWARD.getLiteral());
if (_equals_5) {
_builder.append("\t\t\t");
_builder.append("// if no element was found to focus go back to the last focusable element");
_builder.newLine();
_builder.append("\t\t\t");
CharSequence _findLastElementToFocus_1 = this.getFindLastElementToFocus(focusingStrategy);
_builder.append(_findLastElementToFocus_1, "\t\t\t");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
_builder.append("nextFocusElement = (YEmbeddable) service.getModelElement(lastElementIdx);");
_builder.newLine();
}
}
}
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.append("if (nextFocusElement instanceof YField) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("YField field = ((YField) nextFocusElement);");
_builder.newLine();
_builder.append("\t\t");
_builder.append("if (!field.isEditable() || !field.isEnabled() || !field.isVisible()) {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("nextFocusElement = findElementToFocus(nextFocusElement, service);");
_builder.newLine();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.append("return nextFocusElement;");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.append("return null;");
return _builder;
}
public CharSequence getFindLastElementToFocus(final FocusingStrategy focusingStrategy) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("// finding the index of the last focusable element");
_builder.newLine();
_builder.append("int lastElementIdx = 0;");
_builder.newLine();
_builder.append("for (YElement element : service.getModelElements()) {");
_builder.newLine();
_builder.append("\t");
_builder.append("if (element instanceof YFocusable) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("int layoutIdx = ((YFocusable) element).getLayoutIdx();");
_builder.newLine();
_builder.append("\t\t");
_builder.append("lastElementIdx = lastElementIdx < layoutIdx ? layoutIdx : lastElementIdx;");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("}");
return _builder;
}
public String findNextElementToFocusInSingularLayout(final FocusingStrategy focusingStrategy) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("int idx = getIndexToFocus(yParent, index);");
_builder.newLine();
_builder.append("return yParent.getElements().get(idx);");
return _builder.toString();
}
public String findNextElementToFocusInAComplexLayout(final FocusingStrategy focusingStrategy) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("int layoutElementListSize = layout.getElements().size();");
_builder.newLine();
_builder.append("// in case of more than one element");
_builder.newLine();
_builder.append("if (layoutElementListSize > 0){");
_builder.newLine();
_builder.append("\t");
_builder.append("for (int i = 0; i < layoutElementListSize; i++) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("YEmbeddable layoutElement = layout.getElement(i);");
_builder.newLine();
_builder.append("\t\t");
_builder.append("// in case that the layout elements are again layouts");
_builder.newLine();
_builder.append("\t\t");
_builder.append("if (layoutElement instanceof YLayout){");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("// in case that the layout containing the focus event sending element is found");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("if (yParent.equals(layoutElement)){");
_builder.newLine();
{
boolean _equals = focusingStrategy.getFocus().getLiteral().equals(FocusingEnum.FORWARD.getLiteral());
if (_equals) {
_builder.append("\t\t\t\t");
_builder.append("if (i!=layoutElementListSize-1){");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("\t");
_builder.append("i = i+1;");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("} else {");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("\t");
_builder.append("i = 0;");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("// find next layout");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("YLayout yParentTemp = (YLayout)layout.getElement(i);");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("// get the first element of the layout");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("return yParentTemp.getElement(0);");
_builder.newLine();
} else {
boolean _equals_1 = focusingStrategy.getFocus().getLiteral().equals(FocusingEnum.BACKWARD.getLiteral());
if (_equals_1) {
_builder.append("\t\t\t\t");
_builder.append("if (i!=0){");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("\t");
_builder.append("i = i-1;");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("} else {");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("\t");
_builder.append("i = layoutElementListSize - 1;");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("// find next layout");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("YLayout yParentTemp = (YLayout)layout.getElement(i);");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("// get the last element of the layout");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("int idx = yParentTemp.getElements().size() - 1;");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("return yParentTemp.getElement(idx);");
_builder.newLine();
}
}
}
_builder.append("\t\t\t");
_builder.append("} else {");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("YEmbeddable yEmbeddable = findNextElementToFocusInAComplexLayout(yParent, (YLayout)layoutElement, index);");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("// without this if statement the for loop will be broken.");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("if (yEmbeddable!=null){");
_builder.newLine();
_builder.append("\t\t\t\t\t");
_builder.append("return yEmbeddable;");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("} else {");
_builder.newLine();
_builder.append("\t");
_builder.append("YEmbeddable layoutElement = layout.getElement(0);");
_builder.newLine();
_builder.append("\t");
_builder.append("// in case that the layout containing the focus event sending element is found");
_builder.newLine();
_builder.append("\t");
_builder.append("if (yParent.equals(layoutElement)){");
_builder.newLine();
_builder.append("\t\t");
_builder.append("return findNextElementToFocusInSingularLayout(yParent, index);");
_builder.newLine();
_builder.append("\t");
_builder.append("} else {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("// in case that the layout elements are again layouts");
_builder.newLine();
_builder.append("\t\t");
_builder.append("if (layoutElement instanceof YLayout){");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("return findNextElementToFocusInAComplexLayout(yParent, (YLayout)layoutElement, index);");
_builder.newLine();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.append("return null;");
return _builder.toString();
}
public String getIndexToFocus(final FocusingStrategy focusingStrategy) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("int index = idx;");
String body = _builder.toString();
boolean _equals = focusingStrategy.getFocus().getLiteral().equals(FocusingEnum.FORWARD.getLiteral());
if (_equals) {
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append(body);
CharSequence _forwardFocusBodyCode = this.getForwardFocusBodyCode();
_builder_1.append(_forwardFocusBodyCode);
body = _builder_1.toString();
} else {
boolean _equals_1 = focusingStrategy.getFocus().getLiteral().equals(FocusingEnum.BACKWARD.getLiteral());
if (_equals_1) {
StringConcatenation _builder_2 = new StringConcatenation();
_builder_2.append(body);
CharSequence _backwardFocusBodyCode = this.getBackwardFocusBodyCode();
_builder_2.append(_backwardFocusBodyCode);
body = _builder_2.toString();
}
}
StringConcatenation _builder_3 = new StringConcatenation();
_builder_3.append(body);
_builder_3.append("return index;");
return _builder_3.toString();
}
public String getKeyStrokeDefinitionBody(final FocusingStrategy focusingStrategy) {
StringConcatenation _builder = new StringConcatenation();
String body = _builder.toString();
int modifierKeysLength = 0;
EList<String> _modifierKeys = focusingStrategy.getModifierKeys();
boolean _notEquals = (!Objects.equal(_modifierKeys, null));
if (_notEquals) {
modifierKeysLength = ((Object[])Conversions.unwrapArray(focusingStrategy.getModifierKeys(), Object.class)).length;
}
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append(body);
_builder_1.append("int[] modifierKeys = new int[");
_builder_1.append(modifierKeysLength);
_builder_1.append("];");
body = _builder_1.toString();
boolean _isEmpty = focusingStrategy.getModifierKeys().isEmpty();
boolean _not = (!_isEmpty);
if (_not) {
int idx = 0;
EList<String> _modifierKeys_1 = focusingStrategy.getModifierKeys();
for (final String modifierKey : _modifierKeys_1) {
{
StringConcatenation _builder_2 = new StringConcatenation();
_builder_2.append(body);
_builder_2.newLineIfNotEmpty();
_builder_2.append("\t\t\t\t");
_builder_2.append("modifierKeys[");
_builder_2.append(idx, "\t\t\t\t");
_builder_2.append("] = ModifierKey.");
_builder_2.append(modifierKey, "\t\t\t\t");
_builder_2.append(";");
body = _builder_2.toString();
idx++;
}
}
}
if (((!Objects.equal(focusingStrategy.getKeyCode(), null)) && (!focusingStrategy.getKeyCode().isEmpty()))) {
StringConcatenation _builder_2 = new StringConcatenation();
_builder_2.append(body);
_builder_2.newLineIfNotEmpty();
_builder_2.append("\t\t\t\t");
_builder_2.append("KeyStrokeDefinition def = new KeyStrokeDefinition(\"\", KeyCode.");
String _keyCode = focusingStrategy.getKeyCode();
_builder_2.append(_keyCode, "\t\t\t\t");
_builder_2.append(", modifierKeys);");
_builder_2.newLineIfNotEmpty();
_builder_2.append("\t\t\t\t");
_builder_2.append("return def;");
body = _builder_2.toString();
}
return body;
}
public CharSequence getForwardFocusBodyCode() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("if (index == yParent.getElements().size() - 1) {");
_builder.newLine();
_builder.append("\t");
_builder.append("// element is the last -> Start again");
_builder.newLine();
_builder.append("\t");
_builder.append("index = 0;");
_builder.newLine();
_builder.append("} else {");
_builder.newLine();
_builder.append("\t");
_builder.append("// use the next element");
_builder.newLine();
_builder.append("\t");
_builder.append("index = index + 1;");
_builder.newLine();
_builder.append("}");
_builder.newLine();
return _builder;
}
public CharSequence getBackwardFocusBodyCode() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("if (index == 0) {");
_builder.newLine();
_builder.append("\t");
_builder.append("// element is the first -> Start again");
_builder.newLine();
_builder.append("\t");
_builder.append("index = yParent.getElements().size() - 1;");
_builder.newLine();
_builder.append("} else {");
_builder.newLine();
_builder.append("\t");
_builder.append("// use the previous element");
_builder.newLine();
_builder.append("\t");
_builder.append("index = index - 1;");
_builder.newLine();
_builder.append("}");
_builder.newLine();
return _builder;
}
/**
* <p>build the class variables.</p>
*/
public void toProviderFields(final JvmGenericType type, final FocusingStrategy focusingStrategy) {
JvmField field = null;
final Procedure1<JvmField> _function = (JvmField it) -> {
final Procedure1<ITreeAppendable> _function_1 = (ITreeAppendable it_1) -> {
StringConcatenation _builder = new StringConcatenation();
_builder.append("\"");
String _ecviewFocusingId = focusingStrategy.getEcviewFocusingId();
_builder.append(_ecviewFocusingId);
_builder.append("\"");
it_1.append(_builder);
};
this._jvmTypesBuilder.setInitializer(it, _function_1);
};
field = this._jvmTypesBuilder.toField(focusingStrategy, "ID", this._typeReferenceBuilder.typeRef(String.class), _function);
field.setFinal(true);
field.setStatic(true);
EList<JvmMember> _members = type.getMembers();
this._jvmTypesBuilder.<JvmField>operator_add(_members, field);
}
public void toProviderOperations(final JvmGenericType type, final FocusingStrategy focusingStrategy) {
EList<JvmMember> _members = type.getMembers();
final Procedure1<JvmOperation> _function = (JvmOperation it) -> {
EList<JvmAnnotationReference> _annotations = it.getAnnotations();
JvmAnnotationReference _annotationRef = this._annotationTypesBuilder.annotationRef(Override.class);
this._jvmTypesBuilder.<JvmAnnotationReference>operator_add(_annotations, _annotationRef);
it.setVisibility(JvmVisibility.PUBLIC);
final Procedure1<ITreeAppendable> _function_1 = (ITreeAppendable it_1) -> {
StringConcatenation _builder = new StringConcatenation();
_builder.append("return new ");
CharSequence _strategyClassName = this.getStrategyClassName(focusingStrategy);
_builder.append(_strategyClassName);
_builder.append("();");
it_1.append(_builder);
};
this._jvmTypesBuilder.setBody(it, _function_1);
};
JvmOperation _method = this._jvmTypesBuilder.toMethod(focusingStrategy, "getStrategy", this._typeReferenceBuilder.typeRef(IFocusingStrategy.class), _function);
this._jvmTypesBuilder.<JvmOperation>operator_add(_members, _method);
}
public CharSequence getStrategyClassName(final FocusingStrategy focusingStrategy) {
StringConcatenation _builder = new StringConcatenation();
String _name = focusingStrategy.getName();
_builder.append(_name);
String _literal = focusingStrategy.getFocus().getLiteral();
_builder.append(_literal);
_builder.append(this._strategyConstants.FOCUSING_STRATEGY_PREFIX);
return _builder;
}
public CharSequence getStrategyProviderClassName(final FocusingStrategy focusingStrategy) {
StringConcatenation _builder = new StringConcatenation();
String _name = focusingStrategy.getName();
_builder.append(_name);
String _literal = focusingStrategy.getFocus().getLiteral();
_builder.append(_literal);
_builder.append(this._strategyConstants.FOCUSING_STRATEGY_PROVIDER_PREFIX);
return _builder;
}
}