From 8858580bab708c69ddb77244a3f3e1013bad16fc Mon Sep 17 00:00:00 2001 From: Phillipus Date: Sat, 21 Dec 2024 19:51:45 +0000 Subject: [PATCH] Fix Preferences not retrieving default font if set in pluginCustomization - When a user font is set also store a preference key prefixed with ".default" so that if the user exports Archi's preferences they can get both keys to add to their custom preferences file - This gives us a workaround for a default font's value not retrieved if user has a pluginCustomization setting for a default font - Before this, pressing the "Default" button in Preferences showed the system font instead of the one in the pluginCustomization --- .../preferences/ColoursPreferencePage.java | 4 +-- .../preferences/FontsPreferencePage.java | 30 ++++++++-------- .../editor/preferences/PrefUtils.java | 13 +++++++ .../archimatetool/editor/ui/ThemeUtils.java | 35 +++++++++++++++++++ 4 files changed, 64 insertions(+), 18 deletions(-) diff --git a/com.archimatetool.editor/src/com/archimatetool/editor/preferences/ColoursPreferencePage.java b/com.archimatetool.editor/src/com/archimatetool/editor/preferences/ColoursPreferencePage.java index f1be2b746..e8b701f20 100644 --- a/com.archimatetool.editor/src/com/archimatetool/editor/preferences/ColoursPreferencePage.java +++ b/com.archimatetool.editor/src/com/archimatetool/editor/preferences/ColoursPreferencePage.java @@ -44,7 +44,6 @@ import org.eclipse.ui.IWorkbench; import org.eclipse.ui.IWorkbenchPreferencePage; import org.eclipse.ui.PlatformUI; -import org.eclipse.ui.internal.util.PrefUtil; import com.archimatetool.editor.ArchiPlugin; import com.archimatetool.editor.ui.ArchiLabelProvider; @@ -66,7 +65,6 @@ * * @author Phillip Beauvoir */ -@SuppressWarnings("restriction") public class ColoursPreferencePage extends PreferencePage implements IWorkbenchPreferencePage, IPreferenceConstants { @@ -723,7 +721,7 @@ private void saveThemeColors() { } if(themeColorChanged) { - PrefUtil.savePrefs(); + PrefUtils.savePrefs(); ThemeUtils.resetCurrentTheme(); } } diff --git a/com.archimatetool.editor/src/com/archimatetool/editor/preferences/FontsPreferencePage.java b/com.archimatetool.editor/src/com/archimatetool/editor/preferences/FontsPreferencePage.java index fd4c0bbf5..ece397af7 100644 --- a/com.archimatetool.editor/src/com/archimatetool/editor/preferences/FontsPreferencePage.java +++ b/com.archimatetool.editor/src/com/archimatetool/editor/preferences/FontsPreferencePage.java @@ -14,7 +14,6 @@ import org.eclipse.e4.core.services.events.IEventBroker; import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.preference.PreferencePage; -import org.eclipse.jface.resource.JFaceResources; import org.eclipse.jface.viewers.CellLabelProvider; import org.eclipse.jface.viewers.DoubleClickEvent; import org.eclipse.jface.viewers.IDoubleClickListener; @@ -42,7 +41,6 @@ import org.eclipse.ui.IWorkbenchPreferencePage; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.internal.themes.WorkbenchThemeManager; -import org.eclipse.ui.internal.util.PrefUtil; import com.archimatetool.editor.ArchiPlugin; import com.archimatetool.editor.ui.FontFactory; @@ -94,7 +92,9 @@ FontData getFontData() { } FontData getDefaultFontData() { - return getSystemFontData(); + // Get default font that might be set in a pluginCustomization settings file + FontData fontData = ThemeUtils.getDefaultThemeFontData(fontDefinitionId); + return fontData != null ? fontData : getSystemFontData(); } void performOK() { @@ -102,7 +102,17 @@ void performOK() { } FontData getSystemFontData() { - return JFaceResources.getDefaultFont().getFontData()[0]; + return getShell().getDisplay().getSystemFont().getFontData()[0]; + } + + FontData getSafeFontData(String fontDetails) { + try { + return new FontData(fontDetails); + } + catch(Exception ex) { + } + + return getSystemFontData(); } } @@ -165,16 +175,6 @@ void performOK() { FontData getSystemFontData() { return FontFactory.getDefaultViewOSFontData(); } - - FontData getSafeFontData(String fontDetails) { - try { - return new FontData(fontDetails); - } - catch(Exception ex) { - } - - return getSystemFontData(); - } } // Table @@ -497,7 +497,7 @@ public boolean performOk() { } } - PrefUtil.savePrefs(); + PrefUtils.savePrefs(); return true; } diff --git a/com.archimatetool.editor/src/com/archimatetool/editor/preferences/PrefUtils.java b/com.archimatetool.editor/src/com/archimatetool/editor/preferences/PrefUtils.java index fbe0602d1..99d5141d6 100644 --- a/com.archimatetool.editor/src/com/archimatetool/editor/preferences/PrefUtils.java +++ b/com.archimatetool.editor/src/com/archimatetool/editor/preferences/PrefUtils.java @@ -8,6 +8,7 @@ import org.eclipse.core.runtime.preferences.DefaultScope; import org.eclipse.core.runtime.preferences.IEclipsePreferences; import org.eclipse.core.runtime.preferences.InstanceScope; +import org.osgi.service.prefs.BackingStoreException; import com.archimatetool.editor.utils.PlatformUtils; @@ -57,4 +58,16 @@ public static IEclipsePreferences getDefaultUIWorkBenchPrefs() { public static IEclipsePreferences getInstanceUIWorkBenchPrefs() { return InstanceScope.INSTANCE.getNode("org.eclipse.ui.workbench"); } + + public static void savePrefs() { + IEclipsePreferences workBenchUIPrefs = getInstanceUIWorkBenchPrefs(); + if(workBenchUIPrefs != null) { + try { + workBenchUIPrefs.flush(); + } + catch(BackingStoreException ex) { + ex.printStackTrace(); + } + } + } } diff --git a/com.archimatetool.editor/src/com/archimatetool/editor/ui/ThemeUtils.java b/com.archimatetool.editor/src/com/archimatetool/editor/ui/ThemeUtils.java index 8f77e7e15..e2b0c021c 100644 --- a/com.archimatetool.editor/src/com/archimatetool/editor/ui/ThemeUtils.java +++ b/com.archimatetool.editor/src/com/archimatetool/editor/ui/ThemeUtils.java @@ -12,6 +12,7 @@ import org.eclipse.e4.core.contexts.IEclipseContext; import org.eclipse.e4.ui.css.swt.internal.theme.ThemeEngine; import org.eclipse.e4.ui.css.swt.theme.IThemeEngine; +import org.eclipse.jface.preference.PreferenceConverter; import org.eclipse.jface.resource.FontRegistry; import org.eclipse.jface.resource.StringConverter; import org.eclipse.jface.util.IPropertyChangeListener; @@ -269,11 +270,17 @@ public static void setCurrentThemeFont(String fontDefinitionId, FontData fontDat } String preferenceKey = createPreferenceKey(fontDef); + + // Also write a ".default" preference key so that if the user exports Archi's preferences + // They can get both keys to add to their custom preferences file + if(Objects.equals(fontData, defaultFontData)) { // If it's the default, remove it PrefUtil.getInternalPreferenceStore().setToDefault(preferenceKey); + PrefUtil.getInternalPreferenceStore().setToDefault("default." + preferenceKey); } else { PrefUtil.getInternalPreferenceStore().setValue(preferenceKey, fontData.toString()); + PrefUtil.getInternalPreferenceStore().setValue("default." + preferenceKey, fontData.toString()); } } @@ -298,6 +305,34 @@ public static Font getCurrentThemeFont(String fontDefinitionId) { private static FontRegistry getCurrentThemeFontRegistry() { return getThemeManager() != null ? getThemeManager().getCurrentTheme().getFontRegistry() : null; } + + /** + * Get the default FontData value for a font definition or null + */ + public static FontData getDefaultThemeFontData(String fontDefinitionId) { + FontDefinition fontDef = getThemeRegistry().findFont(fontDefinitionId); + if(fontDef == null) { + return null; + } + + String preferenceKey = createPreferenceKey(fontDef); + + // Check if there is a default setting in custom preferences or plugin_customization.ini + // We use our own key for this since Eclipse will have set the default value to that set in the theme's plugin.xml file + String value = PrefUtil.getInternalPreferenceStore().getDefaultString("default." + preferenceKey); + if(StringUtils.isSet(value)) { + try { + return new FontData(value); + } + catch(Exception ex) { + ex.printStackTrace(); + } + } + + // Else check the default value set in the theme plugin.xml file, if any. + // If not set this will return the system font + return PreferenceConverter.getDefaultFontData(PrefUtil.getInternalPreferenceStore(), preferenceKey); + } /** * If theming is disabled set the font on the control from the fontDefinitionId