Skip to content

Commit

Permalink
Merge pull request #65 from xpipe-io/master-11
Browse files Browse the repository at this point in the history
Create renderers lazily
  • Loading branch information
dlemmermann authored Jan 23, 2023
2 parents b4ca599 + feabcc9 commit 490975f
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Boolean fromString(String string) {
return Boolean.parseBoolean(string);
}
};
renderer = new SimpleBooleanControl();
rendererSupplier = () -> new SimpleBooleanControl();

userInput.set(stringConverter.toString(value.getValue()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public DateField(ObjectProperty<LocalDate> valueProperty, ObjectProperty<LocalDa

Chronology chronology = Chronology.ofLocale(Locale.getDefault(Locale.Category.FORMAT));
stringConverter = new LocalDateStringConverter(FormatStyle.SHORT, null, chronology);
renderer = new SimpleDateControl();
rendererSupplier = () -> new SimpleDateControl();
userInput.setValue(null);
userInput.setValue(stringConverter.toString((LocalDate) persistentValue.getValue()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public Double fromString(String string) {
return Double.parseDouble(string);
}
};
renderer = new SimpleDoubleControl();
rendererSupplier = () -> new SimpleDoubleControl();

userInput.set(stringConverter.toString(value.getValue()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -145,6 +146,7 @@ public abstract class Field<F extends Field<F>> extends Element<F> implements Fo
protected TranslationService translationService;

protected SimpleControl<F> renderer;
protected Supplier<SimpleControl<F>> rendererSupplier;

protected final Map<EventType<FieldEvent>,List<EventHandler<? super FieldEvent>>> eventHandlers = new ConcurrentHashMap<>();

Expand Down Expand Up @@ -633,6 +635,20 @@ public F render(SimpleControl<F> newValue) {
return (F) this;
}

/**
* Sets the control supplier that renders this field.
* The supplier is only called when required, i.e., when the GUI is created.
*
* @param newValue
* The new control supplier to render the field.
*
* @return Returns the current field to allow for chaining.
*/
public F render(Supplier<SimpleControl<F>> newValue) {
rendererSupplier = newValue;
return (F) this;
}

/**
* Activates or deactivates the {@code bindingModeListener} based on the
* given {@code BindingMode}.
Expand Down Expand Up @@ -794,6 +810,10 @@ public boolean isI18N() {
}

public SimpleControl<F> getRenderer() {
if (renderer == null) {
renderer = rendererSupplier.get();
}

return renderer;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Integer fromString(String string) {
return Integer.parseInt(string);
}
};
renderer = new SimpleIntegerControl();
rendererSupplier = () -> new SimpleIntegerControl();

userInput.set(stringConverter.toString(value.getValue()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ protected MultiSelectionField(ListProperty<V> items, List<Integer> selection) {
persistentSelection.clear();
});

renderer = new SimpleListViewControl<>();
rendererSupplier = () -> new SimpleListViewControl<>();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public String fromString(String string) {
return string;
}
};
renderer = new SimplePasswordControl();
rendererSupplier = () -> new SimplePasswordControl();

userInput.set(stringConverter.toString(value.getValue()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ protected SingleSelectionField(ListProperty<V> items, int selection) {
persistentSelection.setValue(null);
});

renderer = new SimpleComboBoxControl<>();
rendererSupplier = () -> new SimpleComboBoxControl<>();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public String fromString(String string) {
return string;
}
};
renderer = new SimpleTextControl();
rendererSupplier = () -> new SimpleTextControl();

userInput.set(stringConverter.toString(value.getValue()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private void createForm() {
.required("required_error_message")
.label("driving_label")
.span(ColSpan.HALF)
.render(new SimpleRadioButtonControl<>()),
.render(() -> new SimpleRadioButtonControl<>()),
Field.ofStringType(country.timeZoneProperty())
.label("time_zone_label")
.placeholder("time_zone_placeholder")
Expand Down Expand Up @@ -142,7 +142,7 @@ private void createForm() {
.label("continent_label")
.required("required_error_message")
.span(ColSpan.HALF)
.render(new SimpleCheckBoxControl<>()),
.render(() -> new SimpleCheckBoxControl<>()),
Field.ofMultiSelectionType(country.allCitiesProperty(), country.germanCitiesProperty())
.label("german_cities_label")
.span(ColSpan.HALF),
Expand Down

0 comments on commit 490975f

Please sign in to comment.