Skip to content

Commit

Permalink
Add PrefUtils to set and get Eclipse internal preferences
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
Phillipus committed Dec 21, 2024
1 parent 2d817e5 commit 2c89a71
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -94,15 +93,27 @@ 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() {
ThemeUtils.setCurrentThemeFont(fontDefinitionId, getFontData(), getSystemFontData());
}

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();
}
}

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> 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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;


Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2c89a71

Please sign in to comment.