Skip to content

Commit

Permalink
feat: remake PlaceholderHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
teletha committed Nov 9, 2024
1 parent fbc03c8 commit 292d10c
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 107 deletions.
35 changes: 4 additions & 31 deletions src/main/java/viewtify/ui/AbstractComboBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
*/
package viewtify.ui;

import java.util.Objects;

import javafx.beans.property.Property;
import javafx.beans.property.StringProperty;
import javafx.scene.Node;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListView;
import javafx.scene.control.skin.ComboBoxListViewSkin;

import viewtify.ui.helper.CollectableHelper;
import viewtify.ui.helper.ContextMenuHelper;
import viewtify.ui.helper.PlaceholderHelper;
Expand Down Expand Up @@ -83,33 +82,7 @@ public final Self toggle() {
* {@inheritDoc}
*/
@Override
public Self placeholder(Object text) {
comboBox().setPromptText(Objects.toString(text));
return (Self) this;
}

/**
* {@inheritDoc}
*/
@Override
public Self placeholder(Property text) {
comboBox().promptTextProperty().bind(text);
return (Self) this;
}

/**
* {@inheritDoc}
*/
@Override
public Self placeholder(UserInterfaceProvider text) {
throw new UnsupportedOperationException("Text field doesn't support the placeholder by node.");
}

/**
* {@inheritDoc}
*/
@Override
public Self placeholder(Node node) {
throw new UnsupportedOperationException("Text field doesn't support the placeholder by node.");
public StringProperty placeholderProperty() {
return comboBox().promptTextProperty();
}
}
5 changes: 2 additions & 3 deletions src/main/java/viewtify/ui/UIChoiceBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@
import javafx.beans.property.Property;
import javafx.collections.ObservableList;
import javafx.scene.control.ChoiceBox;

import viewtify.ui.helper.Actions;
import viewtify.ui.helper.CollectableHelper;
import viewtify.ui.helper.ContextMenuHelper;
import viewtify.ui.helper.PlaceholderHelper;
import viewtify.ui.helper.User;
import viewtify.ui.helper.ValueHelper;

public class UIChoiceBox<T> extends UserInterface<UIChoiceBox<T>, ChoiceBox<T>>
implements CollectableHelper<UIChoiceBox<T>, T>, ValueHelper<UIChoiceBox<T>, T>, ContextMenuHelper<UIChoiceBox<T>>,
PlaceholderHelper<UIChoiceBox<T>> {
implements CollectableHelper<UIChoiceBox<T>, T>, ValueHelper<UIChoiceBox<T>, T>, ContextMenuHelper<UIChoiceBox<T>> {

/**
* Builde {@link ChoiceBox}.
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/viewtify/ui/UIComboCheckBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package viewtify.ui;

import javafx.beans.property.Property;
import javafx.beans.property.StringProperty;
import javafx.collections.ObservableList;
import javafx.scene.control.ComboBox;

Expand Down Expand Up @@ -67,4 +68,12 @@ public UIComboCheckBox<T> title(CharSequence title) {
protected ComboBox<T> comboBox() {
return (ComboBox) ui.getChildrenUnmodifiable().get(0);
}

/**
* {@inheritDoc}
*/
@Override
public StringProperty placeholderProperty() {
return ui.titleProperty();
}
}
19 changes: 19 additions & 0 deletions src/main/java/viewtify/ui/UITableBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
*/
package viewtify.ui;

import javafx.beans.property.Property;
import javafx.beans.property.StringProperty;
import javafx.scene.Node;
import javafx.scene.control.Control;
import javafx.scene.control.Label;

import viewtify.ui.helper.CollectableHelper;
import viewtify.ui.helper.ContextMenuHelper;
import viewtify.ui.helper.PlaceholderHelper;
Expand All @@ -32,4 +37,18 @@ abstract class UITableBase<RowV, FXTable extends Control, Self extends UITableBa
protected UITableBase(FXTable ui, View view) {
super(ui, view);
}

/**
* {@inheritDoc}
*/
@Override
public StringProperty placeholderProperty() {
Property<Node> property = property(Type.Placeholder);
Label label = (Label) property.getValue();
if (label == null) {
label = new Label();
property.setValue(label);
}
return label.textProperty();
}
}
32 changes: 3 additions & 29 deletions src/main/java/viewtify/ui/UIText.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
import java.text.Normalizer.Form;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.regex.Pattern;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.Property;
import javafx.beans.property.StringProperty;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Node;
Expand Down Expand Up @@ -369,34 +369,8 @@ public final UIText<V> clearable() {
* {@inheritDoc}
*/
@Override
public UIText<V> placeholder(Object text) {
ui.setPromptText(Objects.toString(text));
return this;
}

/**
* {@inheritDoc}
*/
@Override
public UIText<V> placeholder(Property text) {
ui.promptTextProperty().bind(text);
return this;
}

/**
* {@inheritDoc}
*/
@Override
public UIText<V> placeholder(UserInterfaceProvider text) {
throw new UnsupportedOperationException("Text field doesn't support the placeholder by node.");
}

/**
* {@inheritDoc}
*/
@Override
public UIText<V> placeholder(Node node) {
throw new UnsupportedOperationException("Text field doesn't support the placeholder by node.");
public StringProperty placeholderProperty() {
return ui.promptTextProperty();
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/viewtify/ui/UITextArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.Property;
import javafx.scene.control.TextArea;

import viewtify.ui.helper.BlockHelper;
import viewtify.ui.helper.ContextMenuHelper;
import viewtify.ui.helper.EditableHelper;
import viewtify.ui.helper.PlaceholderHelper;
import viewtify.ui.helper.ValueHelper;
import viewtify.util.MonkeyPatch;

public class UITextArea extends UserInterface<UITextArea, TextArea>
implements ValueHelper<UITextArea, String>, BlockHelper<UITextArea>, ContextMenuHelper<UITextArea>, EditableHelper<UITextArea>,
PlaceholderHelper<UITextArea> {
implements ValueHelper<UITextArea, String>, BlockHelper<UITextArea>, ContextMenuHelper<UITextArea>, EditableHelper<UITextArea> {

/**
* Enchanced view.
Expand Down
51 changes: 12 additions & 39 deletions src/main/java/viewtify/ui/helper/PlaceholderHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,29 @@
*/
package viewtify.ui.helper;

import java.util.function.Consumer;

import javafx.beans.property.Property;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.beans.property.StringProperty;

import kiss.I;
import kiss.Variable;
import viewtify.Viewtify;
import viewtify.ui.UserInterfaceProvider;

public interface PlaceholderHelper<Self extends PlaceholderHelper> extends PropertyAccessHelper {

/**
* Set placeholder text.
* Get the placeholder property.
*
* @param text A text to set.
* @return Chainable API.
* @return
*/
default Self placeholder(Object text) {
return placeholder(new Label(String.valueOf(text)));
}
StringProperty placeholderProperty();

/**
* Set placeholder text.
*
* @param text A text {@link Variable} to set.
* @param text A text to set.
* @return Chainable API.
*/
default Self placeholder(Variable text) {
text.observing().on(Viewtify.UIThread).to((Consumer) this::placeholder);
return (Self) this;
default Self placeholder(Object text) {
return placeholder(Variable.of(text));
}

/**
Expand All @@ -48,29 +40,10 @@ default Self placeholder(Variable text) {
* @param text A text {@link Variable} to set.
* @return Chainable API.
*/
default Self placeholder(Property text) {
property(Type.Text).bindBidirectional(text);
return (Self) this;
}

/**
* Set the specified {@link Node} as literal component.
*
* @param text A literal component to set.
* @return Chainable API.
*/
default Self placeholder(UserInterfaceProvider text) {
return placeholder(text.ui().getStyleableNode());
}

/**
* Set the specified {@link Node} component.
*
* @param node A node component to set.
* @return Chainable API.
*/
default Self placeholder(Node node) {
property(Type.Placeholder).setValue(node);
default Self placeholder(Variable text) {
text.observing().on(Viewtify.UIThread).to(v -> {
placeholderProperty().set(I.transform(text, String.class));
});
return (Self) this;
}
}
5 changes: 4 additions & 1 deletion src/main/java/viewtify/ui/helper/PropertyAccessHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,14 @@ class Type<T> {
public static final Type<Double> Opacity = new Type("opacity");

/** The defined property type. */
public static final Type Orientation = new Type("orientation");;
public static final Type Orientation = new Type("orientation");

/** The defined property type. */
public static final Type<Node> Placeholder = new Type("placeholder");

/** The defined property type. */
public static final Type<String> PromptText = new Type("promptText");

/** The defined property type. */
public static final Type<SelectionModel> SelectionModel = new Type("selectionModel");

Expand Down
10 changes: 10 additions & 0 deletions src/test/java/viewtify/ui/Providers.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.junit.jupiter.params.provider.ArgumentsProvider;

import viewtify.ui.helper.LabelHelper;
import viewtify.ui.helper.PlaceholderHelper;

public class Providers {

Expand Down Expand Up @@ -89,4 +90,13 @@ public Stream<? extends Arguments> provideArguments(ExtensionContext context) th
}
}

public static class PlaceholderHelpers implements ArgumentsProvider {
/**
* {@inheritDoc}
*/
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) throws Exception {
return collect(PlaceholderHelper.class);
}
}
}
1 change: 0 additions & 1 deletion src/test/java/viewtify/ui/helper/LabelHelperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/
package viewtify.ui.helper;

import javafx.beans.property.SimpleStringProperty;
import javafx.scene.control.Label;
import javafx.scene.paint.Color;

Expand Down
40 changes: 40 additions & 0 deletions src/test/java/viewtify/ui/helper/PlaceholderHelperTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2023 Nameless Production Committee
*
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://opensource.org/licenses/mit-license.php
*/
package viewtify.ui.helper;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ArgumentsSource;

import kiss.Variable;
import viewtify.JavaFXTester;
import viewtify.ui.Providers;

public class PlaceholderHelperTest extends JavaFXTester {

@ParameterizedTest
@ArgumentsSource(Providers.PlaceholderHelpers.class)
void text(PlaceholderHelper<?> ui) {
ui.placeholder("TEST");
assert ui.placeholderProperty().getValue().equals("TEST");
}

@ParameterizedTest
@ArgumentsSource(Providers.PlaceholderHelpers.class)
void variable(PlaceholderHelper<?> ui) {
Variable<String> text = Variable.of("TEST");

ui.placeholder(text);
assert ui.placeholderProperty().getValue().equals("TEST");

// update from model
text.set("UPDATE");
assert ui.placeholderProperty().getValue().equals("UPDATE");
}
}

0 comments on commit 292d10c

Please sign in to comment.