From fbc40ecda3c1e534aa1b1f1795ca5edbcd55560d Mon Sep 17 00:00:00 2001 From: lempinen Date: Mon, 5 Mar 2012 14:08:04 +0000 Subject: [PATCH] Choose font and color for sysdyn elements (refs #2959) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@24350 ac1ea38d-2e2b-0410-8846-a27921b304fc --- org.simantics.sysdyn.ui/META-INF/MANIFEST.MF | 4 +- .../ui/elements2/SysdynElementFactory.java | 33 +- .../ui/properties/VariableInformationTab.java | 313 ++++++++++++++++- .../properties/widgets/CustomFontDialog.java | 328 ++++++++++++++++++ .../widgets/FontModifyListener.java | 34 ++ .../widgets/FontSelectionComposite.java | 60 ++-- 6 files changed, 729 insertions(+), 43 deletions(-) create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/CustomFontDialog.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/FontModifyListener.java diff --git a/org.simantics.sysdyn.ui/META-INF/MANIFEST.MF b/org.simantics.sysdyn.ui/META-INF/MANIFEST.MF index 2852c03b..3996a4b8 100644 --- a/org.simantics.sysdyn.ui/META-INF/MANIFEST.MF +++ b/org.simantics.sysdyn.ui/META-INF/MANIFEST.MF @@ -52,7 +52,9 @@ Require-Bundle: org.simantics.layer0.utils;bundle-version="0.6.2", org.simantics.utils.thread.swt;bundle-version="1.1.0", org.simantics.jfreechart.ontology;bundle-version="0.1.0", org.eclipse.ui.forms;bundle-version="3.5.2", - org.simantics.scenegraph.swing;bundle-version="1.0.0" + org.simantics.scenegraph.swing;bundle-version="1.0.0", + org.eclipse.nebula.widgets;bundle-version="1.0.0", + org.eclipse.nebula.widgets.tablecombo;bundle-version="1.0.0" Bundle-Activator: org.simantics.sysdyn.ui.Activator Bundle-ActivationPolicy: lazy Export-Package: org.simantics.sysdyn.ui.browser.nodes diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/SysdynElementFactory.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/SysdynElementFactory.java index 784c6bac..65e6e064 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/SysdynElementFactory.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/SysdynElementFactory.java @@ -60,8 +60,6 @@ public abstract class SysdynElementFactory extends SyncElementFactory { @Override public void load(ReadGraph graph, final ICanvasContext canvas, final IDiagram diagram, final Resource element, final IElement e) throws DatabaseException { Layer0 l0 = Layer0.getInstance(graph); - G2DResource g2d = G2DResource.getInstance(graph); - DiagramResource dr = DiagramResource.getInstance(graph); ModelingResources mr = ModelingResources.getInstance(graph); Resource component = graph.getPossibleObject(element, mr.ElementToComponent); @@ -73,7 +71,30 @@ public abstract class SysdynElementFactory extends SyncElementFactory { text = "[empty]"; ElementUtils.setText(e, text); + + getVisualProperties(graph, element, e); + AffineTransform at = DiagramGraphUtil.getAffineTransform(graph, element); + ElementUtils.setTransform(e, at); + + // This synchronizes only text and transformation (not font and color) + e.setHint(SynchronizationHints.HINT_SYNCHRONIZER, SYNCHRONIZER); + + e.setHint(ElementHints.KEY_HOVER, false); + } + + /** + * Reads a collection of visualization properties e.g. colour and font. + * @param graph ReadGraph + * @param element Element resource + * @param e Diagram element + * @throws DatabaseException + */ + private void getVisualProperties(ReadGraph graph, Resource element, IElement e) throws DatabaseException { + G2DResource g2d = G2DResource.getInstance(graph); + DiagramResource dr = DiagramResource.getInstance(graph); + ModelingResources mr = ModelingResources.getInstance(graph); + if (graph.isInstanceOf(element, dr.FontProvider)) { Resource fontResource = graph.getPossibleObject(element, g2d.HasFont); if (fontResource != null) @@ -85,19 +106,13 @@ public abstract class SysdynElementFactory extends SyncElementFactory { ElementUtils.setTextColor(e, G2DUtils.getColor(graph, colorResource)); } + Resource component = graph.getPossibleObject(element, mr.ElementToComponent); if (component != null && graph.hasStatement(component, SysdynResource.getInstance(graph).IsOutput)) { Font font = ElementUtils.getTextFont(e); font = font.deriveFont(Font.BOLD); ElementUtils.setTextFont(e, font); } - AffineTransform at = DiagramGraphUtil.getAffineTransform(graph, element); - ElementUtils.setTransform(e, at); - - // This synchronizes only text and transformation (not font and color) - e.setHint(SynchronizationHints.HINT_SYNCHRONIZER, SYNCHRONIZER); - - e.setHint(ElementHints.KEY_HOVER, false); } @Override 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 0aa38816..9781e8e2 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 @@ -11,30 +11,53 @@ *******************************************************************************/ package org.simantics.sysdyn.ui.properties; +import java.awt.Color; + +import javax.swing.JPanel; + import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; +import org.eclipse.jface.viewers.ISelection; import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.Device; +import org.eclipse.swt.graphics.Font; +import org.eclipse.swt.graphics.FontData; +import org.eclipse.swt.graphics.GC; import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Group; import org.eclipse.swt.widgets.Label; import org.eclipse.ui.IWorkbenchSite; +import org.simantics.browsing.ui.swt.widgets.Button; import org.simantics.browsing.ui.swt.widgets.StringPropertyFactory; import org.simantics.browsing.ui.swt.widgets.StringPropertyModifier; import org.simantics.browsing.ui.swt.widgets.TrackedText; +import org.simantics.browsing.ui.swt.widgets.impl.SelectionListenerImpl; import org.simantics.browsing.ui.swt.widgets.impl.Widget; import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.utils.NameUtils; import org.simantics.db.exception.DatabaseException; import org.simantics.db.management.ISessionContext; +import org.simantics.db.procedure.Listener; import org.simantics.db.request.Read; +import org.simantics.diagram.G2DUtils; +import org.simantics.diagram.stubs.G2DResource; import org.simantics.layer0.Layer0; +import org.simantics.modeling.ModelingResources; import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.ui.properties.widgets.CustomFontDialog; import org.simantics.sysdyn.ui.properties.widgets.ValveOrientationGroup; import org.simantics.sysdyn.ui.properties.widgets.ValveTextLocationGroup; import org.simantics.sysdyn.ui.properties.widgets.factories.DoublePropertyFactory; import org.simantics.sysdyn.ui.properties.widgets.factories.DoublePropertyModifier; +import org.simantics.ui.SimanticsUI; import org.simantics.ui.utils.AdaptionUtils; +import org.simantics.utils.datastructures.Pair; +import org.simantics.utils.datastructures.Triple; +import org.simantics.utils.ui.ISelectionUtils; import org.simantics.utils.ui.validators.DoubleValidator; /** @@ -43,15 +66,18 @@ import org.simantics.utils.ui.validators.DoubleValidator; * */ public class VariableInformationTab extends LabelPropertyTabContributor implements Widget { - Composite orientationComposite; - WidgetSupport support; - + private Composite orientationComposite; + private WidgetSupport support; + private Resource component; + private org.simantics.browsing.ui.swt.widgets.Label sample; + + @Override public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) { this.support = support; support.register(this); - - Composite composite = new Composite(body, SWT.NONE); + + final Composite composite = new Composite(body, SWT.NONE); GridDataFactory.fillDefaults().grab(true, true).applyTo(composite); GridLayoutFactory.fillDefaults().numColumns(2).margins(3, 3).applyTo(composite); @@ -59,7 +85,7 @@ public class VariableInformationTab extends LabelPropertyTabContributor implemen informationGroup.setText("Information"); GridDataFactory.fillDefaults().grab(false, true).applyTo(informationGroup); GridLayoutFactory.fillDefaults().margins(3, 3).applyTo(informationGroup); - + // Textual format documentation TrackedText information = new TrackedText(informationGroup, support, SWT.MULTI | SWT.BORDER); information.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasDescription)); @@ -70,7 +96,7 @@ public class VariableInformationTab extends LabelPropertyTabContributor implemen orientationComposite = new Composite(composite, SWT.NONE); GridDataFactory.fillDefaults().span(1, 2).applyTo(orientationComposite); GridLayoutFactory.fillDefaults().margins(3,3).applyTo(orientationComposite); - + // Range of a variable (e.g. from 0 to 10). Does not affect simulation, but the infor can be used for example in charts Group rangeGroup = new Group(composite, SWT.SHADOW_ETCHED_IN); rangeGroup.setText("Range"); @@ -105,12 +131,122 @@ public class VariableInformationTab extends LabelPropertyTabContributor implemen rangeStep.addModifyListener(new DoublePropertyModifier(context, SysdynResource.URIs.HasRangeStep)); rangeStep.setInputValidator(new DoubleValidator()); GridDataFactory.fillDefaults().grab(true, false).applyTo(rangeStep.getWidget()); + + + // Font options. FIXME: very bad appearance right now + final Composite fontComposite = new Composite(composite, SWT.NONE); + GridDataFactory.fillDefaults().applyTo(fontComposite); + GridLayoutFactory.fillDefaults().numColumns(2).applyTo(fontComposite); + Button b = new Button(fontComposite, support, SWT.PUSH); + b.setText("Choose Font"); + + // Sample text with selected font + sample = new org.simantics.browsing.ui.swt.widgets.Label(fontComposite, support, SWT.NONE); + sample.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasName, "Sample")); + + b.addSelectionListener(new SelectionListenerImpl(context) { + + java.awt.Font f; + Color color; + Object input; + + public void beforeApply() { + + Triple result = null; + + try { + result = SimanticsUI.getSession().syncRequest(new Read>(){ + + @Override + public Triple perform(ReadGraph graph) throws DatabaseException { + Resource component = ISelectionUtils.filterSingleSelection((ISelection)input, Resource.class); + String name = NameUtils.getSafeName(graph, component); + + Resource element = graph.getPossibleObject(component, ModelingResources.getInstance(graph).ComponentToElement); + if(element != null) { + G2DResource g2d = G2DResource.getInstance(graph); + Resource fontResource = graph.getPossibleObject(element, g2d.HasFont); + Resource colorResource = graph.getPossibleObject(element, g2d.HasColor); + + java.awt.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 null; + } + + }); + } catch (DatabaseException e) { + } + + + CustomFontDialog dialog = new CustomFontDialog(composite.getShell(), (result != null ? result.third : null)); + + if(result != null) { + if(result.first != null) { + dialog.setAWTFont(result.first); + f = result.first; + } + if(result.second != null) { + dialog.setColor(result.second); + color = result.second; + } + } + + dialog.open(); + if(dialog.getAWTFont() != null) + f = dialog.getAWTFont(); + if(dialog.getAWTColor() != null) { + 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); + fontComposite.layout(); + } + + @Override + public void apply(WriteGraph graph, Resource input) throws DatabaseException { + Resource element = graph.getPossibleObject(input, ModelingResources.getInstance(graph).ComponentToElement); + if(element != null) { + G2DResource g2d = G2DResource.getInstance(graph); + graph.deny(element, g2d.HasFont); + if(f != null) + graph.claim(element, g2d.HasFont, G2DUtils.createFont(graph, f)); + graph.deny(element, g2d.HasColor); + if(color != null) + graph.claim(element, g2d.HasColor, G2DUtils.createColor(graph, color)); + } + } + + @Override + public void setInput(ISessionContext context, Object parameter) { + super.setInput(context, parameter); + input = parameter; + } + + }); + + + } + private Read> fontAndColorRead; + @Override public void setInput(ISessionContext context, Object input) { - final Resource valve = AdaptionUtils.adaptToSingle(input, Resource.class); + component = AdaptionUtils.adaptToSingle(input, Resource.class); // is the displayed variable a valve? Boolean isValve = false; try { @@ -119,7 +255,7 @@ public class VariableInformationTab extends LabelPropertyTabContributor implemen @Override public Boolean perform(ReadGraph graph) throws DatabaseException { SysdynResource sr = SysdynResource.getInstance(graph); - return graph.isInstanceOf(valve, sr.Valve); + return graph.isInstanceOf(component, sr.Valve); } }); @@ -134,5 +270,164 @@ public class VariableInformationTab extends LabelPropertyTabContributor implemen vtlg.setInput(context, input); orientationComposite.getParent().layout(); } + + // Read font and color information for sample text + if(fontAndColorRead == null) { + fontAndColorRead = new Read>() { + + @Override + public Pair perform(ReadGraph graph) throws DatabaseException { + java.awt.Font font = null; + Color color = null; + if(component != null) { + Resource element = graph.getPossibleObject(component, ModelingResources.getInstance(graph).ComponentToElement); + if(element != null) { + G2DResource g2d = G2DResource.getInstance(graph); + Resource fontResource = graph.getPossibleObject(element, g2d.HasFont); + if(fontResource != null) + font = G2DUtils.getFont(graph, fontResource); + Resource colorResource = graph.getPossibleObject(element, g2d.HasColor); + if(colorResource != null) + color = G2DUtils.getColor(graph, colorResource); + } + } + return new Pair(font, color); + } + }; + + SimanticsUI.getSession().asyncRequest(fontAndColorRead, new Listener>() { + + @Override + public void execute(final Pair result) { + final Display device = sample.getWidget().getDisplay(); + + device.asyncExec(new Runnable() { + + @Override + public void run() { + if(result.first != null) { + FontData fd = toSwtFontData(device, result.first, true); + sample.setFont(new Font(device, fd)); + } + if(result.second != null) { + sample.setForeground(new org.eclipse.swt.graphics.Color( + device, + result.second.getRed(), + result.second.getGreen(), + result.second.getBlue()) + ); + } + sample.getWidget().getParent().getParent().layout(); + } + }); + + } + + @Override + public void exception(Throwable t) { + t.printStackTrace(); + } + + @Override + public boolean isDisposed() { + return sample == null || sample.getWidget().isDisposed(); + } + + }); + } } + + + + /** + * Copied from jfreechart experimental SWTUtils + */ + private final static String Az = "ABCpqr"; + public static java.awt.Font toAwtFont(Device device, FontData fontData, + boolean ensureSameSize) { + int height = (int) Math.round(fontData.getHeight() * device.getDPI().y + / 72.0); + // hack to ensure the newly created awt fonts will be rendered with the + // same height as the swt one + if (ensureSameSize) { + GC tmpGC = new GC(device); + Font tmpFont = new Font(device, fontData); + tmpGC.setFont(tmpFont); + JPanel DUMMY_PANEL = new JPanel(); + java.awt.Font tmpAwtFont = new java.awt.Font(fontData.getName(), + fontData.getStyle(), height); + if (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az) + > tmpGC.textExtent(Az).x) { + while (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az) + > tmpGC.textExtent(Az).x) { + height--; + tmpAwtFont = new java.awt.Font(fontData.getName(), + fontData.getStyle(), height); + } + } + else if (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az) + < tmpGC.textExtent(Az).x) { + while (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az) + < tmpGC.textExtent(Az).x) { + height++; + tmpAwtFont = new java.awt.Font(fontData.getName(), + fontData.getStyle(), height); + } + } + tmpFont.dispose(); + tmpGC.dispose(); + } + return new java.awt.Font(fontData.getName(), fontData.getStyle(), + height); + } + + + /** + * Copied from jfreechart SWTUtils + */ + private static final JPanel DUMMY_PANEL = new JPanel(); + public static FontData toSwtFontData(Device device, java.awt.Font font, + boolean ensureSameSize) { + FontData fontData = new FontData(); + fontData.setName(font.getFamily()); + // SWT and AWT share the same style constants. + fontData.setStyle(font.getStyle()); + // convert the font size (in pt for awt) to height in pixels for swt + int height = (int) Math.round(font.getSize() * 72.0 + / device.getDPI().y); + fontData.setHeight(height); + // hack to ensure the newly created swt fonts will be rendered with the + // same height as the awt one + if (ensureSameSize) { + GC tmpGC = new GC(device); + Font tmpFont = new Font(device, fontData); + tmpGC.setFont(tmpFont); + if (tmpGC.textExtent(Az).x + > DUMMY_PANEL.getFontMetrics(font).stringWidth(Az)) { + while (tmpGC.textExtent(Az).x + > DUMMY_PANEL.getFontMetrics(font).stringWidth(Az)) { + tmpFont.dispose(); + height--; + fontData.setHeight(height); + tmpFont = new Font(device, fontData); + tmpGC.setFont(tmpFont); + } + } + else if (tmpGC.textExtent(Az).x + < DUMMY_PANEL.getFontMetrics(font).stringWidth(Az)) { + while (tmpGC.textExtent(Az).x + < DUMMY_PANEL.getFontMetrics(font).stringWidth(Az)) { + tmpFont.dispose(); + height++; + fontData.setHeight(height); + tmpFont = new Font(device, fontData); + tmpGC.setFont(tmpFont); + } + } + tmpFont.dispose(); + tmpGC.dispose(); + } + return fontData; + } + } 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 new file mode 100644 index 00000000..51f70069 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/CustomFontDialog.java @@ -0,0 +1,328 @@ +/******************************************************************************* + * Copyright (c) 2007, 2012 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.sysdyn.ui.properties.widgets; + +import java.awt.Color; +import java.awt.Font; +import java.util.LinkedHashMap; +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.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.Rectangle; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Group; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.swt.widgets.Table; +import org.eclipse.swt.widgets.TableItem; + +/** + * Custom dialog for selecting font and font color. Similar to SWT FontDialog. + * @author Teemu Lempinen + * + */ +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 Color color; + private Color resultAWTColor; + + private FontSelectionComposite vpc; + private TableCombo tc; + + private String example = "Example"; + private Label sample; + private Group sampleGroup; + private org.eclipse.swt.graphics.Font swtFont; + private org.eclipse.swt.graphics.Color swtColor; + + // Default color map + private static Map createColorMap() { + LinkedHashMap colors = new LinkedHashMap(); + colors.put("Black", SWT.COLOR_BLACK); + colors.put("White", SWT.COLOR_WHITE); + colors.put("Blue", SWT.COLOR_BLUE); + colors.put("Dark Blue", SWT.COLOR_DARK_BLUE); + colors.put("Red", SWT.COLOR_RED); + colors.put("Dark Red", SWT.COLOR_DARK_RED); + colors.put("Yellow", SWT.COLOR_YELLOW); + colors.put("Dark Yellow", SWT.COLOR_DARK_YELLOW); + colors.put("Gray", SWT.COLOR_GRAY); + colors.put("Dark Gray", SWT.COLOR_DARK_GRAY); + colors.put("Green", SWT.COLOR_GREEN); + colors.put("Dark Green", SWT.COLOR_DARK_GREEN); + colors.put("Cyan", SWT.COLOR_CYAN); + colors.put("Dark Cyan", SWT.COLOR_DARK_CYAN); + colors.put("Magenta", SWT.COLOR_MAGENTA); + colors.put("Dark Magenta", SWT.COLOR_DARK_MAGENTA); + return colors; + } + + /** + * Creates a font dialog with sample text + * @param parentShell + * @param example Sample text in the dialog. Null example => "Example" + */ + public CustomFontDialog(Shell parentShell, String example) { + super(parentShell); + if(example != null) + this.example = example; + } + + /** + * Sets the initial font for this dialog + * @param awtFont Current AWT font + */ + public void setAWTFont(java.awt.Font awtFont) { + this.awtFont = awtFont; + this.resultAWTFont = awtFont; + } + + /** + * Get selected font as AWT font + * @return AWT font + */ + public java.awt.Font getAWTFont() { + return resultAWTFont; + } + + /** + * Get selected font as SWT font + * @return + */ + public org.eclipse.swt.graphics.Font getSWTFont() { + return resultSWTFont; + } + + /** + * Set initial color for this dialog + * @param color AWT color + */ + public void setColor(Color color) { + this.color = color; + this.resultAWTColor = color; + } + + /** + * Get selected color as AWT color + * @return + */ + public Color getAWTColor() { + return resultAWTColor; + } + + /** + * Get selected color as SWT color + * @return + */ + public org.eclipse.swt.graphics.Color getSWTColor() { + return swtColor; + } + + @Override + protected Control createDialogArea(Composite parent) + { + Composite composite = ( Composite )super.createDialogArea(parent); + composite.getShell().setText("Choose Font"); + + GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(composite); + + // Font selection composite + vpc = new FontSelectionComposite(composite, SWT.NONE); + vpc.setFont(awtFont, false); + GridDataFactory.fillDefaults().span(2, 1).applyTo(vpc); + + vpc.addFontModifiedListener(new FontModifyListener() { + + @Override + public void swtFontChanged(org.eclipse.swt.graphics.Font font) { + sample.setFont(font); + sampleGroup.layout(); + } + + @Override + public void awtFontChanged(Font font) { + } + }); + + // Color selection + Composite colorComposite = new Composite(composite, SWT.NONE); + GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(colorComposite); + GridLayoutFactory.fillDefaults().numColumns(2).applyTo(colorComposite); + + Label label = new Label(colorComposite, SWT.NONE); + GridDataFactory.fillDefaults().align(SWT.END, SWT.FILL).applyTo(label); + label.setText("Color: "); + + tc = new TableCombo(colorComposite, SWT.BORDER | SWT.READ_ONLY); + GridDataFactory.fillDefaults().hint(170, SWT.DEFAULT).applyTo(tc); + + tc.defineColumns(2); + tc.setDisplayColumnIndex(1); + tc.setToolTipText("this is tooltip"); + + createItems(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()) { + tc.setText(ti.getText(1)); + break; + } + } + } + + // add listener + tc.addSelectionListener(new SelectionListener() { + + @Override + 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; + sample.setForeground(swtColor); + } + tc.getTextControl().setSelection(0); + tc.getParent().forceFocus(); + } + + @Override + public void widgetDefaultSelected(SelectionEvent e) { + } + }); + + // Sample text + sampleGroup = new Group(composite, SWT.NONE); + sampleGroup.setText("Sample"); + GridDataFactory.fillDefaults().hint(SWT.DEFAULT, 70).applyTo(sampleGroup); + GridLayoutFactory.fillDefaults().margins(3, 3).applyTo(sampleGroup); + + 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); + } + if(color != null) { + swtColor = new org.eclipse.swt.graphics.Color(sample.getDisplay(), color.getRed(), color.getGreen(), color.getBlue()); + sample.setForeground(swtColor); + } + GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).grab(true, true).applyTo(sample); + + //Set the dialog position in the middle of the monitor + setDialogLocation(); + + return composite; + } + + @Override + protected void cancelPressed() { + resultAWTFont = awtFont; + resultAWTColor = color; + resultSWTFont = new org.eclipse.swt.graphics.Font(Display.getCurrent(), toSwtFontData(resultAWTFont, -1)); + + setReturnCode(CANCEL); + close(); + } + + @Override + protected void okPressed() { + resultAWTFont = vpc.getAWTFont(); + resultSWTFont = swtFont; + + 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()); + + } + + setReturnCode(OK); + close(); + } + + /** + * Sets the dialog location to the middle of the screen + */ + private void setDialogLocation() { + Rectangle monitorArea = getShell().getDisplay().getPrimaryMonitor().getBounds(); + Rectangle shellArea = getShell().getBounds(); + int x = monitorArea.x + (monitorArea.width - shellArea.width)/2; + int y = monitorArea.y + (monitorArea.height - shellArea.height)/2; + getShell().setLocation(x,y); + } + + /** + * Builds SWT FontData from AWT font. Simple conversion. + * + * @param font AWT font + * @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) { + FontData fontData = new FontData(); + fontData.setName(font.getFontName()); + fontData.setStyle(font.getStyle()); + fontData.setHeight(height > 0 ? height : font.getSize()); + return fontData; + } + + /** + * Creates color items for color combo + * @param table + */ + private void createItems(Table table) { + Image image; + GC gc; + TableItem ti; + int code; + org.eclipse.swt.graphics.Color color; + for(String text : systemColors.keySet()) { + code = systemColors.get(text); + color = Display.getCurrent().getSystemColor(code); + + image = new Image(Display.getCurrent(), 25, 15); + gc = new GC (image); + gc.setBackground (color); + gc.fillRectangle (image.getBounds()); + gc.dispose (); + + + ti = new TableItem(table, SWT.NONE); + ti.setImage(0, image); + ti.setText(1, text); + ti.setData(color); + } + + } + +} 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 new file mode 100644 index 00000000..2b72e360 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/FontModifyListener.java @@ -0,0 +1,34 @@ +/******************************************************************************* + * Copyright (c) 2007, 2012 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.sysdyn.ui.properties.widgets; + +import java.awt.Font; + +/** + * Font change listening interface + * @author Teemu Lempinen + * + */ +public interface FontModifyListener { + + /** + * Called when font is changed + * @param font New font as AWT font + */ + public void awtFontChanged(Font font); + + /** + * Called when font is changed + * @param font New font as SWT font + */ + public void swtFontChanged(org.eclipse.swt.graphics.Font 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 fd3b4a3b..f5d07f1a 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 @@ -39,8 +39,6 @@ import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableColumn; import org.eclipse.swt.widgets.TableItem; import org.eclipse.swt.widgets.Text; -import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; -import org.simantics.db.management.ISessionContext; /** * Composite for displaying font selection tools. By default, the composite contains @@ -57,6 +55,8 @@ 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; @@ -99,16 +99,15 @@ public class FontSelectionComposite extends Composite { * Composite containing components for selecting a font * * @param parent Parent composite - * @param context {@link ISessionContext} - * @param support {@link WidgetSupport} * @param style SWT style */ - public FontSelectionComposite(Composite parent, ISessionContext context, WidgetSupport support, int style) { + public FontSelectionComposite(Composite parent, int style) { super(parent, style); modifyListeners = new ListenerList(); GridLayoutFactory.fillDefaults().numColumns(3).applyTo(this); + GridDataFactory.fillDefaults().applyTo(this); /* * Two-row layout. First row consists of editable text boxes, @@ -166,6 +165,9 @@ public class FontSelectionComposite extends Composite { * Set controls to display given font */ public void setFont(Font font, boolean notify) { + if(font == null) + return; + Object[] listeners = new Object[0]; if(!notify) { listeners = modifyListeners.getListeners(); @@ -185,6 +187,15 @@ public class FontSelectionComposite extends Composite { int size = font.getSize(); fontSize.setText("" + size); + + for(int i = 0; i < sizes.length; i++) { + if(sizes[i].equals("" + size)) { + fontSizeTable.select(i); + fontSizeTable.setTopIndex(i); + fontChanged(); + break; + } + } if(!notify) { for(Object listener : listeners) @@ -201,6 +212,14 @@ public class FontSelectionComposite extends Composite { String style = fontStyle.getText(); if(style.equals("Regular")) style = null; + + int stylebits = 0; + if(style != null) { + if(style.toLowerCase().contains("bold")) + stylebits |= SWT.BOLD; + if(style.toLowerCase().contains("italic")) + stylebits |= SWT.ITALIC; + } String name = family + (style != null ? " " + style : ""); @@ -211,7 +230,7 @@ public class FontSelectionComposite extends Composite { } if(name != null && name.length() > 0) - return new Font(name, 0, size); + return new Font(name, stylebits, size); else return null; } @@ -619,18 +638,18 @@ public class FontSelectionComposite extends Composite { } - public void addFontModifiedListener(FontChangeListener listener) { + public void addFontModifiedListener(FontModifyListener listener) { modifyListeners.add(listener); } - public void removeFontModifiedListener(FontChangeListener listener) { + public void removeFontModifiedListener(FontModifyListener listener) { modifyListeners.remove(listener); } - public List getFontModifiedListener() { - ArrayList listeners = new ArrayList(modifyListeners.size()); + public List getFontModifiedListener() { + ArrayList listeners = new ArrayList(modifyListeners.size()); for(Object l : modifyListeners.getListeners()) - listeners.add((FontChangeListener)l); + listeners.add((FontModifyListener)l); return listeners; } @@ -645,24 +664,17 @@ 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 data = new FontData(font.getFamily(Locale.ROOT), font.getSize(), style); - org.eclipse.swt.graphics.Font swt = new org.eclipse.swt.graphics.Font(this.getDisplay(), data); + 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); Object[] listenersArray = modifyListeners.getListeners(); for (int i = 0; i < listenersArray.length; i++) { - ((FontChangeListener)listenersArray[i]).awtFontChanged(font); - ((FontChangeListener)listenersArray[i]).swtFontChanged(swt); + ((FontModifyListener)listenersArray[i]).awtFontChanged(font); + ((FontModifyListener)listenersArray[i]).swtFontChanged(swt); } } } - - - /** - * Font change listening interface - */ - public interface FontChangeListener { - public void awtFontChanged(Font font); - public void swtFontChanged(org.eclipse.swt.graphics.Font font); - } } -- 2.47.1