package com.thingtrack.konekti.view.layout.menu.internal;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import org.vaadin.hene.popupbutton.PopupButton;
import com.vaadin.annotations.AutoGenerated;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.AbsoluteLayout;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Select;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Window.Notification;
import com.thingtrack.konekti.domain.Area;
import com.thingtrack.konekti.domain.Location;
import com.thingtrack.konekti.domain.Organization;
import com.thingtrack.konekti.domain.User;
import com.thingtrack.konekti.view.addon.ui.AbstractViewForm;
import com.thingtrack.konekti.view.kernel.ui.layout.IUserChangeListener;
@SuppressWarnings("serial")
public class ProfileSettingsViewForm extends AbstractViewForm {
@AutoGenerated
private AbsoluteLayout mainLayout;
@AutoGenerated
private VerticalLayout profileLayout;
@AutoGenerated
private HorizontalLayout horizontalProfileLayout;
@AutoGenerated
private Button cancelSettingsButton;
@AutoGenerated
private Button applySettingsButton;
@AutoGenerated
private CheckBox defaultSettings;
@AutoGenerated
private ComboBox languageField;
@AutoGenerated
private ComboBox organizationField;
@AutoGenerated
private ComboBox locationField;
@AutoGenerated
private ComboBox areaField;
/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */
private User user;
private BeanItemContainer<Organization> bcOrganization = new BeanItemContainer<Organization>(Organization.class);
private BeanItemContainer<Location> bcLocation = new BeanItemContainer<Location>(Location.class);
private BeanItemContainer<Area> bcArea = new BeanItemContainer<Area>(Area.class);
private PopupButton parent;
private IUserChangeListener userChangeListener = null;
private static final String LOCALE_SEPARATOR = "-";
/**
* The constructor should first build the main layout, set the
* composition root and then do any custom initialization.
*
* The constructor will not be automatically regenerated by the
* visual editor.
*/
public ProfileSettingsViewForm(PopupButton parent) {
this.parent = parent;
buildMainLayout();
setCompositionRoot(mainLayout);
// TODO add user code here
applySettingsButton.addListener(new ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
// change konekti mobile context
user.setActiveOrganization((Organization)organizationField.getValue());
user.setActiveLocation((Location)locationField.getValue());
user.setActiveArea((Area)areaField.getValue());
Locale activeLocale;
if (languageField.getValue().toString().equals("Español"))
activeLocale = new Locale("es");
else if (languageField.getValue().toString().equals("English"))
activeLocale = new Locale("en");
else if (languageField.getValue().toString().equals("Française"))
activeLocale = new Locale("fr");
else if (languageField.getValue().toString().equals("简体中文"))
activeLocale = new Locale("zh");
else
activeLocale = new Locale("es");
user.setActiveLocale(activeLocale);
// close popup panel
ProfileSettingsViewForm.this.parent.setPopupVisible(false);
// regenerate menu for the new active area
if (userChangeListener != null)
userChangeListener.userChangedButtonClick(user);
}
});
cancelSettingsButton.addListener(new ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
// close popup panel
ProfileSettingsViewForm.this.parent.setPopupVisible(false);
}
});
}
public void setUser(User user) {
this.user = user;
fillDataSource();
bindingSource();
}
private void fillDataSource() {
try {
bcOrganization.removeAllItems();
bcOrganization.addAll(user.getOrganizations());
// set language selected
languageField.addItem("Español");
languageField.addItem("English");
languageField.addItem("Française");
//languageField.addItem("简体中文");
} catch (Exception e) {
getWindow().showNotification(
"¡Error refrescando organizaciones!",
"",
Notification.TYPE_WARNING_MESSAGE);
}
if (user.getDefaultOrganization() != null)
organizationField.setValue(user.getDefaultOrganization());
}
private void bindingSource() {
organizationField.setValue(user.getActiveOrganization());
locationField.setValue(user.getActiveLocation());
areaField.setValue(user.getActiveArea());
// create locale for employee agent
String[] localeParams = user.getDefaultLocale().split(LOCALE_SEPARATOR);
String language = localeParams[0];
//String country = localeParams[1];
if (language.equals("es"))
languageField.setValue("Español");
else if (language.equals("en"))
languageField.setValue("English");
else if (language.equals("fr"))
languageField.setValue("Française");
else if (language.equals("zh"))
languageField.setValue("简体中文");
else
languageField.setValue("Español");
}
private List<Location> getAllLocationByOrganization(Organization organization, User user) {
List<Location> locations = new ArrayList<Location>();
for (Location location : user.getLocations()) {
if (location.getOrganizations().contains(organization))
locations.add(location);
}
return locations;
}
private List<Area> getAllAreaByLocation(Location location, User user) {
List<Area> areas = new ArrayList<Area>();
for (Area area : user.getAreas()) {
if (area.getLocation().equals(location))
areas.add(area);
}
return areas;
}
public void addListenerUserChange(IUserChangeListener listener) {
this.userChangeListener = listener;
}
@AutoGenerated
private AbsoluteLayout buildMainLayout() {
// common part: create layout
mainLayout = new AbsoluteLayout();
mainLayout.setImmediate(false);
mainLayout.setWidth("300px");
mainLayout.setHeight("280px");
mainLayout.setMargin(false);
// top-level component properties
setWidth("300px");
setHeight("280px");
// profileLayout
profileLayout = buildVerticalProfileLayout();
mainLayout.addComponent(profileLayout, "top:0.0px;left:0.0px;");
return mainLayout;
}
@AutoGenerated
private VerticalLayout buildVerticalProfileLayout() {
// common part: create layout
profileLayout = new VerticalLayout();
profileLayout.setImmediate(false);
profileLayout.setWidth("100.0%");
profileLayout.setHeight("100.0%");
profileLayout.setMargin(true);
profileLayout.setSpacing(true);
// organizationField
organizationField = new ComboBox();
organizationField.setCaption("Organización");
organizationField.setNullSelectionAllowed(false);
organizationField.setImmediate(true);
organizationField.setWidth("100.0%");
organizationField.setHeight("-1px");
organizationField.setItemCaptionMode(Select.ITEM_CAPTION_MODE_PROPERTY);
organizationField.setItemCaptionPropertyId("name");
organizationField.setContainerDataSource(bcOrganization);
organizationField.addListener(new ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
if (organizationField.getValue() != null) {
try {
bcLocation.removeAllItems();
bcLocation.addAll(getAllLocationByOrganization((Organization)organizationField.getValue(), user));
if (bcLocation.getItemIds().size() > 0)
locationField.setValue(bcLocation.getIdByIndex(0));
} catch (Exception e) {
getWindow().showNotification(
"¡Error refrescando Localizacionesl!",
"",
Notification.TYPE_WARNING_MESSAGE);
}
}
}
});
profileLayout.addComponent(organizationField);
// locationField
locationField = new ComboBox();
locationField.setCaption("Localización");
locationField.setNullSelectionAllowed(false);
locationField.setImmediate(true);
locationField.setWidth("100.0%");
locationField.setHeight("-1px");
locationField.setItemCaptionMode(Select.ITEM_CAPTION_MODE_PROPERTY);
locationField.setItemCaptionPropertyId("name");
locationField.setContainerDataSource(bcLocation);
locationField.addListener(new ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
if (locationField.getValue() != null) {
try {
bcArea.removeAllItems();
bcArea.addAll(getAllAreaByLocation((Location)locationField.getValue(), user));
if (bcArea.getItemIds().size() > 0)
areaField.setValue(bcArea.getIdByIndex(0));
} catch (Exception e) {
getWindow().showNotification(
"¡Error refrescando areas!",
"",
Notification.TYPE_WARNING_MESSAGE);
}
}
}
});
profileLayout.addComponent(locationField);
// areaField
areaField = new ComboBox();
areaField.setCaption("Area");
areaField.setNullSelectionAllowed(false);
areaField.setImmediate(true);
areaField.setWidth("100.0%");
areaField.setHeight("-1px");
areaField.setNullSelectionAllowed(false);
areaField.setItemCaptionMode(Select.ITEM_CAPTION_MODE_PROPERTY);
areaField.setItemCaptionPropertyId("name");
areaField.setContainerDataSource(bcArea);
profileLayout.addComponent(areaField);
// languageField
languageField = new ComboBox();
languageField.setCaption("Lenguaje");
languageField.setImmediate(true);
languageField.setNullSelectionAllowed(false);
languageField.setWidth("100.0%");
languageField.setHeight("-1px");
profileLayout.addComponent(languageField);
// defaultSettings
defaultSettings = new CheckBox();
defaultSettings.setCaption("Por defecto");
defaultSettings.setImmediate(false);
defaultSettings.setWidth("-1px");
defaultSettings.setHeight("-1px");
profileLayout.addComponent(defaultSettings);
// horizontalProfileLayout
horizontalProfileLayout = buildHorizontalProfileLayout();
profileLayout.addComponent(horizontalProfileLayout);
profileLayout.setExpandRatio(horizontalProfileLayout, 1.0f);
profileLayout.setComponentAlignment(horizontalProfileLayout, new Alignment(6));
return profileLayout;
}
@AutoGenerated
private HorizontalLayout buildHorizontalProfileLayout() {
// common part: create layout
horizontalProfileLayout = new HorizontalLayout();
horizontalProfileLayout.setImmediate(false);
horizontalProfileLayout.setWidth("-1px");
horizontalProfileLayout.setHeight("100.0%");
horizontalProfileLayout.setMargin(false);
horizontalProfileLayout.setSpacing(true);
// applySettingsButton
applySettingsButton = new Button();
applySettingsButton.setCaption("Aplicar");
applySettingsButton.setImmediate(false);
applySettingsButton.setWidth("-1px");
applySettingsButton.setHeight("-1px");
horizontalProfileLayout.addComponent(applySettingsButton);
horizontalProfileLayout.setComponentAlignment(applySettingsButton,
new Alignment(34));
// cancelSettingsButton
cancelSettingsButton = new Button();
cancelSettingsButton.setCaption("Cancelar");
cancelSettingsButton.setImmediate(false);
cancelSettingsButton.setWidth("-1px");
cancelSettingsButton.setHeight("-1px");
horizontalProfileLayout.addComponent(cancelSettingsButton);
horizontalProfileLayout.setExpandRatio(cancelSettingsButton, 1.0f);
horizontalProfileLayout.setComponentAlignment(cancelSettingsButton,
new Alignment(33));
return horizontalProfileLayout;
}
@Override
protected void updateLabels() {
organizationField.setCaption(getI18N().getMessage("com.thingtrack.konekti.view.layout.menu.internal.ProfileSettingsViewForm.organizationField.caption"));
locationField.setCaption(getI18N().getMessage("com.thingtrack.konekti.view.layout.menu.internal.ProfileSettingsViewForm.locationField.caption"));
areaField.setCaption(getI18N().getMessage("com.thingtrack.konekti.view.layout.menu.internal.ProfileSettingsViewForm.areaField.caption"));
defaultSettings.setCaption(getI18N().getMessage("com.thingtrack.konekti.view.layout.menu.internal.cancelSettingsButton.defaultSettings.caption"));
applySettingsButton.setCaption(getI18N().getMessage("com.thingtrack.konekti.view.layout.menu.internal.ProfileSettingsViewForm.applySettingsButton.caption"));
applySettingsButton.setDescription(getI18N().getMessage("com.thingtrack.konekti.view.layout.menu.internal.ProfileSettingsViewForm.applySettingsButton.description"));
cancelSettingsButton.setCaption(getI18N().getMessage("com.thingtrack.konekti.view.layout.menu.internal.ProfileSettingsViewForm.cancelSettingsButton.caption"));
cancelSettingsButton.setDescription(getI18N().getMessage("com.thingtrack.konekti.view.layout.menu.internal.ProfileSettingsViewForm.cancelSettingsButton.description"));
}
}