Skip to content

Commit b2bd3cc

Browse files
committed
Externalize the Marker for validation for easy tooltip customization
1 parent 385ff79 commit b2bd3cc

5 files changed

Lines changed: 272 additions & 161 deletions

File tree

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
package org.int4.fx.controls.validation;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
import java.util.Objects;
7+
8+
import javafx.beans.property.ObjectProperty;
9+
import javafx.beans.property.ObjectPropertyBase;
10+
import javafx.beans.property.ReadOnlyObjectProperty;
11+
import javafx.beans.property.ReadOnlyObjectWrapper;
12+
import javafx.css.CssMetaData;
13+
import javafx.css.Styleable;
14+
import javafx.css.StyleableObjectProperty;
15+
import javafx.css.StyleableProperty;
16+
import javafx.css.converter.FontConverter;
17+
import javafx.scene.control.Tooltip;
18+
import javafx.scene.layout.StackPane;
19+
import javafx.scene.text.Font;
20+
21+
/**
22+
* Represents a validation marker that can be further customized. It exposes
23+
* a tooltip property and the current validation issue associated with the marker.
24+
* <p>
25+
* The marker also has a CSS {@code -fx-font} property; this allows children of
26+
* the marker to use relative {@code em} units in CSS, which will resolve against
27+
* the font set on this marker.
28+
*/
29+
public class Marker extends StackPane {
30+
private final ObjectProperty<Font> font = new StyleableObjectProperty<>(Font.getDefault()) {
31+
@Override
32+
public CssMetaData<Marker, Font> getCssMetaData() {
33+
return FONT;
34+
}
35+
36+
@Override
37+
public Object getBean() {
38+
return Marker.this;
39+
}
40+
41+
@Override
42+
public String getName() {
43+
return "font";
44+
}
45+
};
46+
47+
private final ObjectProperty<Tooltip> tooltip = new ObjectPropertyBase<>() {
48+
private Tooltip old;
49+
50+
@Override
51+
protected void invalidated() {
52+
Tooltip t = get();
53+
// install / uninstall
54+
if(t != old) {
55+
if(old != null) {
56+
Tooltip.uninstall(Marker.this, old);
57+
}
58+
59+
if(t != null) {
60+
Tooltip.install(Marker.this, t);
61+
}
62+
63+
old = t;
64+
}
65+
}
66+
67+
@Override
68+
public Object getBean() {
69+
return Marker.this;
70+
}
71+
72+
@Override
73+
public String getName() {
74+
return "tooltip";
75+
}
76+
};
77+
78+
private final ReadOnlyObjectWrapper<ValidationIssue<?>> validationIssue = new ReadOnlyObjectWrapper<>(this, "validation-issue");
79+
80+
Marker(ValidationIssue<?> validationIssue) {
81+
Objects.requireNonNull(validationIssue, "validationIssue");
82+
83+
this.validationIssue.set(validationIssue);
84+
}
85+
86+
/**
87+
* Sets the font for this pane.
88+
*
89+
* @param value the {@link Font} to set, can be {@code null}
90+
*/
91+
public final void setFont(Font value) {
92+
fontProperty().set(value);
93+
}
94+
95+
/**
96+
* Returns the font for this pane.
97+
*
98+
* @return the {@link Font}, can be {@code null}
99+
*/
100+
public final Font getFont() {
101+
return font.get();
102+
}
103+
104+
/**
105+
* Returns the font property for this pane.
106+
*
107+
* @return the font property, never {@code null}
108+
*/
109+
public final ObjectProperty<Font> fontProperty() {
110+
return font;
111+
}
112+
113+
/**
114+
* Sets the tooltip for this pane.
115+
*
116+
* @param value the tooltip to set, can be {@code null}
117+
*/
118+
public final void setTooltip(Tooltip value) {
119+
tooltipProperty().setValue(value);
120+
}
121+
122+
/**
123+
* Returns the validation issue for this pane.
124+
*
125+
* @return the {@link ValidationIssue}, never {@code null}
126+
*/
127+
public final ValidationIssue<?> getValidationIssue() {
128+
return validationIssue.get();
129+
}
130+
131+
/**
132+
* Returns the validation issue property for this pane.
133+
*
134+
* @return the validation issue property, never {@code null}
135+
*/
136+
public final ReadOnlyObjectProperty<ValidationIssue<?>> validationIssueProperty() {
137+
return validationIssue.getReadOnlyProperty();
138+
}
139+
140+
/**
141+
* Sets the validation issue for this pane.
142+
*
143+
* @param value the validation issue to set, can be {@code null}
144+
*/
145+
final void setValidationIssue(ValidationIssue<?> value) {
146+
validationIssue.setValue(Objects.requireNonNull(value, "value"));
147+
}
148+
149+
/**
150+
* Returns the tooltip for this pane.
151+
*
152+
* @return the {@link Tooltip}, can be {@code null}
153+
*/
154+
public final Tooltip getTooltip() {
155+
return tooltip.get();
156+
}
157+
158+
/**
159+
* Returns the tooltip property for this pane.
160+
*
161+
* @return the tooltip property, never {@code null}
162+
*/
163+
public final ObjectProperty<Tooltip> tooltipProperty() {
164+
return tooltip;
165+
}
166+
167+
@Override
168+
public List<CssMetaData<? extends Styleable, ?>> getCssMetaData() {
169+
return getClassCssMetaData();
170+
}
171+
172+
private static final CssMetaData<Marker, Font> FONT = new CssMetaData<>("-fx-font", FontConverter.getInstance(), Font.getDefault()) {
173+
@Override
174+
public boolean isSettable(Marker node) {
175+
return node.font == null || !node.font.isBound();
176+
}
177+
178+
@SuppressWarnings("unchecked")
179+
@Override
180+
public StyleableProperty<Font> getStyleableProperty(Marker node) {
181+
return (StyleableProperty<Font>)node.fontProperty();
182+
}
183+
};
184+
185+
private static final List<CssMetaData<? extends Styleable, ?>> STYLEABLES;
186+
187+
static {
188+
List<CssMetaData<? extends Styleable, ?>> styleables = new ArrayList<>(StackPane.getClassCssMetaData());
189+
190+
styleables.add(FONT);
191+
192+
STYLEABLES = Collections.unmodifiableList(styleables);
193+
}
194+
195+
/**
196+
* Returns the list of CSS metadata associated with this class.
197+
*
198+
* @return a list of CSS metadata, never {@code null}
199+
*/
200+
public static List<CssMetaData<? extends Styleable, ?>> getClassCssMetaData() {
201+
return STYLEABLES;
202+
}
203+
}

fx-controls/src/main/java/org/int4/fx/controls/validation/ScaledStackPane.java

Lines changed: 0 additions & 103 deletions
This file was deleted.

0 commit comments

Comments
 (0)