Skip to content

Commit c525ac4

Browse files
committed
Implementation of application preferences
1 parent d0011b2 commit c525ac4

File tree

7 files changed

+513
-13
lines changed

7 files changed

+513
-13
lines changed

modules/javafx.graphics/src/main/java/com/sun/javafx/application/PlatformImpl.java

+15
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import static com.sun.javafx.FXPermissions.CREATE_TRANSPARENT_WINDOW_PERMISSION;
2929
import com.sun.javafx.PlatformUtil;
3030
import com.sun.javafx.SecurityUtil;
31+
import com.sun.javafx.application.preferences.ApplicationPreferences;
3132
import com.sun.javafx.application.preferences.PlatformPreferences;
3233
import com.sun.javafx.application.preferences.PreferenceMapping;
3334
import com.sun.javafx.css.StyleManager;
@@ -942,6 +943,16 @@ public static PlatformPreferences getPlatformPreferences() {
942943
return platformPreferences;
943944
}
944945

946+
private static ApplicationPreferences applicationPreferences;
947+
948+
public static ApplicationPreferences getApplicationPreferences() {
949+
if (applicationPreferences == null) {
950+
throw new IllegalStateException("Toolkit not initialized");
951+
}
952+
953+
return applicationPreferences;
954+
}
955+
945956
/**
946957
* Called by Glass when the toolkit is initialized.
947958
*
@@ -953,6 +964,9 @@ public static void initPreferences(Map<String, Class<?>> platformKeys,
953964
Map<String, Object> preferences) {
954965
platformPreferences = new PlatformPreferences(platformKeys, platformKeyMappings);
955966
platformPreferences.update(preferences);
967+
968+
applicationPreferences = new ApplicationPreferences(platformKeys, platformKeyMappings);
969+
applicationPreferences.update(preferences);
956970
}
957971

958972
/**
@@ -968,6 +982,7 @@ public static void updatePreferences(Map<String, Object> preferences) {
968982
if (isFxApplicationThread()) {
969983
checkHighContrastThemeChanged(preferences);
970984
platformPreferences.update(preferences);
985+
applicationPreferences.update(preferences);
971986
} else {
972987
// Make a defensive copy in case the caller of this method decides to re-use or
973988
// modify its preferences map after the method returns. Don't use Map.copyOf
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package com.sun.javafx.application.preferences;
27+
28+
import javafx.application.Application;
29+
import javafx.application.ColorScheme;
30+
import javafx.scene.paint.Color;
31+
import java.util.Arrays;
32+
import java.util.HashMap;
33+
import java.util.Map;
34+
import java.util.Objects;
35+
36+
/**
37+
* Contains the implementation of a modifiable map of application preferences.
38+
* <p>
39+
* Like {@link PlatformPreferences}, this map is updated when the operating system signals that a
40+
* preference has changed. This map also supports overriding existing mappings with the
41+
* {@link #put(String, Object)} operation. Overridden mappings can be reset to their platform
42+
* defaults by invoking {@link #reset(String)} or {@link #reset()}.
43+
*/
44+
public final class ApplicationPreferences extends PlatformPreferences implements Application.Preferences {
45+
46+
private final Map<String, Object> platformPreferences = new HashMap<>();
47+
private final Map<String, Object> userPreferences = new HashMap<>();
48+
49+
public ApplicationPreferences(Map<String, Class<?>> platformKeys,
50+
Map<String, PreferenceMapping<?>> platformKeyMappings) {
51+
super(platformKeys, platformKeyMappings);
52+
}
53+
54+
@Override
55+
public Object put(String key, Object value) {
56+
Objects.requireNonNull(key, "key cannot be null");
57+
Objects.requireNonNull(value, "value cannot be null");
58+
Class<?> platformType = platformKeys.get(key);
59+
Object effectiveValue = effectivePreferences.get(key);
60+
61+
// Check whether the declared platform type is convertible to the type of the value.
62+
// Note that this is the same check that happens in getValue(String, Class<?>).
63+
if (platformType != null && !isConvertible(platformType, value.getClass())) {
64+
throw new IllegalArgumentException(
65+
"Incompatible types: expected = " + platformType.getName() +
66+
", actual = " + value.getClass().getName());
67+
}
68+
69+
userPreferences.put(key, value);
70+
effectivePreferences.put(key, value);
71+
72+
if (!Objects.equals(effectiveValue, value)) {
73+
var changedPreferences = Map.of(key, new ChangedValue(effectiveValue, value));
74+
properties.update(changedPreferences, platformKeyMappings);
75+
fireValueChangedEvent(changedPreferences);
76+
}
77+
78+
return effectiveValue;
79+
}
80+
81+
@Override
82+
public void reset(String key) {
83+
Objects.requireNonNull(key, "key cannot be null");
84+
Object oldValue = effectivePreferences.get(key);
85+
Object newValue;
86+
87+
userPreferences.remove(key);
88+
89+
if (platformPreferences.containsKey(key)) {
90+
newValue = platformPreferences.get(key);
91+
effectivePreferences.put(key, newValue);
92+
} else {
93+
newValue = null;
94+
effectivePreferences.remove(key);
95+
}
96+
97+
boolean changed = oldValue instanceof Object[] array ?
98+
!Arrays.equals(array, (Object[])newValue) : !Objects.equals(oldValue, newValue);
99+
100+
if (changed) {
101+
var changedPreferences = Map.of(key, new ChangedValue(oldValue, newValue));
102+
properties.update(changedPreferences, platformKeyMappings);
103+
fireValueChangedEvent(changedPreferences);
104+
}
105+
}
106+
107+
@Override
108+
public void reset() {
109+
forEach((key, _) -> reset(key));
110+
}
111+
112+
@Override
113+
public void update(Map<String, Object> preferences) {
114+
Map<String, Object> currentEffectivePreferences = Map.copyOf(effectivePreferences);
115+
116+
// The given preference map may contain null values, which indicates that a mapping was removed.
117+
platformPreferences.putAll(preferences);
118+
platformPreferences.entrySet().removeIf(entry -> entry.getValue() == null);
119+
effectivePreferences.clear();
120+
effectivePreferences.putAll(platformPreferences);
121+
effectivePreferences.putAll(userPreferences);
122+
123+
// Only fire change notifications if any preference has effectively changed.
124+
Map<String, ChangedValue> effectivelyChangedPreferences =
125+
ChangedValue.getEffectiveChanges(currentEffectivePreferences, effectivePreferences);
126+
127+
if (!effectivelyChangedPreferences.isEmpty()) {
128+
properties.update(effectivelyChangedPreferences, platformKeyMappings);
129+
fireValueChangedEvent(effectivelyChangedPreferences);
130+
}
131+
}
132+
133+
@Override
134+
public void setReducedMotion(Boolean value) {
135+
properties.setReducedMotion(value);
136+
}
137+
138+
@Override
139+
public void setReducedTransparency(Boolean value) {
140+
properties.setReducedTransparency(value);
141+
}
142+
143+
@Override
144+
public void setColorScheme(ColorScheme colorScheme) {
145+
properties.setColorScheme(colorScheme);
146+
}
147+
148+
@Override
149+
public void setBackgroundColor(Color color) {
150+
properties.setBackgroundColor(color);
151+
}
152+
153+
@Override
154+
public void setForegroundColor(Color color) {
155+
properties.setForegroundColor(color);
156+
}
157+
158+
@Override
159+
public void setAccentColor(Color color) {
160+
properties.setAccentColor(color);
161+
}
162+
}

modules/javafx.graphics/src/main/java/com/sun/javafx/application/preferences/PlatformPreferences.java

+10-9
Original file line numberDiff line numberDiff line change
@@ -51,31 +51,32 @@
5151
* When the operating system signals that a preference has changed, the mappings are updated
5252
* by calling the {@link #update(Map)} method.
5353
*/
54-
public final class PlatformPreferences extends AbstractMap<String, Object> implements Platform.Preferences {
55-
54+
public sealed class PlatformPreferences extends AbstractMap<String, Object>
55+
implements Platform.Preferences
56+
permits ApplicationPreferences {
5657
/**
5758
* Contains mappings from platform-specific keys to their types. This information is
5859
* used to catch misuse of typed getters even if the preferences map doesn't contain
5960
* the preference mapping at runtime.
6061
*/
61-
private final Map<String, Class<?>> platformKeys;
62+
final Map<String, Class<?>> platformKeys;
6263

6364
/**
6465
* Contains mappings from platform-specific keys to well-known keys, which are used
6566
* in the implementation of the property-based API in {@link PreferenceProperties}.
6667
*/
67-
private final Map<String, PreferenceMapping<?>> platformKeyMappings;
68+
final Map<String, PreferenceMapping<?>> platformKeyMappings;
6869

6970
/**
7071
* Contains the current set of effective preferences, i.e. the set of preferences that
7172
* we know to be the current state of the world, and are exposed to users of this map.
7273
*/
73-
private final Map<String, Object> effectivePreferences = new HashMap<>();
74-
private final Map<String, Object> unmodifiableEffectivePreferences = Collections.unmodifiableMap(effectivePreferences);
74+
final Map<String, Object> effectivePreferences = new HashMap<>();
7575

7676
/** Contains the implementation of the property-based API. */
77-
private final PreferenceProperties properties = new PreferenceProperties(this);
77+
final PreferenceProperties properties = new PreferenceProperties(this);
7878

79+
private final Map<String, Object> unmodifiableEffectivePreferences = Collections.unmodifiableMap(effectivePreferences);
7980
private final List<InvalidationListener> invalidationListeners = new CopyOnWriteArrayList<>();
8081
private final List<MapChangeListener<? super String, Object>> mapChangeListeners = new CopyOnWriteArrayList<>();
8182

@@ -290,7 +291,7 @@ public void update(Map<String, Object> preferences) {
290291
}
291292
}
292293

293-
private void fireValueChangedEvent(Map<String, ChangedValue> changedEntries) {
294+
void fireValueChangedEvent(Map<String, ChangedValue> changedEntries) {
294295
invalidationListeners.forEach(listener -> listener.invalidated(this));
295296
var change = new MapExpressionHelper.SimpleChange<>(this);
296297

@@ -320,7 +321,7 @@ private void fireValueChangedEvent(Map<String, ChangedValue> changedEntries) {
320321
* @param target the target type
321322
* @return {@code true} if a casting conversion exists, {@code false} otherwise
322323
*/
323-
private boolean isConvertible(Class<?> source, Class<?> target) {
324+
boolean isConvertible(Class<?> source, Class<?> target) {
324325
if (source.isArray()) {
325326
return isArrayConvertible(source, target);
326327
}

modules/javafx.graphics/src/main/java/com/sun/javafx/application/preferences/PreferenceProperties.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public boolean isReducedMotion() {
7373
return reducedMotion.get();
7474
}
7575

76-
public void setReducedMotion(boolean value) {
76+
public void setReducedMotion(Boolean value) {
7777
reducedMotion.setValueOverride(value);
7878
}
7979

@@ -85,7 +85,7 @@ public boolean isReducedTransparency() {
8585
return reducedTransparency.get();
8686
}
8787

88-
public void setReducedTransparency(boolean value) {
88+
public void setReducedTransparency(Boolean value) {
8989
reducedTransparency.setValueOverride(value);
9090
}
9191

0 commit comments

Comments
 (0)