|
| 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 | +} |
0 commit comments