package org.simantics.sysdyn.ui.properties;\r
\r
import java.awt.Color;\r
+import java.awt.Font;\r
\r
import org.eclipse.jface.layout.GridDataFactory;\r
import org.eclipse.jface.layout.GridLayoutFactory;\r
+import org.eclipse.jface.resource.FontDescriptor;\r
+import org.eclipse.jface.resource.JFaceResources;\r
+import org.eclipse.jface.resource.LocalResourceManager;\r
import org.eclipse.swt.SWT;\r
import org.eclipse.swt.SWTException;\r
-import org.eclipse.swt.graphics.Font;\r
import org.eclipse.swt.graphics.FontData;\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.Group;\r
private WidgetSupport support;\r
private Resource component;\r
private org.simantics.browsing.ui.swt.widgets.Label sample;\r
+ private LocalResourceManager resourceManager;\r
\r
\r
@Override\r
public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) {\r
this.support = support;\r
support.register(this);\r
+ \r
+ // Create a ResourceManager to dispose images when the widget is disposed.\r
+ this.resourceManager = new LocalResourceManager(JFaceResources.getResources(), body);\r
+ \r
\r
final Composite composite = new Composite(body, SWT.NONE);\r
GridDataFactory.fillDefaults().grab(true, true).applyTo(composite);\r
\r
b.addSelectionListener(new SelectionListenerImpl<Resource>(context) {\r
\r
- java.awt.Font f;\r
+ Font f;\r
Color color;\r
Object input;\r
\r
@Override\r
public void beforeApply() {\r
\r
- Triple<java.awt.Font, Color, String> result = null;\r
+ Triple<Font, Color, String> result = null;\r
\r
try {\r
- result = SimanticsUI.getSession().syncRequest(new Read<Triple<java.awt.Font, Color, String>>(){\r
+ result = SimanticsUI.getSession().syncRequest(new Read<Triple<Font, Color, String>>(){\r
\r
@Override\r
- public Triple<java.awt.Font, Color, String> perform(ReadGraph graph) throws DatabaseException {\r
+ public Triple<Font, Color, String> perform(ReadGraph graph) throws DatabaseException {\r
Resource component = ISelectionUtils.filterSingleSelection(input, Resource.class);\r
String name = NameUtils.getSafeName(graph, component);\r
\r
Resource fontResource = graph.getPossibleObject(element, g2d.HasFont);\r
Resource colorResource = graph.getPossibleObject(element, g2d.HasColor);\r
\r
- java.awt.Font font = null;\r
+ Font font = null;\r
if(fontResource != null)\r
font = G2DUtils.getFont(graph, fontResource);\r
Color color = null;\r
if(colorResource != null)\r
color = G2DUtils.getColor(graph, colorResource);\r
\r
- return new Triple<java.awt.Font, Color, String>(font, color, name);\r
+ return new Triple<Font, Color, String>(font, color, name);\r
}\r
\r
return null;\r
color = dialog.getAWTColor();\r
}\r
\r
- Font swt = dialog.getSWTFont();\r
- if(swt != null)\r
- sample.setFont(swt);\r
- org.eclipse.swt.graphics.Color swtColor = dialog.getSWTColor();\r
- if(swtColor != null)\r
- sample.setForeground(swtColor);\r
+ FontData fd = dialog.getSWTFontData();\r
+ if(fd != null)\r
+ sample.setFont(resourceManager.createFont(FontDescriptor.createFrom(fd)));\r
+ RGB rgb = dialog.getRGB(); \r
+ if(rgb != null)\r
+ sample.setForeground(resourceManager.createColor(rgb));\r
fontComposite.layout();\r
}\r
\r
\r
}\r
\r
- private Read<Pair<java.awt.Font, Color>> fontAndColorRead;\r
+ private Read<Pair<Font, Color>> fontAndColorRead;\r
\r
@Override\r
public void setInput(ISessionContext context, Object input) {\r
\r
// Read font and color information for sample text\r
if(fontAndColorRead == null) {\r
- fontAndColorRead = new Read<Pair<java.awt.Font, Color>>() {\r
+ fontAndColorRead = new Read<Pair<Font, Color>>() {\r
\r
@Override\r
- public Pair<java.awt.Font, Color> perform(ReadGraph graph) throws DatabaseException {\r
- java.awt.Font font = null;\r
+ public Pair<Font, Color> perform(ReadGraph graph) throws DatabaseException {\r
+ Font font = null;\r
Color color = null;\r
if(component != null) {\r
Resource element = graph.getPossibleObject(component, ModelingResources.getInstance(graph).ComponentToElement);\r
color = G2DUtils.getColor(graph, colorResource);\r
}\r
}\r
- return new Pair<java.awt.Font, Color>(font, color);\r
+ return new Pair<Font, Color>(font, color);\r
}\r
};\r
\r
- SimanticsUI.getSession().asyncRequest(fontAndColorRead, new Listener<Pair<java.awt.Font, Color>>() {\r
+ SimanticsUI.getSession().asyncRequest(fontAndColorRead, new Listener<Pair<Font, Color>>() {\r
\r
@Override\r
- public void execute(final Pair<java.awt.Font, Color> result) {\r
+ public void execute(final Pair<Font, Color> result) {\r
final Display device;\r
try {\r
device = sample.getWidget().getDisplay();\r
}\r
if(result.first != null) {\r
FontData fd = toSwtFontData(result.first);\r
- sample.setFont(new Font(device, fd));\r
+ sample.setFont(resourceManager.createFont(FontDescriptor.createFrom(fd)));\r
}\r
if(result.second != null) {\r
- sample.setForeground(new org.eclipse.swt.graphics.Color(\r
- device, \r
- result.second.getRed(), \r
- result.second.getGreen(), \r
- result.second.getBlue())\r
- );\r
+ RGB rgb = new RGB(result.second.getRed(), result.second.getGreen(), \r
+ result.second.getBlue());\r
+ sample.setForeground(resourceManager.createColor(rgb));\r
}\r
try {\r
sample.getWidget().getParent().getParent().layout();\r
* @param font AWT Font\r
* @return SWT FontData based on AWT Font\r
*/\r
- private static FontData toSwtFontData(java.awt.Font font) {\r
+ private static FontData toSwtFontData(Font font) {\r
FontData fontData = new FontData();\r
fontData.setName(font.getFamily());\r
fontData.setStyle(font.getStyle());\r
import org.eclipse.jface.dialogs.Dialog;\r
import org.eclipse.jface.layout.GridDataFactory;\r
import org.eclipse.jface.layout.GridLayoutFactory;\r
+import org.eclipse.jface.resource.FontDescriptor;\r
+import org.eclipse.jface.resource.JFaceResources;\r
+import org.eclipse.jface.resource.LocalResourceManager;\r
import org.eclipse.nebula.widgets.tablecombo.TableCombo;\r
import org.eclipse.swt.SWT;\r
import org.eclipse.swt.events.SelectionEvent;\r
import org.eclipse.swt.events.SelectionListener;\r
import org.eclipse.swt.graphics.FontData;\r
-import org.eclipse.swt.graphics.GC;\r
import org.eclipse.swt.graphics.Image;\r
+import org.eclipse.swt.graphics.RGB;\r
import org.eclipse.swt.graphics.Rectangle;\r
import org.eclipse.swt.widgets.Composite;\r
import org.eclipse.swt.widgets.Control;\r
import org.eclipse.swt.widgets.Shell;\r
import org.eclipse.swt.widgets.Table;\r
import org.eclipse.swt.widgets.TableItem;\r
+import org.simantics.utils.ui.gfx.ColorImageDescriptor;\r
\r
/**\r
* Custom dialog for selecting font and font color. Similar to SWT FontDialog.\r
\r
private Map<String, Integer> systemColors = createColorMap();\r
\r
- private org.eclipse.swt.graphics.Font resultSWTFont;\r
- private java.awt.Font awtFont;\r
- private java.awt.Font resultAWTFont;\r
+ private FontData resultSWTFontData;\r
+ private Font awtFont;\r
+ private Font resultAWTFont;\r
private Color color;\r
private Color resultAWTColor;\r
\r
private String example = "Example";\r
private Label sample;\r
private Group sampleGroup;\r
- private org.eclipse.swt.graphics.Font swtFont;\r
- private org.eclipse.swt.graphics.Color swtColor;\r
+ private RGB rgb;\r
+ \r
+ private LocalResourceManager resourceManager;\r
+\r
\r
// Default color map\r
protected static Map<String, Integer> createColorMap() {\r
}\r
\r
/**\r
- * Get selected font as SWT font\r
+ * Get selected font as SWT font dta\r
* @return\r
*/\r
- public org.eclipse.swt.graphics.Font getSWTFont() {\r
- return resultSWTFont;\r
+ public FontData getSWTFontData() {\r
+ return resultSWTFontData;\r
}\r
\r
/**\r
}\r
\r
/**\r
- * Get selected color as SWT color\r
+ * Get selected color as RGB\r
* @return\r
*/\r
- public org.eclipse.swt.graphics.Color getSWTColor() {\r
- return swtColor;\r
+ public RGB getRGB() {\r
+ return rgb;\r
}\r
\r
\r
vpc.addFontModifiedListener(new FontModifyListener() {\r
\r
@Override\r
- public void swtFontChanged(org.eclipse.swt.graphics.Font font) {\r
- sample.setFont(font);\r
+ public void swtFontDataChanged(FontData fd) {\r
+ sample.setFont(resourceManager.createFont(FontDescriptor.createFrom(fd)));\r
sampleGroup.layout();\r
}\r
\r
* @param parent Parent composite\r
*/\r
protected void createColorChooser(Composite parent) {\r
+ // Create a ResourceManager to dispose images when the widget is disposed.\r
+ this.resourceManager = new LocalResourceManager(JFaceResources.getResources(), parent);\r
+ \r
Composite colorComposite = new Composite(parent, SWT.NONE);\r
GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(colorComposite);\r
GridLayoutFactory.fillDefaults().numColumns(2).applyTo(colorComposite);\r
tc.setDisplayColumnIndex(1);\r
tc.setToolTipText("this is tooltip");\r
\r
- if(swtColor != null) {\r
- tc.setForeground(swtColor);\r
- }\r
- \r
createColorItems(tc.getTable());\r
\r
if(this.color != null) {\r
for(int i = 0; i < tc.getTable().getItemCount(); i++) {\r
TableItem ti = tc.getTable().getItem(i);\r
- org.eclipse.swt.graphics.Color color = (org.eclipse.swt.graphics.Color) ti.getData();\r
- if(color.getRed() == this.color.getRed() &&\r
- color.getGreen() == this.color.getGreen() &&\r
- color.getBlue() == this.color.getBlue()) {\r
+ RGB rgb = (RGB) ti.getData();\r
+ if(rgb.red == this.color.getRed() &&\r
+ rgb.green == this.color.getGreen() &&\r
+ rgb.blue == this.color.getBlue()) {\r
tc.setText(ti.getText(1));\r
+ tc.setForeground(resourceManager.createColor(rgb));\r
break;\r
}\r
}\r
public void widgetSelected(SelectionEvent e) {\r
TableItem[] selection = tc.getTable().getSelection();\r
if(selection.length == 1) {\r
- org.eclipse.swt.graphics.Color color = (org.eclipse.swt.graphics.Color) selection[0].getData();\r
- swtColor = color;\r
+ rgb = (RGB) selection[0].getData();\r
+ org.eclipse.swt.graphics.Color swtColor = resourceManager.createColor(rgb);\r
sample.setForeground(swtColor);\r
tc.setForeground(swtColor);\r
}\r
sample = new Label(sampleGroup, SWT.NONE);\r
sample.setText(example);\r
if(awtFont != null) {\r
- swtFont = new org.eclipse.swt.graphics.Font(sample.getDisplay(), toSwtFontData(awtFont, -1));\r
- sample.setFont(swtFont);\r
+ sample.setFont(resourceManager.createFont(FontDescriptor.createFrom(toSwtFontData(awtFont))));\r
}\r
- if(swtColor != null) {\r
- sample.setForeground(swtColor);\r
+ if(rgb != null) {\r
+ sample.setForeground(resourceManager.createColor(rgb));\r
}\r
GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).grab(true, true).applyTo(sample);\r
}\r
\r
GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(composite);\r
\r
- // Init SWT color, if AWT color has been set\r
+ // Init SWT RGB, if AWT color has been set\r
if(color != null)\r
- this.swtColor = new org.eclipse.swt.graphics.Color(Display.getCurrent(), color.getRed(), color.getGreen(), color.getBlue());\r
+ this.rgb = new RGB(color.getRed(), color.getGreen(), color.getBlue());\r
\r
// Font selection composite\r
createFontChooser(composite);\r
resultAWTFont = awtFont;\r
resultAWTColor = color;\r
if(resultAWTFont != null)\r
- resultSWTFont = new org.eclipse.swt.graphics.Font(Display.getCurrent(), toSwtFontData(resultAWTFont, -1));\r
+ resultSWTFontData = toSwtFontData(resultAWTFont);\r
\r
setReturnCode(CANCEL);\r
close();\r
@Override\r
protected void okPressed() {\r
resultAWTFont = vpc.getAWTFont();\r
- resultSWTFont = swtFont;\r
+ resultSWTFontData = toSwtFontData(resultAWTFont);\r
\r
TableItem[] selection = tc.getTable().getSelection();\r
if(selection.length == 1) {\r
- org.eclipse.swt.graphics.Color color = (org.eclipse.swt.graphics.Color) selection[0].getData();\r
- resultAWTColor = new Color(color.getRed(), color.getGreen(), color.getBlue());\r
- \r
+ RGB rgb = (RGB) selection[0].getData();\r
+ resultAWTColor = new Color(rgb.red, rgb.green, rgb.blue);\r
}\r
\r
setReturnCode(OK);\r
* @param height Height for the created data (or -1 if inherited directly from awt font, size matching not guaranteed)\r
* @return\r
*/\r
- protected static FontData toSwtFontData(Font font, int height) {\r
+ protected static FontData toSwtFontData(Font font) {\r
FontData fontData = new FontData();\r
fontData.setName(font.getFontName());\r
fontData.setStyle(font.getStyle());\r
- fontData.setHeight(height > 0 ? height : font.getSize());\r
+ fontData.setHeight(font.getSize());\r
return fontData;\r
}\r
\r
*/\r
protected void createColorItems(Table table) {\r
Image image;\r
- GC gc;\r
TableItem ti;\r
int code;\r
- org.eclipse.swt.graphics.Color color;\r
+ RGB color;\r
Display display = Display.getCurrent();\r
for(String text : systemColors.keySet()) {\r
code = systemColors.get(text);\r
- color = display.getSystemColor(code);\r
- \r
- image = new Image(display, 25, 15);\r
- gc = new GC (image);\r
- gc.setBackground (color);\r
- gc.fillRectangle (image.getBounds());\r
- gc.dispose ();\r
- \r
+ color = display.getSystemColor(code).getRGB();\r
+ image = resourceManager.createImage(new ColorImageDescriptor(color.red, color.green, color.blue, 25, 15, false));\r
\r
ti = new TableItem(table, SWT.NONE);\r
ti.setImage(0, image);\r
\r
import java.awt.Font;\r
\r
+import org.eclipse.swt.graphics.FontData;\r
+\r
/**\r
* Font change listening interface\r
* @author Teemu Lempinen\r
\r
/**\r
* Called when font is changed\r
- * @param font New font as SWT font\r
+ * @param font New font data as SWT font data\r
*/\r
- public void swtFontChanged(org.eclipse.swt.graphics.Font font);\r
+ public void swtFontDataChanged(FontData font);\r
}\r
import org.eclipse.core.runtime.ListenerList;\r
import org.eclipse.jface.layout.GridDataFactory;\r
import org.eclipse.jface.layout.GridLayoutFactory;\r
+import org.eclipse.jface.resource.FontDescriptor;\r
+import org.eclipse.jface.resource.JFaceResources;\r
+import org.eclipse.jface.resource.LocalResourceManager;\r
import org.eclipse.swt.SWT;\r
import org.eclipse.swt.events.KeyEvent;\r
import org.eclipse.swt.events.KeyListener;\r
protected TreeMap<String, ArrayList<Font>> fonts = getFonts(familyIndex);\r
protected Table fontFamilyTable, fontStyleTable, fontSizeTable;\r
protected String[] sizes = new String[] {"8", "9", "10", "11", "12", "14", "16", "18", "20", "24", "26", "28", "36", "48", "72"};\r
- protected org.eclipse.swt.graphics.Font swt;\r
- protected FontData fontData;\r
\r
private ListenerList modifyListeners;\r
-\r
+ \r
+ private final LocalResourceManager resourceManager;\r
\r
/**\r
* Gets all available fonts\r
*/\r
public FontSelectionComposite(Composite parent, int style) {\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
modifyListeners = new ListenerList();\r
\r
else\r
return null;\r
}\r
-\r
-\r
+ \r
/**\r
* Adds listeners for font family name text and table\r
*/\r
Font font = fonts.get(family).get(0);\r
if(font.canDisplay('a')) {\r
FontData fontData = toSwtFontData(font, 10);\r
- org.eclipse.swt.graphics.Font swtFont = new org.eclipse.swt.graphics.Font(item.getDisplay(), fontData);\r
+ org.eclipse.swt.graphics.Font swtFont = resourceManager.createFont(FontDescriptor.createFrom(fontData));\r
item.setFont(swtFont);\r
}\r
}\r
// If the font is not symbolic, use the font in the created item\r
if(font.canDisplay('a')) {\r
FontData fontData = toSwtFontData(font, 10);\r
- org.eclipse.swt.graphics.Font swtFont = new org.eclipse.swt.graphics.Font(item.getDisplay(), fontData);\r
+ org.eclipse.swt.graphics.Font swtFont = resourceManager.createFont(FontDescriptor.createFrom(fontData));\r
item.setFont(swtFont);\r
}\r
}\r
int style = 0;\r
style |= (font.getFontName(Locale.ROOT).contains("Bold") ? SWT.BOLD : 0);\r
style |= (font.getFontName(Locale.ROOT).contains("Italic") ? SWT.ITALIC : 0);\r
- fontData = new FontData(font.getFamily(Locale.ROOT), font.getSize(), style);\r
- if(swt != null)\r
- swt.dispose();\r
- swt = new org.eclipse.swt.graphics.Font(this.getDisplay(), fontData);\r
+ FontData fontData = new FontData(font.getFamily(Locale.ROOT), font.getSize(), style);\r
\r
Object[] listenersArray = modifyListeners.getListeners();\r
for (int i = 0; i < listenersArray.length; i++) {\r
((FontModifyListener)listenersArray[i]).awtFontChanged(font);\r
- ((FontModifyListener)listenersArray[i]).swtFontChanged(swt);\r
+ ((FontModifyListener)listenersArray[i]).swtFontDataChanged(fontData);\r
}\r
}\r
}\r