Skip to content

Commit 2b994bc

Browse files
committed
Refactor find method in fxview utils
1 parent 64f1f5f commit 2b994bc

1 file changed

Lines changed: 25 additions & 23 deletions

File tree

patternfx-mvp/src/main/java/com/techsenger/patternfx/mvp/FxViewUtils.java

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -72,29 +72,6 @@ public static void clearView(Node node) {
7272
node.getProperties().remove(VIEW_KEY);
7373
}
7474

75-
/**
76-
* Traverses the JavaFX node tree upward from the given {@link Node}, searching for the nearest node that has
77-
* an associated {@link ComponentFxView} component.
78-
* <p>
79-
* The search starts at the given node itself and walks up the parent chain until a component is found or the
80-
* root is reached.
81-
*
82-
* @param node the JavaFX node to start the search from; must not be {@code null}
83-
* @return the nearest {@link ComponentFxView} component found in the parent chain,
84-
* or {@code null} if no component is associated with any node up to the root
85-
*/
86-
public static @Nullable ComponentFxView<?> findComponent(Node node) {
87-
Node current = node;
88-
while (current != null) {
89-
FxView<?> view = getView(current);
90-
if (view instanceof ParentFxView<?> component) {
91-
return component;
92-
}
93-
current = current.getParent();
94-
}
95-
return null;
96-
}
97-
9875
/**
9976
* Associates the given root {@link FxView} with the specified JavaFX {@link Scene}.
10077
*/
@@ -139,6 +116,31 @@ public static void clearView(Tab tab) {
139116
tab.getProperties().remove(VIEW_KEY);
140117
}
141118

119+
/**
120+
* Traverses the JavaFX node tree upward from the given {@link Node}, searching for the nearest node that has
121+
* an associated view of the specified type.
122+
* <p>
123+
* The search starts at the given node itself and walks up the parent chain until a matching view is found
124+
* or the root is reached.
125+
*
126+
* @param node the JavaFX node to start the search from; must not be {@code null}
127+
* @param viewClass the class or interface of the view to search for; must not be {@code null}
128+
* @param <T> the type of the view to search for
129+
* @return the nearest {@link FxView} of the specified type found in the parent chain,
130+
* or {@code null} if no matching view is associated with any node up to the root
131+
*/
132+
public static <T extends FxView<?>> @Nullable T findView(Node node, Class<T> viewClass) {
133+
Node current = node;
134+
while (current != null) {
135+
FxView<?> view = getView(current);
136+
if (view != null && viewClass.isInstance(view)) {
137+
return viewClass.cast(view);
138+
}
139+
current = current.getParent();
140+
}
141+
return null;
142+
}
143+
142144
private FxViewUtils() {
143145
// empty
144146
}

0 commit comments

Comments
 (0)