Skip to content

Commit

Permalink
[Preferences] Workaround for default font not retrieved from pluginCu…
Browse files Browse the repository at this point in the history
…stomization file

If user has a pluginCustomization file set as follows a default font is not loaded by Eclipse's preferences

-pluginCustomization
custom.prefs

Pressing the "Default" button in Preferences will show the system font instead of the one in the pluginCustomization file

So manually load it from the pluginCustomization file if present
  • Loading branch information
Phillipus committed Dec 20, 2024
1 parent 2d817e5 commit ad3cfcc
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 13 deletions.
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
Expand Up @@ -5,13 +5,18 @@
*/
package com.archimatetool.editor.ui;

import java.io.File;
import java.io.FileInputStream;
import java.util.Objects;
import java.util.Properties;

import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
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 @@ -298,6 +303,59 @@ 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) {
// Eclipse over-writes any default font properties that are declared in the pluginCustomization file with the default font from the theme plug-in
// So we'll manually check that file first
try {
String prefsFile = null;

// Is there a "-pluginCustomization" argument with file name?
String[] args = Platform.getCommandLineArgs();
for(int i = 0; i < args.length; i++) {
if(args[i].equalsIgnoreCase("-pluginCustomization")) {
if(args.length > i + 1) {
prefsFile = args[i + 1];
break;
}
}
}

// Yes, does the file exist?
if(prefsFile != null) {
File file = new File(prefsFile);
if(file.exists()) {
// Open it as a properties file
Properties props = new Properties();

try(FileInputStream is = new FileInputStream(file)) {
props.load(is);
}

// Get the font definition value, if any
if(props.get("org.eclipse.ui.workbench/" + fontDefinitionId) instanceof String value) {
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 ad3cfcc

Please sign in to comment.