Skip to content

Commit

Permalink
Fix Preferences not retrieving default font if set in pluginCustomiza…
Browse files Browse the repository at this point in the history
…tion

- 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
  • Loading branch information
Phillipus committed Dec 22, 2024
1 parent cd2b72b commit 8858580
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -66,7 +65,6 @@
*
* @author Phillip Beauvoir
*/
@SuppressWarnings("restriction")
public class ColoursPreferencePage
extends PreferencePage
implements IWorkbenchPreferencePage, IPreferenceConstants {
Expand Down Expand Up @@ -723,7 +721,7 @@ private void saveThemeColors() {
}

if(themeColorChanged) {
PrefUtil.savePrefs();
PrefUtils.savePrefs();
ThemeUtils.resetCurrentTheme();
}
}
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 @@ -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;
Expand Down Expand Up @@ -94,15 +92,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 +175,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 Expand Up @@ -497,7 +497,7 @@ public boolean performOk() {
}
}

PrefUtil.savePrefs();
PrefUtils.savePrefs();

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}
}
}
}
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 Down Expand Up @@ -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());
}
}

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

0 comments on commit 8858580

Please sign in to comment.