gpt4 book ai didi

java - GWT TextBox、DateBox 等不共享相同的基本输入类

转载 作者:行者123 更新时间:2023-12-01 16:53:57 27 4
gpt4 key购买 nike

我目前正在开发一个现有的 GWT 项目。在此项目中,我在 View 上添加了一个列表,如下所示:

  • 标签1:[文本框]
  • 标签2:[日期框]
  • 标签3:[文本框]
  • 标签4:[文本框]
  • 标签5:[日期框]
  • 标签6:[文本框]

我制作了一个从 FlowPanel 派生的小部件,在其中创建了这些 Label-InputBox 对:

import java.sql.Date;

import com.google.gwt.dom.client.Style.Display;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;

public class LabelInputWidget extends FlowPanel {

private final Label label;
private final Widget inputField;

private static final int LABEL_AND_INPUTFIELD_WIDTH = 200;

public LabelInputWidget(final String labelText, final Widget inputField) {
super();

this.label = new Label(labelText);
this.label.getElement().getStyle().setDisplay(Display.INLINE_BLOCK); // Used to put the inputfield next to the label, instead of below it
this.label.setWidth(LABEL_AND_INPUTFIELD_WIDTH + "px");
this.add(this.label);

this.inputField = inputField;
// The DateBox width looks smaller when the same width is set, so we'll add 4 pixels to line it up with the other Input-Widgets
int inputWidth = LABEL_AND_INPUTFIELD_WIDTH;
if (inputField instanceof DateBox) {
inputWidth += 4;
}
this.inputField.setWidth(inputWidth + "px");
this.add(this.inputField);
}

// TODO KC: Use a less ugly way of determine which InputField is used
// POTENTIAL TODO KC: Use custom widgets for Numeric / Currency input fields
public void setInputValue(final String value) {
if (this.inputField instanceof TextBox) {
((TextBox) this.inputField).setValue(value);
} else if (this.inputField instanceof DateBox) {
((DateBox) this.inputField).setValue(value != null && value.length() > 0 ? Date.valueOf(value) : null);
}
}
}

在我创建这些字段的文件中,我有以下内容:

private void fillEditedProperties(final List<Property> list) {
for (final Property p : list) {
if (!containsProperty(p)) {
this.editedProperties.add(p);

// The moment the property is loaded and known, we add it to the view
Widget inputWidget = new TextBox();
if (Type.DATUM.name().equals(p.getPdf().getType().name())) {
final DateBox dateBox = new DateBox();
dateBox.setFormat(new DateBox.DefaultFormat(DateTimeFormat.getFormat("dd-MM-yyyy")));
inputWidget = new DateBox();
}
final LabelInputWidget labelInput = new LabelInputWidget(p.getPdf().getName(), inputWidget);
labelInput.setInputValue(p.getValue());
labelInput.getElement().getStyle().setDisplay(Display.BLOCK); // Used to put the LabelInputWidgets below each other
this.labelInputFieldsContainer.add(labelInput);
}
}
}

而且我还想在用户输入内容(TODO/正在进行的工作)时验证输入。

正如您所看到的,使用常规的 com.google.gwt.user.client.ui.Widget 作为 TextBox 和 DateBox 的共享基类是非常丑陋的。因为它们不共享类似 InputBox 类的东西,所以我必须在确定使用哪个 (Input)Widget 之后设置它们的值,并且可能对 ValueChangedHandlers 执行相同的操作以验证我即将创建的用户输入。

有谁知道更好的解决方案,可以在同一个LabelInputWidgets列表中使用TextBoxes和DateBoxes,但不要在几乎所有地方使用丑陋的instanceof,而只需使用一种通用方法?

如果不是,我可能会将此行为隐藏在某个地方,以使代码更具可读性..

PS:我不是 100% 确定这是一个适合 StackOverflow 的问题,所以我在 CodeReview.StackExchange.com 上提出了一个交叉问题。以及。我也将其发布在这里,但有两个原因:

  • 有人可能会以完全不同的方式找到合适的解决方案。
  • 这里的社区更大,所以我可能会更快得到答案..

最佳答案

你可以使用

public class LabelInputWidget<T> extends FlowPanel {

private final Label label;
private final HasValue<T> inputField;

...

哪里<T>可以是字符串或日期。

关于java - GWT TextBox、DateBox 等不共享相同的基本输入类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35365674/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com