From: Tuukka Lehtonen Date: Wed, 4 Oct 2017 20:10:00 +0000 (+0300) Subject: Made SCL Console font configurable through preferences X-Git-Tag: v1.31.0~142 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F77%2F1077%2F2;p=simantics%2Fplatform.git Made SCL Console font configurable through preferences The implementation is in AbstractCommandChannel so it touches all SCL console, SCL Script Output console included. The default font is now Courier New-regular-11 which is one size smaller than before. refs #7529 Change-Id: I421c3fc299d37c15ea05aa469b7163d4aec66624 --- diff --git a/bundles/org.simantics.scl.ui/plugin.xml b/bundles/org.simantics.scl.ui/plugin.xml index be5735e6e..56473fc29 100644 --- a/bundles/org.simantics.scl.ui/plugin.xml +++ b/bundles/org.simantics.scl.ui/plugin.xml @@ -225,5 +225,21 @@ + + + + + + The SCL Console font is used by SCL Console and SCL Script Output console views. + + + diff --git a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/AbstractCommandConsole.java b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/AbstractCommandConsole.java index a98d65715..a293eaee1 100644 --- a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/AbstractCommandConsole.java +++ b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/AbstractCommandConsole.java @@ -12,8 +12,11 @@ import org.eclipse.core.runtime.preferences.InstanceScope; import org.eclipse.jface.preference.IPersistentPreferenceStore; import org.eclipse.jface.preference.IPreferenceStore; import org.eclipse.jface.resource.FontDescriptor; +import org.eclipse.jface.resource.FontRegistry; import org.eclipse.jface.resource.JFaceResources; import org.eclipse.jface.resource.LocalResourceManager; +import org.eclipse.jface.util.IPropertyChangeListener; +import org.eclipse.jface.util.PropertyChangeEvent; import org.eclipse.jface.window.DefaultToolTip; import org.eclipse.jface.window.ToolTip; import org.eclipse.swt.SWT; @@ -23,6 +26,7 @@ import org.eclipse.swt.events.VerifyEvent; import org.eclipse.swt.events.VerifyListener; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.Font; +import org.eclipse.swt.graphics.FontData; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.RGB; @@ -36,6 +40,7 @@ import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Sash; +import org.eclipse.ui.PlatformUI; import org.eclipse.ui.preferences.ScopedPreferenceStore; import org.simantics.scl.runtime.tuple.Tuple2; @@ -63,6 +68,7 @@ public abstract class AbstractCommandConsole extends Composite { StyledText output; Sash sash; + StyledText deco; protected StyledText input; int userInputHeight=0; @@ -70,7 +76,10 @@ public abstract class AbstractCommandConsole extends Composite { protected Color greenColor; protected Color redColor; - protected Font textFont; + + FontRegistry fontRegistry; + FontDescriptor textFontDescriptor; + Font textFont; ArrayList commandHistory = new ArrayList(); int commandHistoryPos = 0; @@ -97,7 +106,7 @@ public abstract class AbstractCommandConsole extends Composite { return true; } - private boolean hasOption(int mask) { + protected boolean hasOption(int mask) { return (options & mask) != 0; } @@ -105,7 +114,12 @@ public abstract class AbstractCommandConsole extends Composite { resourceManager = new LocalResourceManager(JFaceResources.getResources(), this); greenColor = resourceManager.createColor(new RGB(0, 128, 0)); redColor = resourceManager.createColor(new RGB(172, 0, 0)); - textFont = resourceManager.createFont( FontDescriptor.createFrom("Courier New", 12, SWT.NONE) ); + + // Initialize current text font + fontRegistry = PlatformUI.getWorkbench().getThemeManager().getCurrentTheme().getFontRegistry(); + fontRegistry.addListener(fontRegistryListener); + FontDescriptor font = FontDescriptor.createFrom( fontRegistry.getFontData("org.simantics.scl.consolefont") ); + setTextFont(font); setLayout(new FormLayout()); @@ -152,6 +166,8 @@ public abstract class AbstractCommandConsole extends Composite { readPreferences(); addListener(SWT.Dispose, event -> { + if (fontRegistry != null) + fontRegistry.removeListener(fontRegistryListener); try { writePreferences(); } catch (IOException e) { @@ -161,8 +177,7 @@ public abstract class AbstractCommandConsole extends Composite { } protected void createInputArea() { - // "> " Decoration - StyledText deco = new StyledText(this, SWT.MULTI | SWT.READ_ONLY); + deco = new StyledText(this, SWT.MULTI | SWT.READ_ONLY); deco.setFont(textFont); deco.setEnabled(false); GC gc = new GC(deco); @@ -513,15 +528,12 @@ public abstract class AbstractCommandConsole extends Composite { private void asyncSetErrorAnnotations(final String forCommand, final ErrorAnnotation[] annotations) { if(input.isDisposed()) return; - input.getDisplay().asyncExec(new Runnable() { - @Override - public void run() { - if(input.isDisposed()) - return; - if(!input.getText().equals(forCommand)) - return; - syncSetErrorAnnotations(forCommand, annotations); - } + input.getDisplay().asyncExec(() -> { + if(input.isDisposed()) + return; + if(!input.getText().equals(forCommand)) + return; + syncSetErrorAnnotations(forCommand, annotations); }); } @@ -562,4 +574,33 @@ public abstract class AbstractCommandConsole extends Composite { return output; } + IPropertyChangeListener fontRegistryListener = new IPropertyChangeListener() { + @Override + public void propertyChange(PropertyChangeEvent event) { + setTextFont( FontDescriptor.createFrom((FontData[]) event.getNewValue()) ); + } + }; + + private void setTextFont(FontDescriptor font) { + FontDescriptor oldFontDesc = textFontDescriptor; + textFont = resourceManager.createFont(font); + textFontDescriptor = font; + applyTextFont(textFont); + + // Only destroy old font after the new font has been set! + if (oldFontDesc != null) + resourceManager.destroyFont(oldFontDesc); + } + + private void applyTextFont(Font font) { + if (output != null) + output.setFont(font); + if (deco != null) + deco.setFont(font); + if (input != null) { + input.setFont(font); + adjustInputSize(input.getText()); + } + } + } diff --git a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/SCLConsole.java b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/SCLConsole.java index dcb56cc3a..9207bef39 100644 --- a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/SCLConsole.java +++ b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/SCLConsole.java @@ -33,6 +33,13 @@ import gnu.trove.set.hash.THashSet; * @author Hannu Niemistö */ public class SCLConsole extends AbstractCommandConsole { + + /** + * Use this option mask to exclude {@link SCLConsoleListener}s contributed as + * OSGi services from listening to this console. + */ + public static final int EXCLUDE_CONTRIBUTED_LISTENERS = 1 << 10; + public static final String JOB_NAME = "org.simantics.scl.console.job"; public static final long TERMINATE_GRACE_PERIOD = 1000L; @@ -66,7 +73,8 @@ public class SCLConsole extends AbstractCommandConsole { public SCLConsole(Composite parent, int style, int options) { super(parent, style, options); createContentProposalAdapter(); - addContributedListeners(); + if (!hasOption(EXCLUDE_CONTRIBUTED_LISTENERS)) + addContributedListeners(); } protected void createContentProposalAdapter() { diff --git a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/SCLScriptConsoleView.java b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/SCLScriptConsoleView.java index 8df0caa36..3bcd1c61b 100644 --- a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/SCLScriptConsoleView.java +++ b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/SCLScriptConsoleView.java @@ -14,21 +14,12 @@ import org.simantics.scl.runtime.reporting.SCLReportingHandler; */ public class SCLScriptConsoleView extends ViewPart { - static class SCLOutputConsole extends SCLConsole { - public SCLOutputConsole(Composite parent, int style) { - super(parent, style, AbstractCommandConsole.HIDE_INPUT); - } - - @Override - protected void addContributedListeners() { - } - } - - SCLOutputConsole console; + private SCLConsole console; @Override public void createPartControl(Composite parent) { - this.console = new SCLOutputConsole(parent, SWT.NONE); + this.console = new SCLConsole(parent, SWT.NONE, + AbstractCommandConsole.HIDE_INPUT | SCLConsole.EXCLUDE_CONTRIBUTED_LISTENERS); IToolBarManager toolBarManager = getViewSite().getActionBars().getToolBarManager(); Action interruptAction = ConsoleActions.createInterruptAction(console);