]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/AbstractCommandConsole.java
Externalize strings in org.simantics.scl.ui
[simantics/platform.git] / bundles / org.simantics.scl.ui / src / org / simantics / scl / ui / console / AbstractCommandConsole.java
index a98d65715c7fa89a652ac6a0293667027bfda51a..b0019688386fcbbf8f7d019dad052630c75410d0 100644 (file)
@@ -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,8 +40,10 @@ 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;
+import org.slf4j.Logger;
 
 /**
  * A console with input and output area that can be embedded
@@ -51,7 +57,7 @@ public abstract class AbstractCommandConsole extends Composite {
      */
     public static final int HIDE_INPUT = 1 << 0;
 
-    public static final String PLUGIN_ID = "org.simantics.scl.ui";
+    public static final String PLUGIN_ID = "org.simantics.scl.ui"; //$NON-NLS-1$
 
     public static final int COMMAND_HISTORY_SIZE = 50;
     
@@ -63,6 +69,7 @@ public abstract class AbstractCommandConsole extends Composite {
 
     StyledText output;
     Sash sash;
+    StyledText deco;
     protected StyledText input;
     
     int userInputHeight=0;
@@ -70,7 +77,10 @@ public abstract class AbstractCommandConsole extends Composite {
     
     protected Color greenColor;
     protected Color redColor;
-    protected Font textFont;
+
+    FontRegistry fontRegistry;
+    FontDescriptor textFontDescriptor;
+    Font textFont;
 
     ArrayList<String> commandHistory = new ArrayList<String>();
     int commandHistoryPos = 0;
@@ -97,7 +107,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 +115,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") ); //$NON-NLS-1$
+        setTextFont(font);
 
         setLayout(new FormLayout());
 
@@ -152,6 +167,8 @@ public abstract class AbstractCommandConsole extends Composite {
         readPreferences();
 
         addListener(SWT.Dispose, event -> {
+            if (fontRegistry != null)
+                fontRegistry.removeListener(fontRegistryListener);
             try {
                 writePreferences();
             } catch (IOException e) {
@@ -161,21 +178,20 @@ 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);
         int inputLeftPos = gc.getFontMetrics().getAverageCharWidth()*2;
         gc.dispose();
-        deco.setText(">");
+        deco.setText(">"); //$NON-NLS-1$
         deco.setLayoutData( formData(sash, 100, 0, new Tuple2(0, inputLeftPos)) );
 
         // Input area
         input = new StyledText(this, SWT.MULTI);
         input.setFont(textFont);
         input.setLayoutData( formData(sash, 100, new Tuple2(0, inputLeftPos), 100) );
-        adjustInputSize("");
+        adjustInputSize(""); //$NON-NLS-1$
         input.addVerifyKeyListener(event -> {
             switch(event.keyCode) {
             case SWT.KEYPAD_CR:
@@ -212,19 +228,19 @@ public abstract class AbstractCommandConsole extends Composite {
             }
         });
         input.addVerifyListener(e -> {
-            if(e.text.contains("\n")) {
+            if(e.text.contains("\n")) { //$NON-NLS-1$
                 int lineId = input.getLineAtOffset(e.start);
                 int lineOffset = input.getOffsetAtLine(lineId);
                 int indentAmount;
                 for(indentAmount=0;
                         lineOffset+indentAmount < input.getCharCount() && 
-                        input.getTextRange(lineOffset+indentAmount, 1).equals(" ");
+                        input.getTextRange(lineOffset+indentAmount, 1).equals(" "); //$NON-NLS-1$
                         ++indentAmount);
                 StringBuilder indent = new StringBuilder();
                 indent.append('\n');
                 for(int i=0;i<indentAmount;++i)
                     indent.append(' ');
-                e.text = e.text.replace("\n", indent);
+                e.text = e.text.replace("\n", indent); //$NON-NLS-1$
             }
         });
         input.addModifyListener(e -> {
@@ -320,7 +336,7 @@ public abstract class AbstractCommandConsole extends Composite {
             Tuple2 t = (Tuple2) o;
             return new FormAttachment((Integer) t.c0, (Integer) t.c1);
         }
-        throw new IllegalArgumentException("argument not supported: " + o);
+        throw new IllegalArgumentException("argument not supported: " + o); //$NON-NLS-1$
     }
 
     private int getOffsetInInput(int x, int y) {
@@ -350,7 +366,7 @@ public abstract class AbstractCommandConsole extends Composite {
     
     String validatedText;
     
-    Job validationJob = new Job("SCL input validation") {
+    Job validationJob = new Job("SCL input validation") { //$NON-NLS-1$
 
         @Override
         protected IStatus run(IProgressMonitor monitor) {
@@ -361,7 +377,7 @@ public abstract class AbstractCommandConsole extends Composite {
         
     };
     
-    Job preValidationJob = new Job("SCL input validation") {
+    Job preValidationJob = new Job("SCL input validation") { //$NON-NLS-1$
         @Override
         protected IStatus run(IProgressMonitor monitor) {
             if(!input.isDisposed()) {
@@ -467,7 +483,7 @@ public abstract class AbstractCommandConsole extends Composite {
         
         // Print it into output area
         //appendOutput("> " + command.replace("\n", "\n  ") + "\n", greenColor, null);
-        input.setText("");
+        input.setText(""); //$NON-NLS-1$
         
         // Execute
         execute(command);
@@ -504,8 +520,7 @@ public abstract class AbstractCommandConsole extends Composite {
                 range.start = 0;
                 range.length = 1;
                 input.setStyleRange(range);
-                System.err.println("The following error message didn't have a proper location:");
-                System.err.println(annotation.description);
+                getLogger().error("The following error message didn't have a proper location: {}", annotation.description, e); //$NON-NLS-1$
             }
         }
     }
@@ -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);
         });
     }
     
@@ -554,7 +566,7 @@ public abstract class AbstractCommandConsole extends Composite {
     
     public void clear() {
         outputModiLock = true;
-        output.setText("");
+        output.setText(""); //$NON-NLS-1$
         outputModiLock = false;
     }
 
@@ -562,4 +574,34 @@ 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());
+        }
+    }
+
+    public abstract Logger getLogger();
 }