Skip to content

Commit f978652

Browse files
committed
Add FxView utils
1 parent 2757bb0 commit f978652

1 file changed

Lines changed: 86 additions & 0 deletions

File tree

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2024-2025 Pavel Castornii.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.techsenger.patternfx.mvp;
18+
19+
import com.techsenger.annotations.Nullable;
20+
import javafx.scene.Node;
21+
22+
/**
23+
*
24+
* @author Pavel Castornii
25+
*/
26+
public final class FxViewUtils {
27+
28+
private static final Object VIEW_KEY = new Object();
29+
30+
/**
31+
* Associates the given {@link FxView} view with the specified JavaFX {@link Node}.
32+
* <p>
33+
* This method stores a reference to the view in the node's property map, allowing the view to be
34+
* retrieved later by traversing the JavaFX node tree. This is useful in scenarios where only a node is available
35+
* (e.g., during focus traversal or event handling) and the associated view needs to be identified.
36+
* <p>
37+
* This method should be called during initialization.
38+
*
39+
* @param node the JavaFX node to associate with the component; must not be {@code null}
40+
* @param view the view to associate with the node; must not be {@code null}
41+
*/
42+
public static void setView(Node node, FxView<?> view) {
43+
node.getProperties().put(VIEW_KEY, view);
44+
}
45+
46+
/**
47+
* Returns the {@link FxView} view associated with the given {@link Node}, or {@code null} if no view
48+
* has been associated with it.
49+
* <p>
50+
* This method is intended for use when traversing the JavaFX node tree — for example, when walking up the parent
51+
* chain from a focused node to find the nearest component boundary.
52+
*
53+
* @param node the JavaFX node to look up; must not be {@code null}
54+
* @return the associated view, or {@code null} if none is associated
55+
*/
56+
public static @Nullable FxView<?> getView(Node node) {
57+
return (FxView<?>) node.getProperties().get(VIEW_KEY);
58+
}
59+
60+
/**
61+
* Traverses the JavaFX node tree upward from the given {@link Node}, searching for the nearest node that has
62+
* an associated {@link ComponentFxView} component.
63+
* <p>
64+
* The search starts at the given node itself and walks up the parent chain until a component is found or the
65+
* root is reached.
66+
*
67+
* @param node the JavaFX node to start the search from; must not be {@code null}
68+
* @return the nearest {@link ComponentFxView} component found in the parent chain,
69+
* or {@code null} if no component is associated with any node up to the root
70+
*/
71+
public static @Nullable ComponentFxView<?> findComponent(Node node) {
72+
Node current = node;
73+
while (current != null) {
74+
FxView<?> view = getView(current);
75+
if (view instanceof ParentFxView<?> component) {
76+
return component;
77+
}
78+
current = current.getParent();
79+
}
80+
return null;
81+
}
82+
83+
private FxViewUtils() {
84+
// empty
85+
}
86+
}

0 commit comments

Comments
 (0)