org.teamapps.ux.component.field.Button Maven / Gradle / Ivy
/*-
* ========================LICENSE_START=================================
* TeamApps
* ---
* Copyright (C) 2014 - 2024 TeamApps.org
* ---
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =========================LICENSE_END==================================
*/
package org.teamapps.ux.component.field;
import org.teamapps.common.format.Color;
import org.teamapps.data.extract.BeanPropertyExtractor;
import org.teamapps.data.extract.PropertyExtractor;
import org.teamapps.data.extract.PropertyProvider;
import org.teamapps.dto.UiButton;
import org.teamapps.dto.UiEvent;
import org.teamapps.dto.UiField;
import org.teamapps.event.Event;
import org.teamapps.icons.Icon;
import org.teamapps.ux.component.Component;
import org.teamapps.ux.component.template.BaseTemplate;
import org.teamapps.ux.component.template.BaseTemplateRecord;
import org.teamapps.ux.component.template.Template;
public class Button extends AbstractField {
public final Event onClicked = new Event<>();
public final Event onDropDownOpened = new Event<>();
private Template template; // null: toString!
private RECORD templateRecord;
private PropertyProvider propertyProvider = new BeanPropertyExtractor<>();
private boolean openDropDownIfNotSet = false;
private Component dropDownComponent;
private Integer minDropDownWidth = null;
private Integer minDropDownHeight = 300;
private String onClickJavaScript;
public Button(Template template, RECORD templateRecord, Component dropDownComponent) {
super();
this.template = template;
this.templateRecord = templateRecord;
this.dropDownComponent = dropDownComponent;
}
public Button(Template template, RECORD templateRecord) {
this(template, templateRecord, null);
}
public static Button create(Template template, Icon icon, String caption, Component dropDownComponent) {
return new Button<>(template, new BaseTemplateRecord(icon, caption), dropDownComponent);
}
public static Button create(Template template, Icon icon, String caption) {
return create(template, icon, caption, null);
}
public static Button create(Template template, String caption) {
return create(template, null, caption, null);
}
public static Button create(Icon icon, String caption, Component dropDownComponent) {
return create(BaseTemplate.BUTTON, icon, caption, dropDownComponent);
}
public static Button create(String caption, Component dropDownComponent) {
return create(BaseTemplate.BUTTON, null, caption, dropDownComponent);
}
public static Button create(Icon icon, String caption) {
return create(BaseTemplate.BUTTON, icon, caption, null);
}
public static Button create(String caption) {
return create(BaseTemplate.BUTTON, null, caption, null);
}
@Override
public UiField createUiComponent() {
Object uiRecord = createUiRecord();
UiButton ui = new UiButton(template != null ? template.createUiTemplate() : null, uiRecord);
mapAbstractFieldAttributesToUiField(ui);
ui.setDropDownComponent(Component.createUiClientObjectReference(dropDownComponent));
ui.setMinDropDownWidth(minDropDownWidth != null ? minDropDownWidth : 0);
ui.setMinDropDownHeight(minDropDownHeight != null ? minDropDownHeight : 0);
ui.setOpenDropDownIfNotSet(this.openDropDownIfNotSet);
ui.setOnClickJavaScript(onClickJavaScript);
return ui;
}
private Object createUiRecord() {
Object uiRecord;
if (template != null) {
uiRecord = propertyProvider.getValues(templateRecord, template.getPropertyNames());
} else {
uiRecord = templateRecord.toString();
}
return uiRecord;
}
@Override
public void handleUiEvent(UiEvent event) {
super.handleUiEvent(event);
switch (event.getUiEventType()) {
case UI_BUTTON_CLICKED: {
if (isVisible()) {
this.onClicked.fire();
}
break;
}
case UI_BUTTON_DROP_DOWN_OPENED: {
if (isVisible()) {
this.onDropDownOpened.fire(null);
}
break;
}
}
}
public Template getTemplate() {
return template;
}
public Button setTemplate(Template template) {
this.template = template;
queueCommandIfRendered(() -> new UiButton.SetTemplateCommand(getId(), template.createUiTemplate(), createUiRecord()));
return this;
}
public RECORD getTemplateRecord() {
return templateRecord;
}
public Button setTemplateRecord(RECORD templateRecord) {
this.templateRecord = templateRecord;
queueCommandIfRendered(() -> new UiButton.SetTemplateRecordCommand(getId(), templateRecord));
return this;
}
public Button setColor(Color color) {
this.setCssStyle(".btn", "background-color", color != null ? color.toHtmlColorString() : null);
return this;
}
public PropertyProvider getPropertyProvider() {
return propertyProvider;
}
public void setPropertyProvider(PropertyProvider propertyProvider) {
this.propertyProvider = propertyProvider;
}
public void setPropertyExtractor(PropertyExtractor propertyExtractor) {
this.propertyProvider = propertyExtractor;
}
public Integer getMinDropDownWidth() {
return minDropDownWidth;
}
public Button setMinDropDownWidth(Integer minDropDownWidth) {
this.minDropDownWidth = minDropDownWidth;
queueCommandIfRendered(() -> new UiButton.SetDropDownSizeCommand(getId(), minDropDownWidth != null ? minDropDownWidth : 0, minDropDownHeight != null ? minDropDownHeight : 0));
return this;
}
public Integer getMinDropDownHeight() {
return minDropDownHeight;
}
public Button setMinDropDownHeight(Integer minDropDownHeight) {
this.minDropDownHeight = minDropDownHeight;
queueCommandIfRendered(() -> new UiButton.SetDropDownSizeCommand(getId(), minDropDownWidth != null ? minDropDownWidth : 0, minDropDownHeight != null ? minDropDownHeight : 0));
return this;
}
public Button setMinDropDownSize(Integer minDropDownWidth, Integer minDropDownHeight) {
this.minDropDownWidth = minDropDownWidth;
this.minDropDownHeight = minDropDownHeight;
queueCommandIfRendered(() -> new UiButton.SetDropDownSizeCommand(getId(), minDropDownWidth, minDropDownHeight));
return this;
}
public boolean isOpenDropDownIfNotSet() {
return openDropDownIfNotSet;
}
public Button setOpenDropDownIfNotSet(boolean openDropDownIfNotSet) {
this.openDropDownIfNotSet = openDropDownIfNotSet;
queueCommandIfRendered(() -> new UiButton.SetOpenDropDownIfNotSetCommand(getId(), openDropDownIfNotSet));
return this;
}
public Component getDropDownComponent() {
return dropDownComponent;
}
public Button setDropDownComponent(Component dropDownComponent) {
this.dropDownComponent = dropDownComponent;
queueCommandIfRendered(() -> new UiButton.SetDropDownComponentCommand(getId(), Component.createUiClientObjectReference(dropDownComponent)));
return this;
}
public void closeDropDown() {
queueCommandIfRendered(() -> new UiButton.CloseDropDownCommand(getId()));
}
public String getOnClickJavaScript() {
return onClickJavaScript;
}
public void setOnClickJavaScript(String onClickJavaScript) {
this.onClickJavaScript = onClickJavaScript;
queueCommandIfRendered(() -> new UiButton.SetOnClickJavaScriptCommand(getId(), onClickJavaScript));
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy