From 2c89a71a01bf92b52168cc6d136b191a4fb48883 Mon Sep 17 00:00:00 2001
From: Phillipus
Date: Sat, 21 Dec 2024 15:05:02 +0000
Subject: [PATCH] Add PrefUtils to set and get Eclipse internal preferences
- Set/Get Eclipse internal preferences before the workbench starts
- Move all existing prefs settings to here
- This gives us a workaround for a default font not set 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
---
.../com/archimatetool/editor/Application.java | 17 +---
.../editor/ArchiWorkbenchAdvisor.java | 10 --
.../preferences/FontsPreferencePage.java | 27 +++---
.../editor/preferences/PrefUtils.java | 95 +++++++++++++++++++
.../preferences/PreferenceInitializer.java | 8 --
.../archimatetool/editor/ui/ThemeUtils.java | 28 ++++++
6 files changed, 140 insertions(+), 45 deletions(-)
create mode 100644 com.archimatetool.editor/src/com/archimatetool/editor/preferences/PrefUtils.java
diff --git a/com.archimatetool.editor/src/com/archimatetool/editor/Application.java b/com.archimatetool.editor/src/com/archimatetool/editor/Application.java
index 9ec0c7a93..20ef30af6 100644
--- a/com.archimatetool.editor/src/com/archimatetool/editor/Application.java
+++ b/com.archimatetool.editor/src/com/archimatetool/editor/Application.java
@@ -9,14 +9,13 @@
import java.net.URL;
import org.eclipse.core.runtime.Platform;
-import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.osgi.service.datalocation.Location;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;
-import com.archimatetool.editor.preferences.IPreferenceConstants;
+import com.archimatetool.editor.preferences.PrefUtils;
import com.archimatetool.editor.utils.PlatformUtils;
@@ -61,18 +60,8 @@ public Object start(IApplicationContext context) throws Exception {
return EXIT_RESTART;
}
- /*
- * Mac item heights.
- * Read the preference setting and set it as a System Property before the workbench Display is created.
- * We access it via the InstanceScope as that doesn't trigger Archi's PreferenceInitializer.
- * Archi's PreferenceInitializer triggers the creation of the workbench Display by getting the Device zoom level which in turn creates the default Display
- * and then it's too late to set the property.
- */
- if(PlatformUtils.isMac()) {
- boolean useNativeItemHeights = InstanceScope.INSTANCE.getNode("com.archimatetool.editor")
- .getBoolean(IPreferenceConstants.MAC_ITEM_HEIGHT_PROPERTY_KEY, false);
- System.setProperty(IPreferenceConstants.MAC_ITEM_HEIGHT_PROPERTY_KEY, Boolean.toString(useNativeItemHeights));
- }
+ // Initialise internal preferences
+ PrefUtils.init();
// Create Main Display
Display display = PlatformUI.createDisplay();
diff --git a/com.archimatetool.editor/src/com/archimatetool/editor/ArchiWorkbenchAdvisor.java b/com.archimatetool.editor/src/com/archimatetool/editor/ArchiWorkbenchAdvisor.java
index 76e715216..3dca10c1c 100644
--- a/com.archimatetool.editor/src/com/archimatetool/editor/ArchiWorkbenchAdvisor.java
+++ b/com.archimatetool.editor/src/com/archimatetool/editor/ArchiWorkbenchAdvisor.java
@@ -5,8 +5,6 @@
*/
package com.archimatetool.editor;
-import org.eclipse.core.runtime.preferences.IEclipsePreferences;
-import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jface.dialogs.TrayDialog;
import org.eclipse.jface.preference.IPreferenceNode;
import org.eclipse.jface.preference.PreferenceManager;
@@ -86,14 +84,6 @@ public void initialize(IWorkbenchConfigurer configurer) {
}
}
- // We use Eclipse's org.eclipse.ui.internal.handlers.FullScreenHandler which shows
- // a popup with a message to tell you this and a "do not show again" checkbox.
- // This is hard to see and unnecessary, so set this preference now so it doesn't show.
- IEclipsePreferences prefs = InstanceScope.INSTANCE.getNode("org.eclipse.ui.workbench");
- if(prefs != null) {
- prefs.putBoolean("org.eclipse.ui.window.fullscreenmode.donotshowinfoagain", true);
- }
-
// Initialise Proxy
NetUtils.initialise();
}
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/preferences/PrefUtils.java b/com.archimatetool.editor/src/com/archimatetool/editor/preferences/PrefUtils.java
new file mode 100644
index 000000000..ea4849dc7
--- /dev/null
+++ b/com.archimatetool.editor/src/com/archimatetool/editor/preferences/PrefUtils.java
@@ -0,0 +1,95 @@
+/**
+ * This program and the accompanying materials
+ * are made available under the terms of the License
+ * which accompanies this distribution in the file LICENSE.txt
+ */
+package com.archimatetool.editor.preferences;
+
+import java.util.HashMap;
+import java.util.Map;
+
+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;
+
+/**
+ * Utils to access internal Eclipse Preferences before the Workbench starts.
+ * We can set and store preferences before the Workbench starts.
+ *
+ * @author Phillip Beauvoir
+ */
+@SuppressWarnings("nls")
+public class PrefUtils {
+
+ // Store default preferences before they are mangled by the Workbench Theme helper
+ private static Map defaultPrefs = new HashMap<>();
+
+ /**
+ * Initialise this now before the workbench starts
+ */
+ public static void init() {
+ processDefaultWorkBenchUIPrefss();
+ processOtherPrefs();
+ }
+
+ private static void processDefaultWorkBenchUIPrefss() {
+ IEclipsePreferences defaultWorkBenchUIPrefs = getDefaultUIWorkBenchPrefs();
+
+ if(defaultWorkBenchUIPrefs != null) {
+ // Store workbench UI prefs before the workbench starts
+ // Once it's started any default font set in a preference initialiser are over-written
+ try {
+ for(String key : defaultWorkBenchUIPrefs.keys()) {
+ if(key.startsWith("com.archimatetool.editor")) {
+ defaultPrefs.put(key, defaultWorkBenchUIPrefs.get(key, null));
+ }
+ }
+ }
+ catch(BackingStoreException ex) {
+ ex.printStackTrace();
+ }
+
+ // We use Eclipse's org.eclipse.ui.internal.handlers.FullScreenHandler which shows
+ // a popup with a message to tell you this and a "do not show again" checkbox.
+ // This is hard to see and unnecessary, so set the default preference to false now so it doesn't show.
+ defaultWorkBenchUIPrefs.putBoolean("org.eclipse.ui.window.fullscreenmode.donotshowinfoagain", true);
+ }
+ }
+
+ private static void processOtherPrefs() {
+ // Mac item heights.
+ // Read the preference setting and set it as a System Property before the workbench Display is created.
+ // We access it via the InstanceScope as that doesn't trigger Archi's PreferenceInitializer.
+ // Archi's PreferenceInitializer triggers the creation of the workbench Display by getting the Device zoom level which in turn creates the default Display
+ // and then it's too late to set the property.
+ if(PlatformUtils.isMac()) {
+ boolean useNativeItemHeights = InstanceScope.INSTANCE.getNode("com.archimatetool.editor")
+ .getBoolean(IPreferenceConstants.MAC_ITEM_HEIGHT_PROPERTY_KEY, false);
+ System.setProperty(IPreferenceConstants.MAC_ITEM_HEIGHT_PROPERTY_KEY, Boolean.toString(useNativeItemHeights));
+ }
+
+ // Set Eclipse theming default enabled to true to counteract possible future regressions
+ // See https://github.com/eclipse-platform/eclipse.platform.ui/issues/629
+ // See https://github.com/eclipse-platform/eclipse.platform.ui/pull/630
+ IEclipsePreferences preferences = DefaultScope.INSTANCE.getNode("org.eclipse.e4.ui.workbench.renderers.swt");
+ preferences.putBoolean("themeEnabled", true);
+ }
+
+ /**
+ * @return a possible value or null from the "org.eclipse.ui.workbench" DefaultScope
+ */
+ public static String getDefaultUIWorkBenchPreference(String key) {
+ return defaultPrefs.get(key);
+ }
+
+ public static IEclipsePreferences getDefaultUIWorkBenchPrefs() {
+ return DefaultScope.INSTANCE.getNode("org.eclipse.ui.workbench");
+ }
+
+ public static IEclipsePreferences getInstanceUIWorkBenchPrefs() {
+ return InstanceScope.INSTANCE.getNode("org.eclipse.ui.workbench");
+ }
+}
diff --git a/com.archimatetool.editor/src/com/archimatetool/editor/preferences/PreferenceInitializer.java b/com.archimatetool.editor/src/com/archimatetool/editor/preferences/PreferenceInitializer.java
index 62cbfe451..612d1005a 100644
--- a/com.archimatetool.editor/src/com/archimatetool/editor/preferences/PreferenceInitializer.java
+++ b/com.archimatetool.editor/src/com/archimatetool/editor/preferences/PreferenceInitializer.java
@@ -6,8 +6,6 @@
package com.archimatetool.editor.preferences;
import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer;
-import org.eclipse.core.runtime.preferences.DefaultScope;
-import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.jface.preference.IPreferenceStore;
import com.archimatetool.editor.ArchiPlugin;
@@ -167,11 +165,5 @@ public void initializeDefaultPreferences() {
store.setDefault(UPDATE_URL, "https://www.archimatetool.com/archi-version.txt");
store.setDefault(PROPERTIES_SINGLE_COLUMN, false);
-
- // Set Eclipse theming default enabled to true to counteract possible future regressions
- // See https://github.com/eclipse-platform/eclipse.platform.ui/issues/629
- // See https://github.com/eclipse-platform/eclipse.platform.ui/pull/630
- IEclipsePreferences preferences = DefaultScope.INSTANCE.getNode("org.eclipse.e4.ui.workbench.renderers.swt");
- preferences.putBoolean("themeEnabled", true);
}
}
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..317044454 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;
@@ -30,6 +31,7 @@
import org.eclipse.ui.internal.util.PrefUtil;
import org.eclipse.ui.themes.IThemeManager;
+import com.archimatetool.editor.preferences.PrefUtils;
import com.archimatetool.editor.utils.StringUtils;
@@ -298,6 +300,32 @@ 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) {
+ // Check if this was set in a custom preference initialiser
+ String value = PrefUtils.getDefaultUIWorkBenchPreference(fontDefinitionId);
+ if(value != null) {
+ 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
+
+ FontDefinition fontDef = getThemeRegistry().findFont(fontDefinitionId);
+ if(fontDef == null) {
+ return null;
+ }
+
+ return PreferenceConverter.getDefaultFontData(PrefUtil.getInternalPreferenceStore(), createPreferenceKey(fontDef));
+ }
/**
* If theming is disabled set the font on the control from the fontDefinitionId