]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Choose font and color for sysdyn elements (refs #2959)
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 5 Mar 2012 14:08:04 +0000 (14:08 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 5 Mar 2012 14:08:04 +0000 (14:08 +0000)
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
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/SysdynElementFactory.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/VariableInformationTab.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/CustomFontDialog.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/FontModifyListener.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/FontSelectionComposite.java

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