]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Made sure that SWT fonts, images and colors are disposed correctly. (fixes #4105)
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 25 Feb 2013 09:49:27 +0000 (09:49 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 25 Feb 2013 09:49:27 +0000 (09:49 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@26893 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ColorManager.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionField.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionFieldConfiguration.java

index 12baba9105108e36c8927d378d024b87fe488cb6..363a01f6a6bd8bbb1ad82634704a5d7727648eca 100644 (file)
@@ -93,6 +93,7 @@ public class EquationTab extends LabelPropertyTabContributor implements Widget {
        private IWorkbenchSite site;\r
        private Button deleteExpression, newExpression;\r
        private WidgetSupportImpl support;\r
+       private ExpressionComposite expressionComposite;\r
        private final WidgetSupportImpl expressionSupport = new WidgetSupportImpl();\r
 \r
        @Override\r
@@ -178,7 +179,7 @@ public class EquationTab extends LabelPropertyTabContributor implements Widget {
                GridDataFactory.fillDefaults().grab(true, false).align(SWT.END, SWT.FILL).applyTo(isOutput.getWidget());\r
 \r
                // The actual expression\r
-               ExpressionComposite expressionComposite = new ExpressionComposite(composite, SWT.NONE);\r
+               expressionComposite = new ExpressionComposite(composite, SWT.NONE);\r
                GridDataFactory.fillDefaults().grab(true, true).applyTo(expressionComposite);\r
                expressionWidget = new ExpressionWidget(expressionComposite, expressionSupport, SWT.NONE);\r
                expressionWidget.setVariableTable(shortcutTabWidget.getVariableTable());\r
@@ -671,6 +672,8 @@ public class EquationTab extends LabelPropertyTabContributor implements Widget {
                if(focusLostListener != null && site != null)\r
                        site.getPage().removePartListener(focusLostListener);\r
                super.dispose();\r
+               if(expressionComposite != null && !expressionComposite.isDisposed())\r
+                   expressionComposite.dispose();\r
        }\r
 \r
        /**\r
index d9cef77ce344cff24bb57f1b350bc0940857f1b8..a25d21556f8541aab7b35ceb78a0327c96089488 100644 (file)
@@ -11,7 +11,7 @@
  *******************************************************************************/\r
 package org.simantics.sysdyn.ui.properties.widgets.expressions;\r
 \r
-import java.util.HashMap;\r
+import gnu.trove.THashMap;\r
 \r
 import org.eclipse.jface.text.source.ISharedTextColors;\r
 import org.eclipse.swt.graphics.Color;\r
@@ -20,21 +20,21 @@ import org.eclipse.swt.widgets.Display;
 \r
 public class ColorManager implements ISharedTextColors {\r
 \r
-    protected HashMap<RGB, Color> colorTable = new HashMap<RGB, Color>();\r
+    THashMap<RGB,Color> map = new THashMap<RGB,Color>();\r
 \r
     @Override\r
     public void dispose() {\r
-        for(Color c : colorTable.values())\r
+        for(Color c : map.values())\r
             c.dispose();\r
-        colorTable.clear();\r
+        map.clear();\r
     }\r
 \r
     @Override\r
     public Color getColor(RGB rgb) {\r
-        Color color = colorTable.get(rgb);\r
+        Color color = map.get(rgb);\r
         if (color == null) {\r
             color = new Color(Display.getCurrent(), rgb);\r
-            colorTable.put(rgb, color);\r
+            map.put(rgb, color);\r
         }\r
         return color;\r
 \r
index 24c02b7eeed58eb9e7c530f820932fc4399f1057..a5a2994e2abe3f743a391f1ee4e4244ce3d01116 100644 (file)
@@ -15,6 +15,8 @@ import java.util.List;
 \r
 import org.eclipse.jface.layout.GridDataFactory;\r
 import org.eclipse.jface.layout.GridLayoutFactory;\r
+import org.eclipse.jface.resource.JFaceResources;\r
+import org.eclipse.jface.resource.LocalResourceManager;\r
 import org.eclipse.jface.text.Document;\r
 import org.eclipse.jface.text.IDocument;\r
 import org.eclipse.jface.text.PaintManager;\r
@@ -38,7 +40,6 @@ import org.eclipse.swt.graphics.Color;
 import org.eclipse.swt.graphics.Point;\r
 import org.eclipse.swt.graphics.RGB;\r
 import org.eclipse.swt.widgets.Composite;\r
-import org.eclipse.swt.widgets.Display;\r
 import org.eclipse.swt.widgets.Table;\r
 import org.eclipse.ui.texteditor.DefaultMarkerAnnotationAccess;\r
 import org.simantics.sysdyn.ui.utils.SyntaxError;\r
@@ -65,12 +66,14 @@ public class ExpressionField extends Composite {
 \r
     String oldExpression;\r
 \r
-    ColorManager cManager = new ColorManager();\r
-\r
     IAnnotationAccess annotationAccess = new DefaultMarkerAnnotationAccess();\r
 \r
     ExpressionFieldConfiguration expressionFieldConfiguration;\r
     \r
+    private final LocalResourceManager resourceManager;\r
+    private final RGB warningRGB = new RGB(255,215,0);\r
+    private final RGB errorRGB = new RGB(255,0,0);\r
+\r
     /**\r
      * Create a new expression field\r
      * @param parent\r
@@ -79,6 +82,9 @@ public class ExpressionField extends Composite {
     public ExpressionField(Composite parent, int style, Table allowedVariables, boolean allowFunctions) {\r
         super(parent, style);\r
         \r
+        // Create a ResourceManager to dispose images when the widget is disposed.\r
+        this.resourceManager = new LocalResourceManager(JFaceResources.getResources(), this);\r
+        \r
         GridLayoutFactory.fillDefaults().applyTo(this);\r
         \r
         int styles = SWT.V_SCROLL\r
@@ -100,20 +106,24 @@ public class ExpressionField extends Composite {
                 styles);\r
         \r
         // Configuration for color management\r
-        expressionFieldConfiguration = new ExpressionFieldConfiguration(cManager, allowedVariables, allowFunctions);\r
+        expressionFieldConfiguration = new ExpressionFieldConfiguration(\r
+                new LocalResourceManager(JFaceResources.getResources(), _sourceViewer.getControl()), \r
+                allowedVariables, allowFunctions);\r
         _sourceViewer.configure(expressionFieldConfiguration);\r
         AnnotationPainter painter = new AnnotationPainter(_sourceViewer, annotationAccess);\r
         _sourceViewer.addPainter(painter);\r
 \r
         // Annotation types\r
+        Color warningColor =  resourceManager.createColor(warningRGB);\r
+        Color errorColor = resourceManager.createColor(errorRGB);\r
         painter.addAnnotationType(MISSING_LINK);\r
-        painter.setAnnotationTypeColor(MISSING_LINK, new Color(this.getDisplay(), 255,215,0));\r
+        painter.setAnnotationTypeColor(MISSING_LINK, warningColor);\r
         painter.addAnnotationType(NO_SUCH_VARIABLE);\r
-        painter.setAnnotationTypeColor(NO_SUCH_VARIABLE, new Color(this.getDisplay(), 255,0,0));\r
+        painter.setAnnotationTypeColor(NO_SUCH_VARIABLE, errorColor);\r
         painter.addAnnotationType(SYNTAX_ERROR);\r
-        painter.setAnnotationTypeColor(SYNTAX_ERROR, new Color(this.getDisplay(), 255,0,0));        \r
+        painter.setAnnotationTypeColor(SYNTAX_ERROR, errorColor);        \r
         painter.addAnnotationType(SYNTAX_WARNING);\r
-        painter.setAnnotationTypeColor(SYNTAX_WARNING, new Color(this.getDisplay(), 255,215,0));\r
+        painter.setAnnotationTypeColor(SYNTAX_WARNING, warningColor);\r
         \r
         _sourceViewer.setDocument(_document, _annotationModel);\r
 \r
@@ -123,7 +133,7 @@ public class ExpressionField extends Composite {
         PaintManager paintManager = new PaintManager(_sourceViewer);\r
         MatchingCharacterPainter matchingCharacterPainter = new MatchingCharacterPainter(_sourceViewer,\r
                 new DefaultCharacterPairMatcher( new char[] {'(', ')', '{', '}', '[', ']'} ));\r
-        matchingCharacterPainter.setColor(new Color(Display.getCurrent(), new RGB(160, 160, 160)));\r
+        matchingCharacterPainter.setColor(resourceManager.createColor(new RGB(160, 160, 160)));\r
         paintManager.addPainter(matchingCharacterPainter);\r
         \r
         \r
index ee4c8e962a759c237d49fa7b42cc282fbc718308..d2fa797dae084218023966574f94e9810f49c26f 100644 (file)
@@ -12,6 +12,7 @@
 package org.simantics.sysdyn.ui.properties.widgets.expressions;\r
 \r
 import org.eclipse.jface.internal.text.html.HTMLTextPresenter;\r
+import org.eclipse.jface.resource.ResourceManager;\r
 import org.eclipse.jface.text.DefaultInformationControl;\r
 import org.eclipse.jface.text.DefaultTextHover;\r
 import org.eclipse.jface.text.IDocument;\r
@@ -49,15 +50,16 @@ public class ExpressionFieldConfiguration extends SourceViewerConfiguration {
 \r
        private final long WAIT_BEFORE_STATUS_CHANGE = 100;\r
        \r
-       ColorManager colorManager;\r
        Table allowedVariables;\r
        boolean allowFunctions;\r
        boolean assistSessionActive; \r
        CompletionProcessor completionProcessor;\r
+       ResourceManager resourceManager;\r
+\r
        \r
-       public ExpressionFieldConfiguration(ColorManager colorManager, Table allowedVariables, boolean allowFunctions) {\r
+       public ExpressionFieldConfiguration(ResourceManager resourceManager, Table allowedVariables, boolean allowFunctions) {\r
                super();\r
-               this.colorManager = colorManager;\r
+               this.resourceManager = resourceManager;\r
                this.allowedVariables = allowedVariables;\r
                this.allowFunctions = allowFunctions;\r
                this.assistSessionActive = false;\r
@@ -79,7 +81,7 @@ public class ExpressionFieldConfiguration extends SourceViewerConfiguration {
     public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {\r
                PresentationReconciler reconciler = new PresentationReconciler();\r
 \r
-               DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getSclTokenScanner());\r
+               DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getTokenScanner());\r
 \r
                reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);\r
                reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);\r
@@ -88,18 +90,18 @@ public class ExpressionFieldConfiguration extends SourceViewerConfiguration {
        }\r
 \r
 \r
-       ITokenScanner getSclTokenScanner() {\r
+       ITokenScanner getTokenScanner() {\r
                RuleBasedScanner scanner = new RuleBasedScanner();\r
 \r
                final Token reserved = new Token(\r
                                new TextAttribute(\r
-                                               colorManager.getColor(new RGB(127, 0, 85)),\r
+                                       resourceManager.createColor(new RGB(127, 0, 85)),\r
                                                null,\r
                                                SWT.BOLD\r
                                ));\r
-           final Token defaultToken = new Token(new TextAttribute(colorManager.getColor(new RGB(0, 0, 0))));\r
+           final Token defaultToken = new Token(new TextAttribute(resourceManager.createColor(new RGB(0, 0, 0))));\r
 \r
-                final Token comment = new Token(new TextAttribute(colorManager.getColor(new RGB(63, 127, 95))));\r
+                final Token comment = new Token(new TextAttribute(resourceManager.createColor(new RGB(63, 127, 95))));\r
 \r
 \r
                WordRule reservedWord = new WordRule(new IWordDetector() {   \r