| /** |
| * Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), 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.runtime.web.ecview.services.vaadin.impl; |
| |
| import java.util.HashSet; |
| import java.util.Set; |
| import java.util.Stack; |
| |
| import org.eclipse.osbp.ecview.core.common.model.core.YLayout; |
| import org.eclipse.osbp.dsl.semantic.common.types.LDataType; |
| import org.osgi.framework.Bundle; |
| import org.osgi.framework.FrameworkUtil; |
| |
| // TODO: Auto-generated Javadoc |
| /** |
| * The Class AbstractRenderer. |
| */ |
| public class AbstractRenderer { |
| |
| /** The Constant NUMERICTYPES. */ |
| protected static final Set<String> NUMERICTYPES = new HashSet<String>(); |
| static { |
| NUMERICTYPES.add(Byte.class.getName()); |
| NUMERICTYPES.add(Long.class.getName()); |
| NUMERICTYPES.add(Short.class.getName()); |
| NUMERICTYPES.add(Integer.class.getName()); |
| |
| NUMERICTYPES.add("byte"); |
| NUMERICTYPES.add("long"); |
| NUMERICTYPES.add("short"); |
| NUMERICTYPES.add("int"); |
| } |
| |
| /** The Constant DECIMALTYPES. */ |
| protected static final Set<String> DECIMALTYPES = new HashSet<String>(); |
| static { |
| DECIMALTYPES.add(Double.class.getName()); |
| DECIMALTYPES.add(Float.class.getName()); |
| |
| NUMERICTYPES.add("double"); |
| NUMERICTYPES.add("float"); |
| } |
| |
| /** The contexts. */ |
| protected Stack<Context> contexts = new Stack<Context>(); |
| |
| /** The model bundle. */ |
| protected Bundle modelBundle; |
| |
| /** |
| * Inits the. |
| * |
| * @param yLayout |
| * the y layout |
| * @param clazz |
| * the clazz |
| */ |
| public void init(YLayout yLayout, Class<?> clazz) { |
| modelBundle = FrameworkUtil.getBundle(clazz); |
| contexts.push(new Context(null, null, yLayout)); |
| } |
| |
| /** |
| * Returns the currently active layout. |
| * |
| * @return the layout |
| */ |
| public YLayout getLayout() { |
| return contexts.peek().getLayout(); |
| } |
| |
| /** |
| * Returns the binding path under respect of nested attributes. |
| * |
| * @param attribute |
| * the attribute |
| * @return the binding path |
| */ |
| public String getBindingPath(String attribute) { |
| return contexts.peek().getBindingPathFor(attribute); |
| } |
| |
| /** |
| * Checks if is string. |
| * |
| * @param object |
| * the object |
| * @return true, if is string |
| */ |
| public static boolean isString(LDataType object) { |
| String fqn = object.getJvmTypeReference().getQualifiedName(); |
| return "java.lang.String".equals(fqn); |
| } |
| |
| /** |
| * Checks if is boolean. |
| * |
| * @param object |
| * the object |
| * @return true, if is boolean |
| */ |
| public static boolean isBoolean(LDataType object) { |
| String name = object.getJvmTypeReference().getQualifiedName(); |
| return "java.lang.Boolean".equals(name); |
| } |
| |
| /** |
| * Checks if is numeric. |
| * |
| * @param object |
| * the object |
| * @return true, if is numeric |
| */ |
| public static boolean isNumeric(LDataType object) { |
| String name = object.getJvmTypeReference().getQualifiedName(); |
| return NUMERICTYPES.contains(name); |
| } |
| |
| /** |
| * Checks if is decimal. |
| * |
| * @param object |
| * the object |
| * @return true, if is decimal |
| */ |
| public static boolean isDecimal(LDataType object) { |
| String name = object.getJvmTypeReference().getQualifiedName(); |
| return DECIMALTYPES.contains(name); |
| } |
| |
| /** |
| * The Class Context. |
| */ |
| protected static class Context { |
| |
| /** The parent. */ |
| protected final Context parent; |
| |
| /** The binding attribute. */ |
| protected final String bindingAttribute; |
| |
| /** The layout. */ |
| protected final YLayout layout; |
| |
| /** |
| * Instantiates a new context. |
| * |
| * @param parent |
| * the parent |
| * @param bindingAttribute |
| * the binding attribute |
| * @param layout |
| * the layout |
| */ |
| public Context(Context parent, String bindingAttribute, YLayout layout) { |
| super(); |
| this.parent = parent; |
| this.bindingAttribute = bindingAttribute; |
| this.layout = layout; |
| } |
| |
| /** |
| * Gets the layout. |
| * |
| * @return the parentLayout |
| */ |
| public YLayout getLayout() { |
| if (layout != null) { |
| return layout; |
| } |
| |
| if (parent != null) { |
| return parent.getLayout(); |
| } |
| |
| throw new IllegalStateException("No layout found"); |
| } |
| |
| /** |
| * Gets the binding path. |
| * |
| * @return the binding path |
| */ |
| public String getBindingPath() { |
| String path = ""; |
| if (parent != null) { |
| path = parent.getBindingPath(); |
| } |
| |
| if (bindingAttribute != null) { |
| if (path != null && !path.equals("")) { |
| if (!bindingAttribute.equals("")) { |
| // attache the binding attribute separated by a "." |
| path = path + "." + bindingAttribute; |
| } |
| } else { |
| path = bindingAttribute; |
| } |
| } |
| |
| return path; |
| } |
| |
| /** |
| * Gets the binding path for. |
| * |
| * @param attribute |
| * the attribute |
| * @return the binding path for |
| */ |
| public String getBindingPathFor(String attribute) { |
| String path = getBindingPath(); |
| if (path != null && !path.equals("")) { |
| path = path + "." + attribute; |
| } else { |
| path = attribute; |
| } |
| return path; |
| } |
| |
| } |
| } |