Skip to content

Commit 20c1e70

Browse files
committed
updated property controls API.
1 parent aa368e0 commit 20c1e70

File tree

9 files changed

+281
-18
lines changed

9 files changed

+281
-18
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ dependencies {
5959
compile 'org.controlsfx:controlsfx:8.40.13'
6060

6161
compile 'com.github.JavaSaBr:RlibFX:4.1.3'
62-
compile 'com.github.JavaSaBr:RLib:6.4.0'
62+
compile 'com.github.JavaSaBr:RLib:6.4.1'
6363
compile 'com.github.JavaSaBr:JME3-JFX:1.6.1'
6464

6565
// https://mvnrepository.com/artifact/org.apache.commons/commons-lang3

src/main/java/com/ss/editor/ui/control/property/PropertyControl.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,16 @@ protected void createComponents() {
332332
FXUtils.addToPane(container, this);
333333
}
334334

335+
/**
336+
* Get the property name label.
337+
*
338+
* @return the property name label.
339+
*/
340+
@FXThread
341+
protected @NotNull Label getPropertyNameLabel() {
342+
return propertyNameLabel;
343+
}
344+
335345
/**
336346
* Change control width percent.
337347
*
@@ -344,6 +354,7 @@ public void changeControlWidthPercent(final double controlWidthPercent) {
344354
return;
345355
}
346356

357+
final Label propertyNameLabel = getPropertyNameLabel();
347358
propertyNameLabel.maxWidthProperty().unbind();
348359
propertyNameLabel.maxWidthProperty().bind(widthProperty().multiply(1D - controlWidthPercent));
349360
}

src/main/java/com/ss/editor/ui/control/property/impl/BooleanPropertyControl.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
import com.ss.editor.ui.css.CSSClasses;
99
import com.ss.rlib.function.SixObjectConsumer;
1010
import com.ss.rlib.ui.util.FXUtils;
11+
import javafx.beans.property.DoubleProperty;
1112
import javafx.scene.control.CheckBox;
13+
import javafx.scene.control.Label;
1214
import javafx.scene.layout.HBox;
1315
import javafx.scene.layout.Region;
1416
import org.jetbrains.annotations.NotNull;
@@ -55,14 +57,32 @@ protected void createComponents(@NotNull final HBox container) {
5557
FXUtils.addClassTo(checkBox, CSSClasses.ABSTRACT_PARAM_CONTROL_CHECK_BOX);
5658
}
5759

60+
@Override
61+
@FXThread
62+
public void changeControlWidthPercent(final double controlWidthPercent) {
63+
64+
final CheckBox checkBox = getCheckBox();
65+
final DoubleProperty widthProperty = checkBox.prefWidthProperty();
66+
67+
if (widthProperty.isBound()) {
68+
super.changeControlWidthPercent(controlWidthPercent);
69+
widthProperty.unbind();
70+
widthProperty.bind(widthProperty().multiply(controlWidthPercent));
71+
}
72+
}
73+
5874
/**
5975
* Disable the offset of checkbox control.
6076
*/
6177
@FXThread
6278
public void disableCheckboxOffset() {
79+
6380
final CheckBox checkBox = getCheckBox();
6481
checkBox.prefWidthProperty().unbind();
6582
checkBox.setPrefWidth(Region.USE_COMPUTED_SIZE);
83+
84+
final Label propertyNameLabel = getPropertyNameLabel();
85+
propertyNameLabel.maxWidthProperty().unbind();
6686
}
6787

6888
@Override
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package com.ss.editor.ui.control.property.impl;
2+
3+
import static com.ss.rlib.util.ObjectUtils.notNull;
4+
import com.ss.editor.annotation.FXThread;
5+
import com.ss.editor.annotation.FromAnyThread;
6+
import com.ss.editor.model.undo.editor.ChangeConsumer;
7+
import com.ss.editor.ui.control.property.PropertyControl;
8+
import com.ss.editor.ui.css.CSSClasses;
9+
import com.ss.rlib.function.SixObjectConsumer;
10+
import com.ss.rlib.ui.util.FXUtils;
11+
import com.ss.rlib.util.ArrayUtils;
12+
import com.ss.rlib.util.StringUtils;
13+
import javafx.scene.control.TextField;
14+
import javafx.scene.input.KeyCode;
15+
import javafx.scene.input.KeyEvent;
16+
import javafx.scene.layout.HBox;
17+
import org.jetbrains.annotations.NotNull;
18+
import org.jetbrains.annotations.Nullable;
19+
20+
import java.util.function.BiConsumer;
21+
22+
/**
23+
* The implementation of the {@link PropertyControl} to edit float array values.
24+
*
25+
* @param <C> the type parameter
26+
* @param <T> the type parameter
27+
* @author JavaSaBr
28+
*/
29+
public class FloatArrayPropertyControl<C extends ChangeConsumer, T> extends PropertyControl<C, T, float[]> {
30+
31+
/**
32+
* The filed with current value.
33+
*/
34+
@Nullable
35+
private TextField valueField;
36+
37+
public FloatArrayPropertyControl(@Nullable final float[] propertyValue, @NotNull final String propertyName,
38+
@NotNull final C changeConsumer) {
39+
super(propertyValue, propertyName, changeConsumer);
40+
}
41+
42+
public FloatArrayPropertyControl(@Nullable final float[] propertyValue, @NotNull final String propertyName,
43+
@NotNull final C changeConsumer,
44+
@Nullable final SixObjectConsumer<C, T, String, float[], float[], BiConsumer<T, float[]>> changeHandler) {
45+
super(propertyValue, propertyName, changeConsumer, changeHandler);
46+
}
47+
48+
@Override
49+
@FXThread
50+
public void changeControlWidthPercent(final double controlWidthPercent) {
51+
super.changeControlWidthPercent(controlWidthPercent);
52+
53+
final TextField valueField = getValueField();
54+
valueField.prefWidthProperty().unbind();
55+
valueField.prefWidthProperty().bind(widthProperty().multiply(controlWidthPercent));
56+
}
57+
58+
@Override
59+
@FXThread
60+
protected void createComponents(@NotNull final HBox container) {
61+
super.createComponents(container);
62+
63+
valueField = new TextField();
64+
valueField.setOnKeyReleased(this::updateValue);
65+
valueField.prefWidthProperty()
66+
.bind(widthProperty().multiply(CONTROL_WIDTH_PERCENT));
67+
68+
FXUtils.addClassTo(valueField, CSSClasses.ABSTRACT_PARAM_CONTROL_COMBO_BOX);
69+
FXUtils.addToPane(valueField, container);
70+
}
71+
72+
@Override
73+
@FromAnyThread
74+
protected boolean isSingleRow() {
75+
return true;
76+
}
77+
78+
/**
79+
* @return the filed with current value.
80+
*/
81+
@FXThread
82+
private @NotNull TextField getValueField() {
83+
return notNull(valueField);
84+
}
85+
86+
@Override
87+
@FXThread
88+
protected void reload() {
89+
90+
final float[] element = getPropertyValue();
91+
92+
final TextField valueField = getValueField();
93+
final int caretPosition = valueField.getCaretPosition();
94+
95+
if (element == null) {
96+
valueField.setText(StringUtils.EMPTY);
97+
} else {
98+
valueField.setText(ArrayUtils.toString(element, " ", false, false));
99+
}
100+
101+
valueField.positionCaret(caretPosition);
102+
}
103+
104+
/**
105+
* Update the value.
106+
*/
107+
@FXThread
108+
private void updateValue(@Nullable final KeyEvent event) {
109+
if (isIgnoreListener() || (event != null && event.getCode() != KeyCode.ENTER)) return;
110+
111+
final String textValue = getValueField().getText();
112+
float[] newValue = null;
113+
114+
if (!StringUtils.isEmpty(textValue)) {
115+
116+
final String splitter = textValue.contains(" ") ? " " : ",";
117+
final String[] splited = textValue.split(splitter);
118+
119+
newValue = new float[splited.length];
120+
121+
for (int i = 0; i < splited.length; i++) {
122+
try {
123+
newValue[i] = Float.parseFloat(splited[i]);
124+
} catch (final NumberFormatException e) {
125+
LOGGER.warning(this, e);
126+
newValue = getPropertyValue();
127+
break;
128+
}
129+
}
130+
}
131+
132+
changed(newValue, getPropertyValue());
133+
}
134+
}

src/main/java/com/ss/editor/ui/control/property/impl/IntArrayPropertyControl.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ public IntArrayPropertyControl(@Nullable final int[] propertyValue, @NotNull fin
4545
super(propertyValue, propertyName, changeConsumer, changeHandler);
4646
}
4747

48+
@Override
49+
@FXThread
50+
public void changeControlWidthPercent(final double controlWidthPercent) {
51+
super.changeControlWidthPercent(controlWidthPercent);
52+
53+
final TextField valueField = getValueField();
54+
valueField.prefWidthProperty().unbind();
55+
valueField.prefWidthProperty().bind(widthProperty().multiply(controlWidthPercent));
56+
}
57+
4858
@Override
4959
@FXThread
5060
protected void createComponents(@NotNull final HBox container) {

src/main/java/com/ss/editor/ui/control/property/impl/Texture2DPropertyControl.java

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ public class Texture2DPropertyControl<C extends ChangeConsumer, T> extends Prope
9090
@Nullable
9191
private Label textureLabel;
9292

93+
/**
94+
* The field container.
95+
*/
96+
@Nullable
97+
private HBox fieldContainer;
98+
9399
public Texture2DPropertyControl(@Nullable final Texture2D propertyValue, @NotNull final String propertyName,
94100
@NotNull final C changeConsumer) {
95101
super(propertyValue, propertyName, changeConsumer);
@@ -127,7 +133,12 @@ protected void handleDragOverEvent(@NotNull final DragEvent dragEvent) {
127133
protected void createComponents(@NotNull final HBox container) {
128134
super.createComponents(container);
129135

130-
textureLabel = new Label(NO_TEXTURE);
136+
fieldContainer = new HBox();
137+
138+
if (!isSingleRow()) {
139+
fieldContainer.prefWidthProperty().bind(container.widthProperty());
140+
}
141+
131142
textureTooltip = new ImageChannelPreview();
132143

133144
final VBox previewContainer = new VBox();
@@ -152,23 +163,32 @@ protected void createComponents(@NotNull final HBox container) {
152163
removeButton.setOnAction(event -> processRemove());
153164
removeButton.disableProperty().bind(buildDisableRemoveCondition());
154165

155-
textureLabel.prefWidthProperty().bind(widthProperty()
156-
.subtract(removeButton.widthProperty())
157-
.subtract(previewContainer.widthProperty())
158-
.subtract(settingsButton.widthProperty())
159-
.subtract(addButton.widthProperty()));
160-
161-
FXUtils.addToPane(textureLabel, container);
162-
FXUtils.addToPane(previewContainer, container);
163-
FXUtils.addToPane(addButton, container);
164-
FXUtils.addToPane(settingsButton, container);
165-
FXUtils.addToPane(removeButton, container);
166+
if (!isSingleRow()) {
167+
168+
textureLabel = new Label(NO_TEXTURE);
169+
textureLabel.prefWidthProperty().bind(widthProperty()
170+
.subtract(removeButton.widthProperty())
171+
.subtract(previewContainer.widthProperty())
172+
.subtract(settingsButton.widthProperty())
173+
.subtract(addButton.widthProperty()));
174+
175+
FXUtils.addToPane(textureLabel, fieldContainer);
176+
FXUtils.addClassTo(textureLabel, CSSClasses.ABSTRACT_PARAM_CONTROL_ELEMENT_LABEL);
177+
FXUtils.addClassesTo(fieldContainer, CSSClasses.TEXT_INPUT_CONTAINER,
178+
CSSClasses.ABSTRACT_PARAM_CONTROL_INPUT_CONTAINER);
179+
180+
} else {
181+
FXUtils.addClassesTo(fieldContainer, CSSClasses.TEXT_INPUT_CONTAINER_WITHOUT_PADDING);
182+
}
183+
184+
FXUtils.addToPane(previewContainer, fieldContainer);
185+
FXUtils.addToPane(addButton, fieldContainer);
186+
FXUtils.addToPane(settingsButton, fieldContainer);
187+
FXUtils.addToPane(removeButton, fieldContainer);
188+
FXUtils.addToPane(fieldContainer, container);
166189
FXUtils.addToPane(texturePreview, previewContainer);
167190

168-
FXUtils.addClassesTo(container, CSSClasses.DEF_HBOX, CSSClasses.TEXT_INPUT_CONTAINER,
169-
CSSClasses.ABSTRACT_PARAM_CONTROL_INPUT_CONTAINER);
170191
FXUtils.addClassTo(previewContainer, CSSClasses.ABSTRACT_PARAM_CONTROL_PREVIEW_CONTAINER);
171-
FXUtils.addClassTo(textureLabel, CSSClasses.ABSTRACT_PARAM_CONTROL_ELEMENT_LABEL);
172192
FXUtils.addClassesTo(settingsButton, addButton, removeButton, CSSClasses.FLAT_BUTTON,
173193
CSSClasses.INPUT_CONTROL_TOOLBAR_BUTTON);
174194
}
@@ -189,6 +209,16 @@ protected void createComponents(@NotNull final HBox container) {
189209
return notNull(textureLabel);
190210
}
191211

212+
/**
213+
* Get the field container.
214+
*
215+
* @return the field container.
216+
*/
217+
@FXThread
218+
protected @NotNull HBox getFieldContainer() {
219+
return notNull(fieldContainer);
220+
}
221+
192222
/**
193223
* @return the texture preview.
194224
*/
@@ -325,8 +355,10 @@ protected void reload() {
325355
final Texture2D texture2D = getPropertyValue();
326356
final AssetKey key = texture2D == null ? null : texture2D.getKey();
327357

328-
final Label textureLabel = getTextureLabel();
329-
textureLabel.setText(key == null ? NO_TEXTURE : key.getName());
358+
if (!isSingleRow()) {
359+
final Label textureLabel = getTextureLabel();
360+
textureLabel.setText(key == null ? NO_TEXTURE : key.getName());
361+
}
330362

331363
final ImageChannelPreview textureTooltip = getTextureTooltip();
332364
final ImageView preview = getTexturePreview();
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.ss.editor.ui.control.property.impl;
2+
3+
import com.jme3.texture.Texture2D;
4+
import com.ss.editor.annotation.FromAnyThread;
5+
import com.ss.editor.model.undo.editor.ChangeConsumer;
6+
import org.jetbrains.annotations.NotNull;
7+
import org.jetbrains.annotations.Nullable;
8+
9+
/**
10+
* The single row implementation of the control to edit textures.
11+
*
12+
* @param <C> the type of a {@link ChangeConsumer}
13+
* @param <T> the type of an editing object.
14+
* @author JavaSaBr
15+
*/
16+
public class Texture2DSingleRowPropertyControl<C extends ChangeConsumer, T> extends Texture2DPropertyControl<C, T> {
17+
18+
public Texture2DSingleRowPropertyControl(@Nullable final Texture2D propertyValue,
19+
@NotNull final String propertyName, @NotNull final C changeConsumer) {
20+
super(propertyValue, propertyName, changeConsumer);
21+
}
22+
23+
@Override
24+
@FromAnyThread
25+
protected boolean isSingleRow() {
26+
return true;
27+
}
28+
}

src/main/java/com/ss/editor/ui/css/CSSClasses.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ public interface CSSClasses {
7979
*/
8080
String TEXT_INPUT_CONTAINER = "text-input-container";
8181

82+
/**
83+
* The constant TEXT_INPUT_CONTAINER_WITHOUT_PADDING.
84+
*/
85+
String TEXT_INPUT_CONTAINER_WITHOUT_PADDING = "text-input-container-without-padding";
86+
8287
/**
8388
* The constant CHOOSE_TEXTURE_CONTROL.
8489
*/

0 commit comments

Comments
 (0)