diff --git a/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/colorpicker/ColorPickerField.java b/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/colorpicker/ColorPickerField.java index 959965c..9b35da5 100644 --- a/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/colorpicker/ColorPickerField.java +++ b/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/colorpicker/ColorPickerField.java @@ -1,16 +1,31 @@ package com.merkle.oss.magnolia.definition.custom.colorpicker; +import java.util.Optional; +import java.util.stream.Stream; + +import javax.annotation.Nullable; + import com.vaadin.shared.ui.colorpicker.Color; +import com.vaadin.ui.CheckBox; import com.vaadin.ui.ColorPicker; import com.vaadin.ui.Component; import com.vaadin.ui.CustomField; +import com.vaadin.ui.HorizontalLayout; import com.vaadin.ui.Window; import com.vaadin.ui.components.colorpicker.ColorPickerPopup; public class ColorPickerField extends CustomField { private final ColorPicker colorPicker; + @Nullable + private final CheckBox noColorCheckbox; - public ColorPickerField(final ColorPickerFieldDefinition definition) { + public ColorPickerField(final ColorPickerFieldDefinition definition, final String noColorCheckboxLabel) { + if(!definition.isRequired()) { + noColorCheckbox = new CheckBox(); + noColorCheckbox.setCaption(noColorCheckboxLabel); + } else { + noColorCheckbox = null; + } colorPicker = new ColorPicker() { @Override protected void showPopup(final boolean open) { @@ -38,19 +53,24 @@ private void addPopupStyles(final Window window) { } @Override - protected void doSetValue(final Integer color) { - if(color != null) { - colorPicker.setValue(new Color(color)); - } + protected void doSetValue(@Nullable final Integer color) { + Optional.ofNullable(color).map(Color::new).ifPresent(colorPicker::setValue); + Optional.ofNullable(noColorCheckbox).ifPresent(checkbox -> checkbox.setValue(color == null)); } @Override protected Component initContent() { - return colorPicker; + return new HorizontalLayout(Stream.concat( + Stream.of(colorPicker), + Stream.ofNullable(noColorCheckbox) + ).toArray(Component[]::new)); } @Override public Integer getValue() { + if(Optional.ofNullable(noColorCheckbox).map(CheckBox::getValue).orElse(false)) { + return null; + } return this.colorPicker.getValue().getRGB(); } } diff --git a/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/colorpicker/ColorPickerFieldDefinition.java b/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/colorpicker/ColorPickerFieldDefinition.java index 7e002a4..fdb024d 100644 --- a/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/colorpicker/ColorPickerFieldDefinition.java +++ b/custom-definitions/src/main/java/com/merkle/oss/magnolia/definition/custom/colorpicker/ColorPickerFieldDefinition.java @@ -3,6 +3,8 @@ import info.magnolia.ui.field.ConfiguredFieldDefinition; import info.magnolia.ui.field.FieldType; +import javax.annotation.Nullable; + @FieldType("colorPickerField") public class ColorPickerFieldDefinition extends ConfiguredFieldDefinition { private boolean rgb = false; @@ -10,6 +12,7 @@ public class ColorPickerFieldDefinition extends ConfiguredFieldDefinition { final ColorPickerFieldDefinition definition; + private final LocaleContext localeContext; + private final TranslationService translationService; - public ColorPickerFieldFactory(final ColorPickerFieldDefinition definition, final ComponentProvider componentProvider) { + @Inject + public ColorPickerFieldFactory( + final ColorPickerFieldDefinition definition, + final ComponentProvider componentProvider, + final LocaleContext localeContext, + final TranslationService translationService + ) { super(definition, componentProvider); this.definition = definition; + this.localeContext = localeContext; + this.translationService = translationService; } @Override protected Component createFieldComponent() { - return new ColorPickerField(definition); + return new ColorPickerField(definition, getLabel()); + } + + private String getLabel() { + final String key = Optional + .ofNullable(definition.getNoColorCheckboxLabelKey()) + .orElse("merkle.customDefinitions.colorpicker.noColorCheckbox.fallbackLabel"); + return translationService.translate(new FixedLocaleProvider(localeContext.getCurrent()), new String[] { key }); } } diff --git a/custom-definitions/src/main/resources/mgnl-i18n/magnolia-custom-definitions-messages_en.properties b/custom-definitions/src/main/resources/mgnl-i18n/magnolia-custom-definitions-messages_en.properties index 5f5615e..b71c3a3 100644 --- a/custom-definitions/src/main/resources/mgnl-i18n/magnolia-custom-definitions-messages_en.properties +++ b/custom-definitions/src/main/resources/mgnl-i18n/magnolia-custom-definitions-messages_en.properties @@ -24,6 +24,8 @@ merkle.customDefinitions.videoSet.field.videoType.vimeo.label=Vimeo ID merkle.customDefinitions.videoSet.video.label=Video source merkle.customDefinitions.videoSet.previewImage.label=Preview image +merkle.customDefinitions.colorpicker.noColorCheckbox.fallbackLabel=No color + validators.mimeTypeValidator.errorMessage=Invalid mime type! Must be one of [{0}]. validators.nodeTypeValidator.errorMessage=Invalid nodeType! Must be of type {0}. validators.templateValidator.errorMessage=Invalid template! Must be of template {0}.