blob: bdebf3477a3d1b609401a53d6acc7cfed003aa8f [file] [log] [blame]
/**
* Copyright (c) 2013 Loetz GmbH&Co.KG(Heidelberg). 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,
t https://www.eclipse.org/legal/epl-2.0/
t
t SPDX-License-Identifier: EPL-2.0
*
* Contributors: Christophe Loetz (Loetz GmbH&Co.KG) - initial API and implementation
*/
package org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.internal;
import java.util.ArrayList;
import java.util.Locale;
import org.eclipse.osbp.ecview.core.common.editpart.IElementEditpart;
import org.eclipse.osbp.ecview.core.extension.model.extension.ExtensionModelPackage;
import org.eclipse.osbp.ecview.core.extension.model.extension.YButton;
import org.eclipse.osbp.ecview.core.extension.model.extension.listener.YButtonClickListener;
import org.eclipse.osbp.ecview.core.ui.core.editparts.extension.IButtonEditpart;
import org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.common.AbstractVaadinWidgetPresenter;
import org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.internal.util.Util;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.ComponentContainer;
// TODO: Auto-generated Javadoc
/**
* This presenter is responsible to render a text field on the given layout.
*/
public class ButtonPresentation extends
AbstractVaadinWidgetPresenter<Component> {
/** The model access. */
private final ModelAccess modelAccess;
/** The button. */
private FocusLosingButton button;
/**
* Constructor.
*
* @param editpart
* The editpart of that presenter
*/
public ButtonPresentation(IElementEditpart editpart) {
super((IButtonEditpart) editpart);
this.modelAccess = new ModelAccess((YButton) editpart.getModel());
}
/**
* {@inheritDoc}
*/
@Override
public Component doCreateWidget(Object parent) {
if (button == null) {
button = new FocusLosingButton();
button.addStyleName(CSS_CLASS_CONTROL);
button.setImmediate(true);
setupComponent(button, getCastedModel());
associateWidget(button, modelAccess.yField);
if (modelAccess.isCssIdValid()) {
button.setId(modelAccess.getCssID());
} else {
button.setId(getEditpart().getId());
}
initialize(button, getCastedModel());
// creates the binding for the field
createBindings(modelAccess.yField, button);
if (modelAccess.isCssClassValid()) {
button.addStyleName(modelAccess.getCssClass());
}
applyCaptions();
}
return button;
}
/*
* (non-Javadoc)
*
* @see org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.common.
* AbstractVaadinWidgetPresenter#doUpdateLocale(java.util.Locale)
*/
@Override
protected void doUpdateLocale(Locale locale) {
// no need to set the locale to the ui elements. Is handled by vaadin
// internally.
// update the captions
applyCaptions();
}
/**
* Applies the labels to the widgets.
*/
protected void applyCaptions() {
Util.applyCaptions(getI18nService(), modelAccess.getLabel(),
modelAccess.getLabelI18nKey(), getLocale(), button);
}
/**
* Creates the bindings for the given values.
*
* @param yField
* the y field
* @param button
* the button
*/
@SuppressWarnings("serial")
protected void createBindings(final YButton yField, Button button) {
registerBinding(createBindingsButtonClick(castEObject(getModel()),
ExtensionModelPackage.Literals.YBUTTON__LAST_CLICK_TIME, button));
registerBinding(createBindingsButtonImage(castEObject(getModel()),
ExtensionModelPackage.Literals.YBUTTON__IMAGE, button));
super.createBindings(yField, button, null);
button.addClickListener(new Button.ClickListener() {
@Override
public void buttonClick(Button.ClickEvent event) {
for (YButtonClickListener listener : new ArrayList<YButtonClickListener>(
yField.getClickListeners())) {
listener.clicked(yField);
}
}
});
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.osbp.ecview.core.common.presentation.IWidgetPresentation#
* getWidget()
*/
@Override
public Component getWidget() {
return button;
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.osbp.ecview.core.common.presentation.IWidgetPresentation#
* isRendered()
*/
@Override
public boolean isRendered() {
return button != null;
}
/**
* {@inheritDoc}
*/
@Override
public void doUnrender() {
if (button != null) {
// unbind all active bindings
unbind();
Component parent = ((Component) button.getParent());
if (parent != null && parent instanceof ComponentContainer) {
((ComponentContainer) parent).removeComponent(button);
}
// remove assocations
unassociateWidget(button);
button = null;
}
}
/**
* {@inheritDoc}
*/
@Override
protected void internalDispose() {
try {
unrender();
} finally {
super.internalDispose();
}
}
/**
* A helper class.
*/
private static class ModelAccess {
/** The y field. */
private final YButton yField;
/**
* Instantiates a new model access.
*
* @param yField
* the y field
*/
public ModelAccess(YButton yField) {
super();
this.yField = yField;
}
/**
* Gets the css class.
*
* @return the css class
* @see org.eclipse.osbp.ecview.core.ui.core.model.core.YCssAble#getCssClass()
*/
public String getCssClass() {
return yField.getCssClass();
}
/**
* Returns true, if the css class is not null and not empty.
*
* @return true, if is css class valid
*/
public boolean isCssClassValid() {
return getCssClass() != null && !getCssClass().equals("");
}
/**
* Gets the css id.
*
* @return the css id
* @see org.eclipse.osbp.ecview.core.ui.core.model.core.YCssAble#getCssID()
*/
public String getCssID() {
return yField.getCssID();
}
/**
* Returns true, if the css id is not null and not empty.
*
* @return true, if is css id valid
*/
public boolean isCssIdValid() {
return getCssID() != null && !getCssID().equals("");
}
/**
* Returns the label.
*
* @return the label
*/
public String getLabel() {
return yField.getDatadescription() != null ? yField
.getDatadescription().getLabel() : null;
}
/**
* Returns the label.
*
* @return the label i18n key
*/
public String getLabelI18nKey() {
return yField.getDatadescription() != null ? yField
.getDatadescription().getLabelI18nKey() : null;
}
}
}