--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in 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.modeling.ui.actions.style;\r
+\r
+import java.awt.Color;\r
+import java.awt.Dimension;\r
+import java.awt.Font;\r
+import java.awt.Point;\r
+\r
+import javax.swing.SwingUtilities;\r
+import javax.swing.UIManager;\r
+import javax.swing.UnsupportedLookAndFeelException;\r
+\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.eclipse.core.runtime.IStatus;\r
+import org.eclipse.core.runtime.Status;\r
+import org.eclipse.core.runtime.jobs.Job;\r
+import org.eclipse.jface.dialogs.IDialogSettings;\r
+import org.simantics.Simantics;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.ReadRequest;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.diagram.G2DUtils;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
+import org.simantics.diagram.stubs.G2DResource;\r
+import org.simantics.modeling.ui.Activator;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.utils.strings.format.MetricsFormat;\r
+import org.simantics.utils.ui.ErrorLogger;\r
+\r
+/**\r
+ * Style Edit\r
+ * \r
+ * TODO : should we have extension point for expanding styles?\r
+ * TODO : default ColorChooser is not suitable for this task\r
+ * TODO : how to store MetricsFormat template list\r
+ * \r
+ * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
+ */\r
+public class EditStyle {\r
+\r
+ private static final String SECTION_AWT_STYLE_DIALOG = "AWTStyleDialog";\r
+ private static final String SETTING_DIALOG_HEIGHT = "h";\r
+ private static final String SETTING_DIALOG_WIDTH = "w";\r
+ private static final String SETTING_DIALOG_Y = "y";\r
+ private static final String SETTING_DIALOG_X = "x";\r
+\r
+ public static void openStyleDialog(final Resource[] resources) {\r
+ if (resources.length == 0)\r
+ return;\r
+\r
+ SimanticsUI.getSession().asyncRequest(new ReadRequest() {\r
+\r
+ @Override\r
+ public void run(ReadGraph graph) throws DatabaseException {\r
+ G2DResource g2d = G2DResource.getInstance(graph);\r
+ DiagramResource dr = DiagramResource.getInstance(graph);\r
+ boolean hasStyle = true;\r
+ Color color = null;\r
+ Font font = null;\r
+ MetricsFormat format = null;\r
+\r
+ boolean hasColor = true;\r
+ boolean hasFont = true;\r
+ boolean hasFormat = true;\r
+\r
+ // Find what kind of styles selected objects support\r
+ for (Resource r : resources) {\r
+ if (!graph.isInstanceOf(r, dr.StyleProvider)) {\r
+ hasStyle = false;\r
+ break;\r
+ }\r
+ if (graph.isInstanceOf(r, dr.FontProvider)) {\r
+ if (font == null) {\r
+ Resource fontR = graph.getPossibleObject(r, g2d.HasFont);\r
+ if (fontR != null)\r
+ font = G2DUtils.getFont(graph,fontR);\r
+ }\r
+ } else {\r
+ hasFont = false;\r
+ }\r
+ if (graph.isInstanceOf(r, dr.ColorProvider)) {\r
+ if (color == null) {\r
+ Resource colorR = graph.getPossibleObject(r, g2d.HasColor);\r
+ if (colorR != null)\r
+ color = G2DUtils.getColor(graph,colorR);\r
+ }\r
+ } else {\r
+ hasColor = false;\r
+ }\r
+ if (graph.isInstanceOf(r, dr.FormatProvider)) {\r
+ if (format == null) {\r
+ Resource formatR = graph.getPossibleObject(r, dr.HasFormat);\r
+ if (formatR != null)\r
+ format = G2DUtils.getMetricsFormat(graph,formatR);\r
+ }\r
+ } else {\r
+ hasFormat = false;\r
+ }\r
+ }\r
+\r
+ if (!hasStyle)\r
+ return; // TODO : show error\r
+\r
+ if (!hasFont && !hasColor && !hasFormat)\r
+ return; // TODO : show error\r
+\r
+ final Font currentFont = font;\r
+ final Color currentColor = color;\r
+ final MetricsFormat currentFormat = format;\r
+ final boolean useFont = hasFont;\r
+ final boolean useColor = hasColor;\r
+ final boolean useFormat = hasFormat;\r
+\r
+ Job job = new Job("Open Style Dialog") {\r
+ @Override\r
+ protected IStatus run(IProgressMonitor monitor) {\r
+ monitor.beginTask("Open dialog", IProgressMonitor.UNKNOWN);\r
+ SwingUtilities.invokeLater(new Runnable() {\r
+ @Override\r
+ public void run() {\r
+ setThread(Thread.currentThread());\r
+ try {\r
+ UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());\r
+ // there's no reason to show errors to user.\r
+ } catch (ClassNotFoundException e) {\r
+ ErrorLogger.defaultLogError(e);\r
+ } catch (InstantiationException e) {\r
+ ErrorLogger.defaultLogError(e);\r
+ } catch (IllegalAccessException e) {\r
+ ErrorLogger.defaultLogError(e);\r
+ } catch (UnsupportedLookAndFeelException e) {\r
+ ErrorLogger.defaultLogError(e);\r
+ }\r
+\r
+ AWTStyleDialog dialog = new AWTStyleDialog(useFont, useColor, useFormat);\r
+ if (useColor && currentColor != null)\r
+ dialog.setStartColor(currentColor);\r
+ if (useFont && currentFont != null)\r
+ dialog.setStartFont(currentFont);\r
+ if (useFormat && currentFormat != null) {\r
+ dialog.setStartFormat(currentFormat);\r
+ }\r
+\r
+ // Restore dialog settings\r
+ IDialogSettings ds = Activator.getDefault().getDialogSettings();\r
+ IDialogSettings sd = ds.getSection(SECTION_AWT_STYLE_DIALOG);\r
+ boolean restoredSettings = false;\r
+ if (sd == null) {\r
+ sd = ds.addNewSection(SECTION_AWT_STYLE_DIALOG);\r
+ } else {\r
+ try {\r
+ int x = sd.getInt(SETTING_DIALOG_X);\r
+ int y = sd.getInt(SETTING_DIALOG_Y);\r
+ int w = sd.getInt(SETTING_DIALOG_WIDTH);\r
+ int h = sd.getInt(SETTING_DIALOG_HEIGHT);\r
+ // Sanity check\r
+ if (w > 0 && h > 0) {\r
+ dialog.setLocation(x, y);\r
+ dialog.setSize(w, h);\r
+ restoredSettings = true;\r
+ }\r
+ } catch (NumberFormatException e) {\r
+ // Ignore.\r
+ }\r
+ }\r
+ if (!restoredSettings) {\r
+ dialog.setLocationByPlatform(true);\r
+ dialog.pack();\r
+ }\r
+ done(Status.OK_STATUS);\r
+ dialog.setVisible(true);\r
+\r
+ // Save settings\r
+ Point loc = dialog.getLocation();\r
+ Dimension dim = dialog.getSize();\r
+ sd.put(SETTING_DIALOG_X, loc.x);\r
+ sd.put(SETTING_DIALOG_Y, loc.y);\r
+ sd.put(SETTING_DIALOG_WIDTH, dim.width);\r
+ sd.put(SETTING_DIALOG_HEIGHT, dim.height);\r
+\r
+ if (!dialog.isCancelled()) {\r
+ // OK was pressed\r
+ final Font font = dialog.getFont();\r
+ final Color color = dialog.getColor();\r
+ final MetricsFormat format = dialog.getFormat();\r
+ Session session = Simantics.getSession();\r
+ session.markUndoPoint();\r
+ session.asyncRequest(new WriteRequest() {\r
+\r
+ @Override\r
+ public void perform(WriteGraph graph) throws DatabaseException {\r
+ G2DResource g2d = G2DResource.getInstance(graph);\r
+ DiagramResource dr = DiagramResource.getInstance(graph);\r
+ // create style definitions\r
+ Resource fontResource = null;\r
+ if (useFont && font != null)\r
+ fontResource = G2DUtils.createFont(graph, font);\r
+\r
+ Resource colorResource = null;\r
+ if (useColor && color != null)\r
+ colorResource = G2DUtils.createColor(graph, color);\r
+\r
+ Resource formatResource = null;\r
+ if (useFormat)\r
+ formatResource = G2DUtils.createMetricsFormat(graph, format);\r
+\r
+ // use style definitions in selected objects\r
+ for (Resource r : resources) {\r
+ if (useFont && fontResource != null && graph.isInstanceOf(r, dr.FontProvider)) {\r
+ graph.deny(r, g2d.HasFont);\r
+ graph.claim(r, g2d.HasFont, fontResource);\r
+ }\r
+ if (useColor && colorResource != null && graph.isInstanceOf(r, dr.ColorProvider)) {\r
+ graph.deny(r, g2d.HasColor);\r
+ graph.claim(r, g2d.HasColor, colorResource);\r
+ }\r
+ if (useFormat && formatResource != null && graph.isInstanceOf(r, dr.FormatProvider)) {\r
+ graph.deny(r,dr.HasFormat);\r
+ graph.claim(r, dr.HasFormat, formatResource);\r
+ }\r
+ }\r
+ }\r
+ });\r
+ }\r
+ }\r
+ });\r
+ return Job.ASYNC_FINISH;\r
+ }\r
+ };\r
+ job.setUser(true);\r
+ job.schedule();\r
+ }\r
+ });\r
+ }\r
+\r
+}\r