From 4c9561a19c1535906397064d8325d340a6ca4092 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/FontsPreferencePage.java | 27 +++++++-------
.../archimatetool/editor/ui/ThemeUtils.java | 35 +++++++++++++++++++
2 files changed, 49 insertions(+), 13 deletions(-)
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..510cad1cc 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;
@@ -94,7 +93,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 +103,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 +176,6 @@ void performOK() {
FontData getSystemFontData() {
return FontFactory.getDefaultViewOSFontData();
}
-
- FontData getSafeFontData(String fontDetails) {
- try {
- return new FontData(fontDetails);
- }
- catch(Exception ex) {
- }
-
- return getSystemFontData();
- }
}
// Table
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