32
32
import javafx .collections .ObservableList ;
33
33
34
34
import java .lang .ref .WeakReference ;
35
- import java .util .*;
35
+ import java .util .Collections ;
36
+ import java .util .HashMap ;
37
+ import java .util .HashSet ;
38
+ import java .util .Iterator ;
39
+ import java .util .Map ;
40
+ import java .util .Objects ;
41
+ import java .util .Set ;
42
+ import java .util .UUID ;
36
43
37
44
import static com .sun .javafx .application .ThemeHelper .THEME_INSTANCE_SCHEME ;
38
45
@@ -127,44 +134,31 @@ public abstract class Theme {
127
134
static {
128
135
ThemeHelper .setAccessor (new ThemeHelper .Accessor () {
129
136
@ Override
130
- public void platformThemeChanged (Theme theme , Map <String , String > properties ) {
131
- theme .handlePlatformThemeChanged (properties );
137
+ public void platformThemeChanged (Map <String , String > properties ) {
138
+ Iterator <WeakReference <Theme >> it = instances .iterator ();
139
+ while (it .hasNext ()) {
140
+ Theme theme = it .next ().get ();
141
+ if (theme != null ) {
142
+ theme .handlePlatformThemeChanged (properties );
143
+ } else {
144
+ it .remove ();
145
+ }
146
+ }
132
147
}
133
148
});
134
149
}
135
150
136
151
private static final Set <WeakReference <Theme >> instances = new HashSet <>();
137
152
138
- private final Theme baseTheme ;
139
- private Map <String , String > properties ;
140
- private Map <String , String > propertiesUnmodifiable ;
141
- private String url ;
153
+ private final String url = getClass ().getName () + ":" + UUID .randomUUID ();
154
+ private Map <String , String > properties = Toolkit .getToolkit ().getPlatformThemeProperties ();
155
+ private Map <String , String > propertiesUnmodifiable = Collections .unmodifiableMap (properties );
142
156
143
157
/**
144
158
* Creates a new {@code Theme} instance.
145
159
*/
146
160
protected Theme () {
147
- this .baseTheme = null ;
148
- this .properties = Toolkit .getToolkit ().getPlatformThemeProperties ();
149
- this .propertiesUnmodifiable = Collections .unmodifiableMap (properties );
150
- }
151
-
152
- /**
153
- * Creates a new {@code Theme} instance that extends the specified base theme.
154
- * <p>
155
- * Sub-classes should implement {@link #getStylesheets()} by returning a concatenation
156
- * of the base theme's stylesheets with additional extended stylesheets.
157
- *
158
- * @param baseTheme the base theme
159
- */
160
- protected Theme (Theme baseTheme ) {
161
- if (baseTheme == null ) {
162
- throw new NullPointerException ("baseTheme cannot be null" );
163
- }
164
-
165
- this .baseTheme = baseTheme ;
166
- this .properties = baseTheme .properties ;
167
- this .propertiesUnmodifiable = baseTheme .propertiesUnmodifiable ;
161
+ instances .add (new WeakReference <>(this ));
168
162
}
169
163
170
164
/**
@@ -214,15 +208,6 @@ public static Theme of(String... stylesheets) {
214
208
FXCollections .observableArrayList (stylesheets ) : null );
215
209
}
216
210
217
- /**
218
- * Gets the base theme that is extended by this theme.
219
- *
220
- * @return the base theme, or {@code null} if this theme has no base theme
221
- */
222
- protected final Theme getBaseTheme () {
223
- return baseTheme ;
224
- }
225
-
226
211
/**
227
212
* The list of stylesheets that comprise this theme.
228
213
* <p>
@@ -251,13 +236,8 @@ private void handlePlatformThemeChanged(Map<String, String> properties) {
251
236
changed .keySet ().removeIf (key -> Objects .equals (properties .get (key ), this .properties .get (key )));
252
237
253
238
if (!changed .isEmpty ()) {
254
- this .properties = new HashMap <>( properties ) ;
239
+ this .properties = properties ;
255
240
this .propertiesUnmodifiable = Collections .unmodifiableMap (this .properties );
256
-
257
- if (baseTheme != null ) {
258
- baseTheme .platformThemeChanged (changed );
259
- }
260
-
261
241
platformThemeChanged (changed );
262
242
}
263
243
}
@@ -285,15 +265,6 @@ protected final Map<String, String> getPlatformThemeProperties() {
285
265
* @return the URL that corresponds to this {@code Theme} instance.
286
266
*/
287
267
public final String toURL () {
288
- if (url == null ) {
289
- url = getClass ().getName () + ":" + UUID .randomUUID ();
290
-
291
- synchronized (instances ) {
292
- instances .removeIf (ref -> ref .get () == null );
293
- instances .add (new WeakReference <>(this ));
294
- }
295
- }
296
-
297
268
return THEME_INSTANCE_SCHEME + url ;
298
269
}
299
270
@@ -318,12 +289,10 @@ public static Theme fromURL(String url) {
318
289
319
290
url = url .substring (15 );
320
291
321
- synchronized (instances ) {
322
- for (var instance : instances ) {
323
- Theme theme = instance .get ();
324
- if (theme != null && theme .url .equals (url )) {
325
- return theme ;
326
- }
292
+ for (var instance : instances ) {
293
+ Theme theme = instance .get ();
294
+ if (theme != null && theme .url .equals (url )) {
295
+ return theme ;
327
296
}
328
297
}
329
298
@@ -344,20 +313,20 @@ public ObservableList<String> getStylesheets() {
344
313
}
345
314
346
315
private static class ExtensionTheme extends Theme {
316
+ final Theme baseTheme ;
347
317
final ObservableList <String > allStylesheets ;
348
318
349
319
ExtensionTheme (Theme baseTheme , ObservableList <String > additionalStylesheets ) {
350
- super (baseTheme );
351
-
352
- allStylesheets = new ListBinding <>() {
320
+ this .baseTheme = baseTheme ;
321
+ this .allStylesheets = new ListBinding <>() {
353
322
{
354
- bind (getBaseTheme () .getStylesheets (), additionalStylesheets );
323
+ bind (baseTheme .getStylesheets (), additionalStylesheets );
355
324
}
356
325
357
326
@ Override
358
- @ SuppressWarnings ({ "unchecked" , "ConstantConditions" } )
327
+ @ SuppressWarnings ("unchecked" )
359
328
protected ObservableList <String > computeValue () {
360
- return FXCollections .concat (getBaseTheme () .getStylesheets (), additionalStylesheets );
329
+ return FXCollections .concat (baseTheme .getStylesheets (), additionalStylesheets );
361
330
}
362
331
};
363
332
}
@@ -366,6 +335,11 @@ protected ObservableList<String> computeValue() {
366
335
public ObservableList <String > getStylesheets () {
367
336
return allStylesheets ;
368
337
}
338
+
339
+ @ Override
340
+ protected void platformThemeChanged (Map <String , String > properties ) {
341
+ baseTheme .platformThemeChanged (properties );
342
+ }
369
343
}
370
344
371
345
}
0 commit comments