From 1305dd81751414240d5c0ccd82e7ef740ed6def4 Mon Sep 17 00:00:00 2001 From: lempinen Date: Mon, 25 Feb 2013 09:48:37 +0000 Subject: [PATCH] Updated Font dialog to not use SWT graphics when possible. If SWT graphics are used, they are disposed correctly using LocalResourceManager. (fixes #4104) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@26892 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../ui/properties/VariableInformationTab.java | 62 +++++++------ .../properties/widgets/CustomFontDialog.java | 91 +++++++++---------- .../widgets/FontModifyListener.java | 6 +- .../widgets/FontSelectionComposite.java | 25 ++--- 4 files changed, 95 insertions(+), 89 deletions(-) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/VariableInformationTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/VariableInformationTab.java index 506e7a75..53f7c9aa 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/VariableInformationTab.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/VariableInformationTab.java @@ -12,13 +12,17 @@ package org.simantics.sysdyn.ui.properties; import java.awt.Color; +import java.awt.Font; import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; +import org.eclipse.jface.resource.FontDescriptor; +import org.eclipse.jface.resource.JFaceResources; +import org.eclipse.jface.resource.LocalResourceManager; import org.eclipse.swt.SWT; import org.eclipse.swt.SWTException; -import org.eclipse.swt.graphics.Font; import org.eclipse.swt.graphics.FontData; +import org.eclipse.swt.graphics.RGB; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Group; @@ -66,12 +70,17 @@ public class VariableInformationTab extends LabelPropertyTabContributor implemen private WidgetSupport support; private Resource component; private org.simantics.browsing.ui.swt.widgets.Label sample; + private LocalResourceManager resourceManager; @Override public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) { this.support = support; support.register(this); + + // Create a ResourceManager to dispose images when the widget is disposed. + this.resourceManager = new LocalResourceManager(JFaceResources.getResources(), body); + final Composite composite = new Composite(body, SWT.NONE); GridDataFactory.fillDefaults().grab(true, true).applyTo(composite); @@ -143,20 +152,20 @@ public class VariableInformationTab extends LabelPropertyTabContributor implemen b.addSelectionListener(new SelectionListenerImpl(context) { - java.awt.Font f; + Font f; Color color; Object input; @Override public void beforeApply() { - Triple result = null; + Triple result = null; try { - result = SimanticsUI.getSession().syncRequest(new Read>(){ + result = SimanticsUI.getSession().syncRequest(new Read>(){ @Override - public Triple perform(ReadGraph graph) throws DatabaseException { + public Triple perform(ReadGraph graph) throws DatabaseException { Resource component = ISelectionUtils.filterSingleSelection(input, Resource.class); String name = NameUtils.getSafeName(graph, component); @@ -166,14 +175,14 @@ public class VariableInformationTab extends LabelPropertyTabContributor implemen Resource fontResource = graph.getPossibleObject(element, g2d.HasFont); Resource colorResource = graph.getPossibleObject(element, g2d.HasColor); - java.awt.Font font = null; + Font font = null; if(fontResource != null) font = G2DUtils.getFont(graph, fontResource); Color color = null; if(colorResource != null) color = G2DUtils.getColor(graph, colorResource); - return new Triple(font, color, name); + return new Triple(font, color, name); } return null; @@ -204,12 +213,12 @@ public class VariableInformationTab extends LabelPropertyTabContributor implemen color = dialog.getAWTColor(); } - Font swt = dialog.getSWTFont(); - if(swt != null) - sample.setFont(swt); - org.eclipse.swt.graphics.Color swtColor = dialog.getSWTColor(); - if(swtColor != null) - sample.setForeground(swtColor); + FontData fd = dialog.getSWTFontData(); + if(fd != null) + sample.setFont(resourceManager.createFont(FontDescriptor.createFrom(fd))); + RGB rgb = dialog.getRGB(); + if(rgb != null) + sample.setForeground(resourceManager.createColor(rgb)); fontComposite.layout(); } @@ -239,7 +248,7 @@ public class VariableInformationTab extends LabelPropertyTabContributor implemen } - private Read> fontAndColorRead; + private Read> fontAndColorRead; @Override public void setInput(ISessionContext context, Object input) { @@ -270,11 +279,11 @@ public class VariableInformationTab extends LabelPropertyTabContributor implemen // Read font and color information for sample text if(fontAndColorRead == null) { - fontAndColorRead = new Read>() { + fontAndColorRead = new Read>() { @Override - public Pair perform(ReadGraph graph) throws DatabaseException { - java.awt.Font font = null; + public Pair perform(ReadGraph graph) throws DatabaseException { + Font font = null; Color color = null; if(component != null) { Resource element = graph.getPossibleObject(component, ModelingResources.getInstance(graph).ComponentToElement); @@ -288,14 +297,14 @@ public class VariableInformationTab extends LabelPropertyTabContributor implemen color = G2DUtils.getColor(graph, colorResource); } } - return new Pair(font, color); + return new Pair(font, color); } }; - SimanticsUI.getSession().asyncRequest(fontAndColorRead, new Listener>() { + SimanticsUI.getSession().asyncRequest(fontAndColorRead, new Listener>() { @Override - public void execute(final Pair result) { + public void execute(final Pair result) { final Display device; try { device = sample.getWidget().getDisplay(); @@ -319,15 +328,12 @@ public class VariableInformationTab extends LabelPropertyTabContributor implemen } if(result.first != null) { FontData fd = toSwtFontData(result.first); - sample.setFont(new Font(device, fd)); + sample.setFont(resourceManager.createFont(FontDescriptor.createFrom(fd))); } if(result.second != null) { - sample.setForeground(new org.eclipse.swt.graphics.Color( - device, - result.second.getRed(), - result.second.getGreen(), - result.second.getBlue()) - ); + RGB rgb = new RGB(result.second.getRed(), result.second.getGreen(), + result.second.getBlue()); + sample.setForeground(resourceManager.createColor(rgb)); } try { sample.getWidget().getParent().getParent().layout(); @@ -358,7 +364,7 @@ public class VariableInformationTab extends LabelPropertyTabContributor implemen * @param font AWT Font * @return SWT FontData based on AWT Font */ - private static FontData toSwtFontData(java.awt.Font font) { + private static FontData toSwtFontData(Font font) { FontData fontData = new FontData(); fontData.setName(font.getFamily()); fontData.setStyle(font.getStyle()); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/CustomFontDialog.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/CustomFontDialog.java index 4fd2fe1b..51bcf324 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/CustomFontDialog.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/CustomFontDialog.java @@ -19,13 +19,16 @@ import java.util.Map; import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; +import org.eclipse.jface.resource.FontDescriptor; +import org.eclipse.jface.resource.JFaceResources; +import org.eclipse.jface.resource.LocalResourceManager; import org.eclipse.nebula.widgets.tablecombo.TableCombo; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.graphics.FontData; -import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.RGB; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; @@ -35,6 +38,7 @@ import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableItem; +import org.simantics.utils.ui.gfx.ColorImageDescriptor; /** * Custom dialog for selecting font and font color. Similar to SWT FontDialog. @@ -45,9 +49,9 @@ public class CustomFontDialog extends Dialog { private Map systemColors = createColorMap(); - private org.eclipse.swt.graphics.Font resultSWTFont; - private java.awt.Font awtFont; - private java.awt.Font resultAWTFont; + private FontData resultSWTFontData; + private Font awtFont; + private Font resultAWTFont; private Color color; private Color resultAWTColor; @@ -57,8 +61,10 @@ public class CustomFontDialog extends Dialog { private String example = "Example"; private Label sample; private Group sampleGroup; - private org.eclipse.swt.graphics.Font swtFont; - private org.eclipse.swt.graphics.Color swtColor; + private RGB rgb; + + private LocalResourceManager resourceManager; + // Default color map protected static Map createColorMap() { @@ -111,11 +117,11 @@ public class CustomFontDialog extends Dialog { } /** - * Get selected font as SWT font + * Get selected font as SWT font dta * @return */ - public org.eclipse.swt.graphics.Font getSWTFont() { - return resultSWTFont; + public FontData getSWTFontData() { + return resultSWTFontData; } /** @@ -136,11 +142,11 @@ public class CustomFontDialog extends Dialog { } /** - * Get selected color as SWT color + * Get selected color as RGB * @return */ - public org.eclipse.swt.graphics.Color getSWTColor() { - return swtColor; + public RGB getRGB() { + return rgb; } @@ -156,8 +162,8 @@ public class CustomFontDialog extends Dialog { vpc.addFontModifiedListener(new FontModifyListener() { @Override - public void swtFontChanged(org.eclipse.swt.graphics.Font font) { - sample.setFont(font); + public void swtFontDataChanged(FontData fd) { + sample.setFont(resourceManager.createFont(FontDescriptor.createFrom(fd))); sampleGroup.layout(); } @@ -173,6 +179,9 @@ public class CustomFontDialog extends Dialog { * @param parent Parent composite */ protected void createColorChooser(Composite parent) { + // Create a ResourceManager to dispose images when the widget is disposed. + this.resourceManager = new LocalResourceManager(JFaceResources.getResources(), parent); + Composite colorComposite = new Composite(parent, SWT.NONE); GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(colorComposite); GridLayoutFactory.fillDefaults().numColumns(2).applyTo(colorComposite); @@ -188,20 +197,17 @@ public class CustomFontDialog extends Dialog { tc.setDisplayColumnIndex(1); tc.setToolTipText("this is tooltip"); - if(swtColor != null) { - tc.setForeground(swtColor); - } - createColorItems(tc.getTable()); if(this.color != null) { for(int i = 0; i < tc.getTable().getItemCount(); i++) { TableItem ti = tc.getTable().getItem(i); - org.eclipse.swt.graphics.Color color = (org.eclipse.swt.graphics.Color) ti.getData(); - if(color.getRed() == this.color.getRed() && - color.getGreen() == this.color.getGreen() && - color.getBlue() == this.color.getBlue()) { + RGB rgb = (RGB) ti.getData(); + if(rgb.red == this.color.getRed() && + rgb.green == this.color.getGreen() && + rgb.blue == this.color.getBlue()) { tc.setText(ti.getText(1)); + tc.setForeground(resourceManager.createColor(rgb)); break; } } @@ -214,8 +220,8 @@ public class CustomFontDialog extends Dialog { public void widgetSelected(SelectionEvent e) { TableItem[] selection = tc.getTable().getSelection(); if(selection.length == 1) { - org.eclipse.swt.graphics.Color color = (org.eclipse.swt.graphics.Color) selection[0].getData(); - swtColor = color; + rgb = (RGB) selection[0].getData(); + org.eclipse.swt.graphics.Color swtColor = resourceManager.createColor(rgb); sample.setForeground(swtColor); tc.setForeground(swtColor); } @@ -242,11 +248,10 @@ public class CustomFontDialog extends Dialog { sample = new Label(sampleGroup, SWT.NONE); sample.setText(example); if(awtFont != null) { - swtFont = new org.eclipse.swt.graphics.Font(sample.getDisplay(), toSwtFontData(awtFont, -1)); - sample.setFont(swtFont); + sample.setFont(resourceManager.createFont(FontDescriptor.createFrom(toSwtFontData(awtFont)))); } - if(swtColor != null) { - sample.setForeground(swtColor); + if(rgb != null) { + sample.setForeground(resourceManager.createColor(rgb)); } GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).grab(true, true).applyTo(sample); } @@ -259,9 +264,9 @@ public class CustomFontDialog extends Dialog { GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(composite); - // Init SWT color, if AWT color has been set + // Init SWT RGB, if AWT color has been set if(color != null) - this.swtColor = new org.eclipse.swt.graphics.Color(Display.getCurrent(), color.getRed(), color.getGreen(), color.getBlue()); + this.rgb = new RGB(color.getRed(), color.getGreen(), color.getBlue()); // Font selection composite createFontChooser(composite); @@ -283,7 +288,7 @@ public class CustomFontDialog extends Dialog { resultAWTFont = awtFont; resultAWTColor = color; if(resultAWTFont != null) - resultSWTFont = new org.eclipse.swt.graphics.Font(Display.getCurrent(), toSwtFontData(resultAWTFont, -1)); + resultSWTFontData = toSwtFontData(resultAWTFont); setReturnCode(CANCEL); close(); @@ -292,13 +297,12 @@ public class CustomFontDialog extends Dialog { @Override protected void okPressed() { resultAWTFont = vpc.getAWTFont(); - resultSWTFont = swtFont; + resultSWTFontData = toSwtFontData(resultAWTFont); TableItem[] selection = tc.getTable().getSelection(); if(selection.length == 1) { - org.eclipse.swt.graphics.Color color = (org.eclipse.swt.graphics.Color) selection[0].getData(); - resultAWTColor = new Color(color.getRed(), color.getGreen(), color.getBlue()); - + RGB rgb = (RGB) selection[0].getData(); + resultAWTColor = new Color(rgb.red, rgb.green, rgb.blue); } setReturnCode(OK); @@ -323,11 +327,11 @@ public class CustomFontDialog extends Dialog { * @param height Height for the created data (or -1 if inherited directly from awt font, size matching not guaranteed) * @return */ - protected static FontData toSwtFontData(Font font, int height) { + protected static FontData toSwtFontData(Font font) { FontData fontData = new FontData(); fontData.setName(font.getFontName()); fontData.setStyle(font.getStyle()); - fontData.setHeight(height > 0 ? height : font.getSize()); + fontData.setHeight(font.getSize()); return fontData; } @@ -337,21 +341,14 @@ public class CustomFontDialog extends Dialog { */ protected void createColorItems(Table table) { Image image; - GC gc; TableItem ti; int code; - org.eclipse.swt.graphics.Color color; + RGB color; Display display = Display.getCurrent(); for(String text : systemColors.keySet()) { code = systemColors.get(text); - color = display.getSystemColor(code); - - image = new Image(display, 25, 15); - gc = new GC (image); - gc.setBackground (color); - gc.fillRectangle (image.getBounds()); - gc.dispose (); - + color = display.getSystemColor(code).getRGB(); + image = resourceManager.createImage(new ColorImageDescriptor(color.red, color.green, color.blue, 25, 15, false)); ti = new TableItem(table, SWT.NONE); ti.setImage(0, image); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/FontModifyListener.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/FontModifyListener.java index 2b72e360..eb1e11b1 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/FontModifyListener.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/FontModifyListener.java @@ -13,6 +13,8 @@ package org.simantics.sysdyn.ui.properties.widgets; import java.awt.Font; +import org.eclipse.swt.graphics.FontData; + /** * Font change listening interface * @author Teemu Lempinen @@ -28,7 +30,7 @@ public interface FontModifyListener { /** * Called when font is changed - * @param font New font as SWT font + * @param font New font data as SWT font data */ - public void swtFontChanged(org.eclipse.swt.graphics.Font font); + public void swtFontDataChanged(FontData font); } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/FontSelectionComposite.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/FontSelectionComposite.java index f5d07f1a..9a8f4388 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/FontSelectionComposite.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/FontSelectionComposite.java @@ -21,6 +21,9 @@ import java.util.TreeMap; import org.eclipse.core.runtime.ListenerList; import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; +import org.eclipse.jface.resource.FontDescriptor; +import org.eclipse.jface.resource.JFaceResources; +import org.eclipse.jface.resource.LocalResourceManager; import org.eclipse.swt.SWT; import org.eclipse.swt.events.KeyEvent; import org.eclipse.swt.events.KeyListener; @@ -55,11 +58,10 @@ public class FontSelectionComposite extends Composite { protected TreeMap> fonts = getFonts(familyIndex); protected Table fontFamilyTable, fontStyleTable, fontSizeTable; protected String[] sizes = new String[] {"8", "9", "10", "11", "12", "14", "16", "18", "20", "24", "26", "28", "36", "48", "72"}; - protected org.eclipse.swt.graphics.Font swt; - protected FontData fontData; private ListenerList modifyListeners; - + + private final LocalResourceManager resourceManager; /** * Gets all available fonts @@ -103,6 +105,9 @@ public class FontSelectionComposite extends Composite { */ public FontSelectionComposite(Composite parent, int style) { super(parent, style); + + // Create a ResourceManager to dispose images when the widget is disposed. + this.resourceManager = new LocalResourceManager(JFaceResources.getResources(), this); modifyListeners = new ListenerList(); @@ -234,8 +239,7 @@ public class FontSelectionComposite extends Composite { else return null; } - - + /** * Adds listeners for font family name text and table */ @@ -289,7 +293,7 @@ public class FontSelectionComposite extends Composite { Font font = fonts.get(family).get(0); if(font.canDisplay('a')) { FontData fontData = toSwtFontData(font, 10); - org.eclipse.swt.graphics.Font swtFont = new org.eclipse.swt.graphics.Font(item.getDisplay(), fontData); + org.eclipse.swt.graphics.Font swtFont = resourceManager.createFont(FontDescriptor.createFrom(fontData)); item.setFont(swtFont); } } @@ -590,7 +594,7 @@ public class FontSelectionComposite extends Composite { // If the font is not symbolic, use the font in the created item if(font.canDisplay('a')) { FontData fontData = toSwtFontData(font, 10); - org.eclipse.swt.graphics.Font swtFont = new org.eclipse.swt.graphics.Font(item.getDisplay(), fontData); + org.eclipse.swt.graphics.Font swtFont = resourceManager.createFont(FontDescriptor.createFrom(fontData)); item.setFont(swtFont); } } @@ -664,15 +668,12 @@ public class FontSelectionComposite extends Composite { int style = 0; style |= (font.getFontName(Locale.ROOT).contains("Bold") ? SWT.BOLD : 0); style |= (font.getFontName(Locale.ROOT).contains("Italic") ? SWT.ITALIC : 0); - fontData = new FontData(font.getFamily(Locale.ROOT), font.getSize(), style); - if(swt != null) - swt.dispose(); - swt = new org.eclipse.swt.graphics.Font(this.getDisplay(), fontData); + FontData fontData = new FontData(font.getFamily(Locale.ROOT), font.getSize(), style); Object[] listenersArray = modifyListeners.getListeners(); for (int i = 0; i < listenersArray.length; i++) { ((FontModifyListener)listenersArray[i]).awtFontChanged(font); - ((FontModifyListener)listenersArray[i]).swtFontChanged(swt); + ((FontModifyListener)listenersArray[i]).swtFontDataChanged(fontData); } } } -- 2.47.1