Skip to content

Commit 64f1f5f

Browse files
committed
Overload setView/getView/clearView for Scene and Tab
1 parent 0bf2fc6 commit 64f1f5f

2 files changed

Lines changed: 62 additions & 2 deletions

File tree

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

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import com.techsenger.annotations.Nullable;
2020
import javafx.scene.Node;
21+
import javafx.scene.Scene;
22+
import javafx.scene.control.Tab;
2123

2224
/**
2325
*
@@ -33,8 +35,6 @@ public final class FxViewUtils {
3335
* This method stores a reference to the view in the node's property map, allowing the view to be
3436
* retrieved later by traversing the JavaFX node tree. This is useful in scenarios where only a node is available
3537
* (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.
3838
*
3939
* @param node the JavaFX node to associate with the component; must not be {@code null}
4040
* @param view the view to associate with the node; must not be {@code null}
@@ -57,6 +57,21 @@ public static void setView(Node node, FxView<?> view) {
5757
return (FxView<?>) node.getProperties().get(VIEW_KEY);
5858
}
5959

60+
/**
61+
* Removes the {@link FxView} associated with the specified {@link Node}.
62+
* <p>
63+
* This method clears the reference previously stored via {@link #setView(Node, FxView)} from the node's property
64+
* map. After calling this method, {@link #getView(Node)} will return {@code null} for the given node.
65+
* <p>
66+
* This can be useful during cleanup or when the association between a node and its view
67+
* is no longer valid.
68+
*
69+
* @param node the JavaFX node whose associated view should be removed; must not be {@code null}
70+
*/
71+
public static void clearView(Node node) {
72+
node.getProperties().remove(VIEW_KEY);
73+
}
74+
6075
/**
6176
* Traverses the JavaFX node tree upward from the given {@link Node}, searching for the nearest node that has
6277
* an associated {@link ComponentFxView} component.
@@ -80,6 +95,50 @@ public static void setView(Node node, FxView<?> view) {
8095
return null;
8196
}
8297

98+
/**
99+
* Associates the given root {@link FxView} with the specified JavaFX {@link Scene}.
100+
*/
101+
public static void setView(Scene scene, FxView<?> root) {
102+
scene.getProperties().put(VIEW_KEY, root);
103+
}
104+
105+
/**
106+
* Returns the root {@link FxView} associated with the given {@link Scene}, or {@code null} if no view
107+
* has been associated with it.
108+
*/
109+
public static @Nullable FxView<?> getView(Scene scene) {
110+
return (FxView<?>) scene.getProperties().get(VIEW_KEY);
111+
}
112+
113+
/**
114+
* Removes the {@link FxView} associated with the specified {@link Scene}.
115+
*/
116+
public static void clearView(Scene scene) {
117+
scene.getProperties().remove(VIEW_KEY);
118+
}
119+
120+
/**
121+
* Associates the given {@link FxView} view with the specified JavaFX {@link Tab}.
122+
*/
123+
public static void setView(Tab tab, FxView<?> root) {
124+
tab.getProperties().put(VIEW_KEY, root);
125+
}
126+
127+
/**
128+
* Returns the {@link FxView} view associated with the given {@link Tab}, or {@code null} if no view has been
129+
* associated with it.
130+
*/
131+
public static @Nullable FxView<?> getView(Tab tab) {
132+
return (FxView<?>) tab.getProperties().get(VIEW_KEY);
133+
}
134+
135+
/**
136+
* Removes the {@link FxView} associated with the specified {@link Tab}.
137+
*/
138+
public static void clearView(Tab tab) {
139+
tab.getProperties().remove(VIEW_KEY);
140+
}
141+
83142
private FxViewUtils() {
84143
// empty
85144
}

patternfx-mvp/src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
requires com.google.errorprone.annotations;
2222
requires javafx.base;
2323
requires javafx.graphics;
24+
requires javafx.controls;
2425

2526
exports com.techsenger.patternfx.mvp;
2627
}

0 commit comments

Comments
 (0)