| /** |
| * 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.presentation.vaadin.internal.util; |
| |
| import java.util.Collections; |
| import java.util.List; |
| import java.util.Locale; |
| |
| import org.eclipse.osbp.ecview.core.common.model.core.YEmbeddable; |
| import org.eclipse.osbp.ecview.core.common.model.core.util.II18nConstants; |
| import org.eclipse.osbp.ecview.core.common.model.visibility.VisibilityFactory; |
| import org.eclipse.osbp.ecview.core.common.model.visibility.YColor; |
| import org.eclipse.osbp.ecview.core.common.model.visibility.YVisibilityProperties; |
| import org.eclipse.osbp.ecview.core.common.visibility.Color; |
| import org.eclipse.osbp.ecview.core.common.visibility.IVisibilityHandler; |
| import org.eclipse.osbp.runtime.common.i18n.I18nUtil; |
| import org.eclipse.osbp.runtime.common.i18n.II18nService; |
| import org.eclipse.osbp.runtime.common.validation.IStatus; |
| import org.eclipse.osbp.runtime.common.validation.IStatus.Severity; |
| import org.eclipse.osbp.runtime.common.validation.Status; |
| import org.eclipse.osbp.runtime.web.vaadin.common.resource.IResourceProvider; |
| |
| import com.vaadin.server.ErrorMessage; |
| import com.vaadin.server.ErrorMessage.ErrorLevel; |
| import com.vaadin.server.Resource; |
| import com.vaadin.ui.Component; |
| |
| // TODO: Auto-generated Javadoc |
| /** |
| * The Class Util. TODO - merge with |
| * org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.internal.util.Util |
| * later |
| */ |
| public class Util { |
| |
| /** |
| * Maps the handler properties to the eObject. |
| * |
| * @param handler |
| * the handler |
| * @return the y visibility properties |
| */ |
| public static YVisibilityProperties mapYProperties(IVisibilityHandler handler) { |
| YVisibilityProperties yProps = VisibilityFactory.eINSTANCE.createYVisibilityProperties(); |
| yProps.setBackgroundColor(mapYColor(handler.getBackgroundColor())); |
| yProps.setBackgroundColorCode(handler.getBackgroundColorCode()); |
| yProps.setBold(handler.isBold()); |
| yProps.setBorder(handler.isBorder()); |
| yProps.setEditable(handler.isEditable()); |
| yProps.setEnabled(handler.isEnabled()); |
| yProps.setForegroundColor(mapYColor(handler.getForegroundColor())); |
| yProps.setForegroundColorCode(handler.getForegroundColorCode()); |
| yProps.setItalic(handler.isItalic()); |
| yProps.setStrikethrough(handler.isStrikethrough()); |
| yProps.setUnderline(handler.isUnderline()); |
| yProps.setVisible(handler.isVisible()); |
| yProps.setCssClass(handler.getCssClass()); |
| yProps.setCssId(handler.getCssId()); |
| |
| return yProps; |
| } |
| |
| /** |
| * Maps the handler color to the eObject. |
| * |
| * @param color |
| * the color |
| * @return the y color |
| */ |
| public static YColor mapYColor(Color color) { |
| if (color == null) { |
| return YColor.UNDEFINED; |
| } |
| switch (color) { |
| case RED: |
| return YColor.RED; |
| case BLACK: |
| return YColor.BLACK; |
| case BLUE: |
| return YColor.BLUE; |
| case GREEN: |
| return YColor.GREEN; |
| case WHITE: |
| return YColor.WHITE; |
| case DARK_GRAY: |
| return YColor.DARK_GRAY; |
| case LIGHT_GRAY: |
| return YColor.LIGHT_GRAY; |
| case UNDEFINED: |
| return YColor.UNDEFINED; |
| case YELLOW: |
| return YColor.YELLOW; |
| default: |
| break; |
| } |
| |
| return null; |
| } |
| |
| /** |
| * Apply captions. |
| * |
| * @param service |
| * the service |
| * @param label |
| * the label |
| * @param i18nLabelKey |
| * the i18n label key |
| * @param locale |
| * the locale |
| * @param component |
| * the component |
| */ |
| public static void applyCaptions(II18nService service, String label, String i18nLabelKey, Locale locale, |
| Component component) { |
| |
| if (II18nConstants.NO_CAPTION.equals(i18nLabelKey)) { |
| return; |
| } |
| |
| if (component != null) { |
| if (service != null && isValid(i18nLabelKey)) { |
| String translation = service.getValue(i18nLabelKey, locale); |
| if (translation == null || translation.equals("")) { |
| // caption must be set in all cases - even if it is null, |
| // else icon-only mode will not work |
| // if already set by other process then do not translate |
| component.setCaption(label); |
| } else { |
| component.setCaption(translation); |
| } |
| |
| } else { |
| // caption must be set in all cases - even if it is null, else |
| // icon-only mode will not work |
| component.setCaption(label); |
| } |
| } |
| } |
| |
| /** |
| * Gets the caption. |
| * |
| * @param service |
| * the service |
| * @param label |
| * the label |
| * @param i18nLabelKey |
| * the i18n label key |
| * @param locale |
| * the locale |
| * @return the caption |
| */ |
| public static String getCaption(II18nService service, String label, String i18nLabelKey, Locale locale) { |
| |
| String result = i18nLabelKey; |
| |
| if (service != null && isValid(i18nLabelKey)) { |
| String translation = service.getValue(i18nLabelKey, locale); |
| if (translation == null || translation.equals("")) { |
| if (isValid(label)) { |
| result = label; |
| } |
| } else { |
| result = translation; |
| } |
| |
| } else { |
| if (isValid(label)) { |
| result = label; |
| } |
| } |
| return result != null ? result : ""; |
| } |
| |
| /** |
| * Apply caption icons. |
| * |
| * @param service |
| * the service |
| * @param provider |
| * the provider |
| * @param i18nLabelKey |
| * the i18n label key |
| * @param locale |
| * the locale |
| * @param component |
| * the component |
| */ |
| public static void applyCaptionIcons(II18nService service, IResourceProvider provider, String i18nLabelKey, |
| Locale locale, Component component) { |
| String iconKey = I18nUtil.getImageKey(i18nLabelKey); |
| |
| if (service != null && provider != null && isValid(iconKey)) { |
| String iconResourcePath = service.getValue(iconKey, locale); |
| if (iconResourcePath != null && !iconResourcePath.equals("")) { |
| component.setIcon(provider.getResource(iconResourcePath)); |
| } |
| } |
| } |
| |
| /** |
| * Apply caption icons. |
| * |
| * @param service |
| * the service |
| * @param provider |
| * the provider |
| * @param i18nLabelKey |
| * the i18n label key |
| * @param locale |
| * the locale |
| * @param callback |
| * the callback |
| */ |
| public static void applyCaptionIcons(II18nService service, IResourceProvider provider, String i18nLabelKey, |
| Locale locale, ResourceCallback callback) { |
| String iconKey = I18nUtil.getImageKey(i18nLabelKey); |
| |
| if (service != null && provider != null && isValid(iconKey)) { |
| String iconResourcePath = service.getValue(iconKey, locale); |
| if (iconResourcePath != null && !iconResourcePath.equals("")) { |
| callback.setIcon(provider.getResource(iconResourcePath)); |
| } |
| } |
| } |
| |
| /** |
| * Checks if is valid. |
| * |
| * @param value |
| * the value |
| * @return true, if is valid |
| */ |
| private static boolean isValid(String value) { |
| return value != null && !value.equals(""); |
| } |
| |
| /** |
| * Creates a collection of status for the given InvalidValueException. |
| * |
| * @param yElement |
| * the y element |
| * @param message |
| * the message |
| * @return the list |
| */ |
| public static List<org.eclipse.osbp.runtime.common.validation.IStatus> createStatus(YEmbeddable yElement, |
| ErrorMessage message) { |
| if (message == null) { |
| return Collections.emptyList(); |
| } |
| |
| String text = message.getFormattedHtmlMessage(); |
| IStatus status = Status.createStatus("", Util.class, mapSeverity(message.getErrorLevel()), text); |
| status.putProperty(IStatus.PROP_FIELD_ID, yElement.getId()); |
| status.putProperty(IStatus.PROP_FIELD_I18N_KEY, yElement.getLabelI18nKey()); |
| status.putProperty(IStatus.PROP_CREATOR, "Presentation of " + yElement.getId()); |
| |
| return Collections.singletonList(status); |
| } |
| |
| /** |
| * Map severity. |
| * |
| * @param level |
| * the level |
| * @return the severity |
| */ |
| private static Severity mapSeverity(ErrorLevel level) { |
| switch (level) { |
| case INFORMATION: |
| return IStatus.Severity.INFO; |
| case WARNING: |
| return IStatus.Severity.WARNING; |
| case ERROR: |
| return IStatus.Severity.ERROR; |
| case CRITICAL: |
| return IStatus.Severity.CRITICAL; |
| case SYSTEMERROR: |
| return IStatus.Severity.SYSTEMERROR; |
| } |
| return IStatus.Severity.ERROR; |
| } |
| |
| /** |
| * Callback to set resources. |
| */ |
| public interface ResourceCallback { |
| |
| /** |
| * Sets the icon. |
| * |
| * @param resource |
| * the new icon |
| */ |
| void setIcon(Resource resource); |
| |
| } |
| |
| } |